package cn.pluss.platform.entity;

import cn.pluss.platform.api.SplitRefund;
import cn.pluss.platform.api.SplitReq;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分账订单退款详情表
 * </p>
 *
 * @author bzg
 * @since 2022-07-04
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_pluss_merchant_order_split_refund")
@NoArgsConstructor
public class MerchantOrderSplitRefund implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId("id")
    private Long id;


    /**
     * 订单号
     */
    @TableField("orderNumber")
    private String orderNumber;


    /**
     * 商户订单号
     */
    @TableField("mercOrderNo")
    private String mercOrderNo;


    /**
     * 状态 0：退款中 1：退款成功 2.退款失败
     */
    @TableField("status")
    private Integer status;


    /**
     * 订单金额
     */
    @TableField("orderAmt")
    private BigDecimal orderAmt;

    /**
     * 下单时间
     */
    @TableField("orderTime")
    private Date orderTime;

    /**
     * 退款单号
     */
    @TableField("refundNo")
    private String refundNo;


    /**
     * 商户退款单号
     */
    @TableField("mercRefundNo")
    private String mercRefundNo;


    /**
     * 退款时间
     */
    @TableField("refundTime")
    private Date refundTime;

    /**
     * 退款时间 开始时间
     */
    @TableField(exist = false, condition = "refundTime &gt; #{%s}")
    private Date refundTimeStart;

    /**
     * 退款时间 截止时间
     */
    @TableField(exist = false, condition = "refundTime &lt; #{%s}")
    private Date refundTimeEnd;

    /**
     * 退款通知地址
     */
    @TableField("notifyUrl")
    private String notifyUrl;


    /**
     * 退款金额
     */
    @TableField("refundAmt")
    private BigDecimal refundAmt;

    @TableField("refundReason")
    private String refundReason;

    @TableField("remark")
    private String remark;

    /**
     * 通道退款状态值
     */
    @TableField("channelRefundState")
    private String channelRefundState;

    /**
     * 退款资金状态
     */
    @TableField("channelFundsState")
    private String channelFundsState;

    /**
     * 退款资金装填描述
     */
    @TableField("channelFundsDesc")
    private String channelFundsDesc;

    /**
     * 通道退款流水号
     */
    @TableField("channelRefundNo")
    private String channelRefundNo;

    /**
     *
     */
    @TableField(exist = false)
    private String divisionMode;

    @TableField(value = "createTime", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     *  开始时间
     */
    @TableField(exist = false, condition = "createTime &gt; #{%s}")
    private Date createTimeStart;

    /**
     *  截止时间
     */
    @TableField(exist = false, condition = "createTime &lt; #{%s}")
    private Date createTimeEnd;


    @TableField(exist = false)
    private JSONArray orderDivList;

    @TableField(exist = false)
    private JSONArray refundDivList;

    public MerchantOrderSplitRefund(MerchantOrderSplit orderSplit, SplitReq req) {
        this.setOrderNumber(orderSplit.getOrderNumber());
        this.setMercOrderNo(orderSplit.getMercOrderNo());
        this.setOrderAmt(orderSplit.getOrderAmt());
        this.setRefundNo("YS_MERC_R_"+ StringUtil.getBillno());
        this.setMercRefundNo(req.getMercRefundNo());
        this.setNotifyUrl(req.getNotifyUrl());
        this.setRefundAmt(new BigDecimal(req.getRefundAmt()));
        this.setRefundReason(req.getRefundReason());
        this.setOrderTime(orderSplit.getCreateTime());
        this.setDivisionMode(orderSplit.getDivisionMode());
        List<MerchantOrderSplitDetail> detailList = orderSplit.getDetailList();
        JSONArray divList = new JSONArray(16);
        if(!detailList.isEmpty()){
            for (MerchantOrderSplitDetail detail:detailList) {
                JSONObject result = new JSONObject(16);
                result.put("division_mer_id",detail.getMerchantId());
                result.put("division_amount",detail.getDivAmt());
                result.put("division_ratio",detail.getDivRatio());
                result.put("is_charge_fee",detail.getIsChargeFee());
                divList.add(result);
            }
        }
        this.setOrderDivList(divList);
        List<SplitRefund> refundList = req.getRefundList();
        JSONArray refundDivList = new JSONArray(16);
        for (SplitRefund refund:refundList) {
            JSONObject refObj = new JSONObject(3);
            refObj.put("refund_mer_id",refund.getMerchantId());
            refObj.put("refund_amount",refund.getRefundAmt());
            refObj.put("ori_division_mode",orderSplit.getDivisionMode());
            refundDivList.add(refObj);
        }
        this.setRefundDivList(refundDivList);
    }
}