package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.QualFilingExpertApi;
import org.qinan.safetyeval.client.co.QualFilingExpertCO;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.QualFilingExpertAddCmd;
import org.qinan.safetyeval.client.dto.QualFilingExpertModifyCmd;
import org.qinan.safetyeval.client.dto.QualFilingExpertPageQuery;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.domain.entity.QualFilingExpertEntity;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.query.QualFilingExpertQuery;
import org.qinan.safetyeval.domain.service.QualFilingExpertDomainService;
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 QualFilingExpertExecutor implements QualFilingExpertApi {

    @Lazy
    @Autowired(required = false)
    private QualFilingExpertDomainService qualFilingExpertDomainService;

    @Override
    public SingleResponse<QualFilingExpertCO> add(QualFilingExpertAddCmd cmd) {
        QualFilingExpertEntity entity = new QualFilingExpertEntity();
        entity.setUserName(cmd.getUserName());
        entity.setAccount(cmd.getAccount());
        entity.setGenderCode(cmd.getGenderCode());
        entity.setGenderName(cmd.getGenderName());
        entity.setIdCardNo(cmd.getIdCardNo());
        entity.setCertificate(cmd.getCertificate());

        QualFilingExpertEntity result = qualFilingExpertDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<QualFilingExpertCO> get(Long id) {
        QualFilingExpertEntity entity = qualFilingExpertDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<QualFilingExpertCO> modify(QualFilingExpertModifyCmd cmd) {
        QualFilingExpertEntity entity = new QualFilingExpertEntity();
        entity.setId(cmd.getId());
        entity.setUserName(cmd.getUserName());
        entity.setAccount(cmd.getAccount());
        entity.setGenderCode(cmd.getGenderCode());
        entity.setGenderName(cmd.getGenderName());
        entity.setIdCardNo(cmd.getIdCardNo());
        entity.setCertificate(cmd.getCertificate());

        QualFilingExpertEntity result = qualFilingExpertDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<QualFilingExpertCO> page(QualFilingExpertPageQuery query) {
        QualFilingExpertQuery domainQuery = new QualFilingExpertQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setUserName(query.getUserName());
        domainQuery.setAccount(query.getAccount());
        domainQuery.setGenderCode(query.getGenderCode());
        domainQuery.setIdCardNo(query.getIdCardNo());

        PageResult<QualFilingExpertEntity> pageResult = qualFilingExpertDomainService.page(domainQuery);

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

    private QualFilingExpertCO toCO(QualFilingExpertEntity entity) {
        if (entity == null) {
            return null;
        }
        QualFilingExpertCO co = new QualFilingExpertCO();
        co.setId(entity.getId());
        co.setUserName(entity.getUserName());
        co.setAccount(entity.getAccount());
        co.setGenderCode(entity.getGenderCode());
        co.setGenderName(entity.getGenderName());
        co.setIdCardNo(entity.getIdCardNo());
        co.setCertificate(entity.getCertificate());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
