package cn.pluss.platform.entity;

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

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

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

    public static final int VALID_STATUS_BAN = -1;

    public static final int VALID_STATUS_NORMAL = 0;

    /**
     * 微信认证授权状态 - 未授权
     */
    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;

    private String merchantCode;

    private String merchantId;

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


    /**
     * 微信认证状态，0：未认证；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_EXAMINING;
        }

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

        return status;
    }

     public String getStatus() {
         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;

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

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

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

    /**
     * @description:结算类型：D0、D1、T1
     * @date: 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;

    /**
     * 其他数据
     */
    private JSONObject extra;

    private Integer valid;

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

    /**
     * 是否能付款
     *
     * @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_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());
    }

    /**
     * (已进件的)商户当前是否可编辑
     * @param mcs   进件记录信息
     * @return
     */
    public static boolean isAuditedEditable(MerchantChannelStatus mcs) {
        if (mcs == null) {
            return false;
        }

        return mcs.getStatus().equals(MerchantChannelStatus.AUDIT_STATUS_SUCCESS);
    }

    /**
     * (未进件的)商户当前是否可编辑
     * @param mcs   进件记录信息
     * @return
     */
    public static boolean isUnauditedEditable(MerchantChannelStatus mcs) {
        if (mcs == null) {
            return true;
        }

        return MerchantChannelStatus.AUDIT_STATUS_REJECT.equals(mcs.getStatus()) ||
                MerchantChannelStatus.AUDIT_STATUS_RESET.equals(mcs.getStatus());
    }
}
