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.extension.handlers.FastjsonTypeHandler;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.BaseModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 路由管理
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-04-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "t_route_manage",autoResultMap = true)
public class RouteManage extends BaseModel {

    /**
     * 门店id
     */
    @TableId(type = IdType.INPUT)
    private String storeId;


    private String applyId;
    /**
     * 所属用户
     */
    private String mchNo;

    /**
     * 所属应用
     */
    private String appid;

    private String ifCode;

    private String isvNo;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> payType;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> amtRange;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> timeRange;


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

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

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

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

    /**
     * 路由id
     */
    private String routeId;

    /**
     * 结算类型
     */
    private String settleType;

    /**
     * 状态 0：禁用 1：启用
     */
    private Byte state;

    /**
     * 路由状态 0：不路由 1：正常
     */
    private Byte routeState;

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

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

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

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

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

    /**
     * 门店名称
     */
    @TableField(exist = false)
    private String storeName;

    /**
     * 所属通道名称
     */
    @TableField(exist = false)
    private String ifName;


    /**
     * 商户名称
     */
    @TableField(exist = false)
    private String mchShortName;

    /**
     * 路由策略名称
     */
    @TableField(exist = false)
    private String routeName;

    /**
     * 路由策略名称
     */
    @TableField(exist = false)
    private String appName;

    /**
     * 路由策略名称
     */
    @TableField(exist = false)
    private String isvName;

    /**
     * 门店id集合
     */
    @TableField(exist = false)
    private List<String> storeIdList;

    /**
     * 订单号
     */
    @TableField(exist = false)
    private String payOrderId;

    @TableField(exist = false)
    private String opType;

    @TableField(exist = false)
    private Boolean isSend = false;

    @TableField(exist = false)
    private String sendVal;

    @TableField(exist = false)
    private RouteRecord routeRecord;

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

    /**
     * 参数校验
     */
    public void preCheck() {
        Assert.notEmpty(this.getStoreIdList(),"请选择门店信息不能为空");
        Assert.notEmpty(this.getRouteId(),"路由策略不能为空");
    }

    /**
     * 重写成功率
     * @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));
    }


}
