package nckd.jxccl.hr.psms.plugin.form.annualadjust;

import kd.bos.common.enums.EnableEnum;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.BeforeCreateListDataProviderArgs;
import kd.bos.form.events.FilterContainerInitArgs;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.ORMHint;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.enums.psms.TypeStateEnum;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.base.common.utils.DateUtil;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 未生成年度调整列表
* @author W.Y.C
* @date 2025/10/13 12:40
* @version 1.0
*/
public class UnAnnualAdjustListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void filterContainerInit(FilterContainerInitArgs args) {
        super.filterContainerInit(args);
    }


    @Override
    public void setFilter(SetFilterEvent e) {
        //TODO 【待修改】-职位体系-默认过滤掉副科级及以上
        //TODO 【待修改】-职位体系-本年度7月前退休、辞职，则列表中删除该人员
        //过滤掉未初定或初定为当前年或当前年之前或本年度已做过调整的员工
        LocalDateTime localDateTime = DateUtil.beginOfYear(DateUtil.now());
        //查询当年已执行年度调整的人员
        QueryFieldBuilder queryFieldBuilder = QueryFieldBuilder.create().addIdNumberName(FormConstant.NCKD_PERSON);
        QFilter yearFilter = new QFilter(PositionStructureConstant.NCKD_EXECUTEYEAR, QCP.equals, localDateTime.getYear())
                .and(new QFilter(PositionStructureConstant.NCKD_DISABLE, QCP.equals, EnableEnum.NO.getCode()))
                .and(new QFilter(PositionStructureConstant.NCKD_TYPESTATE, QCP.equals, TypeStateEnum.ANNUAL_ADJUSTMENT.getCode()));
        DynamicObjectCollection query = QueryServiceHelper.query(PositionStructureConstant.PERSONPOSFILE_ENTITYID, queryFieldBuilder.buildSelect(), new QFilter[]{yearFilter});
        List<Long> personIds = new ArrayList<>(query.size());
        for (DynamicObject dynamicObject : query) {
            long personId = dynamicObject.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY));
            personIds.add(personId);
        }





        LocalDateTime lastYear = DateUtil.minusYears(localDateTime, 1);
        LocalDateTime lastBegin = DateUtil.beginOfYear(lastYear);
        LocalDateTime lastEnd = DateUtil.endOfYear(lastYear);
        QFilter qFilter = new QFilter(FormConstant.IS_PRIMARY, QCP.equals, EnableEnum.YES.getCode())
                .and(FormConstant.IS_SEQLATESTRECORD, QCP.equals, EnableEnum.YES.getCode())
                .and(FormConstant.IS_DELETED, QCP.equals, EnableEnum.NO.getCode())
                .and(String.join(".", FormConstant.ASSIGNMENT, FormConstant.IS_PRIMARY), QCP.equals, EnableEnum.YES.getCode())
                .and(String.join(".", FormConstant.ASSIGNMENT, FormConstant.IS_DELETED), QCP.equals, EnableEnum.NO.getCode())
                .and(String.join(".", PositionStructureConstant.PERSONPOSFILE_ENTITYID, PositionStructureConstant.NCKD_DISABLE), QCP.equals, EnableEnum.NO.getCode())
                .and(String.join(".", PositionStructureConstant.PERSONPOSFILE_ENTITYID, PositionStructureConstant.NCKD_ISCURRENTNEWEST), QCP.equals, EnableEnum.YES.getCode())
                //有初定的人员
                .and(new QFilter(String.join(".", PositionStructureConstant.PERSONPOSFILE_ENTITYID+"init", PositionStructureConstant.NCKD_FIRSTRANK), QCP.equals, EnableEnum.YES.getCode()))
                //初定时间不等于当年
                .and(new QFilter(String.join(".", PositionStructureConstant.PERSONPOSFILE_ENTITYID+"init", PositionStructureConstant.NCKD_EXECUTEYEAR), QCP.not_equals2, localDateTime.getYear()));
                //上年度有考核结果的人员
               /* .and(new QFilter(String.join(".", PositionStructureConstant.PERFMANAGER_ENTITYID, PositionStructureConstant.PERFMANAGER_ENTRY_ENTITYID, PositionStructureConstant.APPRAISAL_YEAR_KEY), QCP.large_equals, DateUtil.toDate(lastBegin)))
                .and(new QFilter(String.join(".", PositionStructureConstant.PERFMANAGER_ENTITYID, PositionStructureConstant.PERFMANAGER_ENTRY_ENTITYID, PositionStructureConstant.APPRAISAL_YEAR_KEY), QCP.less_equals, DateUtil.toDate(lastEnd)))*/
                //当年还未年度调整的人员
                if(!personIds.isEmpty()) {
                    qFilter.and(new QFilter(FormConstant.EMPLOYEE_KEY, QCP.not_in, personIds));
                }
        QFilter qFilter1 = QFilter.joinSQL(FormConstant.HRPI_EMPPOSORGREL,
                "left join tk_nckd_perfmanager pm on pm.fk_nckd_personid = hrpi_empposorgrel.femployeeid",
                null);
        e.addCustomQFilter(qFilter);
//        e.getQFilters().add(qFilter1);
    }

    @Override
    public void packageData(PackageDataEvent e) {
        super.packageData(e);
        String colKey = e.getColKey();
        int year = DateUtil.now().getYear();
        if ("nckd_currentyear".equals(colKey)) {
            e.setFormatValue(year);
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();

        if("nckd_new".equals(itemKey) ) {
            ListSelectedRowCollection selectedRows = this.getSelectedRows();
            if (selectedRows.isEmpty()) {
                this.getView().showTipNotification("请至少选择一条数据");
                return;
            }
            //任职经历ID
            List<Long> empPosOrgRelIds = new ArrayList<>(selectedRows.size());
            for (ListSelectedRow selectedRow : selectedRows) {
                empPosOrgRelIds.add(ConvertUtil.toLong(selectedRow.getPrimaryKeyValue()));
            }
            QFilter qFilter = new QFilter(FormConstant.ID_KEY, QCP.in, empPosOrgRelIds);
            DynamicObject[] load = BusinessDataServiceHelper.load(FormConstant.HRPI_EMPPOSORGREL, String.join(".", FormConstant.EMPLOYEE_KEY, FormConstant.ID_KEY), new QFilter[]{qFilter});
            List<Long> personIds = new ArrayList<>(selectedRows.size());
            for (DynamicObject dynamicObject : load) {
                DynamicObject employee = dynamicObject.getDynamicObject(FormConstant.EMPLOYEE_KEY);
                personIds.add(employee.getLong(FormConstant.ID_KEY));
            }

            if (personIds.isEmpty()) {
                this.getView().showErrorNotification("请选择正确的数据");
                return;
            }
            if ("nckd_new".equals(itemKey)) {
                //弹出【批量】在职人员初定窗口
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId(PositionStructureConstant.NEWANNUALADJUST_ENTITYID);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("生成年度调整");
                showParameter.setSendToClient(true);
                showParameter.setCustomParam(FormConstant.NCKD_PERSON, personIds);
                showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWANNUALADJUST_ENTITYID));
                this.getView().showForm(showParameter);

            }
        }
    }
}