package nckd.jimin.jyyy.hr.plugin.workflow;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.workflow.api.AgentExecution;
import kd.bos.workflow.engine.extitf.IWorkflowPlugin;
import kd.hrmp.hrpi.business.domian.service.impl.PersonRoleServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通用工作流插件 Tyx 2025-05-26
 * 获取一级部门负责人
 */
public class GetFirstDeptChargerWorkflowPlugin implements IWorkflowPlugin {
    private static final Log log = LogFactory.getLog(GetFirstDeptChargerWorkflowPlugin.class);
    @Override
    public List<Long> calcUserIds(AgentExecution execution) {
        List<Long> approvers = execution.getCurrentApprover();
        log.info(" before 获取流程变量 personId ");
        String personId = execution.getVariable("personId").toString();
        log.info(" 获取流程变量 personId : {} ", personId);
        //获取人员主职部门
        Map<String, Object> posOrgRel = DispatchServiceHelper.invokeBizService("hrmp", "hrpi", "IHRPIPersonService", "getPrimaryEmpposorgrel", Long.valueOf(personId));
        log.info(" posOrgRel : {}", posOrgRel);
        Long adminOrgId = Long.valueOf(posOrgRel.get("adminorg_id").toString());
        log.info(" personId对应部门Id : {}", adminOrgId);
        //获取一级部门
        Long firstDeptId = HRWorkflowUtils.getFirstDeptId(adminOrgId);
        log.info(" 一级部门ID : {}", firstDeptId);
        //获取一级部门负责人
        List deptIds = new ArrayList();
        deptIds.add(firstDeptId);
        List mainChargeInfos = PersonRoleServiceImpl.getInstance().getMainChargeByOrg(deptIds);
        List chargerIds = new ArrayList();
        for(Object obj : mainChargeInfos) {
            Map map = (Map) obj;
            String charger = map.get("person").toString();
            log.info(" 计算出参与人personId : {}", charger);
            chargerIds.add(Long.valueOf(charger));
        }
        //personId转换userId
        List chargerUserId = HRWorkflowUtils.transPersonIdToUserId(chargerIds);
        approvers.addAll(chargerUserId);
        return approvers;
    }
}