package com.chaozhanggui.dao.system.entity.DTO;

import com.baomidou.mybatisplus.annotation.TableField;
import com.chaozhanggui.dao.system.entity.TbPlussAccount;
import com.chaozhanggui.dao.system.entity.TbPlussBankCard;
import com.chaozhanggui.dao.system.entity.TbPlussIdCard;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;
import com.chaozhanggui.common.system.config.MsgException;

/**
 * 结算信息
 * @author DJH
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class AccountDTO extends TbPlussAccount {

    /**
     * 进件状态。0或无、待审核，1、审核中 2、审核失败
     */
    private String status;

    /**
     * 结算人银行卡
     */
    @Valid
    @NotNull(message = "请完善银行卡信息")
    private TbPlussBankCard bankCard;

    /**
     * 结算人身份信息
     */
    @Valid
    private TbPlussIdCard idcard;

    /**
     * 商户类型，-1则表示查询现有信息，其他类型则会过滤某些数据
     */
    private String merchantType;

    @TableField(exist = false)
    private String picUrl1;
    @TableField(exist = false)
    private String picUrl2;
    @TableField(exist = false)
    private String picUrl3;
    @TableField(exist = false)
    private String picUrl6;
    @TableField(exist = false)
    private String picUrl7;
    @TableField(exist = false)
    private String picUrl8;
    @TableField(exist = false)
    private String picUrl9;
    @TableField(exist = false)
    private String picUrl999;
    @TableField(exist = false)
    private String picUrl101;
    @TableField(exist = false)
    private String picUrl102;
    @TableField(exist = false)
    private String picUrl11;

    /**
     * 通过数据获取进件类型
     */
    public static void initSettleType(AccountDTO accountDTO) {
        TbPlussBankCard bankCard = accountDTO.getBankCard();
        TbPlussIdCard idCard = accountDTO.getIdcard();

        // 对公
        if (TbPlussBankCard.ACCOUNT_TYPE_CORPORATE.equals(bankCard.getAccounttype())) {
            accountDTO.setSettletype(TbPlussAccount.SETTLE_TYPE_CORPORATE);
            return;
        }

        // 法人-对私法人
        if (TbPlussBankCard.ACCOUNT_TYPE_PRIVATE.equals(bankCard.getAccounttype()) && idCard == null) {
            accountDTO.setSettletype(TbPlussAccount.LEGAL_PRIVATE_LEGAL);
            return;
        }
        if (idCard == null) {
           throw new MsgException("请上传身份证");
        }

        accountDTO.setSettletype(TbPlussAccount.LEGAL_PRIVATE_ILLEGAL);
    }

    /**
     * 请求数据校验
     */
    public void paramCheck() {
        if (StringUtils.isBlank(getSettletype())) {
            // 为空时候，默认为对公
            setSettletype(TbPlussAccount.SETTLE_TYPE_CORPORATE);
//            throw new MsgException("缺少结算类型参数");
        }

        if (Objects.equals(getSettletype(), TbPlussAccount.SETTLE_TYPE_PRIVATE_LEGAL) || Objects.equals(getSettletype(), TbPlussAccount.SETTLE_TYPE_CORPORATE)) {
            idcard = null;
        }

        if (idcard != null && !Objects.equals(idcard.getUserid(), getUserid())) {
            throw new MsgException("用户id应保持一致");
        }

        if (bankCard != null && !bankCard.getUserid().equals(getUserid())) {
            throw new MsgException("用户id应保持一致");
        }

        if (bankCard == null) {
            throw new MsgException("缺少银行卡信息");
        }

        if (bankCard.getAccounttype() == null) {
            bankCard.setAccounttype(TbPlussBankCard.ACCOUNT_TYPE_PRIVATE);
        }

        if (TbPlussBankCard.ACCOUNT_TYPE_CORPORATE.equals(bankCard.getAccounttype()) && StringUtils.isBlank(bankCard.getLicenseurl())) {
            throw new MsgException("对公账户需要传入开户许可证");
        }

        if (TbPlussBankCard.ACCOUNT_TYPE_CERT.equals(bankCard.getAccounttype())) {
            // 结算信息这里实名认证的类型作为对私账户来识别
            bankCard.setAccounttype(TbPlussBankCard.ACCOUNT_TYPE_PRIVATE);
        }

        if (!TbPlussBankCard.ACCOUNT_TYPE_PRIVATE.equals(bankCard.getAccounttype()) && !TbPlussBankCard.ACCOUNT_TYPE_CORPORATE.equals(bankCard.getAccounttype())) {
            throw new MsgException("请传入正确的银行卡类型");
        }
    }
}