package org.qinan.safetyeval.app.support;

import org.qinan.safetyeval.domain.entity.OrgPersonnelCertEntity;
import org.qinan.safetyeval.domain.entity.OrgPersonnelEntity;
import org.qinan.safetyeval.domain.entity.QualFilingPersonnelCertEntity;
import org.qinan.safetyeval.domain.entity.QualFilingPersonnelEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelCertGateway;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelGateway;
import org.qinan.safetyeval.domain.gateway.QualFilingPersonnelCertGateway;
import org.qinan.safetyeval.domain.gateway.QualFilingPersonnelGateway;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 从企业人员库批量引入备案人员及证书快照
 */
@Component
public class QualFilingOrgPersonnelImporter {

    @Resource
    private OrgPersonnelGateway orgPersonnelGateway;

    @Resource
    private OrgPersonnelCertGateway orgPersonnelCertGateway;

    @Resource
    private QualFilingPersonnelGateway qualFilingPersonnelGateway;

    @Resource
    private QualFilingPersonnelCertGateway qualFilingPersonnelCertGateway;

    public int importFromOrg(Long filingId, List<Long> sourcePersonnelIds) {
        if (sourcePersonnelIds == null || sourcePersonnelIds.isEmpty()) {
            return 0;
        }
        Set<Long> existing = new HashSet<>();
        for (QualFilingPersonnelEntity item : qualFilingPersonnelGateway.listByFilingId(filingId)) {
            if (item.getSourcePersonnelId() != null) {
                existing.add(item.getSourcePersonnelId());
            }
        }
        int count = 0;
        for (Long sourceId : sourcePersonnelIds) {
            if (existing.contains(sourceId)) {
                throw new BizException(ErrorCode.QUAL_FILING_PERSONNEL_DUPLICATE);
            }
            OrgPersonnelEntity orgPersonnel = orgPersonnelGateway.get(sourceId);
            if (orgPersonnel == null) {
                throw new BizException(ErrorCode.ORG_PERSONNEL_NOT_FOUND);
            }
            QualFilingPersonnelEntity filingPersonnel = toFilingPersonnel(filingId, orgPersonnel);
            qualFilingPersonnelGateway.save(filingPersonnel);
            List<OrgPersonnelCertEntity> certs = orgPersonnelCertGateway.listByPersonnelId(sourceId);
            for (OrgPersonnelCertEntity cert : certs) {
                qualFilingPersonnelCertGateway.save(toFilingCert(filingId, filingPersonnel.getId(), cert));
            }
            count++;
        }
        return count;
    }

    private QualFilingPersonnelEntity toFilingPersonnel(Long filingId, OrgPersonnelEntity source) {
        QualFilingPersonnelEntity entity = new QualFilingPersonnelEntity();
        entity.setFilingId(filingId);
        entity.setSourcePersonnelId(source.getId());
        entity.setPersonName(source.getUserName());
        entity.setPersonTypeCode(source.getPersonTypeCode());
        entity.setPersonTypeName(source.getPersonTypeName());
        entity.setTitleName(source.getTitleName());
        entity.setGenderCode(source.getGenderCode());
        entity.setGenderName(source.getGenderName());
        entity.setBirthDate(source.getBirthDate());
        entity.setIdCardNo(source.getIdCardNo());
        entity.setCurrentAddress(source.getCurrentAddress());
        entity.setOfficeAddress(source.getOfficeAddress());
        entity.setEducationCode(source.getEducationCode());
        entity.setEducationName(source.getEducationName());
        entity.setGraduateSchool(source.getGraduateSchool());
        entity.setMajor(source.getMajor());
        entity.setQualScope(source.getQualScope());
        entity.setPublications(source.getPublications());
        if (StringUtils.hasText(source.getProfessionalLevelName()) || StringUtils.hasText(source.getEvaluatorCertNo())) {
            entity.setProfessionalLevelCert(source.getProfessionalLevelName() + "/" + source.getEvaluatorCertNo());
        }
        entity.setRegisterEngineerFlag(source.getRegisterEngineerFlag());
        entity.setAbilityDeclaration(source.getAbilityDeclaration());
        entity.setWorkExperience(source.getWorkExperience());
        entity.setProofMaterialUrl(source.getProofMaterialUrl());
        return entity;
    }

    private QualFilingPersonnelCertEntity toFilingCert(Long filingId, Long filingPersonnelId, OrgPersonnelCertEntity source) {
        QualFilingPersonnelCertEntity entity = new QualFilingPersonnelCertEntity();
        entity.setFilingId(filingId);
        entity.setFilingPersonnelId(filingPersonnelId);
        entity.setSourceCertId(source.getId());
        entity.setCertName(source.getCertName());
        entity.setCertTypeCode(source.getCertTypeCode());
        entity.setCertTypeName(source.getCertTypeName());
        entity.setCertCategoryCode(source.getCertCategoryCode());
        entity.setCertCategoryName(source.getCertCategoryName());
        entity.setOperationCategoryCode(source.getOperationCategoryCode());
        entity.setOperationCategoryName(source.getOperationCategoryName());
        entity.setCertNo(source.getCertNo());
        entity.setIssueOrg(source.getIssueOrg());
        entity.setValidStartDate(source.getValidStartDate());
        entity.setValidEndDate(source.getValidEndDate());
        entity.setReviewDate(source.getReviewDate());
        entity.setCertAttachmentUrl(source.getCertAttachmentUrl());
        return entity;
    }
}
