package cn.pluss.platform.dto;

import cn.pluss.platform.entity.Account;
import cn.pluss.platform.entity.BankCard;
import cn.pluss.platform.entity.IdCard;
import cn.pluss.platform.exception.MsgException;
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;

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

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

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

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

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

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

        // 对公
        if (BankCard.ACCOUNT_TYPE_CORPORATE.equals(bankCard.getAccountType())) {
            accountDTO.setSettleType(Account.SETTLE_TYPE_CORPORATE);
            return;
        }

        // 法人-对私法人
        if (BankCard.ACCOUNT_TYPE_PRIVATE.equals(bankCard.getAccountType()) && idCard == null) {
            accountDTO.setSettleType(Account.LEGAL_PRIVATE_LEGAL);
            return;
        }

        MsgException.check(idCard == null, "请传递身份证信息");

        accountDTO.setSettleType(Account.LEGAL_PRIVATE_ILLEGAL);
    }

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

        if (Objects.equals(getSettleType(), Account.SETTLE_TYPE_PRIVATE_LEGAL) || Objects.equals(getSettleType(), Account.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(BankCard.ACCOUNT_TYPE_PRIVATE);
        }

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

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

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