package com.ruoyi.ss.riskInfo.service.impl;

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.realName.domain.RealNameVO;
import com.ruoyi.ss.realName.service.RealNameService;
import com.ruoyi.ss.risk.domain.enums.RiskSubmitType;
import com.ruoyi.ss.risk.service.RiskValidator;
import com.ruoyi.ss.riskInfo.domain.RiskInfo;
import com.ruoyi.ss.riskInfo.domain.RiskInfoQuery;
import com.ruoyi.ss.riskInfo.domain.RiskInfoVO;
import com.ruoyi.ss.riskInfo.domain.enums.RiskInfoStatus;
import com.ruoyi.ss.riskInfo.service.RiskInfoService;
import com.ruoyi.ss.riskInfo.service.RiskInfoValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author wjh
 * 2024/11/25
 */
@Service
public class RiskInfoValidatorImpl implements RiskInfoValidator {
    @Autowired
    private RiskInfoService riskInfoService;

    @Autowired
    private RiskValidator riskValidator;

    @Autowired
    private RealNameService realNameService;

    @Override
    public boolean isUser(Long infoId, Long userId) {
        if (userId == null || infoId == null) {
            return false;
        }
        RiskInfoVO info = riskInfoService.selectRiskInfoByInfoId(infoId);
        return isUser(info, userId);
    }

    @Override
    public boolean isUser(RiskInfoVO info, Long userId) {
        return info != null && userId != null && Objects.equals(userId, info.getUserId());
    }


    @Override
    public void preCheck(RiskInfo data) {
        ServiceUtil.assertion(data == null, "参数错误");

        // 校验风控是否存在
        if (data.getRiskId() != null) {
            boolean exist = riskValidator.isExist(data.getRiskId());
            ServiceUtil.assertion(!exist, "风控信息不存在");
        }

        // 判断信息是否可修改
        if (data.getInfoId() != null) {
            RiskInfoVO old = riskInfoService.selectRiskInfoByInfoId(data.getInfoId());
            ServiceUtil.assertion(old == null, "待修改的材料信息不存在");
            ServiceUtil.assertion(!RiskInfoStatus.canUpdate().contains(old.getStatus()), "风控材料当前状态不允许修改");
        }
    }

    @Override
    public void preSubmitCheck(RiskInfoVO data) {
        ServiceUtil.assertion(data == null, "参数错误");
        ServiceUtil.assertion(!RiskInfoStatus.canSubmit().contains(data.getStatus()), "风控材料当前状态不允许提交");

        // 判断是否已有提交的，不允许重复提交
        this.checkRepeatSubmit(data.getRiskId(), data.getInfoId());

        if (CollectionUtils.isEmptyElement(data.getRiskSubmitType())) {
            return;
        }

        List<String> types = data.getRiskSubmitType();
        if (types.contains(RiskSubmitType.REAL_NAME.getCode())) {
            RealNameVO realName = realNameService.selectRealNameById(data.getRealNameId());
            ServiceUtil.assertion(realName == null, "请提交正确的实名认证信息");
        }
        if (types.contains(RiskSubmitType.ID_CARD.getCode())) {
            ServiceUtil.assertion(StringUtils.isAnyBlank(data.getIdCardFront(), data.getIdCardBack()), "请填写完整的身份证信息");
        }
        if (types.contains(RiskSubmitType.ID_CARD_HAND.getCode())) {
            ServiceUtil.assertion(StringUtils.isAnyBlank(data.getIdCardHand()), "请上传手持身份证照片");
        }
        if (types.contains(RiskSubmitType.VIDEO.getCode())) {
            ServiceUtil.assertion(StringUtils.isAnyBlank(data.getVideo()), "请上传使用场景视频");
        }
        if (types.contains(RiskSubmitType.DUTY_VIDEO.getCode())) {
            ServiceUtil.assertion(StringUtils.isAnyBlank(data.getDutyVideo()), "请上传责任视频");
        }
        if (types.contains(RiskSubmitType.BUSINESS_LICENCE.getCode())) {
            ServiceUtil.assertion(StringUtils.isAnyBlank(data.getBusinessLicence()), "请上传营业执照");
        }

    }

    private void checkRepeatSubmit(Long riskId, Long infoId) {
        if (riskId == null) {
            return;
        }
        RiskInfoQuery query = new RiskInfoQuery();
        query.setRiskId(riskId);
        query.setExcludeInfoId(infoId);
        query.setStatus(RiskInfoStatus.WAIT_VERIFY.getStatus());
        ServiceUtil.assertion(riskInfoService.selectCount(query) > 0, "当前风控有正在审核中的材料，请勿重复提交");
    }
}
