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

import kd.bos.common.enums.EnableEnum;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
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.control.events.ItemClickListener;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
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.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* 职位体系-未定级人员列表
* @author W.Y.C
* @date 2025/9/6 14:05
* @version 1.0
*/
public class UngradedPersonQueryListPlugin extends AbstractListPlugin implements Plugin, ItemClickListener {

    private final static Log logger = LogFactory.getLog(UngradedPersonQueryListPlugin.class);

    @Override
    public void setFilter(SetFilterEvent setFilterEvent) {
        //只查询没有初定的人员
        QFilter filter = new QFilter(String.join(".", PositionStructureConstant.PERSONPOSFILE_ENTITYID, FormConstant.ID_KEY), QCP.is_null, null)
                //组织分配为主组织分配的人员
                .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(String.join(".", FormConstant.POSTYPE,FormConstant.POSTCATEGORY,FormConstant.NUMBER_KEY),QCP.equals,"1010_S")
                //只查询在职人员
                .and(String.join(".",FormConstant.HRPI_EMPENTREL, FormConstant.LABOR_REL_STATUS,FormConstant.IS_HIRED), QCP.equals,EnableEnum.YES.getCode());
        setFilterEvent.addCustomQFilter(filter);
    }

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

        if("nckd_settingjobgrade".equals(itemKey) || "nckd_settingjobgradenew".equals(itemKey) || "nckd_settingjobgradeb".equals(itemKey) || "nckd_settingjobgradenewb".equals(itemKey) || "nckd_affirm".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_settingjobgrade".equals(itemKey) || "nckd_settingjobgradeb".equals(itemKey)) {
                if(selectedRows.size() > 1 || "nckd_settingjobgradeb".equals(itemKey)){
                    //弹出【批量】在职人员初定窗口
                    FormShowParameter showParameter = new FormShowParameter();
                    showParameter.setFormId(PositionStructureConstant.SERVINGINITIALBATH_ENTITYID);
                    showParameter.getOpenStyle().setShowType(ShowType.Modal);
                    showParameter.setCaption("【批量】在职人员初定");
                    showParameter.setSendToClient(true);
                    showParameter.setCustomParam("personId", personIds);
                    showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.SERVINGINITIALBATH_ENTITYID));
                    this.getView().showForm(showParameter);
                }else {
                    //弹出在职人员初定窗口
                    FormShowParameter showParameter = new FormShowParameter();
                    showParameter.setFormId(PositionStructureConstant.SERVINGINITIAL_ENTITYID);
                    showParameter.getOpenStyle().setShowType(ShowType.Modal);
                    showParameter.setCaption("在职人员初定-定级信息确认");
                    showParameter.setSendToClient(true);
                    showParameter.setCustomParam("personId", personIds.get(0));
                    showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.SERVINGINITIAL_ENTITYID));
                    this.getView().showForm(showParameter);
                }
            } else if ("nckd_settingjobgradenew".equals(itemKey) || "nckd_settingjobgradenewb".equals(itemKey) || "nckd_affirm".equals(itemKey)) {
                boolean affirm = "nckd_affirm".equals(itemKey);
                if(selectedRows.size() > 1 || "nckd_settingjobgradenewb".equals(itemKey)){
                    //弹出【批量】新入职人员初定窗口
                    FormShowParameter showParameter = new FormShowParameter();
                    showParameter.setFormId(PositionStructureConstant.NEWHIREINITIALBATCH_ENTITYID);
                    showParameter.getOpenStyle().setShowType(ShowType.Modal);
                    showParameter.setCaption("【批量】新入职人员初定");
                    showParameter.setSendToClient(true);
                    showParameter.setCustomParam("personId", personIds);
                    showParameter.setCustomParam("affirm", affirm);
                    showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWHIREINITIALBATCH_ENTITYID));
                    this.getView().showForm(showParameter);
                }else {
                    //弹出【单人】新入职人员初定窗口
                    FormShowParameter showParameter = new FormShowParameter();
                    showParameter.setFormId(PositionStructureConstant.NEWHIREINITIAL_ENTITYID);
                    showParameter.getOpenStyle().setShowType(ShowType.Modal);
                    showParameter.setCaption("新入职人员初定-定级信息确认");
                    showParameter.setCustomParam("personId", personIds.get(0));
                    showParameter.setCustomParam("affirm", affirm);
                    showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWHIREINITIAL_ENTITYID));
                    this.getView().showForm(showParameter);
                }
            }
        }
    }

    /*@Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        FormOperate formOperate = (FormOperate) args.getSource();
        String operateKey = formOperate.getOperateKey();
        ListSelectedRowCollection selectedRows = this.getSelectedRows();
        if(selectedRows.size() > 1){
            this.getView().showTipNotification("请选择一条数据，不支持多选！");
            return;
        }
        if(PositionStructureConstant.OP_SETTINGJOBGRADE.equals(operateKey)){
            //弹出在职人员初定窗口
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setFormId(PositionStructureConstant.SERVINGINITIAL_ENTITYID);
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setCaption("在职人员初定-定级信息确认");
            showParameter.setSendToClient(true);
            showParameter.setCustomParam("personId",selectedRows.get(0).getPrimaryKeyValue());
            showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.SERVINGINITIAL_ENTITYID));
            this.getView().showForm(showParameter);
        }else if(PositionStructureConstant.OP_SETTINGJOBGRADENEW.equals(operateKey)){
            //弹出新入职人员初定窗口
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setFormId(PositionStructureConstant.NEWHIREINITIAL_ENTITYID);
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setCaption("新入职人员初定-定级信息确认");
            showParameter.setCustomParam("personId",selectedRows.get(0).getPrimaryKeyValue());
            showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.NEWHIREINITIAL_ENTITYID));
            this.getView().showForm(showParameter);
        }
    }*/
    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if(StringUtils.equalsAny(actionId,PositionStructureConstant.SERVINGINITIAL_ENTITYID,PositionStructureConstant.NEWHIREINITIAL_ENTITYID)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null) {
                //刷新列表
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
            }
        }
    }
}