package com.yiqi.core.domain;

import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yiqi.common.annotation.Excel;
import com.yiqi.common.enums.OrderBillType;
import com.yiqi.common.enums.purchaseGoodsStatusType;
import com.yiqi.order.domain.OrderPurchaseGoods;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.yiqi.common.core.domain.BaseEntity;
import lombok.Data;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

/**
 * 结算余额记录明细对象 settlement_balance_record
 *
 * @author ruoyi
 * @date 2024-02-21
 */
@ApiModel("结算余额记录明细")
@TableName("settlement_balance_record")
@Data
public class SettlementBalanceRecord extends BaseEntity
{
    private static final long serialVersionUID=1L;

    /** 主键ID */
    @ApiModelProperty("主键ID")
    @TableId(type = IdType.AUTO)
    private Long id;

    /** 来源类型 */
    @Excel(name = "来源类型")
    @ApiModelProperty("来源类型")
    private String sourceType;

    /** 组织ID */
    @Excel(name = "组织ID")
    @ApiModelProperty("组织ID")
    private Long orgId;

    /** 主账单ID */
    @Excel(name = "主账单ID")
    @ApiModelProperty("主账单ID")
    private Long mstrBillId;

    /** 账单标题 */
    @Excel(name = "账单标题")
    @ApiModelProperty("账单标题")
    private String title;

    /** 账单类型（0=支出，1=充值） */
    @Excel(name = "账单类型", readConverterExp = "0==支出，1=充值")
    @ApiModelProperty("账单类型（0=支出，1=充值）")
    private String billType;

    /** 交易金额 */
    @Excel(name = "交易金额")
    @ApiModelProperty("交易金额")
    private BigDecimal transactionAmount;


    public static SettlementBalanceRecord buildSettlementBalanceRecordByFactory(SettlementManageFactory settlementManageFactory){
        SettlementBalanceRecord settlementBalanceRecord = new SettlementBalanceRecord();
        settlementBalanceRecord.buildCreateData();
        settlementBalanceRecord.setTitle(settlementManageFactory.getTitle()+"-结算金额");
        settlementBalanceRecord.setBillType(OrderBillType.recharge.getCode());
        settlementBalanceRecord.setMstrBillId(settlementManageFactory.getBillId());
        settlementBalanceRecord.setOrgId(settlementManageFactory.getOrgId());
        settlementBalanceRecord.setSourceType(settlementManageFactory.getSourceType());
        settlementBalanceRecord.setTransactionAmount(settlementManageFactory.getMaterialAmount());
        return settlementBalanceRecord;
    }

    public static SettlementBalanceRecord buildSettlementBalanceRecordByStore(SettlementManageStore settlementManageStore){
        SettlementBalanceRecord settlementBalanceRecord = new SettlementBalanceRecord();
        settlementBalanceRecord.buildCreateData();
        settlementBalanceRecord.setTitle(settlementManageStore.getTitle()+"-结算金额");
        settlementBalanceRecord.setBillType(OrderBillType.recharge.getCode());
        settlementBalanceRecord.setMstrBillId(settlementManageStore.getBillId());
        settlementBalanceRecord.setOrgId(settlementManageStore.getOrgId());
        settlementBalanceRecord.setSourceType(settlementManageStore.getSourceType());
        settlementBalanceRecord.setTransactionAmount(settlementManageStore.getSettleSettlementAmount());
        return settlementBalanceRecord;
    }

    /**
     * 构建消费记录， 通过进货订单
     * @param orderPurchaseGoods 进货订单对象
     * @return
     */
    public static SettlementBalanceRecord buildConsumeBalanceRecordByPurchaseOrder(OrderPurchaseGoods orderPurchaseGoods){
        SettlementBalanceRecord settlementBalanceRecord = new SettlementBalanceRecord();
        settlementBalanceRecord.buildCreateData();
        settlementBalanceRecord.setTitle(orderPurchaseGoods.getOrderNo()+"-进货消费抵扣金额");
        settlementBalanceRecord.setBillType(OrderBillType.pay.getCode());
        settlementBalanceRecord.setMstrBillId(orderPurchaseGoods.getId());
        settlementBalanceRecord.setOrgId(orderPurchaseGoods.getOrgId());
        settlementBalanceRecord.setSourceType(orderPurchaseGoods.getSourceType());
        settlementBalanceRecord.setTransactionAmount(orderPurchaseGoods.getBalanceDeductAmount());
        return settlementBalanceRecord;
    }
    /**
     * 构建退款记录， 通过进货订单
     * @param orderPurchaseGoods 进货订单对象
     * @return
     */
    public static SettlementBalanceRecord buildRefundBalanceRecordByPurchaseOrder(OrderPurchaseGoods orderPurchaseGoods){
        SettlementBalanceRecord settlementBalanceRecord = new SettlementBalanceRecord();
        settlementBalanceRecord.buildCreateData();
        if (purchaseGoodsStatusType.REFUSE.getCode().equals(orderPurchaseGoods.getOrderStatus())){
            settlementBalanceRecord.setTitle(orderPurchaseGoods.getOrderNo()+"-进货拒单退款");
        }
        if (purchaseGoodsStatusType.CANCEL_ORDER.getCode().equals(orderPurchaseGoods.getOrderStatus())){
            settlementBalanceRecord.setTitle(orderPurchaseGoods.getOrderNo()+"-进货取消订单退款");
        }
        settlementBalanceRecord.setBillType(OrderBillType.recharge.getCode());
        settlementBalanceRecord.setMstrBillId(orderPurchaseGoods.getId());
        settlementBalanceRecord.setOrgId(orderPurchaseGoods.getOrgId());
        settlementBalanceRecord.setSourceType(orderPurchaseGoods.getSourceType());
        settlementBalanceRecord.setTransactionAmount(orderPurchaseGoods.getBalanceDeductAmount());
        return settlementBalanceRecord;
    }
}
