package nckd.jxccl.base.org.helper;

import kd.bos.common.enums.EnableEnum;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.orm.helper.QFilterCommonHelper;
import nckd.jxccl.base.orm.helper.QFilterPersonHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 人员辅助类
 * @author W.Y.C
 * @date 2025/7/7 18:23
 * @version 1.0
 */
public class PersonHelper {

    /** 组织人 */
    public static final String DEP_EMP_ENTITY_ID = "hrpi_depemp";

    /**
     * 查询根据条件查询有效组织人（任职类型为全职、用工关系状态为在职、组织团队分类为行政组织、业务状态为生效中、最新版本、初始状态为完成）
     * @param selectFields 查询字段
     * @param filters 自定义过滤条件
     * @return: kd.bos.dataentity.entity.DynamicObjectCollection
     * @author W.Y.C
     * @date: 2025/07/07 18:37
     */
    public static DynamicObjectCollection getDepEmpEffectivePerson(String selectFields, QFilter... filters) {
        // 构建基础过滤条件
        QFilter baseFilter = QFilterCommonHelper.getBusinessStatusFilter()
                .and(QFilterPersonHelper.getPostCategoryOfStaffFilter())
                .and(QFilterPersonHelper.getLaborRelStatusOfStaffFilter())
                .and(QFilterPersonHelper.getOtClassifyFilter())
                .and(QFilterCommonHelper.getCurrentVersionFilter())
                .and(QFilterCommonHelper.getDataStatusFilter())
                .and(QFilterCommonHelper.getInitStatusFinishFilter());

        // 合并 filters
        List<QFilter> filterList = new ArrayList<>();
        filterList.add(baseFilter);
        if (filters != null && filters.length > 0) {
            filterList.addAll(Arrays.asList(filters));
        }

        // 防止 selectFields 为空
        if (StringUtils.isBlank(selectFields)) {
            selectFields = FormConstant.ID_KEY;
        }
        // 执行查询并返回结果
        return QueryServiceHelper.query(DEP_EMP_ENTITY_ID, selectFields, filterList.toArray(new QFilter[0]));
    }

    /**
     * 获取人员教育经历信息
     * @param personId 人员ID
     * @param gradutionDate 毕业时间（如果传入此时间则取学历的毕业时间小于等于当前传入的时间，否则取最高学历）
     * @return: kd.bos.dataentity.entity.DynamicObject
     * @author W.Y.C
     * @date: 2025/09/12 11:15
     */
    public static DynamicObject getEducationExperienceByPerson(Long personId, Date gradutionDate) {
        QFilter educationFilter = new QFilter("employee.id", QCP.equals, personId);
        if(gradutionDate != null) {
            educationFilter.and(new QFilter("gradutiondate", QCP.less_equals, gradutionDate));
        }else{
            educationFilter.and(new QFilter("ishighestdegree", QCP.equals, EnableEnum.YES.getCode()));
        }
        String selectFields = String.join(",",
                //入学时间
                "admissiondate",
                //毕业时间
                "gradutiondate",
                //毕业院校
                "graduateschool",
                //学历-编码
                "education.number",
                //学历-名称
                "education.name",
                //学历-排序号
                "education.index",
                //学历-积分
                "education.nckd_score",
                //全日制
                "isfulltime",
                //学制(年)
                "schoolsystem",
                //最高学历
                "ishighestdegree",
                //学位-编码
                "degree.number",
                //学位-名称
                "degree.name",
                //学位顺序号
                "degree.degreeseq",
                //海外教育经历
                "isoverseas"
        );
        DynamicObject[] load = BusinessDataServiceHelper.load("hrpi_pereduexp", selectFields, new QFilter[]{educationFilter}, "gradutiondate desc");
        return load.length > 0 ? load[0] : null;
    }

    /**
     * 获取人员聘任的职称信息
     * TODO [待修改]-目前这里取人员附表的职称后续需要取聘任管理的职称
     * @param personId 人员ID
     * @param appointmentDate 聘任时间（如果传入此时间则取聘任时间小于等于当前传入的时间，否则取最最新聘任）
     * @return: kd.bos.dataentity.entity.DynamicObject
     * @author W.Y.C
     * @date: 2025/09/12 11:15
     */
    public static DynamicObject getProfessionalTitleByPerson(Long personId, Date appointmentDate) {
        QFilter filter = new QFilter("employee.id", QCP.equals, personId);
        if(appointmentDate != null) {
            filter.and(new QFilter("awardtime", QCP.less_equals, appointmentDate));
        }else{
            filter.and(new QFilter("ishigh", QCP.equals, EnableEnum.YES.getCode()));
        }
        String selectFields = String.join(",",
                //职称名称
                "professional",
                //授予日期
                "awardtime",
                //职称级别-编码
                "prolevel.number",
                //职称级别-名称
                "prolevel.name",
                //职称级别-排序号
                "prolevel.index",
                //职称级别-积分
                "prolevel.nckd_score"
        );
        DynamicObject[] load = BusinessDataServiceHelper.load("hrpi_perprotitle", selectFields, new QFilter[]{filter}, "awardtime desc");
        return load.length > 0 ? load[0] : null;
    }

    /**
     * 获取人员聘任的技能信息
     * TODO [待修改]-目前这里取人员附表的职称后续需要取聘任管理的技能
     * @param personId 人员ID
     * @param appointmentDate 聘任时间（如果传入此时间则取聘任时间小于等于当前传入的时间，否则取最最新聘任）
     * @return: kd.bos.dataentity.entity.DynamicObject
     * @author W.Y.C
     * @date: 2025/09/12 11:15
     */
    public static DynamicObject getSkillByPerson(Long personId, Date appointmentDate) {
        QFilter filter = new QFilter("employee.id", QCP.equals, personId);
        if(appointmentDate != null) {
            filter.and(new QFilter("registratedate", QCP.less_equals, appointmentDate));
        }else{
            filter.and(new QFilter("ismajor", QCP.less_equals, EnableEnum.YES.getCode()));
        }
        String selectFields = String.join(",",
                //职业资格-名称
                "qualification.name",
                //职业资格-编码
                "qualification.number",
                //聘任或注册日期
                "registratedate",
                //职业资格等级-编码
                "qualevel.number",
                //职业资格等级-名称
                "qualevel.name",
                //职业资格等级-排序号
                "qualevel.index",
                //职业资格等级-积分
                "qualevel.nckd_score"
        );
        DynamicObject[] load = BusinessDataServiceHelper.load("hrpi_perocpqual", selectFields, new QFilter[]{filter}, "registratedate desc");
        return load.length > 0 ? load[0] : null;
    }

}