package cn.pluss.platform.entity;

import cn.pluss.platform.exception.MsgException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
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 lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

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

@Setter
@Getter
@TableName(value = "tb_pluss_merc_change_record", autoResultMap = true)
public class MercChangeRecord {

    @TableId
    private Long id;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 商户基本信息
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private MerchantBaseInfo baseInfo;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private MerchantBaseInfo preBaseInfo;

    /**
     * 法人身份证信息
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private IdCard legalCardInfo;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private IdCard preLegalCardInfo;

    /**
     * 结算身份证信息
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private IdCard idCardInfo;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private IdCard preIdCardInfo;

    /**
     * 结算银行卡信息
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private BankCard bankCardInfo;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private BankCard preBankCardInfo;

    /**
     * 费率信息
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private MerchantRateNew rateInfo;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private MerchantRateNew preRateInfo;

    private JSONObject ext;

    @TableField("changeForm")
    private String changeForm;

    /**
     * 状态：0: 待审核；1：审核中；2：审核驳回；3：审核通过
     */
    private String status;

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatus(MercChangeRecord.Status status) {
        this.setStatus(status.getValue());
    }

    /**
     * 备注信息，驳回理由
     */
    private String remark;

    /**
     * 通道业务id
     */
    private String applicationId;

    /**
     * 通道id
     */
    private String channelId;

    public String getChannelType() {
        if ("1".equals(channelId) || "3".equals(channelId)) {
            return "D1";
        }

        if ("4".equals(channelId)) {
            return "D0";
        }

        return null;
    }

    public String getChannelName() {
        if ("1".equals(channelId)) {
            return "随行付";
        }

        if ("3".equals(channelId)) {
            return "瑞银信";
        }

        if ("4".equals(channelId)) {
            return "银盛";
        }

        return null;
    }

    public String getTitle() {
        if ("1".equals(channelId)) {
            return "D1次日到账-随行付通道";
        }

        if ("3".equals(channelId)) {
            return "D1次日到账-瑞银信通道";
        }

        if ("4".equals(channelId)) {
            return "D0实时到账-银盛通道";
        }

        return null;
    }

    public String getChannelTypeDesc() {
        if ("1".equals(channelId) || "3".equals(channelId)) {
            return "次日到账";
        }

        if ("4".equals(channelId)) {
            return "实时到账";
        }

        return null;
    }

    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private String virMerchantId;

    /**
     * 变更类型，
     * 1：基本信息变更、法人信息变更(补充)；
     * 2：结算信息变更；
     * 3：费率变更；
     * 5：结算人信息变更(补充)；
     */
    private String changeType;

    public void setChangeType(String changeType) {
        this.changeType = changeType;
    }

    public void setChangeType(MercChangeRecord.Type changeType) {
        this.changeType = changeType.getValue();
    }

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 审核出结果时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date resultTime;

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @Getter
    @AllArgsConstructor
    public enum Status {

        EDIT("0", "编辑中"),
        AUDITING("1", "审核中"),
        REFUSE("2", "已驳回"),
        SUCCESS("3", "通过已生效"),
        /**
         * 审核通过，未生效，该状态主要用于随行付变更费率第二天生效的情况
         */
        SUCCESS_INVALID("4", "通过未生效"),
        /**
         * 已过期
         */
        EXPIRED("5", "已过期"),
        NO_DATA("-1", "未开通");

        public static Status getByStatus(String value) {
            for (Status status : Status.values()) {
                if (status.getValue().equals(value)) {
                    return status;
                }
            }

            return null;
        }

        public static Status getAppStatus(String value) {
            Status byStatus = getByStatus(value);
            if (byStatus == Status.EDIT) {
                return Status.AUDITING;
            }

            return byStatus;
        }

        private final String value;

        private final String desc;
    }

    @Getter
    @AllArgsConstructor
    public enum Type {
        BASE("1"),
        /**
         * 结算信息
         */
        ACCOUNT("2"),
        RATE("3"),
        /**
         * 营业执照
         */
        BLL("4"),
        CARD("5"),
        ACCOUNT_CARD("52"),
        ADDRESS("6"),
        /** 交易提额 */
        AMOUNT_LIMIT("7"),
        /** 远程交易 */
        BOUNDARY("8"),
        /** 分时结算 */
        TIME_OF_USE("9"),
        /** 重置报备 */
        RESET("10"),
        CANCELLATION("11"),
        ;

        private final String value;

        public static Type getByValue(String value) {
            for (Type type : Type.values()) {
                if (Objects.equals(value, type.value)) {
                    return type;
                }
            }

            throw new MsgException("未知的操作类型");
        }
    }
}
