package nckd.jxccl.base.hrpi.helper;

import com.google.common.collect.Lists;
import kd.bos.common.enums.EnableEnum;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;

import java.util.Collection;
import java.util.Date;

/**
 * 任职经历Helper类
 * 提供员工任职经历相关信息的查询功能
 *
 * @author W.Y.C
 * @date 2025/11/4 15:33
 * @version 1.0
 */
public class EmpPosOrgRelHelper {

    //根据id查询任职经历
    public static DynamicObject[] queryEmpPosOrgRelById(Collection<Long> ids) {
        // 构建基础查询条件
        QFilter queryFilter = new QFilter(FormConstant.ID_KEY, QCP.in, ids);

        // 构建查询字段和排序规则
        QueryFieldBuilder queryFieldBuilder = selectProperties();
        queryFieldBuilder.addIdNumberName(FormConstant.POSITION_KEY,FormConstant.NCKD_WORKNATURE);
        queryFieldBuilder.addIdNumberName(FormConstant.ADMINORG,FormConstant.NCKD_FIRSTORG);
        queryFieldBuilder.addIdNumberName(FormConstant.ADMINORG,FormConstant.NCKD_SECONDORG);
        queryFieldBuilder.addIdNumberName(FormConstant.ADMINORG,FormConstant.NCKD_THIRDORG);
        queryFieldBuilder.addIdNumberName(FormConstant.ADMINORG,FormConstant.NCKD_FOURTHORG);
        queryFieldBuilder.addIdNumberName(FormConstant.ADMINORG,FormConstant.NCKD_FIFTHORG);
        queryFieldBuilder.addIdNumberName(FormConstant.ADMINORG,FormConstant.NCKD_SIXTHORG);
        String selectProperties = queryFieldBuilder.buildSelect();
        String order = selectProperties().buildOrder();

        // 执行查询并返回结果
        return BusinessDataServiceHelper.load(FormConstant.HRPI_EMPPOSORGREL, selectProperties, new QFilter[]{queryFilter}, order);
    }
    /**
     * 根据员工ID查询最新的任职经历信息
     *
     * @param employeeId 员工ID
     * @return 员工的任职经历信息，如果不存在则返回null
     * @author W.Y.C
     * @date: 2025/11/04 16:02
     */
    public static DynamicObject queryEmpPosOrgRelByEmployees(Long employeeId) {
        DynamicObject[] dynamicObjects = queryEmpPosOrgRelByEmployees(Lists.newArrayList(employeeId), null);
        return dynamicObjects.length > 0 ? dynamicObjects[0] : null;
    }

    /**
     * 根据多个员工ID查询最新的任职经历信息
     *
     * @param employeeIds 员工ID集合
     * @return 员工的任职经历信息数组
     * @author W.Y.C
     * @date: 2025/11/04 16:02
     */
    public static DynamicObject[] queryEmpPosOrgRelByEmployees(Collection<Long> employeeIds) {
        return queryEmpPosOrgRelByEmployees(employeeIds, null);
    }

    /**
     * 获取员工最新有效任职经历
     * 查询条件包括：
     * 1. 员工ID在指定范围内
     * 2. 是最新记录（IS_SEQLATESTRECORD=YES）
     * 3. 未删除（IS_DELETED=NO）
     * 4. 是主任职（IS_PRIMARY=YES）
     *
     * @param employeeIds 员工ID集合
     * @param otherFilter 其他查询条件（可选）
     * @return 员工的任职经历信息数组
     * @author W.Y.C
     * @date: 2025/11/04 16:02
     */
    public static DynamicObject[] queryEmpPosOrgRelByEmployees(Collection<Long> employeeIds, QFilter otherFilter) {
        // 构建基础查询条件

        QFilter queryFilter = new QFilter(FormConstant.EMPLOYEE_KEY, QCP.in, employeeIds)
                .and(FormConstant.IS_SEQLATESTRECORD, QCP.equals, EnableEnum.YES.getCode())
                .and(FormConstant.IS_DELETED, QCP.equals, EnableEnum.NO.getCode())
                .and(FormConstant.IS_PRIMARY, QCP.equals, EnableEnum.YES.getCode())
                .and(FormConstant.STARTDATE, QCP.less_equals, new Date())
                .and(FormConstant.ENDDATE, QCP.large_equals, new Date());

        // 添加额外查询条件（如果有）
        if (otherFilter != null) {
            queryFilter.and(otherFilter);
        }

        // 构建查询字段和排序规则
        String selectProperties = selectProperties().buildSelect();
        String order = selectProperties().buildOrder();

        // 执行查询并返回结果
        return BusinessDataServiceHelper.load(FormConstant.HRPI_EMPPOSORGREL, selectProperties, new QFilter[]{queryFilter}, order);
    }

    /**
     * 构建查询字段和排序规则
     * 包含员工基本信息、岗位类型、组织架构等相关字段
     *
     * @return QueryFieldBuilder 查询字段构建器
     * @author W.Y.C
     * @date: 2025/11/04 16:02
     */
    public static QueryFieldBuilder selectProperties() {
        return QueryFieldBuilder.create()
                .add(FormConstant.ID_KEY)
                .add(FormConstant.IS_PRIMARY)
                .addIdNumberName(FormConstant.EMPLOYEE_KEY)
                .addIdNumberName(FormConstant.CHGACTION)
                .addIdNumberName(FormConstant.POSTYPE)
                .addIdNumberName(FormConstant.POS_STATUS)
                // 所属公司
                .addIdNumberName(FormConstant.COMPANY_KEY)
                // 行政组织（部门）
                .addIdNumberName(FormConstant.ADMINORG)
                // 岗位
                .addIdNumberName(FormConstant.POSITION_KEY)
                .orderDesc(FormConstant.STARTDATE)
                .orderDesc(FormConstant.ENDDATE);
    }
}
