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.FormShowParameter;
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.base.common.utils.DateUtil;
import nckd.jxccl.opmc.pm.helper.KpiImportUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.EventObject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ExaminprojectResultFormPlugin
 * @Description: 绩效导入结果表单插件
 * @Author: PJJ
 * @Date: 2026/1/15 下午2:30
 * @Version: 1.0
 */
public class ExaminprojectResultFormPlugin extends AbstractFormPlugin  implements BeforeF7SelectListener {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        FormShowParameter parentParameter = this.getView().getParentView().getFormShowParameter();
        Object customParam = parentParameter.getCustomParam("importplan");
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(customParam, "nckd_importplan");
        this.getModel().setValue("nckd_importplan",dynamicObject);
    }

    @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("group",collect.get(0).getDynamicObject("dpt"));
                this.getModel().setValue("nckd_postbase",collect.get(0).getDynamicObject("post"));
            }else{
                this.getModel().setValue("nckd_user",null);
                this.getModel().setValue("nckd_org",null);
                this.getModel().setValue("group",null);
                this.getModel().setValue("nckd_postbase",null);
            }
        }
    }

    @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") ){
            FormShowParameter parentParameter = this.getView().getParentView().getFormShowParameter();
            Object customParam = parentParameter.getCustomParam("importplan");
            DynamicObject importPlan = BusinessDataServiceHelper.loadSingle(customParam, "nckd_importplan");
            Date year = importPlan.getDate("nckd_year");
            Integer month = importPlan.getInt("nckd_month");
            LocalDateTime modifiedDate = DateUtil.toLocalDateTime(year).withMonth(month);
            List<DynamicObject> users = KpiImportUtils.querEmpposorgre(DateUtil.endOfMonth(modifiedDate));
            List<String> userNumber = users.stream().map(i -> i.getString("employee.number")).collect(Collectors.toList());
            e.getCustomQFilters().add(new QFilter("number", QCP.in,userNumber));
        }
    }
}
