package nckd.jxccl.wtc.wtte.report;

import kd.bos.algo.DataSet;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.wtc.wtabm.web.common.constant.WtcConstant;
import nckd.jxccl.wtc.wtabm.web.common.util.WtcUtils;

import java.util.*;

/**
 * Tyx 2025-10-15
 * 出勤率统计报表查询插件
 */
public class AttendanceRateReportDataPlugin extends AbstractReportListDataPlugin implements Plugin {

    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable {
        QFilter filter = buildFilter(reportQueryParam);
        DataSet dataSet = this.queryDataSet(reportQueryParam, filter);
        return dataSet;
    }

    private QFilter buildFilter(ReportQueryParam reportQueryParam) {
        FilterInfo filterInfo = reportQueryParam.getFilter();
        //DynamicObject conOrg = (DynamicObject) filterInfo.getFilterItem("nckd_con_org").getValue();
        DynamicObject conAdminOrg = (DynamicObject) filterInfo.getFilterItem("nckd_con_adminorg").getValue();
        DynamicObject attPeriod = (DynamicObject) filterInfo.getFilterItem("nckd_attperiodentry").getValue();
        Date startDate = filterInfo.getFilterItem("nckd_owndatestart").getDate();
        Date endDate = filterInfo.getFilterItem("nckd_owndateend").getDate();
        boolean isContainChild = filterInfo.getFilterItem("nckd_iscontainchild").getBoolean();

        /* 组织过滤
        QFilter orgFilter = new QFilter("attmain.orgid.id", QCP.equals, conOrg.getPkValue());
        if(!ObjectUtils.isEmpty(conAdminOrg)) {
            orgFilter.and("attmain.departmentvid.id", QCP.equals, conAdminOrg.getPkValue());
        }*/

        // 2025-10-16 改成按行政组织过滤，并根据是否包含下级字段取下级行政组织
        QFilter orgFilter = null;
        if(isContainChild) {
            Set subOrgSet = WtcUtils.getSubOrgFilters(conAdminOrg, "attmain.departmentvid.id");
            orgFilter = new QFilter("attmain.departmentvid.boid", QCP.in, subOrgSet);
        }
        else {
            orgFilter = new QFilter("attmain.departmentvid.boid", QCP.equals, conAdminOrg.getPkValue());
        }

        // 日期过滤，期间和归属日期其中一个必填，如果都填了以日期为准
        QFilter dateFilter = null;
        if(ObjectUtils.isEmpty(attPeriod)) {
            dateFilter = new QFilter("owndate", QCP.large_equals, startDate);
            dateFilter.and("owndate", QCP.less_equals, endDate);
        }
        else {
            dateFilter = new QFilter("owndate", QCP.large_equals, attPeriod.getDate("begindate"));
            dateFilter.and("owndate", QCP.less_equals, attPeriod.getDate("enddate"));
        }
        return orgFilter.and(dateFilter);
    }

    /**
     * 实际查询数据
     * @param filter
     * @return
     */
    public DataSet queryDataSet(ReportQueryParam reportQueryParam, QFilter filter) {
        DynamicObject conAdminOrg = (DynamicObject) reportQueryParam.getFilter().getFilterItem("nckd_con_adminorg").getValue();
        String selectFields = "attmain.orgid.id as orgId,value";
        // 先查询所有休假时长
        QFilter filter1 = filter.copy();
        filter1.and("attitemid.number",QCP.in, WtcConstant.LEAVE_NUMBERS);
        DataSet leaveDataSet = WtcConstant.RECORDDETAIL_HEPLER.queryDataSet("AttendanceRateReportQuery-Leave", selectFields, new QFilter[]{filter1});
        // 汇总
        DataSet leaveDataSetGroup = leaveDataSet.groupBy(new String[]{"orgId"}).sum("value").finish();

        // 再查询所有出勤时长，SUM汇总
        QFilter filter2 = filter.copy();
        filter2.and("attitemid.number",QCP.equals, WtcConstant.SCHEDULED_NUMBER);
        DataSet schDataSet = WtcConstant.RECORDDETAIL_HEPLER.queryDataSet("AttendanceRateReportQuery-Sch", selectFields, new QFilter[]{filter2});
        // 汇总
        DataSet schDataSetGroup = schDataSet.groupBy(new String[]{"orgId"}).sum("value").finish();

        QFilter filter3 = filter.copy();
        filter3.and("attitemid.number",QCP.equals, WtcConstant.ABSENT_NUMBER);
        DataSet absentDataSet = WtcConstant.RECORDDETAIL_HEPLER.queryDataSet("AttendanceRateReportQuery-Sbsent", selectFields, new QFilter[]{filter3});
        // 汇总
        DataSet absentDataSetGroup = absentDataSet.groupBy(new String[]{"orgId"}).sum("value").finish();

        // 应出勤 Join 休假
        DataSet joinDataSet1 = schDataSetGroup.join(leaveDataSetGroup).on("orgId","orgId").select(new String[]{"orgId","value as nckd_scheduledday"}, new String[]{"value as nckd_leaveday"}).finish();
        // 再 Join 旷工
        DataSet joinDataSet2 = joinDataSet1.join(absentDataSetGroup).on("orgId","orgId").select(new String[]{"orgId", "nckd_scheduledday", "nckd_leaveday"}, new String[]{"value as nckd_absentday"}).finish();
        // 处理出勤率
        return joinDataSet2.select(conAdminOrg.getLong("id") + " as nckd_adminorg,nckd_scheduledday, nckd_leaveday, nckd_absentday, nckd_scheduledday - nckd_leaveday - nckd_absentday as nckd_actualday, round((nckd_scheduledday - nckd_leaveday - nckd_absentday) / nckd_scheduledday,2) as nckd_rate");
    }
}