package cn.pluss.platform.entity;

import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.dto.jft.JftPayDataDTO;
import cn.pluss.platform.dto.jft.JftReceiptOrderDTO;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

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

/**
 * <p>
 * 收款单订单详情
 * </p>
 *
 * @author crystal
 * @since 2022-06-16
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_pluss_jft_receipt_order")
@NoArgsConstructor
public class JftReceiptOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String DEFAULT_PAY_STATUS = "01";

    public static String PAY_STATUS_YES = "02";

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;


    /**
     * 收款单ID
     */
    @TableField("rid")
    private Long rid;


    /**
     * 金额
     */
    @TableField("amount")
    private BigDecimal amount;


    /**
     * 状态01：未支付 02 已支付
     */
    @TableField("status")
    private String status;


    /**
     * 支付方式 aliPay：支付宝 wechatPay：微信 bank：云闪付
     */
    @TableField("payType")
    private String payType;


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


    /**
     * 用户ID
     */
    @TableField("userId")
    private Long userId;


    /**
     * 商户编号
     */
    @TableField("merchantCode")
    private String merchantCode;


    /**
     * 商户名称
     */
    @TableField("merchantName")
    private String merchantName;


    /**
     * 付款用户ID
     */
    @TableField("payUserId")
    private String payUserId;

    /**
     * 付款用户所属城市
     */
    @TableField("payUserCity")
    private String payUserCity;

    /**
     * 真实名称
     */
    @TableField("name")
    private String name;


    /**
     * 手机号
     */
    @TableField("phone")
    private String phone;


    /**
     * 物品信息
     */
    @TableField("goods")
    private String goods;


    /**
     * 地址信息
     */
    @TableField("address")
    private String address;


    /**
     * 平台订单号
     */
    @TableField("transNo")
    private String transNo;


    /**
     * 渠道订单号
     */
    @TableField("channelTransNo")
    private String channelTransNo;


    /**
     * 支付时间
     */
    @TableField("transTime")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date transTime;

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

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

    /**
     * 费率
     */
    @TableField("rate")
    private Integer rate;


    /**
     * 创建时间
     */
    @TableField(value = "createTime", fill = FieldFill.INSERT)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

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

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

    /**
     * 渠道手续费
     */
    @TableField("channelFee")
    private BigDecimal channelFee;


    /**
     * 商户手续费
     */
    @TableField("mercFee")
    private BigDecimal mercFee;


    /**
     * 提现手续费
     */
    @TableField("cashFee")
    private BigDecimal cashFee;


    /**
     * 支付数据
     */
    @TableField("payData")
    private String payData;


    /**
     * 备注
     */
    @TableField("remark")
    private String remark;

    /**
     * 收益金额
     */
    @TableField("profitFee")
    private BigDecimal profitFee;

    @TableField("subject")
    private String subject;

    @TableField("mercOrderNo")
    private String mercOrderNo;

    @TableField("mercNotifyUrl")
    private String mercNotifyUrl;



    @TableField("createDate")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JSONField(format = "yyyy-MM-dd")
    private Date createDate;


    @TableField(exist = false)
    private String token;

    @TableField(exist = false)
    private JftMercBaseInfo jftMerc;

    @TableField(exist = false)
    private JftMercPaymentChannel jftChannel;


    public JftReceiptOrder(JftPayDataDTO dto,String payType) {
        this.setName(dto.getName());
        this.setPhone(dto.getPhone());
        this.setGoods(dto.getGoods());
        this.setAddress(dto.getAddress());
        this.setPayUserId(dto.getPayUserId());
        this.setPayType(payType);
    }

    public JftReceiptOrder(JftPayDataDTO dto, JftReceiptInfo info, String payType,JftMercBaseInfo merc,JftMerchantRate jftRate) {
        this(dto,payType);
        this.setOrderNumber("YS_JFT_"+ StringUtil.getBillno());
        this.setRid(info.getId());
        this.setAmount(info.getAmount());
        this.setStatus(DEFAULT_PAY_STATUS);
        this.setUserId(info.getUserId());
        this.setMerchantName(merc.getMerchantName());
        this.setRate(jftRate.getAliRate().intValue());
        this.setCreateTime(DateUtils.currentDate());
        BigDecimal channelFee = Constant.DEFAULT_JFT_COST_RATE.divide(BigDecimal.valueOf(10000)).multiply(info.getAmount()).setScale(2,BigDecimal.ROUND_UP);
        this.setChannelFee(channelFee);
        BigDecimal cashFee = Constant.DEFAULT_JFT_CASH_RATE.multiply(info.getAmount()).setScale(2,BigDecimal.ROUND_UP);
        if(cashFee.compareTo(BigDecimal.ZERO) == 0){
            cashFee = Constant.DEFAULT_JFT_CASH_FEE;
        }
        BigDecimal mercFee = info.getAmount().multiply(jftRate.getAliRate().divide(BigDecimal.valueOf(10000))).setScale(2,BigDecimal.ROUND_UP);
        this.setMercFee(mercFee);
        this.setCashFee(cashFee);
        this.setPayUserCity(dto.getCity());
        BigDecimal profitFee = mercFee.multiply(channelFee).add(cashFee.multiply(BigDecimal.valueOf(0.0008))).setScale(2,BigDecimal.ROUND_HALF_DOWN);
        this.setProfitFee(profitFee);
        this.setMerchantCode(merc.getMerchantCode());
    }

    public JftReceiptOrder(JftReceiptOrderDTO order,JftMerchantRate rate,String payType,String prefix) {
        this.setAmount(order.getOrderAmt());
        this.setPayType(payType);
        this.setOrderNumber(prefix + StringUtil.getBillno());
        this.setUserId(order.getUserId());
        this.setMerchantCode(order.getMerchantCode());
        this.setMerchantName(order.getMerchantName());
        this.setRate(rate.getAliRate().intValue());
        this.setRemark(order.getRemark());
        this.setPayUserCity(order.getCity());
        this.setSubject(order.getSubject());
        this.setMercOrderNo(order.getMercOrderNo());
        this.setMercNotifyUrl(order.getNotifyUrl());
    }

    /**
     * 校验参数
     */
    public void checkParams() {
        MsgException.checkBlank(this.getPayUserId(),"付款用户ID不能为空");
        MsgException.checkBlank(this.getToken(),"token参数为空");
    }

    /**
     * 获取支付类型描述
     * @return
     */
    public String getPayTypeDesc() {
        String payType = this.getPayType();
        String payTypeDesc = "未知";
        if(StringUtil.isEmpty(payType)){
            return payTypeDesc;
        }
        switch (payType){
            case Constant.PAY_TYPE_ALIPAY:
                payTypeDesc = Constant.PAY_TYPE_NAME_ALIPAY;
                break;
            case Constant.PAY_TYPE_WECHAT:
                payTypeDesc = Constant.PAY_TYPE_NAME_WECHAT;
                break;
            case Constant.PAY_TYPE_YSFPAY:
                payTypeDesc = Constant.PAY_TYPE_NAME_YSFPAY;
                break;
        }
        return payTypeDesc;
    }
}