package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.QualFilingPersonnelApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.QualFilingPersonnelCO;
import org.qinan.safetyeval.client.dto.QualFilingPersonnelAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingPersonnelModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingPersonnelPageQuery;
import org.qinan.safetyeval.domain.entity.QualFilingPersonnelEntity;
import org.qinan.safetyeval.domain.query.QualFilingPersonnelQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.QualFilingPersonnelDomainService;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 备案申请人员执行器（App层）
 *
 * @author safety-eval
 */
@Service
public class QualFilingPersonnelExecutor implements QualFilingPersonnelApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingPersonnelDomainService qualFilingPersonnelDomainService;

    @Override
    public SingleResponse<QualFilingPersonnelCO> add(QualFilingPersonnelAddCmd cmd) {
        QualFilingPersonnelEntity entity = new QualFilingPersonnelEntity();
        entity.setPersonName(cmd.getPersonName());
        entity.setPersonTypeCode(cmd.getPersonTypeCode());
        entity.setPersonTypeName(cmd.getPersonTypeName());
        entity.setPositionName(cmd.getPositionName());
        entity.setTitleName(cmd.getTitleName());
        entity.setGenderCode(cmd.getGenderCode());
        entity.setGenderName(cmd.getGenderName());
        entity.setBirthDate(cmd.getBirthDate());
        entity.setJoinWorkDate(cmd.getJoinWorkDate());
        entity.setIdCardNo(cmd.getIdCardNo());
        entity.setCurrentAddress(cmd.getCurrentAddress());
        entity.setOfficeAddress(cmd.getOfficeAddress());
        entity.setEducationCode(cmd.getEducationCode());
        entity.setEducationName(cmd.getEducationName());
        entity.setGraduateSchool(cmd.getGraduateSchool());
        entity.setMajor(cmd.getMajor());
        entity.setQualScope(cmd.getQualScope());
        entity.setPublications(cmd.getPublications());
        entity.setProfessionalLevelCert(cmd.getProfessionalLevelCert());
        entity.setRegisterEngineerFlag(cmd.getRegisterEngineerFlag());
        entity.setAbilityDeclaration(cmd.getAbilityDeclaration());
        entity.setWorkExperience(cmd.getWorkExperience());
        entity.setProofMaterialUrl(cmd.getProofMaterialUrl());

        QualFilingPersonnelEntity result = qualFilingPersonnelDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<QualFilingPersonnelCO> get(Long id) {
        QualFilingPersonnelEntity entity = qualFilingPersonnelDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<QualFilingPersonnelCO> modify(QualFilingPersonnelModifyCmd cmd) {
        QualFilingPersonnelEntity entity = new QualFilingPersonnelEntity();
        entity.setId(cmd.getId());
        entity.setPersonName(cmd.getPersonName());
        entity.setPersonTypeCode(cmd.getPersonTypeCode());
        entity.setPersonTypeName(cmd.getPersonTypeName());
        entity.setPositionName(cmd.getPositionName());
        entity.setTitleName(cmd.getTitleName());
        entity.setGenderCode(cmd.getGenderCode());
        entity.setGenderName(cmd.getGenderName());
        entity.setBirthDate(cmd.getBirthDate());
        entity.setJoinWorkDate(cmd.getJoinWorkDate());
        entity.setIdCardNo(cmd.getIdCardNo());
        entity.setCurrentAddress(cmd.getCurrentAddress());
        entity.setOfficeAddress(cmd.getOfficeAddress());
        entity.setEducationCode(cmd.getEducationCode());
        entity.setEducationName(cmd.getEducationName());
        entity.setGraduateSchool(cmd.getGraduateSchool());
        entity.setMajor(cmd.getMajor());
        entity.setQualScope(cmd.getQualScope());
        entity.setPublications(cmd.getPublications());
        entity.setProfessionalLevelCert(cmd.getProfessionalLevelCert());
        entity.setRegisterEngineerFlag(cmd.getRegisterEngineerFlag());
        entity.setAbilityDeclaration(cmd.getAbilityDeclaration());
        entity.setWorkExperience(cmd.getWorkExperience());
        entity.setProofMaterialUrl(cmd.getProofMaterialUrl());

        QualFilingPersonnelEntity result = qualFilingPersonnelDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<Void> delete(Long id) {
        qualFilingPersonnelDomainService.delete(id);
        return SingleResponse.success();
    }

    @Override
    public PageResponse<QualFilingPersonnelCO> page(QualFilingPersonnelPageQuery query) {
        QualFilingPersonnelQuery domainQuery = new QualFilingPersonnelQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setFilingId(query.getFilingId());
        domainQuery.setPersonName(query.getPersonName());

        PageResult<QualFilingPersonnelEntity> pageResult = qualFilingPersonnelDomainService.page(domainQuery);

        return PageResponse.of(
                pageResult.getRecords().stream()
                        .map(this::toCO)
                        .collect(java.util.stream.Collectors.toList()),
                pageResult.getTotal());
    }

    @javax.annotation.Resource
    private org.qinan.safetyeval.app.orchestrator.QualFilingOrchestrator qualFilingOrchestrator;

    @Override
    public SingleResponse<Integer> batchAddFromOrg(org.qinan.safetyeval.client.dto.QualFilingPersonnelBatchAddCmd cmd) {
        int count = qualFilingOrchestrator.batchAddPersonnel(cmd.getFilingId(), cmd.getSourcePersonnelIds());
        return SingleResponse.success(count);
    }

    private QualFilingPersonnelCO toCO(QualFilingPersonnelEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingPersonnelCO co = new QualFilingPersonnelCO();
        co.setId(entity.getId());
        co.setFilingId(entity.getFilingId());
        co.setSourcePersonnelId(entity.getSourcePersonnelId());
        co.setPersonName(entity.getPersonName());
        co.setPersonTypeCode(entity.getPersonTypeCode());
        co.setPersonTypeName(entity.getPersonTypeName());
        co.setPositionName(entity.getPositionName());
        co.setTitleName(entity.getTitleName());
        co.setGenderCode(entity.getGenderCode());
        co.setGenderName(entity.getGenderName());
        co.setBirthDate(entity.getBirthDate());
        co.setJoinWorkDate(entity.getJoinWorkDate());
        co.setIdCardNo(entity.getIdCardNo());
        co.setCurrentAddress(entity.getCurrentAddress());
        co.setOfficeAddress(entity.getOfficeAddress());
        co.setEducationCode(entity.getEducationCode());
        co.setEducationName(entity.getEducationName());
        co.setGraduateSchool(entity.getGraduateSchool());
        co.setMajor(entity.getMajor());
        co.setQualScope(entity.getQualScope());
        co.setPublications(entity.getPublications());
        co.setProfessionalLevelCert(entity.getProfessionalLevelCert());
        co.setRegisterEngineerFlag(entity.getRegisterEngineerFlag());
        co.setAbilityDeclaration(entity.getAbilityDeclaration());
        co.setWorkExperience(entity.getWorkExperience());
        co.setProofMaterialUrl(entity.getProofMaterialUrl());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
