package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.OrgPersonnelChangeApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.OrgPersonnelChangeCO;
import org.qinan.safetyeval.client.dto.OrgPersonnelChangeAddCmd;
import org.qinan.safetyeval.client.dto.OrgPersonnelChangeModifyCmd;
import org.qinan.safetyeval.client.dto.OrgPersonnelChangePageQuery;
import org.qinan.safetyeval.domain.entity.OrgPersonnelChangeEntity;
import org.qinan.safetyeval.domain.query.OrgPersonnelChangeQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.OrgPersonnelChangeDomainService;
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;
import org.springframework.util.StringUtils;

/**
 * 人员变更记录执行器（App层）
 *
 * @author safety-eval
 */
@Service
public class OrgPersonnelChangeExecutor implements OrgPersonnelChangeApi {

    @Lazy
    @Autowired(required = false)
    private OrgPersonnelChangeDomainService orgPersonnelChangeDomainService;

    @Override
    public SingleResponse<OrgPersonnelChangeCO> add(OrgPersonnelChangeAddCmd cmd) {
        OrgPersonnelChangeEntity entity = new OrgPersonnelChangeEntity();
        entity.setChangeItem(cmd.getChangeItem());
        entity.setChangeTime(cmd.getChangeTime());
        // 操作人从AuthUserContextAdapter获取，不从前端Cmd传入
        String operator = AuthUserContextAdapter.getCurrentUserName();
        entity.setOperatorName(StringUtils.hasText(operator) ? operator : "系统");

        OrgPersonnelChangeEntity result = orgPersonnelChangeDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<OrgPersonnelChangeCO> get(Long id) {
        OrgPersonnelChangeEntity entity = orgPersonnelChangeDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<OrgPersonnelChangeCO> modify(OrgPersonnelChangeModifyCmd cmd) {
        OrgPersonnelChangeEntity entity = new OrgPersonnelChangeEntity();
        entity.setId(cmd.getId());
        entity.setChangeItem(cmd.getChangeItem());
        entity.setChangeTime(cmd.getChangeTime());
        // 操作人从AuthUserContextAdapter获取，不从前端Cmd传入
        String operator = AuthUserContextAdapter.getCurrentUserName();
        entity.setOperatorName(StringUtils.hasText(operator) ? operator : "系统");

        OrgPersonnelChangeEntity result = orgPersonnelChangeDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

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

    @Override
    public PageResponse<OrgPersonnelChangeCO> page(OrgPersonnelChangePageQuery query) {
        OrgPersonnelChangeQuery domainQuery = new OrgPersonnelChangeQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setOrgId(query.getOrgId());
        domainQuery.setPersonnelId(query.getPersonnelId());
        domainQuery.setChangeItem(query.getChangeItem());

        PageResult<OrgPersonnelChangeEntity> pageResult = orgPersonnelChangeDomainService.page(domainQuery);

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

    private OrgPersonnelChangeCO toCO(OrgPersonnelChangeEntity entity) {
        if (entity == null) {
            return null;
        }
        OrgPersonnelChangeCO co = new OrgPersonnelChangeCO();
        co.setId(entity.getId());
        co.setPersonnelId(entity.getPersonnelId());
        co.setChangeItem(entity.getChangeItem());
        co.setChangeTime(entity.getChangeTime());
        co.setOperatorName(entity.getOperatorName());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
