package nckd.jxccl.base.swc.helper;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.hr.hbp.business.servicehelper.HRBaseServiceHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述: 封装SWC常用方法
 * @author turborao
 * @date 2025/11/26 10:05
 */
public class SWCHelper {

    private static final Log log = LogFactory.getLog(SWCHelper.class);
    /**
     * 根据任务ID查询任务信息
     * @param id
     * @return
     */
    public static DynamicObject queryTaskInfoById(Long id) {
        String selPros = "id,number,taskstatus, name, startdate, enddate, ishandleproration,taxitemschemev,country,org,calrulev,paysubjectv,calversionno,tasktype,payrollgroupv.currency.amtprecision,islimitadd,payrollgroupv.currency.name";
        return queryOne(selPros,"hsas_calpayrolltask", id);
    }

    /**
     * 查询单条数据
     * @param selectProperties
     * @param entityName
     * @param pk
     */
    public static DynamicObject queryOne(String selectProperties,String entityName, Object pk) {
        try {
            return BusinessDataServiceHelper.loadSingle(pk, entityName, selectProperties);
        } catch (RuntimeException var4) {
            log.error("Error occurs when query {}[{}]", entityName, pk);
            return null;
        }
    }

    /**
     * 查询单条数据
     * @param entityName
     * @param pk
     * @return
     */
    public static DynamicObject queryOne(String entityName,Object pk) {
        try {
            return BusinessDataServiceHelper.loadSingle(pk, entityName);
        } catch (RuntimeException var3) {
            log.error("Error occurs when query {}[{}]", entityName, pk);
            return null;
        }
    }

    /**
     * 查询单条数据
     * @param entityName
     * @param selectFields
     * @param fieldName
     * @param filtersValue
     * @return
     */
    public static DynamicObject queryOne(String entityName,String selectFields,String fieldName,Object filtersValue) {
        try {
            QFilter filter1 = new QFilter(fieldName, QCP.equals, filtersValue); // 启用
            return QueryServiceHelper.queryOne(entityName, selectFields,new QFilter[]{filter1});
        } catch (RuntimeException var3) {
            log.error("Error occurs when query {}[{},{}]", entityName, selectFields,fieldName);
            return null;
        }
    }

    /**
     * 批量查询数据 按员工ID
     * @param employeeId
     * @return
     */
    public static Map<Long, DynamicObject> queryCalPersonList(Long employeeId) {

        String entityName = "hsas_calperson";
        QFilter filter = new QFilter("employee.id", QCP.equals, employeeId);
        DynamicObjectCollection dyns = QueryServiceHelper.query(entityName, "id,name,adminorg.nckd_firstorg.name,adminorg.nckd_secondorg.name,adminorg.nckd_thirdorg.name,adminorg.nckd_fourthorg.name,adminorg.nckd_fifthorg.name,adminorg.nckd_sixthorg.name,empposorgrelsndet.position.name", new QFilter[]{filter});

        Map<Long, DynamicObject> dynsMap = (Map)dyns.stream().collect(Collectors.toMap((obj) -> {
            return obj.getLong("id");
        }, (obj) -> {
            return obj;
        }, (k1, k2) -> {
            return k1;
        }));



        return dynsMap;
    }

    /**
     * 按员工ID查询薪酬数据
     * @param calPersonIds
     * @param startdate
     * @param enddate
     * @return
     */
    public static DynamicObjectCollection querySalaryList(List<Long> calPersonIds, Date startdate, Date enddate, String type) {
        String entityName = "hsas_caltable";
        // 获取核算列表信息
        QFilter filter = new QFilter("calpersonid", QCP.in, calPersonIds);
        if(type.equals("10")){
            filter.and("caltask.paydate", QCP.large_equals, startdate);
            filter.and("caltask.paydate", QCP.less_equals, enddate);
        }else{
            filter.and("caltask.payrolldate", QCP.large_equals, startdate);
            filter.and("caltask.payrolldate", QCP.less_equals, enddate);
        }
        //filter.and("caltask.taskstatuse", QCP.equals, "4");   ///任务状态  已审核 7  审批通过 4
        filter.and("hsas_caltableentry.calamountvalue", QCP.large_than, 0);
        String orderStr = "hsas_caltableentry.salaryitem.statisticstag.number";
        String selectFields = "calpersonid,caltask.id,caltask.org.name,caltask.calrule.name,caltask.calcount,hsas_caltableentry.salaryitem.number,hsas_caltableentry.salaryitem.name,hsas_caltableentry.calamountvalue,hsas_caltableentry.salaryitem.statisticstag.number";
        if(type.equals("10")){
            selectFields = selectFields + ",caltask.paydate as bizdate";
            //orderStr = "caltask.paydate";
        }else{
            selectFields = selectFields + ",caltask.payrolldate as bizdate";
            //orderStr = "caltask.payrolldate";
        }
        selectFields = selectFields + ",caltask.paydate,caltask.payrolldate";

        DynamicObjectCollection calTableDyns = QueryServiceHelper.query(entityName, selectFields, new QFilter[]{filter}, orderStr);

        return calTableDyns;
    }

    /**
     * 根据用户ID查询员工ID
     * @param userId
     * @return
     */
    public static Long queryEmployeeIdByUserId(Long userId) {
        HRBaseServiceHelper SERVICE_HELPER = new HRBaseServiceHelper("hrpi_personuserrel");
        DynamicObject personuserrel = SERVICE_HELPER.queryOne("employee,user,createtime", new QFilter[]{new QFilter("user", "=", userId), new QFilter("enable", "=", "1")}, "createtime desc");
        return personuserrel == null ? 0L : personuserrel.getLong("employee_id");
    }

    /**
     * 获取 定调薪类型
     */
    public static  DynamicObjectCollection getSalaryTypeDyns() {

        ArrayList<String> salaryTypeList = new ArrayList<>();
        salaryTypeList.add("tiaozhengtiaoxin");  //岗位调整调薪
        salaryTypeList.add("100009");  //入职定薪新
        QFilter qFilter11 = new QFilter("number", QCP.in, salaryTypeList);
        DynamicObjectCollection salaryTypeDyns = QueryServiceHelper.query("hsbs_salaryadjustrsn","id,number,name",new QFilter[]{qFilter11},"number");
        return salaryTypeDyns;
    }

    /**
     * 定调薪人员属性配置
     * @param SalaryPersonPropertyName
     * @return
     */
    public static DynamicObject getSalaryPersonProperty(String SalaryPersonPropertyName) {
        QFilter qFilter = new QFilter("name", QCP.equals, SalaryPersonPropertyName);
        DynamicObject salaryPersonPropertDyn = QueryServiceHelper.queryOne("hcdm_contrastpropconf","id,number,name",new QFilter[]{qFilter});
        return salaryPersonPropertDyn;
    }

    /**
     * 判断集合是否为空
     * @param list
     * @return
     */
    public static <T> boolean isEmpty(List<T> list) {
        return null == list || list.size() == 0;
    }

}
