package org.qinan.safetyeval.app.support;

import org.qinan.safetyeval.client.co.OrgPersonnelCO;
import org.qinan.safetyeval.client.co.OrgResignApplyCO;
import org.qinan.safetyeval.domain.entity.OrgDepartmentEntity;
import org.qinan.safetyeval.domain.entity.OrgPersonnelCertEntity;
import org.qinan.safetyeval.domain.entity.OrgPersonnelChangeEntity;
import org.qinan.safetyeval.domain.entity.OrgPersonnelEntity;
import org.qinan.safetyeval.domain.entity.OrgPositionEntity;
import org.qinan.safetyeval.domain.entity.OrgResignApplyEntity;
import org.qinan.safetyeval.domain.gateway.OrgDepartmentGateway;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelCertGateway;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelChangeGateway;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelGateway;
import org.qinan.safetyeval.domain.gateway.OrgPositionGateway;
import org.qinan.safetyeval.domain.gateway.OrgResignApplyGateway;
import org.qinan.safetyeval.domain.query.OrgPersonnelCertQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 人员/离职列表展示字段组装（部门名、岗位名、证书名、账号等）。
 */
@Component
public class OrgPersonnelViewEnricher {

    @Autowired(required = false)
    private OrgDepartmentGateway orgDepartmentGateway;

    @Autowired(required = false)
    private OrgPositionGateway orgPositionGateway;

    @Autowired(required = false)
    private OrgPersonnelCertGateway orgPersonnelCertGateway;

    @Autowired(required = false)
    private OrgPersonnelGateway orgPersonnelGateway;

    @Autowired(required = false)
    private OrgPersonnelChangeGateway orgPersonnelChangeGateway;

    @Autowired(required = false)
    private OrgResignApplyGateway orgResignApplyGateway;

    public void enrichPersonnel(OrgPersonnelCO co) {
        if (co == null) {
            return;
        }
        if (co.getDeptId() != null && orgDepartmentGateway != null) {
            OrgDepartmentEntity dept = orgDepartmentGateway.get(co.getDeptId());
            if (dept != null) {
                co.setDeptName(dept.getDeptName());
            }
        }
        if (co.getPostId() != null && orgPositionGateway != null) {
            OrgPositionEntity position = orgPositionGateway.get(co.getPostId());
            if (position != null) {
                co.setPostName(position.getPositionName());
            }
        }
        if (co.getId() != null) {
            if (orgPersonnelCertGateway != null) {
                OrgPersonnelCertQuery certQuery = new OrgPersonnelCertQuery();
                certQuery.setPersonnelId(co.getId());
                certQuery.setPageNum(1L);
                certQuery.setPageSize(200L);
                PageResult<OrgPersonnelCertEntity> certPage = orgPersonnelCertGateway.page(certQuery);
                String certNames = certPage.getRecords().stream()
                        .map(OrgPersonnelCertEntity::getCertName)
                        .filter(StringUtils::hasText)
                        .collect(Collectors.joining("、"));
                co.setCertNames(certNames);
            }
            enrichChangeSummary(co);
        }
        if (co.getEmploymentStatusCode() == null) {
            co.setEmploymentStatusCode(1);
            co.setEmploymentStatusName("在职");
        }
    }

    private void enrichChangeSummary(OrgPersonnelCO co) {
        List<OrgPersonnelChangeEntity> changes = orgPersonnelChangeGateway != null ? orgPersonnelChangeGateway.listByPersonnelId(co.getId()) : null;
        co.setChangeCount(changes != null ? changes.size() : 0);

        List<OrgResignApplyEntity> applies = orgResignApplyGateway != null ? orgResignApplyGateway.listByPersonnelId(co.getId()) : null;
        if (applies == null || applies.isEmpty()) {
            return;
        }
        OrgResignApplyEntity displayApply = applies.stream()
                .filter(item -> item.getAuditStatusCode() != null && item.getAuditStatusCode() == 0)
                .findFirst()
                .orElse(applies.get(0));
        co.setResignApplyId(displayApply.getId());
        co.setResignAuditStatus(displayApply.getAuditStatusCode() != null ? displayApply.getAuditStatusCode() : 0);
    }

    public void enrichResignApply(OrgResignApplyCO co) {
        if (co == null) {
            return;
        }
        if (co.getAuditStatusCode() == null) {
            co.setAuditStatusCode(0);
            co.setAuditStatusName("未审核");
        }
        if (co.getPersonnelId() == null) {
            return;
        }
        if (orgPersonnelGateway == null) {
            return;
        }
        OrgPersonnelEntity personnel = orgPersonnelGateway.get(co.getPersonnelId());
        if (personnel == null) {
            return;
        }
        if (StringUtils.hasText(personnel.getUserName())) {
            co.setApplicantName(personnel.getUserName());
        }
        co.setAccount(personnel.getAccount());
        if (personnel.getDeptId() != null && orgDepartmentGateway != null) {
            OrgDepartmentEntity dept = orgDepartmentGateway.get(personnel.getDeptId());
            if (dept != null) {
                co.setDeptName(dept.getDeptName());
            }
        }
    }
}
