package com.jeequan.jeepay.db.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.jeequan.jeepay.core.entity.AccountCashInfo;
import com.jeequan.jeepay.core.entity.AccountRuleExt;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.BaseModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 账户表
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-12-11
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "t_account_info",autoResultMap = true)
public class AccountInfo extends BaseModel {

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

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

    private static final long serialVersionUID=1L;

    /**
     * 开启
     */
    public static final Byte REMIND_OPNE = 1;

    /**
     * 关闭
     */
    public static final Byte REMIND_CLOSE = 0;

    /**
     * 账户编号
     */
    @TableId
    private String accountNo;

    /**
     * 账户所属id
     */
    private String infoId;

    /**
     * 账户所属类型
     */
    private String infoType;

    /**
     * 账户类型 0：退款账户 1：营销账户 2：虚拟银行 3：钱包 4:手续费账户
     */
    private Byte type;

    /**
     * 余额
     */
    private Long balacne;

    /**
     * 不可用余额
     */
    private Long freeze;

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

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


    @Getter
    @AllArgsConstructor
    public enum Type{

        REFUND((byte)0,"退款账户"),

        MARKET((byte)1,"营销账户"),

        VIRTUALLY((byte)2,"虚拟银行账户"),

        WALLET((byte)3,"钱包账户"),

        SERVICE_CHARGE((byte)4,"手续费账户");

        private final Byte type;

        private final String desc;

        public static Type getVal(byte value){
            Type[] values = values();
            for (Type val:values) {
                if(val.getType() == value){
                    return val;
                }
            }
            throw new BizException("账户类型非法");
        }
    }

    @TableField(exist = false)
    private String typeNameDesc;

    public String getTypeNameDesc() {
        if(type == null){
            return "";
        }
        Type val = Type.getVal(this.getType());
        return val.getDesc();
    }


    @TableField(exist = false)
    private String realName;

    @TableField(exist = false)
    private String phone;

    /**
     * 充值金额
     */
    @TableField(exist = false)
    private Long rechargeAmt;

    /**
     * 是否开通提醒
     */
    private Byte remindState;

    /**
     * 提醒规则
     */
    @TableField(value = "remind_rule_ext",typeHandler = FastjsonTypeHandler.class)
    private JSONObject remindRuleExt;
    /**
     * 最后通知时间
     */
    private Date lastNoticeTime;

    @TableField(exist = false)
    private AccountRuleExt ruleExt;

    /**
     * 提现信息
     */
    @TableField(exist = false)
    private AccountCashInfo cashInfo;

    public void preRuleCheck() {
        if(this.getAccountNo() == null){
            throw new BizException("账户编号不能为空");
        }
        if(this.getRemindState() == null){
            throw new BizException("提醒状态不能为空");
        }
        if(REMIND_OPNE.equals(this.getRemindState())){
            if(this.getRuleExt() == null){
                throw new BizException("开启预警提醒时，请设置预警规则");
            }
            Long amount = this.getRuleExt().getAmount();
            if(amount == null){
                throw new BizException("预警规则临界金额不能为空");
            }
            List<String> typeList = this.getRuleExt().getTypeList();
            if(typeList == null || typeList.isEmpty()){
                throw new BizException("预警规则提醒方式至少需要勾选一项");
            }
            Integer interval = this.getRuleExt().getInterval();
            if(interval == null){
                throw new BizException("预警规则提醒频率不能为空");
            }
            List<JSONObject> receiveList = this.getRuleExt().getReceiveList();
            if(receiveList == null || receiveList.isEmpty()){
                throw new BizException("预警规则接收信息人不能为空");
            }
            if(receiveList.size() > 5){
                throw new BizException("接收人设置超出上限");
            }
            boolean email = typeList.contains(AccountRuleExt.EMAIL);
            boolean phone = typeList.contains(AccountRuleExt.PHONE);
            boolean emailCheck = true;
            boolean phoneCheck = true;
            for (JSONObject receive:receiveList) {
                if(email){
                    String receEmail = receive.getString(AccountRuleExt.EMAIL);
                    if(StringUtils.isEmpty(receEmail)){
                        emailCheck = false;
                    }
                }
                if(phone){
                    String recePhone = receive.getString(AccountRuleExt.PHONE);
                    if(StringUtils.isEmpty(recePhone)){
                        phoneCheck = false;
                    }
                }
            }
            if(email && !emailCheck){
                throw new BizException("预警规则提醒方式包含了[邮箱方式]，接收人信息中请填写邮箱");
            }
            if(phone && !phoneCheck){
                throw new BizException("预警规则提醒方式包含了[短信方式]，接收人信息中请填写手机号");
            }
            this.remindRuleExt = (JSONObject) JSON.toJSON(this.getRuleExt());
        }else{
            this.remindRuleExt = null;
        }
    }

    public void preCashCheck() {
        if(this.getAccountNo() == null){
            throw new BizException("账户编号不能为空");
        }
        AccountCashInfo cashInfo = this.getCashInfo();
        if(cashInfo == null){
            throw new BizException("提现信息为空");
        }
        Long cashAmt = cashInfo.getCashAmt();
        if(cashAmt == null || cashAmt <= 0){
            throw new BizException("提现金额不能为空或提现金额必须要大于0");
        }
        String settleName = cashInfo.getSettleName();
        if(StringUtils.isEmpty(settleName)){
            throw new BizException("结算人名称不能为空");
        }
        String settleNo = cashInfo.getSettleNo();
        if(StringUtils.isEmpty(settleNo)){
            throw new BizException("结算人银行卡号不能为空");
        }
        String bankCardAreaCode = cashInfo.getBankCardAreaCode();
        if(StringUtils.isEmpty(bankCardAreaCode)){
            throw new BizException("结算人银行卡开户省市区编码不能为空");
        }
        String bankCardAreaName = cashInfo.getBankCardAreaName();
        if(StringUtils.isEmpty(bankCardAreaName)){
            throw new BizException("结算人银行卡开户省市区不能为空");
        }
        String bankName = cashInfo.getBankName();
        if(StringUtils.isEmpty(bankName)){
            throw new BizException("结算人银行卡开户行不能为空");
        }
        String settleCard = cashInfo.getSettleCard();
        if(StringUtils.isEmpty(settleCard)){
            throw new BizException("结算人身份证号不能为空");
        }
        String bankMobile = cashInfo.getBankMobile();
        if(StringUtils.isEmpty(bankMobile)){
            throw new BizException("开户行手机号不能为空");
        }
    }
}
