package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.QualFilingPersonnelCertApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.QualFilingPersonnelCertCO;
import org.qinan.safetyeval.client.dto.QualFilingPersonnelCertAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingPersonnelCertModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingPersonnelCertPageQuery;
import org.qinan.safetyeval.domain.entity.QualFilingPersonnelCertEntity;
import org.qinan.safetyeval.domain.query.QualFilingPersonnelCertQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.QualFilingPersonnelCertDomainService;
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 QualFilingPersonnelCertExecutor implements QualFilingPersonnelCertApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingPersonnelCertDomainService qualFilingPersonnelCertDomainService;

    @Override
    public SingleResponse<QualFilingPersonnelCertCO> add(QualFilingPersonnelCertAddCmd cmd) {
        QualFilingPersonnelCertEntity entity = new QualFilingPersonnelCertEntity();
        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());

        QualFilingPersonnelCertEntity result = qualFilingPersonnelCertDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<QualFilingPersonnelCertCO> get(Long id) {
        QualFilingPersonnelCertEntity entity = qualFilingPersonnelCertDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<QualFilingPersonnelCertCO> modify(QualFilingPersonnelCertModifyCmd cmd) {
        QualFilingPersonnelCertEntity entity = new QualFilingPersonnelCertEntity();
        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());

        QualFilingPersonnelCertEntity result = qualFilingPersonnelCertDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<QualFilingPersonnelCertCO> page(QualFilingPersonnelCertPageQuery query) {
        QualFilingPersonnelCertQuery domainQuery = new QualFilingPersonnelCertQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setFilingId(query.getFilingId());
        domainQuery.setFilingPersonnelId(query.getFilingPersonnelId());
        domainQuery.setCertName(query.getCertName());

        PageResult<QualFilingPersonnelCertEntity> pageResult = qualFilingPersonnelCertDomainService.page(domainQuery);

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

    private QualFilingPersonnelCertCO toCO(QualFilingPersonnelCertEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingPersonnelCertCO co = new QualFilingPersonnelCertCO();
        co.setId(entity.getId());
        co.setFilingId(entity.getFilingId());
        co.setFilingPersonnelId(entity.getFilingPersonnelId());
        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;
    }
}
