package cn.pluss.platform.entity;

import cn.pluss.platform.constants.jft.JftConstant;
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 com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

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

/**
 * <p>
 * 收银呗缴费通收款单
 * </p>
 *
 * @author crystal
 * @since 2022-06-16
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_pluss_jft_receipt_info")
public class JftReceiptInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final Integer NORMAL = 1;

    public static final Integer CLOSE = -1;


    /**
     * id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;


    /**
     * 项目名称
     */
    @TableField("projectName")
    private String projectName;


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


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

    /**
     * 状态 1:正常 -1：关闭
     */
    @TableField("status")
    private Integer status;


    /**
     * 创建时间
     */
    @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("endTime")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

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

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

    /**
     * 收款单token
     */
    @TableField("token")
    private String token;

    /**
     * 付款时必填参数id集合
     */
    @TableField(exist = false)
    private List<Long> paymentIds;

    /**
     * 开始时间 日期查询
     */
    @TableField(exist = false)
    private String startDate;

    /**
     * 结束日期
     */
    @TableField(exist = false)
    private String endDate;

    /**
     * 收款单qrCodeUrl
     */
    @TableField("qrCodeUrl")
    private String qrCodeUrl;

    /**
     * 收款单imageUrl
     */
    @TableField("imageUrl")
    private String imageUrl;

    /**
     * 付款ip
     */
    @TableField("ip")
    private String ip;

    /**
     * 跳转信息
     */
    @TableField(exist = false)
    private String jumpUrl;


    @TableField(exist = false)
    private JftMerchantRate jftRate;

    @TableField(exist = false)
    private JftMercBaseInfo jftMerc;

    @TableField(exist = false)
    private JftMercPaymentChannel jftChannel;

    /**
     * 检查参数
     */
    public void checkParams(UserApp userApp) {
        MsgException.checkBlank(this.getProjectName(),"项目名称不能为空！");
        MsgException.checkNull(this.getAmount(),"收款单金额不能为空！");
        MsgException.check(this.getAmount().compareTo(JftConstant.JFT_MIN_PAY_AMT) < 0,"收款单最少金额为"+JftConstant.JFT_MIN_PAY_AMT.toPlainString()+"元");
        this.setUserId(userApp.getUserId());
        Date date = DateUtils.currentDate();
        //过期时间一天
        Date endDate = DateUtils.add(Calendar.MINUTE,date,5);
        this.setEndTime(endDate);
        this.setToken(StringUtil.random6V2());
        this.setQrCodeUrl(createUrl());
    }

    public String createUrl() {
        return JftConstant.JUMP_JFT_APPLET_URL.replace("TOKEN",this.getToken());
    }

    public void checkDate() {
        Date now = DateUtils.currentDate();
        boolean flag = now.compareTo(this.getCreateTime()) < 0 || now.compareTo(this.getEndTime()) > 0;
        MsgException.check(flag,"当前收款单已过期");
    }
}