package com.jeequan.jeepay.db.entity;

import cn.hutool.core.lang.Assert;
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.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.jeequan.jeepay.core.model.BaseModel;
import com.jeequan.jeepay.model.RuleStrategy;
import com.jeequan.jeepay.model.SeniorStrategy;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

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

/**
 * <p>
 * 商户支付路由
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-04-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "t_route",autoResultMap = true)
public class Route extends BaseModel {

    private static final long serialVersionUID=1L;

    public static final byte DEFAULT = 0;

    public static final byte LITE = 1;
    /**
     * id
     */
    @TableId(value = "route_id", type = IdType.INPUT)
    private String routeId;

    /**
     * 路由名称
     */
    private String name;

    /**
     * 所属商户
     */
    private String mchNo;


    @TableField(typeHandler = FastjsonTypeHandler.class)
    private RuleStrategy ruleStrategy;

    /**
     * 高级策略
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private SeniorStrategy seniorStrategy;

    /**
     * 创建者用户ID
     */
    private Long createdUid;

    /**
     * 创建者姓名
     */
    private String createdBy;

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

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

    /**
     * 运行次数
     */
    private Integer num;

    /**
     * 成功交易金额
     */
    private Long succAmount;


    /**
     * 总交易金额
     */
    private Long totalAmount;


    /**
     * 成功交易笔数
     */
    private Integer succNum;

    /**
     * 总交易笔数
     */
    private Integer totalNum;

    /**
     * 状态 1：正常  0：禁用
     */
    private Byte state;
    /**
     * 页面支付方式 0：默认 1：小程序
     */
    private Byte payMethod;

    /**
     * 是否默认
     */
    private Byte isDefault;

    /**
     * 所属商户
     */
    @TableField(exist = false)
    private String mchName;

    /**
     * 成功率
     */
    @TableField(exist = false)
    private BigDecimal succRate;

    /**
     * 门店数量
     */
    @TableField(exist = false)
    private Long storeNum;


    /**
     * 参数 校验
     */
    public void preCheck() {
        Assert.notEmpty(this.getName(),"路由名称不能为空");
        Assert.notNull(this.getRuleStrategy(),"路由策略规则不能为空");
        Assert.notNull(this.getIsDefault(),"是否是默认的路由策略不能为空");
        this.getRuleStrategy().preCheck();
        if(this.getSeniorStrategy() != null){
            this.getSeniorStrategy().preCheck();
        }else{
            SeniorStrategy seniorStrategy = new SeniorStrategy();
            this.setSeniorStrategy(seniorStrategy);
        }
    }

    /**
     * 重写成功率
     * @return
     */
    public BigDecimal getSuccRate() {
        if(this.getTotalNum() == null || this.getTotalNum() == BigDecimal.ZERO.intValue()){
            return BigDecimal.ZERO;
        }
        return new BigDecimal(this.getSuccNum()).divide(new BigDecimal(this.getTotalNum()),4, RoundingMode.HALF_EVEN).multiply(BigDecimal.valueOf(100));
    }
}
