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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.hrmp.hrpi.business.domian.service.impl.PersonServiceImpl;
import kd.hrmp.hrpi.business.infrastructure.utils.QFilterUtil;

import java.util.*;

public class HRWorkflowUtils {

    private static final Log log = LogFactory.getLog(GetFirstDeptChargerWorkflowPlugin.class);

    public static String CONTRACT_ENTITY = "hlcm_contractfileother";

    /**
     * 根据部门ID获取一级部门ID
     * @param adminOrgId
     * @return
     */
    public static Long getFirstDeptId (Long adminOrgId) {
        String adminType = "";
        DynamicObject adminDyn = BusinessDataServiceHelper.loadSingle(adminOrgId, "haos_adminorghr");
        adminType = adminDyn.getString("orgtype.name");
        while(!adminType.equals("一级部门")) {
            adminDyn = BusinessDataServiceHelper.loadSingle(adminOrgId, "haos_adminorghr");
            adminType = adminDyn.getString("orgtype.name");
            if(!adminType.equals("一级部门")) {
                adminOrgId = adminDyn.getLong("parent.id");
            }
            if(adminOrgId == 0L)
                break;
        }
        return adminOrgId;
    }

    /**
     * 根据部门ID获取二级部门ID
     * @param adminOrgId
     * @return
     */
    public static Long getSecondDeptId (Long adminOrgId) {
        String adminType = "";
        DynamicObject adminDyn = BusinessDataServiceHelper.loadSingle(adminOrgId, "haos_adminorghr");
        adminType = adminDyn.getString("orgtype.name");
        while(!adminType.equals("二级部门")) {
            adminDyn = BusinessDataServiceHelper.loadSingle(adminOrgId, "haos_adminorghr");
            adminType = adminDyn.getString("orgtype.name");
            if (!adminType.equals("二级部门")) {
                adminOrgId = adminDyn.getLong("parent.id");
            }
            if(adminOrgId == 0L)
                break;
        }
        return adminOrgId;
    }

    /**
     * 根据岗位获取行政组织ID
     * @param positionId
     * @return
     */
    public static Long getAdminOrgIdByPositionId(Long positionId) {
        DynamicObject position = BusinessDataServiceHelper.loadSingle(positionId, "hbpm_positionhr");
        return position.getLong("adminorg.id");
    }

    public static List<Long> transPersonIdToUserId(List<Long> personIds) {
        log.info(" HRWorkflowUtils transPersonIdToUserId {}", personIds);
        List<Long> userIds = new ArrayList(personIds.size());
        Map<String, List<Long>> queryMap = new HashMap();
        queryMap.put("person", personIds);
        Map<String, Object> userInfoMap = PersonServiceImpl.getInstance().getUserIdByPersonInfo(queryMap);
        if (!userInfoMap.isEmpty() && userInfoMap.get("success") == Boolean.TRUE) {
            Map<Long, Object> data = (Map)userInfoMap.get("data");
            data.values().forEach((user) -> {
                Map<String, Long> userMap = (Map)user;
                if (userMap != null && !userMap.isEmpty()) {
                    userIds.add(userMap.get("user"));
                }

            });
        }
        return userIds;
    }

    public static String getPositionByUserId(Long uid) {
        Map<String, Object> map = PersonServiceImpl.getInstance().getPersonModelIdByUserId(uid);
        if(Boolean.valueOf(map.get("success").toString())) {
            Long personId = Long.valueOf(((Map) map.get("data")).get("person").toString());
            Map personMap = PersonServiceImpl.getInstance().getPersonInfo(personId);
            return null;
        }
        return null;
    }

    /**
     * 按照人员查询未终止的其他附属协议
     * @param personId
     * @return
     */
    public static DynamicObjectCollection getOtherContractByPersonId(String personId) {
        QFilter filter = QFilterUtil.getCurrentQf();
        filter.and("datastatus", QCP.equals, "1");
        filter.and("person.id", QCP.equals, Long.valueOf(personId));
        filter.and("startdate", QCP.less_equals, new Date());
        filter.and("enddate", QCP.large_equals, new Date());
        filter.and("actualenddate", QCP.is_null, null);
        filter.and("contracttype.number", QCP.equals, "1030_S");
        String selectFields = "entryentity.contracttemplate.name";
        DynamicObjectCollection contractCols = QueryServiceHelper.query(CONTRACT_ENTITY, selectFields, new QFilter[]{filter},"startdate desc,signstatus desc");
        return contractCols;
    }
}
