package cn.pluss.platform.entity;

import cn.pluss.platform.exception.MsgException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.Objects;

/**
 * @author DJH
 */
@Data
@Accessors(chain = true)
public class MerchantChannelStatus {

    @Getter
    @AllArgsConstructor
    public enum AuditStatus {

        RESET("0", null, "未开通"),
        EXAMINING("1", "-100", "待审核"),

        CHANNEL_REQ("1", "-999","进件请求中"),
        CHANNEL_EXAMINING("1", null,"通道审核中"),
        REJECT("2", null, "已驳回"),
        SUCCESS("3", null, "进件通过"),
        SIGNING("8", null, "待签约"),
        CHANNEL_ARTIFICIAL_EXAMINING("11", null, "通道人工审核中"),
        ;

        public static AuditStatus get(String code, String thirdCode) {
            AuditStatus result = null;
            for (AuditStatus item : AuditStatus.values()) {
                if (Objects.equals(code, item.getCode()) && item.getThirdCode() == null) {
                    result = item;
                }

                if (Objects.equals(code, item.getCode()) && Objects.equals(thirdCode, item.getThirdCode())) {
                    return item;
                }
            }

            if (result == null) {
                throw new MsgException("未知的进件状态");
            }

            return result;
        }

        public static AuditStatus get(MerchantChannelStatus mcs) {
            String mcsStatus = mcs.getStatus();
            String mcsThirdStatus = mcs.getThirdStatus();

            return get(mcsStatus, mcsThirdStatus);
        }

        private final String code;

        private final String thirdCode;

        private final String desc;
    }

    public static final int VALID_STATUS_BAN = -1;

    public static final int VALID_STATUS_NORMAL = 0;

    public static final int VALID_STATUS_IN_USE = 1;

    /**
     * 微信认证授权状态 - 未授权
     */
    public static final String AUTHORIZATION_STATUS_UNAUTHORIZED = "0";

    /**
     * 微信认证授权状态 - 已授权
     */
    public static final String AUTHORIZATION_STATUS_AUTHORIZED = "1";

    /**
     * 后台审核驳回
     */
    public static final String AUDIT_STATUS_RESET = "0";
    /**
     * 审核中
     */
    public static final String AUDIT_STATUS_EXAMINING = "1";

    /**
     * 通道人工审核中
     */
    public static final String AUDIT_STATUS_ARTIFICIAL_EXAMINING = "11";

    /**
     * 审核不通过
     */
    public static final String AUDIT_STATUS_REJECT = "2";
    /**
     * 审核通过
     */
    public static final String AUDIT_STATUS_SUCCESS = "3";
    /**
     * 秒审通过
     */
    public static final String AUDIT_STATUS_FIRST_TRIAL_SUCCESS = "4";
    /**
     * 复审不通过
     */
    public static final String AUDIT_STATUS_REVIEW_REJECT = "5";

    public static final String AUDIT_STATUS_DATA_EDIT = "6";

    /**
     * D0未开通状态
     */
    public static final String AUDIT_STATUS_QUICK_CASH = "7";

    /**
     * 待签约状态
     */
    public static final String AUDIT_STATUS_WAITING_SIGN = "8";

    /**
     * 已签约
     */
    public static final String AUDIT_STATUS_SIGNED = "9";

    /**
     * 自定义第三方待审核状态
     */
    public static final String AUDIT_THIRD_STATUS_WAITING = "-100";

    /**
     * 进件中
     */
    public static final String AUDIT_THIRD_STATUS_AUDITING = "-999";


    @TableId
    private Integer id;

    private Integer channel;

    @TableField(exist = false)
    private String channelName;

    private String merchantCode;

    private String merchantId;

    /**
     * 1、审核中。2、审核不通过。3、审核通过(最终状态)。4、秒审通过。5、秒审通过复审不通过
     */
    private String status;

    @TableField(exist = false)
    private String statusDesc;

    /**
     * 微信认证状态, 0: 未认证; 1: 已认证; -1: 不支持
     */
    private String authorizationStatus;

    /**
     * app需要的状态表
     *
     * @return 简化的状态
     */
    public String getAppStatus() {

        if (Objects.equals(status, MerchantChannelStatus.AUDIT_STATUS_RESET)
                && Objects.equals(thirdStatus, MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING)) {
            return MerchantChannelStatus.AUDIT_STATUS_REJECT;
        }

        if (MerchantChannelStatus.AUDIT_STATUS_DATA_EDIT.equals(status)) {
            return MerchantChannelStatus.AUDIT_STATUS_SUCCESS;
        }

        if (MerchantChannelStatus.AUDIT_STATUS_QUICK_CASH.equals(status)) {
            return MerchantChannelStatus.AUDIT_STATUS_EXAMINING;
        }

        if (MerchantChannelStatus.AUDIT_STATUS_WAITING_SIGN.equals(status)) {
            return MerchantChannelStatus.AUDIT_STATUS_WAITING_SIGN;
        }

        if (MerchantChannelStatus.AUDIT_STATUS_SIGNED.equals(status)) {
            return MerchantChannelStatus.AUDIT_STATUS_EXAMINING;
        }

        if (MerchantChannelStatus.AUDIT_STATUS_ARTIFICIAL_EXAMINING.equals(status)) {
            return MerchantChannelStatus.AUDIT_STATUS_EXAMINING;
        }

        return status;
    }

    private String thirdStatus;

    private String remark;

    /**
     * 两个时间由数据库自动进行更新。
     */
    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private Date createTime;

    /**
     * 进件完成时间
     */
    private Date auditSuccessTime;

    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private Date updateTime;

    /**
     * 业务id，部分通道进件结果查询需要用到
     */
    private String applicationId;

    /**
     * 回调处理状态
     */
    private String callbackStatus;

    /**
     * 结算类型：D0、D1、T1
     * @since 2021/11/2 15:20
     */
    private String settlementType;

    /**
     * 发起方商户号
     */
    private String srcMerchantNo;

    /**
     * 通道标识，默认，1为待进件数据，"D0"为通道4的标识, "D1"为其他通道的标识
     */
    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private String virChannelFlag;

    @TableField(exist = false)
    private Long userId;

    /**
     * 其他数据
     */
    @TableField(whereStrategy = FieldStrategy.NEVER)
    private JSONObject extra;

    public JSONObject initExtra() {
        if (extra == null) {
            extra = new JSONObject();
            setExtra(extra);
        }

        return extra;
    }

    public JSONObject getExtra() {
        if (extra == null) {
            extra = new JSONObject();
            setExtra(extra);
        }

        return extra;
    }

    private Integer valid;

    /**
     * 通道商户名称，虚拟列
     */
    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private String mercName;

    /**
     * 接口版本
     */
    private String interfaceVersion;

    /**
     * 支付宝认证状态, 0: 未认证; 1: 已认证; -1: 不支持
     */
    private String aliAuthorizationStatus;

    private String wxCertUrl;

    private String aliCertUrl;

    /**
     * 是否能付款
     *
     * @return
     */
    public boolean isPayment() {
        boolean flag = false;
        if ("3".equals(this.getStatus()) || "4".equals(this.getStatus()) || "6".equals(this.getStatus())) {
            flag = true;
        }
        return flag;
    }

    /**
     * 判断商户是否可以申请进件
     *
     * @param merchantChannelStatus 进件状态信息
     * @return ignored
     */
    public static void checkReqAudit(MerchantChannelStatus merchantChannelStatus) {
        if (merchantChannelStatus == null) {
            return;
        }

        String status = merchantChannelStatus.getStatus();
        if (status == null) {
            return;
        }

        if (AUDIT_STATUS_EXAMINING.equals(status)) {
            MsgException.throwException("进件审核中，请勿重复进件");
        }
    }

    /**
     * 商户申请进件后，客服进行初次进件校验后
     * 判断商户是否可以进行过审进件
     *
     * @param merchantChannelStatus 进件状态信息
     * @return ignored
     */
    public static void checkCanAudit(MerchantChannelStatus merchantChannelStatus) {
        if (merchantChannelStatus == null) {
            MsgException.throwException("商户未提交审核");
            return;
        }

        String status = merchantChannelStatus.getStatus();
        String thirdStatus = merchantChannelStatus.getThirdStatus();

        if (MerchantChannelStatus.AUDIT_STATUS_REJECT.equals(status)||!MerchantChannelStatus.AUDIT_STATUS_EXAMINING.equals(status) || !AUDIT_THIRD_STATUS_WAITING.equals(thirdStatus)) {
            MsgException.throwException("不符合商户审核条件");
        }
    }

    /**
     * 是否已进件
     * @param mcs
     * @return
     */
    public static boolean isAudited(MerchantChannelStatus mcs) {
        if (mcs == null) {
            return false;
        }

        return MerchantChannelStatus.AUDIT_STATUS_SUCCESS.equals(mcs.getStatus()) ||
                MerchantChannelStatus.AUDIT_STATUS_DATA_EDIT.equals(mcs.getStatus()) ||
                MerchantChannelStatus.AUDIT_STATUS_FIRST_TRIAL_SUCCESS.equals(mcs.getStatus());
    }
}
