package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.OrgPersonnelCertApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.OrgPersonnelCertCO;
import org.qinan.safetyeval.client.dto.OrgPersonnelCertAddCmd;
import org.qinan.safetyeval.client.dto.OrgPersonnelCertModifyCmd;
import org.qinan.safetyeval.client.dto.OrgPersonnelCertPageQuery;
import org.qinan.safetyeval.domain.entity.OrgPersonnelCertEntity;
import org.qinan.safetyeval.domain.query.OrgPersonnelCertQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.OrgPersonnelCertDomainService;
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 OrgPersonnelCertExecutor implements OrgPersonnelCertApi {

    @Lazy
    @Autowired(required = false)
    private OrgPersonnelCertDomainService orgPersonnelCertDomainService;

    @Override
    public SingleResponse<OrgPersonnelCertCO> add(OrgPersonnelCertAddCmd cmd) {
        OrgPersonnelCertEntity entity = new OrgPersonnelCertEntity();
        entity.setCertName(cmd.getCertName());
        entity.setCertTypeCode(cmd.getCertTypeCode());
        entity.setCertTypeName(cmd.getCertTypeName());
        entity.setCertCategoryCode(cmd.getCertCategoryCode());
        entity.setCertCategoryName(cmd.getCertCategoryName());
        entity.setOperationCategoryCode(cmd.getOperationCategoryCode());
        entity.setOperationCategoryName(cmd.getOperationCategoryName());
        entity.setCertNo(cmd.getCertNo());
        entity.setIssueOrg(cmd.getIssueOrg());
        entity.setValidStartDate(cmd.getValidStartDate());
        entity.setValidEndDate(cmd.getValidEndDate());
        entity.setReviewDate(cmd.getReviewDate());
        entity.setCertAttachmentUrl(cmd.getCertAttachmentUrl());
        entity.setPersonnelId(cmd.getPersonnelId());

        OrgPersonnelCertEntity result = orgPersonnelCertDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<OrgPersonnelCertCO> get(Long id) {
        OrgPersonnelCertEntity entity = orgPersonnelCertDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<OrgPersonnelCertCO> modify(OrgPersonnelCertModifyCmd cmd) {
        OrgPersonnelCertEntity entity = new OrgPersonnelCertEntity();
        entity.setId(cmd.getId());
        entity.setCertName(cmd.getCertName());
        entity.setCertTypeCode(cmd.getCertTypeCode());
        entity.setCertTypeName(cmd.getCertTypeName());
        entity.setCertCategoryCode(cmd.getCertCategoryCode());
        entity.setCertCategoryName(cmd.getCertCategoryName());
        entity.setOperationCategoryCode(cmd.getOperationCategoryCode());
        entity.setOperationCategoryName(cmd.getOperationCategoryName());
        entity.setCertNo(cmd.getCertNo());
        entity.setIssueOrg(cmd.getIssueOrg());
        entity.setValidStartDate(cmd.getValidStartDate());
        entity.setValidEndDate(cmd.getValidEndDate());
        entity.setReviewDate(cmd.getReviewDate());
        entity.setCertAttachmentUrl(cmd.getCertAttachmentUrl());
        entity.setPersonnelId(cmd.getPersonnelId());

        OrgPersonnelCertEntity result = orgPersonnelCertDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<OrgPersonnelCertCO> page(OrgPersonnelCertPageQuery query) {
        OrgPersonnelCertQuery domainQuery = new OrgPersonnelCertQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setOrgId(query.getOrgId());
        domainQuery.setPersonnelId(query.getPersonnelId());
        domainQuery.setCertName(query.getCertName());
        domainQuery.setCertTypeCode(query.getCertTypeCode());

        PageResult<OrgPersonnelCertEntity> pageResult = orgPersonnelCertDomainService.page(domainQuery);

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

    private OrgPersonnelCertCO toCO(OrgPersonnelCertEntity entity) {
        if (entity == null) {
            return null;
        }
        OrgPersonnelCertCO co = new OrgPersonnelCertCO();
        co.setId(entity.getId());
        co.setPersonnelId(entity.getPersonnelId());
        co.setCertName(entity.getCertName());
        co.setCertTypeCode(entity.getCertTypeCode());
        co.setCertTypeName(entity.getCertTypeName());
        co.setCertCategoryCode(entity.getCertCategoryCode());
        co.setCertCategoryName(entity.getCertCategoryName());
        co.setOperationCategoryCode(entity.getOperationCategoryCode());
        co.setOperationCategoryName(entity.getOperationCategoryName());
        co.setCertNo(entity.getCertNo());
        co.setIssueOrg(entity.getIssueOrg());
        co.setValidStartDate(entity.getValidStartDate());
        co.setValidEndDate(entity.getValidEndDate());
        co.setReviewDate(entity.getReviewDate());
        co.setCertAttachmentUrl(entity.getCertAttachmentUrl());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
