package nckd.jimin.jyyy.hr.hrmp.hbpm.opplugin.web.position;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hdm.business.domain.parttime.ParttimeServiceHelper;
import kd.hr.hdm.business.domain.transfer.service.external.PersonExternalService;
import kd.sdk.plugin.Plugin;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Tyx 2025-04-29
 * 岗位终止兼职表单插件
 * 元数据标识：nckd_posendparttime
 */
public class PosEndPartTimeConfirmPlugin extends AbstractFormPlugin implements Plugin {

    private static String ermanFile_entity = "hspm_ermanfile";
    private static final Log log = LogFactory.getLog(PosEndPartTimeConfirmPlugin.class);
    public void registerListener(EventObject eventObject) {
        super.registerListener(eventObject);
        Button button = (Button)this.getControl("btnok");
        button.addClickListener(this);
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control control = (Control)evt.getSource();
        String key = control.getKey();
        if ("btnok".equals(key)) {
            FormShowParameter formShowParameter = this.getView().getFormShowParameter();
            //父页面传过来的岗位ID，根据这些ID查找岗位下的兼职
            List<Long> selectIds = (List)formShowParameter.getCustomParam("selectIds");
            QFilter filter = getQFilter(selectIds);
            HRBaseServiceHelper helper = HRBaseServiceHelper.create(ermanFile_entity);
            DynamicObject[] ermanFiles = helper.query(new QFilter[]{filter});
            if(ermanFiles.length == 0) {
                this.getView().showMessage("岗位下不存在兼职！");
            }
            else {
                //转成List<Long>
                List<Long> ermanFileIds = Arrays.stream(ermanFiles).map(dyx -> dyx.getLong("id")).collect(Collectors.toList());
                //调用标准方法获取ermanFile
                List<Map<String, Object>> erManFiles = PersonExternalService.getInstance().invokeGetCardFields(ermanFileIds);
                DynamicObject action = (DynamicObject)this.getModel().getValue("affaction");
                Date partEndDate = (Date)this.getModel().getValue("partenddate");
                //调用标准终止兼职方法
                ParttimeServiceHelper.batchEndParttime(action.getLong("id"), partEndDate, erManFiles);
                this.getView().getParentView().showSuccessNotification(ResManager.loadKDString("兼职终止成功，共终止" + ermanFileIds.size() + "条记录", "EndParttimePlugin_2", "hr-hdm-formplugin", new Object[0]));
                this.getView().sendFormAction(this.getView().getParentView());
                this.getView().close();
            }
        }
    }

    /**
     * 组装查询兼职档案的过滤条件
     * @param positionIds
     * @return
     */
    public QFilter getQFilter(List<Long> positionIds) {
        QFilter filter = new QFilter("depemp.iscurrentversion", QCP.equals, "1");
        filter.and("depemp.businessstatus", QCP.equals, "1");
        filter.and("depemp.postype.number",QCP.equals, "1020_S");
        filter.and("depemp.position", QCP.in, positionIds);
        filter.and("iscurrentversion", QCP.equals, "1");
        filter.and("businessstatus", QCP.equals, "1");
        return filter;
    }

}