package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.OrgPositionApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.OrgPositionCO;
import org.qinan.safetyeval.client.dto.OrgPositionAddCmd;
import org.qinan.safetyeval.client.dto.OrgPositionModifyCmd;
import org.qinan.safetyeval.client.dto.OrgPositionPageQuery;
import org.qinan.safetyeval.domain.entity.OrgPositionEntity;
import org.qinan.safetyeval.domain.query.OrgPositionQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.OrgPositionDomainService;
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 OrgPositionExecutor implements OrgPositionApi {

    @Lazy
    @Autowired(required = false)
    private OrgPositionDomainService orgPositionDomainService;

    @Override
    public SingleResponse<OrgPositionCO> add(OrgPositionAddCmd cmd) {
        OrgPositionEntity entity = new OrgPositionEntity();
        entity.setPositionName(cmd.getPositionName());
        entity.setDutyDesc(cmd.getDutyDesc());
        entity.setDeptId(cmd.getDeptId());
        entity.setRemark(cmd.getRemark());

        OrgPositionEntity result = orgPositionDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<OrgPositionCO> get(Long id) {
        OrgPositionEntity entity = orgPositionDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<OrgPositionCO> modify(OrgPositionModifyCmd cmd) {
        OrgPositionEntity entity = new OrgPositionEntity();
        entity.setId(cmd.getId());
        entity.setPositionName(cmd.getPositionName());
        entity.setDutyDesc(cmd.getDutyDesc());
        entity.setDeptId(cmd.getDeptId());
        entity.setRemark(cmd.getRemark());

        OrgPositionEntity result = orgPositionDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<OrgPositionCO> page(OrgPositionPageQuery query) {
        OrgPositionQuery domainQuery = new OrgPositionQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setOrgId(query.getOrgId());
        domainQuery.setDeptId(query.getDeptId());
        domainQuery.setPositionName(query.getPositionName());

        PageResult<OrgPositionEntity> pageResult = orgPositionDomainService.page(domainQuery);

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

    private OrgPositionCO toCO(OrgPositionEntity entity) {
        if (entity == null) {
            return null;
        }
        OrgPositionCO co = new OrgPositionCO();
        co.setId(entity.getId());
        co.setDeptId(entity.getDeptId());
        co.setPositionName(entity.getPositionName());
        co.setDutyDesc(entity.getDutyDesc());
        co.setRemark(entity.getRemark());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
