package com.ningdatech.carapi.driver.helper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.carapi.driver.constant.DriverContinuingEducationRegistrationStatusEnum;
import com.ningdatech.carapi.driver.constant.DriverRegistrationStatusEnum;
import com.ningdatech.carapi.driver.constant.DriverReplacementRegistrationStatusEnum;
import com.ningdatech.carapi.driver.entity.Certificate;
import com.ningdatech.carapi.driver.entity.DriverInfo;
import com.ningdatech.carapi.driver.model.po.ReqDriverTrainingRegistrationPO;
import com.ningdatech.carapi.driver.service.ICertificateService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author liuxinxin
 * @date 2022/11/11 下午1:58
 */
@Component
@RequiredArgsConstructor
public class RegistrationAvailableCheckHelper {
    private final ICertificateService iCertificateService;

    public void forensicRegistration(DriverInfo driverInfo) {
        String driverRegistrationStatus = driverInfo.getDriverRegistrationStatus();
        Boolean hasAlreadySignUp = StringUtils.isNotBlank(driverRegistrationStatus);
        if (hasAlreadySignUp) {
            throw new BizException("该驾驶员已经报名，无需重复报名");
        }
    }

    public void replacementRegistrationValidCheck(ReqDriverTrainingRegistrationPO request, DriverInfo driverInfo) {
        // 校验驾驶员当前是否有换证培训的权限
        String driverRegistrationStatus = driverInfo.getDriverRegistrationStatus();
        if (!DriverRegistrationStatusEnum.available.name().equals(driverRegistrationStatus)) {
            throw new BizException("请先通过驾驶员取证培训!");
        }
        String replacementRegistrationStatus = driverInfo.getReplacementRegistrationStatus();
        // 此次提交的报名信息为重新提交的换证报名
        if (Objects.nonNull(request.getDriverId())){
            return;
        }
        Boolean hasAlreadySignUp = (!DriverReplacementRegistrationStatusEnum.available.name().equals(replacementRegistrationStatus)
                && StringUtils.isNotBlank(replacementRegistrationStatus));
        if (hasAlreadySignUp) {
            throw new BizException("该驾驶员已经报名，无需重复报名");
        }
        Certificate certificate = iCertificateService.getOne(Wrappers.lambdaQuery(Certificate.class).eq(Certificate::getDriverId, driverInfo.getId()));
        LocalDateTime expiryDate = certificate.getExpiryDate();
        if (expiryDate.isAfter(LocalDateTime.now())) {
            throw new BizException("该合格证尚未过期，无需重复办理");
        }
    }

    public void continuingEducationRegistrationValidCheck(ReqDriverTrainingRegistrationPO request, DriverInfo driverInfo) {
        String driverRegistrationStatus = driverInfo.getDriverRegistrationStatus();
        if (!DriverRegistrationStatusEnum.available.name().equals(driverRegistrationStatus)) {
            throw new BizException("请先通过驾驶员取证培训报名");
        }
        // 此次提交的报名信息为重新提交的继续教育报名
        if (Objects.nonNull(request.getDriverId())){
            return;
        }
        String continuingEducationStatus = driverInfo.getContinuingEducationStatus();
        Boolean hasAlreadySignUp = (!DriverContinuingEducationRegistrationStatusEnum.available.name().equals(continuingEducationStatus)
                && StringUtils.isNotBlank(continuingEducationStatus));
        if (hasAlreadySignUp) {
            throw new BizException("该驾驶员已经报名，无需重复报名");
        }
    }
}
