package cn.pluss.platform.entity;

import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.yinsheng.YsOldSrcMerchantNo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 设备订单token表
 * </p>
 *
 * @author bzg
 * @since 2022-03-23
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@NoArgsConstructor
@TableName("tb_pluss_merc_order_token")
public class MercOrderToken implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int EXPIRE_MINUTE_NUM = 5;

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


    /**
     * token
     */
    @TableField("token")
    private String token;


    @TableField("merchantName")
    private String merchantName;


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


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


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


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


    /**
     * 类型：1：扫码王 2:表示其他
     */
    @TableField("type")
    private String type;


    /**
     * 设备号
     */
    @TableField("snNo")
    private String snNo;


    /**
     * 创建时间
     */
    @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("expireTime")
    private Date expireTime;

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

    /**
     * 商户异步通知地址
     */
    @TableField("mercNotifyUrl")
    private String mercNotifyUrl;

    /**
     * 商户同步返回地址
     */
    @TableField("mercRefundUrl")
    private String mercRefundUrl;

    /**
     * 订单标题
     */
    @TableField("subject")
    private String subject;

    /**
     * 支付方式
     */
    @TableField("payTypeCode")
    private String payTypeCode;


    /**
     * 支付方式名称
     */
    @TableField("payTypeName")
    private String payTypeName;


    /**
     * 商户简称
     */
    @TableField("alias")
    private String alias;

    /**
     * 渠道
     */
    @TableField("channel")
    private Integer channel;

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

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


    public MercOrderToken(MerchantBaseInfo baseInfo, Double fee, String snNo, String orderNumber) {
        Date now = DateUtils.currentDate();
        this.token = StringUtil.genRandomNum(32);
        this.merchantName = baseInfo.getAlias();
        this.merchantCode = baseInfo.getMerchantCode();
        this.userId = Integer.parseInt(baseInfo.getUserId());
        this.orderAmt = BigDecimal.valueOf(fee);
        this.orderNumber = orderNumber;
        this.snNo = snNo;
        this.createTime = now;
        this.expireTime = DateUtils.add(Calendar.MINUTE,now,EXPIRE_MINUTE_NUM);
    }

    public MercOrderToken(MerchantBaseInfo merchant, String payAmt, MerchantChannelStatus channel,String mercOrderNo,JSONObject params) {
        Date now = DateUtils.currentDate();
        String orderNumber = StringUtil.getBillno();
        Integer detaultExpreTime = 5;
        this.token = StringUtil.genRandomNum(32);
        this.merchantName = merchant.getAlias();
        this.merchantCode = merchant.getMerchantCode();
        this.alias = merchant.getMerchantName();
        this.userId = Integer.parseInt(merchant.getUserId());
        this.orderAmt = new BigDecimal(payAmt);
        if(channel.getChannel() == 1){
            this.channel = 2;
            orderNumber = "SXF_MERC_" + orderNumber;
        }else if(channel.getChannel() == 3){
            this.channel = 3;
            orderNumber = "RYX_MERC_" + orderNumber;
        }else if(channel.getChannel() == 4){
            this.channel = 4;
            orderNumber = "YS_MERC_" + orderNumber;
        }
        if("WXZF".equals(params.getString("payWay"))){
            this.payTypeCode = Constant.PAY_TYPE_WECHAT;
            this.payTypeName = Constant.PAY_TYPE_NAME_WECHAT;
        }else if("ZFBZF".equals(params.getString("payWay"))){
            this.payTypeCode = Constant.PAY_TYPE_ALIPAY;
            this.payTypeName = Constant.PAY_TYPE_NAME_ALIPAY;
        }else{
            this.payTypeCode = Constant.PAY_TYPE_YSFPAY;
            this.payTypeName = Constant.PAY_TYPE_NAME_YSFPAY;
        }
        this.orderNumber = orderNumber;
        this.createTime = now;
        this.expireTime = DateUtils.add(Calendar.MINUTE,now,detaultExpreTime);
        this.mercOrderNo = mercOrderNo;
        this.mercNotifyUrl = params.getString("notifyUrl");
        this.mercRefundUrl = params.getString("returnUrl");
        this.subject = params.getString("subject");
    }
}