package com.jeequan.jeepay.db.entity;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeequan.jeepay.core.entity.DivisionSettleInfo;
import com.jeequan.jeepay.core.model.BaseModel;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.db.typehandler.EncryptTypeHandler;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.util.Assert;

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

/**
 * <p>
 * 
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-04-03
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "t_division_subject")
@NoArgsConstructor
public class DivisionSubject extends BaseModel {

    public static final LambdaQueryWrapper<DivisionSubject> gw(){
        return new LambdaQueryWrapper<>();
    }

    private static final long serialVersionUID=1L;

    /**
     *接收方类型
     */
    public static final byte RECEIVE_TYPE = 0;

    /**
     *分出方类型
     */
    public static final byte EXPEND_TYPE = 1;


    /**
     *接收方类型 个人
     */
    public static final byte USER_TYPE_PERSONAL = 0;

    /**
     *分出方类型 商户
     */
    public static final byte USER_TYPE_MERCHANT = 1;


    private static final String BANK = "BANK";

    private static final String ZFB = "ZFB";

    private static final String WX = "WX";

    /**
     *
     */
    @TableId(value = "division_no", type = IdType.INPUT)
    private String divisionNo;

    /**
     * 用户号
     */
    private String mchNo;

    /**
     * 类型 0：接收方 1：分出方
     */
    private Byte type;

    /**
     * 所属通道
     */
    private String ifCode;

    /**
     * 所属渠道
     */
    private String isvNo;



    /**
     * 用户类型  0：个人 1：商户
     */
    private Byte userType;

    /**
     * 个人名称或者商户名称
     */
    private String name;

    /**
     * 平渠道分账账户号
     */
    private String channelDivisionNo;

    /**
     * 结算类型 BANK：到银行卡 ZFB：到支付宝 WX:微信
     */
    private String settleType;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;

    /**
     * 扩展参数
     */
    @TableField(typeHandler = EncryptTypeHandler.class)
    private String extData;

    /**
     * 备注
     */
    private String remark;

    /**
     * 状态
     */
    private Byte state;
    /**
     * 所属通道
     */
    @TableField(exist = false)
    private String ifName;

    /**
     * 渠道简称
     */
    @TableField(exist = false)
    private String isvName;

    /**
     * 是否收取手续费 0：否 1：是
     */
    @TableField(exist = false)
    private Byte isCharge;

    /**
     * 分账值
     */
    @TableField(exist = false)
    private BigDecimal divisionValue;

    @TableField(exist = false)
    private String taskId;

    @TableField(exist = false)
    private DivisionSettleInfo info;

    /**
     * 分账分出方参数校验
     */
    public void preCheck(){
//        Assert.notNull(this.getType(),"类型不能为空");
        Assert.notNull(this.getUserType(),"用户类型不能为空");
        Assert.notNull(this.getName(),"别名不能为空");
        if(USER_TYPE_MERCHANT == this.getUserType()){
            Assert.notNull(this.getDivisionNo(),"分账账号不能为空");
        }else{
            this.setDivisionNo(SeqKit.getDivisionNo());
        }
        Assert.notNull(this.getIfCode(),"所属通道不能为空");
        Assert.notNull(this.getIsvNo(),"所属渠道不能为空");
        Assert.notNull(this.getSettleType(),"结算到类型不能为空");
    }

    public void preDivCheck() {
        //分账方只能是商户
        this.setUserType(USER_TYPE_MERCHANT);
        preCheck();
        Assert.isTrue(USER_TYPE_MERCHANT == this.getUserType(),"分账分出方只能选择商户");
    }

    public void preRecCheck() {
        preCheck();
        Assert.notNull(this.getType(),"分账账号类型不能为空");
        Assert.notNull(this.getInfo(),"分账结算信息不能为空");
        this.getInfo().preCheck(this.getSettleType());
        this.extData = JSON.toJSONString(this.getInfo());
    }
}
