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.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.Control;
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 org.apache.commons.lang3.StringUtils;

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

/**
 * @ClassName: ImportKpiImportTemplateFormPlugin
 * @Description: 绩效结果导入模板表单插件
 * @Author: PJJ
 * @Date: 2026/1/6 下午5:13
 * @Version: 1.0
 */
public class ImportKpiImportTemplateFormPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        //设置默认数据
        createDefaultRow();
    }

    private void createDefaultRow() {
        //查询考核维度项目 中 非考核项目数据
        QFilter qFilter = new QFilter("nckd_check", QCP.equals, false);
        qFilter.and("status",QCP.equals,"C");
        qFilter.and("enable",QCP.equals,"1");
        DynamicObject[] examineDimension = BusinessDataServiceHelper.load("nckd_examine_dimension", "number,name,nckd_check,nckd_datatype", qFilter.toArray(),"number");
        //赋值非考核项目分录行
        DynamicObjectCollection entryEntity = this.getModel().getEntryEntity("nckd_outimptplentry");
        for (DynamicObject obj : examineDimension) {
            DynamicObject row = entryEntity.addNew();
            row.set("nckd_textfield",">>");
            row.set("nckd_examine_dimension",obj);
            row.set("nckd_examinename",obj.getString("name"));
            row.set("nckd_examineid",obj.getLong("id"));
            row.set("nckd_examinenumber",obj.getString("number"));
            row.set("nckd_datatype",obj.getDynamicObject("nckd_datatype"));
            row.set("nckd_check",false);
            row.set("nckd_isprecut",true);
            row.set("nckd_mustinput",true);
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit control = this.getControl("nckd_examine_dimension");
        control.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
        if(StringUtils.equals(name,"nckd_examine_dimension")){
            //过滤已添加数据
            DynamicObjectCollection entryEntity = this.getModel().getEntryEntity("nckd_outimptplentry");
            List<Long> collect = entryEntity.stream().map(i -> i.getLong("nckd_examine_dimension.id")).collect(Collectors.toList());
            QFilter qFilter = new QFilter("id", QCP.not_in, collect);
            e.getCustomQFilters().add(qFilter);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        Object newValue = e.getChangeSet()[0].getNewValue();
        if( StringUtils.equals(name,"nckd_examine_dimension") ){
            //根据考核维度带出对应数据
            DynamicObject obj = (DynamicObject) newValue;
            int rowIndex = e.getChangeSet()[0].getRowIndex();
            this.getModel().setValue("nckd_textfield",">>",rowIndex);
            this.getModel().setValue("nckd_examinename",obj.getString("name"),rowIndex);
            this.getModel().setValue("nckd_examineid",obj.getLong("id"),rowIndex);
            this.getModel().setValue("nckd_examinenumber",obj.getString("number"),rowIndex);
            this.getModel().setValue("nckd_datatype",obj.getDynamicObject("nckd_datatype"),rowIndex);
            this.getModel().setValue("nckd_check",false,rowIndex);
        }
    }
}
