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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
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.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.mvc.list.ListView;
import kd.hr.hbp.common.util.HRJSONUtils;
import kd.hr.hbp.common.util.HRStringUtils;
import kd.hr.hdm.business.domain.parttime.external.IParttimePersonService;
import kd.sdk.plugin.Plugin;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Tyx 2025-04-28
 * 根据岗位终止兼职操作插件
 * 根据选择的岗位Id，找到这个岗位所有兼职的人事档案ID，再调标准方法终止
 */
public class PositionEndPartTimePlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate op = (FormOperate)args.getSource();
        String operateKey = op.getOperateKey();
        if (HRStringUtils.equals(operateKey, "endparttime")) {
            ListView listView = (ListView)this.getView();
            List<Long> selectPkIds = this.getListSelectedPkIds(listView);
            this.showPosEndPartTimeConfirm(selectPkIds);
            args.setCancel(true);
        }
    }

    private List<Long> getListSelectedPkIds(ListView listView) {
        ListSelectedRowCollection selectedRows = listView.getSelectedRows();
        List<Long> selectPkIds = Lists.newArrayListWithExpectedSize(selectedRows.size());
        Iterator it = selectedRows.iterator();

        while(it.hasNext()) {
            ListSelectedRow selectedRow = (ListSelectedRow)it.next();
            Long primaryKeyValue = HRJSONUtils.getLongValOfCustomParam(selectedRow.getPrimaryKeyValue());
            selectPkIds.add(primaryKeyValue);
        }

        return selectPkIds;
    }

    private void showPosEndPartTimeConfirm(List<Long> selectIds) {
        FormShowParameter formShowParameter = new FormShowParameter();
        formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
        formShowParameter.setFormId("nckd_posendparttime");
        formShowParameter.setCustomParam("selectIds", selectIds);
        formShowParameter.setCloseCallBack(new CloseCallBack(this, "posendparttime"));
        this.getView().showForm(formShowParameter);
    }
}