package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.QualFilingReviewApi;
import org.qinan.safetyeval.client.co.QualFilingReviewCO;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.QualFilingReviewAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingReviewModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingReviewPageQuery;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.domain.entity.QualFilingReviewEntity;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.query.QualFilingReviewQuery;
import org.qinan.safetyeval.domain.service.QualFilingReviewDomainService;
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 QualFilingReviewExecutor implements QualFilingReviewApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingReviewDomainService qualFilingReviewDomainService;

    @Override
    public SingleResponse<QualFilingReviewCO> add(QualFilingReviewAddCmd cmd) {
        QualFilingReviewEntity entity = new QualFilingReviewEntity();
        entity.setFilingId(cmd.getFilingId());
        entity.setReviewResult(cmd.getReviewResult());
        entity.setReviewResultRemark(cmd.getReviewResultRemark());
        entity.setFilingExpertId(cmd.getFilingExpertId());
        entity.setFilingExpertName(cmd.getFilingExpertName());
        entity.setReviewOpinion(cmd.getReviewOpinion());

        QualFilingReviewEntity result = qualFilingReviewDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<QualFilingReviewCO> get(Long id) {
        QualFilingReviewEntity entity = qualFilingReviewDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<QualFilingReviewCO> modify(QualFilingReviewModifyCmd cmd) {
        QualFilingReviewEntity entity = new QualFilingReviewEntity();
        entity.setId(cmd.getId());
        entity.setFilingId(cmd.getFilingId());
        entity.setReviewResult(cmd.getReviewResult());
        entity.setReviewResultRemark(cmd.getReviewResultRemark());
        entity.setFilingExpertId(cmd.getFilingExpertId());
        entity.setFilingExpertName(cmd.getFilingExpertName());
        entity.setReviewOpinion(cmd.getReviewOpinion());

        QualFilingReviewEntity result = qualFilingReviewDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<QualFilingReviewCO> page(QualFilingReviewPageQuery query) {
        QualFilingReviewQuery domainQuery = new QualFilingReviewQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setFilingId(query.getFilingId());
        domainQuery.setReviewResult(query.getReviewResult());
        domainQuery.setFilingExpertId(query.getFilingExpertId());

        PageResult<QualFilingReviewEntity> pageResult = qualFilingReviewDomainService.page(domainQuery);

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

    private QualFilingReviewCO toCO(QualFilingReviewEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingReviewCO co = new QualFilingReviewCO();
        co.setId(entity.getId());
        co.setFilingId(entity.getFilingId());
        co.setReviewResult(entity.getReviewResult());
        co.setReviewResultRemark(entity.getReviewResultRemark());
        co.setFilingExpertId(entity.getFilingExpertId());
        co.setFilingExpertName(entity.getFilingExpertName());
        co.setReviewOpinion(entity.getReviewOpinion());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
