package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.app.orchestrator.QualFilingOrchestrator;
import org.qinan.safetyeval.client.api.QualFilingApi;
import org.qinan.safetyeval.client.co.QualFilingCO;
import org.qinan.safetyeval.client.co.QualFilingComplianceCheckResultCO;
import org.qinan.safetyeval.client.co.QualFilingDetailCO;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.dto.QualFilingAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingAttachmentUploadCmd;
import org.qinan.safetyeval.client.dto.QualFilingModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingPageQuery;
import org.qinan.safetyeval.client.dto.QualFilingRejectCmd;
import org.qinan.safetyeval.domain.constant.QualFilingApplyTypeEnum;
import org.qinan.safetyeval.domain.entity.QualFilingEntity;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.QualFilingDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * 资质备案申请执行器（App层）
 */
@Service
public class QualFilingExecutor implements QualFilingApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingDomainService qualFilingDomainService;

    @Resource
    private QualFilingOrchestrator qualFilingOrchestrator;

    @Override
    public SingleResponse<QualFilingCO> add(QualFilingAddCmd cmd) {
        QualFilingEntity entity = new QualFilingEntity();
        entity.setFilingTerritoryCode(cmd.getFilingTerritoryCode());
        entity.setFilingTerritoryName(cmd.getFilingTerritoryName());
        entity.setFilingUnitName(cmd.getFilingUnitName());
        entity.setFilingUnitTypeCode(cmd.getFilingUnitTypeCode());
        entity.setFilingUnitTypeName(cmd.getFilingUnitTypeName());
        entity.setFilingNo(cmd.getFilingNo());
        entity.setBusinessScope(cmd.getBusinessScope());
        entity.setRegisterAddress(cmd.getRegisterAddress());
        entity.setOfficeAddress(cmd.getOfficeAddress());
        entity.setCreditCode(cmd.getCreditCode());
        entity.setQualCertNo(cmd.getQualCertNo());
        entity.setLegalPersonPhone(cmd.getLegalPersonPhone());
        entity.setContactPhone(cmd.getContactPhone());
        entity.setInfoDisclosureUrl(cmd.getInfoDisclosureUrl());
        entity.setFixedAssetAmount(cmd.getFixedAssetAmount());
        entity.setArchiveRoomArea(cmd.getArchiveRoomArea());
        entity.setFulltimeEvaluatorCount(cmd.getFulltimeEvaluatorCount());
        entity.setRegisteredEngineerCount(cmd.getRegisteredEngineerCount());
        entity.setWorkplaceArea(cmd.getWorkplaceArea());
        entity.setUnitIntro(cmd.getUnitIntro());
        entity.setAttachmentUrl(cmd.getAttachmentUrl());
        entity.setFilingStatusCode(cmd.getFilingStatusCode());
        entity.setFilingStatusName(cmd.getFilingStatusName());
        entity.setApplyTypeCode(cmd.getApplyTypeCode());
        entity.setApplyTypeName(cmd.getApplyTypeName());
        QualFilingEntity result = qualFilingDomainService.add(entity);
        return SingleResponse.success(toLegacyCO(result));
    }

    @Override
    public SingleResponse<QualFilingCO> get(Long id) {
        QualFilingEntity entity = qualFilingDomainService.get(id);
        return SingleResponse.success(toLegacyCO(entity));
    }

    @Override
    public SingleResponse<QualFilingCO> modify(QualFilingModifyCmd cmd) {
        return saveDraft(cmd);
    }

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

    @Override
    public PageResponse<QualFilingCO> page(QualFilingPageQuery query) {
        PageResult<QualFilingCO> pageResult = qualFilingOrchestrator.page(query);
        return PageResponse.of(pageResult.getRecords(), pageResult.getTotal());
    }

    @Override
    public SingleResponse<QualFilingCO> draft(Integer applyTypeCode) {
        return SingleResponse.success(qualFilingOrchestrator.createDraft(applyTypeCode));
    }

    @Override
    public SingleResponse<QualFilingCO> saveDraft(QualFilingModifyCmd cmd) {
        return SingleResponse.success(qualFilingOrchestrator.saveDraft(cmd));
    }

    @Override
    public SingleResponse<QualFilingCO> submit(Long id) {
        return SingleResponse.success(qualFilingOrchestrator.submit(id));
    }

    @Override
    public SingleResponse<QualFilingCO> approve(Long id) {
        return SingleResponse.success(qualFilingOrchestrator.approve(id));
    }

    @Override
    public SingleResponse<QualFilingCO> reject(QualFilingRejectCmd cmd) {
        return SingleResponse.success(qualFilingOrchestrator.reject(cmd.getId(), cmd.getRejectReason()));
    }

    @Override
    public SingleResponse<QualFilingDetailCO> detail(Long id) {
        return SingleResponse.success(qualFilingOrchestrator.getDetail(id));
    }

    @Override
    public SingleResponse<QualFilingCO> filedDraft() {
        return SingleResponse.success(
                qualFilingOrchestrator.createDraft(QualFilingApplyTypeEnum.FILED_MANAGE.getCode()));
    }

    @Override
    public SingleResponse<QualFilingCO> uploadAttachment(QualFilingAttachmentUploadCmd cmd) {
        return SingleResponse.success(qualFilingOrchestrator.uploadMainAttachment(cmd.getId(), cmd.getAttachmentUrl()));
    }

    @Override
    public SingleResponse<List<QualFilingComplianceCheckResultCO>> complianceCheck(Long id) {
        return SingleResponse.success(qualFilingOrchestrator.complianceCheck(id));
    }

    private QualFilingCO toLegacyCO(QualFilingEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingCO co = new QualFilingCO();
        co.setId(entity.getId());
        co.setFilingTerritoryCode(entity.getFilingTerritoryCode());
        co.setFilingTerritoryName(entity.getFilingTerritoryName());
        co.setFilingUnitName(entity.getFilingUnitName());
        co.setFilingUnitTypeCode(entity.getFilingUnitTypeCode());
        co.setFilingUnitTypeName(entity.getFilingUnitTypeName());
        co.setFilingNo(entity.getFilingNo());
        co.setBusinessScope(entity.getBusinessScope());
        co.setRegisterAddress(entity.getRegisterAddress());
        co.setOfficeAddress(entity.getOfficeAddress());
        co.setCreditCode(entity.getCreditCode());
        co.setQualCertNo(entity.getQualCertNo());
        co.setLegalPersonPhone(entity.getLegalPersonPhone());
        co.setContactPhone(entity.getContactPhone());
        co.setInfoDisclosureUrl(entity.getInfoDisclosureUrl());
        co.setFixedAssetAmount(entity.getFixedAssetAmount());
        co.setArchiveRoomArea(entity.getArchiveRoomArea());
        co.setFulltimeEvaluatorCount(entity.getFulltimeEvaluatorCount());
        co.setRegisteredEngineerCount(entity.getRegisteredEngineerCount());
        co.setWorkplaceArea(entity.getWorkplaceArea());
        co.setUnitIntro(entity.getUnitIntro());
        co.setAttachmentUrl(entity.getAttachmentUrl());
        co.setFilingStatusCode(entity.getFilingStatusCode());
        co.setFilingStatusName(entity.getFilingStatusName());
        co.setApplyTypeCode(entity.getApplyTypeCode());
        co.setApplyTypeName(entity.getApplyTypeName());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
