package nckd.jxccl.opmc.pm.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import nckd.jxccl.opmc.pm.helper.KpiImportUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.EventObject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: KpiTutoringFormPlugin
 * @Description: 绩效辅导表单插件
 * @Author: PJJ
 * @Date: 2026/1/15 上午10:19
 * @Version: 1.0
 */
public class KpiTutoringFormPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit user = this.getControl("nckd_user");
        user.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
        if(StringUtils.equals(name,"nckd_user")){
            List<DynamicObject> dynamicObjects = KpiImportUtils.yearUnqualifiedUser();
            List<DynamicObject> dynamicObjects1 = KpiImportUtils.monthUnqualifiedUser();
            List<Object> collect = dynamicObjects.stream().map(i -> i.getPkValue()).collect(Collectors.toList());
            collect.addAll(dynamicObjects1.stream().map(i->i.getPkValue()).collect(Collectors.toList()));
            QFilter qFilter = new QFilter("id", QCP.in, collect);
            e.getCustomQFilters().add(qFilter);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        if( StringUtils.equals(name,"nckd_user") ){
            Object changeData = e.getChangeSet()[0].getNewValue();
            if(ObjectUtils.isNotEmpty(changeData)){
                DynamicObject user = (DynamicObject) changeData;
                DynamicObject userData = BusinessDataServiceHelper.loadSingle(user.getPkValue(), "bos_user");
                DynamicObjectCollection dynamicObjectCollection = userData.getDynamicObjectCollection("entryentity");
                List<DynamicObject> collect = dynamicObjectCollection.stream().filter(i -> !i.getBoolean("ispartjob")).collect(Collectors.toList());
                this.getModel().setValue("nckd_org",collect.get(0).getDynamicObject("dpt"));
                this.getModel().setValue("nckd_position",collect.get(0).getDynamicObject("post"));
            }else{
                this.getModel().setValue("nckd_user",null);
                this.getModel().setValue("nckd_org",null);
                this.getModel().setValue("nckd_position",null);
            }
        }
    }
}
