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

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.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
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.hr.psms.common.PositionStructureConstant;

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

/**
* 高级人才聘任
* 实体标识：nckd_mgrappointmgmt
* @author W.Y.C
* @date 2025/12/4 18:23
* @version 1.0
*/
public class SeniorAppointListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void setFilter(SetFilterEvent e) {
        //只查询类型状态
        QFilter filter = new QFilter(PositionStructureConstant.NCKD_TYPESTATE, QCP.equals, TypeStateEnum.HIGH_PROFESSIONAL_EMPLOYMENT.getCode());
        e.addCustomQFilter(filter);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        String operateKey = e.getOperateKey();
        if(e.getOperationResult() != null && e.getOperationResult().isSuccess()){
            ListSelectedRowCollection selectedRows = this.getSelectedRows();
            if(selectedRows.isEmpty()){
                this.getView().showMessage("请选择要解聘的人员");
                return;
            }
            List<Long> ids = new ArrayList<>();
            for (ListSelectedRow selectedRow : selectedRows) {
                ids.add(ConvertUtil.toLong(selectedRow.getPrimaryKeyValue()));
            }
            if("dismissappoint".equalsIgnoreCase(operateKey)) {
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId("nckd_dismissappoint");
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("解聘");
                showParameter.setCustomParam("ids", ids);
                showParameter.setCloseCallBack(new CloseCallBack(this, "nckd_dismissappoint"));
                this.getView().showForm(showParameter);
            }
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if("nckd_dismissappoint".equalsIgnoreCase(actionId)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null) {
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
                this.getView().showSuccessNotification("解聘成功！");
            }
        }
    }
}