package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.QualFilingChangeDetailApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.QualFilingChangeDetailCO;
import org.qinan.safetyeval.client.dto.QualFilingChangeDetailAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingChangeDetailModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingChangeDetailPageQuery;
import org.qinan.safetyeval.domain.entity.QualFilingChangeDetailEntity;
import org.qinan.safetyeval.domain.query.QualFilingChangeDetailQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.QualFilingChangeDetailDomainService;
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 QualFilingChangeDetailExecutor implements QualFilingChangeDetailApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingChangeDetailDomainService qualFilingChangeDetailDomainService;

    @Override
    public SingleResponse<QualFilingChangeDetailCO> add(QualFilingChangeDetailAddCmd cmd) {
        QualFilingChangeDetailEntity entity = new QualFilingChangeDetailEntity();
        entity.setChangeField(cmd.getChangeField());
        entity.setChangeContent(cmd.getChangeContent());

        QualFilingChangeDetailEntity result = qualFilingChangeDetailDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<QualFilingChangeDetailCO> get(Long id) {
        QualFilingChangeDetailEntity entity = qualFilingChangeDetailDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<QualFilingChangeDetailCO> modify(QualFilingChangeDetailModifyCmd cmd) {
        QualFilingChangeDetailEntity entity = new QualFilingChangeDetailEntity();
        entity.setId(cmd.getId());
        entity.setChangeField(cmd.getChangeField());
        entity.setChangeContent(cmd.getChangeContent());

        QualFilingChangeDetailEntity result = qualFilingChangeDetailDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<QualFilingChangeDetailCO> page(QualFilingChangeDetailPageQuery query) {
        QualFilingChangeDetailQuery domainQuery = new QualFilingChangeDetailQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setChangeId(query.getChangeId());
        domainQuery.setChangeField(query.getChangeField());

        PageResult<QualFilingChangeDetailEntity> pageResult = qualFilingChangeDetailDomainService.page(domainQuery);

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

    private QualFilingChangeDetailCO toCO(QualFilingChangeDetailEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingChangeDetailCO co = new QualFilingChangeDetailCO();
        co.setId(entity.getId());
        co.setChangeId(entity.getChangeId());
        co.setChangeField(entity.getChangeField());
        co.setChangeContent(entity.getChangeContent());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
