package cn.pluss.platform.entity;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.leshua.MerchantRegisterDTO;
import cn.pluss.platform.vo.MerchantOrderVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商户退款订单表实体类
 */
@Data
@Accessors(chain = true)
public class MerchantRefundOrder {

    @TableField
    private Integer id;

    /**
     * 商户编号
     */
    private String merchantCode;

    /**
     * 商户简称
     */
    private String alias;

    /**
     * 交易单号
     */
    private String orderNumber;

    /**
     * 退款单号
     */
    private String refundNo;

    /**
     * 通道订单号
     */
    private String channelNo;

    /**
     * 支付时间
     */
    private Date payTime;

    /**
     * 退款时间
     */
    private Date refundTime;

    /**
     * 退款状态 1：退款成功 2：退款中3：退款失败
     */
    private String status;

    /**
     * 订单类型
     */
    private String orderType;

    /**
     * 支付类型code
     */
    private String payTypeCode;

    /**
     * 支付类型名称
     */
    private String payTypeName;

    /**
     * 支付金额
     */
    private BigDecimal payAmt;

    /**
     * 订单来源1：乐刷2：随行付3：其他
     */
    private String aisleSwitch;
    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 通道商户号
     */
    private String channelMercNo;

    /**
     * 退款金额
     */
    private BigDecimal refundAmt;

    /**
     * 商户退款单号
     */
    private String mercRefundNo;

    /**
     * 商户退款异步通知地址
     */
    private String notifyUrl;

    /**
     * 退款失败原因
     */
    private String failMsg;

    @TableField(exist = false)
    private BigDecimal refundAmtCount;
    @TableField(exist = false)
    private Integer refunCount;
    @TableField(exist = false)
    private Integer transferAmtCount;

    @TableField(exist = false)
    private List<String> payTypeCodeList;

    public void convert(MerchantOrderVO order, Map<String,Object> params, String mchNo) {
        this.merchantCode = order.getMerchantCode();
        this.alias = order.getAlias();
        this.orderNumber = order.getOrderNumber();
        this.refundNo = params.get("refundNo") == null ? "" : params.get("refundNo").toString();
        this.channelNo = order.getTransNo();
        this.payTime = order.getTransDt();
        this.refundTime = new Date();
        this.status = params.get("status") == null ? "" : params.get("status").toString();
        this.orderType = order.getOrderType();
        this.payTypeCode = order.getPayTypeCode();
        this.payTypeName = order.getPayTypeName();
        this.payAmt = new BigDecimal(String.valueOf(order.getConsumeFee()));
        this.aisleSwitch = order.getAisleSwitch();
        this.createTime = new Date();
        this.channelMercNo = mchNo;
        this.failMsg = params.get("msg") == null ? "" : params.get("msg").toString();
        this.refundAmt = BigDecimal.valueOf(order.getConsumeFee().doubleValue());
    }

    public void convert(MerchantOrder order, Map<String,Object> result, String mchNo, BigDecimal refundAmt, JSONObject object) {
        this.merchantCode = order.getMerchantCode();
        this.alias = order.getMerchantName();
        this.orderNumber = order.getOrderNumber();
        this.refundNo = (String)result.get("refundNo");
        this.channelNo = order.getTransNo();
        this.payTime = order.getTransDt();
        this.refundTime = new Date();
        this.status = (String)result.get("status");
        this.orderType = order.getOrderType();
        this.payTypeCode = order.getPayTypeCode();
        this.payTypeName = order.getPayTypeName();
        this.payAmt = BigDecimal.valueOf(order.getConsumeFee());
        this.refundAmt = refundAmt;
        this.aisleSwitch = order.getAisleSwitch();
        this.createTime = new Date();
        this.channelMercNo = mchNo;
        this.failMsg = (String)result.get("msg");
        this.mercRefundNo = object.getString("mercRefundNo");
        this.notifyUrl = object.getString("notifyUrl");
    }
}
