package com.jeequan.jeepay.db.entity;

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.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * 
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-12-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_mch_third_config")
public class MchThirdConfig implements Serializable {

    private static final long serialVersionUID=1L;

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

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

    /**
     * 通道商户编号
     */
    private String channelMerchantId;

    /**
     * 类型 取值范围：wx zfb
     */
    private String thirdType;

    /**
     * 配置类型，取值范围：00 支付Appid 01 jsapi授权目录
     */
    private String type;

    /**
     * 子商户号 对应微信或支付宝的子商户号 后期可能涉及银联子商户号
     */
    private String subMchNo;

    /**
     * 支付Appid类型，枚举值. 取值范围：00 公众号; 01 小程序
     */
    private String appidType;

    /**
     * appid
     */
    private String subAppid;

    /**
     * jsapi授权目录，长度256位
     */
    private String jsapiPath;

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

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

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

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

    @Getter
    @AllArgsConstructor
    public enum ThirdType{
        WX,
        ZFB;
    }

    @Getter
    @AllArgsConstructor
    public enum Type{
        APPID("00"),
        PATH("01");
        private final String code;

        public static Type getVal(String code){
            Type[] values = values();
            for (Type val:values) {
                if(val.getCode().equals(code)){
                    return val;
                }
            }
            throw new BizException("非法[type]参数类型");
        }
    }


    @Getter
    @AllArgsConstructor
    public enum AppidType{
        PUBLIC("00","公众号"),
        APPLET("01","小程序");
        private final String code;
        private final String desc;

        public static AppidType getVal(String code){
            AppidType[] values = values();
            for (AppidType val:values) {
                if(val.getCode().equals(code)){
                    return val;
                }
            }
            throw new BizException("非法[appidType]参数类型");
        }
    }

    @TableField(exist = false)
    private String mchNo;


    public void preCheck() {
        if(StringUtils.isEmpty(this.getMchNo())){
            throw new BizException("缺失商户号参数");
        }
        if(StringUtils.isEmpty(this.getType())){
            throw new BizException("缺失[type]参数");
        }
        Type val = Type.getVal(this.getType());
        switch (val){
            case PATH:
                if(StringUtils.isEmpty(this.getJsapiPath())){
                    throw new BizException("支付授权目录配置时，[jsapiPath]参数不能为空");
                }
                if(!this.getJsapiPath().endsWith("/")){
                    throw new BizException("支付授权目录配置时，[jsapiPath]必须要以/结尾");
                }
                break;
            case APPID:
                if(StringUtils.isEmpty(this.getAppidType())){
                    throw new BizException("缺失APPID类型[appidType]参数");
                }
                if(StringUtils.isEmpty(this.getSubAppid())){
                    throw new BizException("缺失[subAppid]参数");
                }
        }
    }
}
