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 com.baomidou.mybatisplus.annotation.TableField;
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;

    @TableField
    private String photoType;
    private String photoUrl;
    private String picUrl;
    @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 String getStatus() {
        return status;
    }

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

    public BankCard getBankCard() {
        return bankCard;
    }

    public void setBankCard(BankCard bankCard) {
        this.bankCard = bankCard;
    }

    public IdCard getIdcard() {
        return idcard;
    }

    public void setIdcard(IdCard idcard) {
        this.idcard = idcard;
    }

    public String getMerchantType() {
        return merchantType;
    }

    public void setMerchantType(String merchantType) {
        this.merchantType = merchantType;
    }

    public String getPhotoType() {
        return photoType;
    }

    public void setPhotoType(String photoType) {
        this.photoType = photoType;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public String getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }

    public String getPicUrl1() {
        return picUrl1;
    }

    public void setPicUrl1(String picUrl1) {
        this.picUrl1 = picUrl1;
    }

    public String getPicUrl2() {
        return picUrl2;
    }

    public void setPicUrl2(String picUrl2) {
        this.picUrl2 = picUrl2;
    }

    public String getPicUrl3() {
        return picUrl3;
    }

    public void setPicUrl3(String picUrl3) {
        this.picUrl3 = picUrl3;
    }

    public String getPicUrl6() {
        return picUrl6;
    }

    public void setPicUrl6(String picUrl6) {
        this.picUrl6 = picUrl6;
    }

    public String getPicUrl7() {
        return picUrl7;
    }

    public void setPicUrl7(String picUrl7) {
        this.picUrl7 = picUrl7;
    }

    public String getPicUrl8() {
        return picUrl8;
    }

    public void setPicUrl8(String picUrl8) {
        this.picUrl8 = picUrl8;
    }

    public String getPicUrl9() {
        return picUrl9;
    }

    public void setPicUrl9(String picUrl9) {
        this.picUrl9 = picUrl9;
    }

    public String getPicUrl999() {
        return picUrl999;
    }

    public void setPicUrl999(String picUrl999) {
        this.picUrl999 = picUrl999;
    }

    public String getPicUrl101() {
        return picUrl101;
    }

    public void setPicUrl101(String picUrl101) {
        this.picUrl101 = picUrl101;
    }

    public String getPicUrl102() {
        return picUrl102;
    }

    public void setPicUrl102(String picUrl102) {
        this.picUrl102 = picUrl102;
    }

    public String getPicUrl11() {
        return picUrl11;
    }

    public void setPicUrl11(String picUrl11) {
        this.picUrl11 = picUrl11;
    }

    /**
     * 通过数据获取进件类型
     */
    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("请传入正确的银行卡类型");
        }
    }
}