package nckd.jxccl.hr.hstu.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.list.events.BuildTreeListFilterEvent;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.dao.factory.HRBaseDaoFactory;
import kd.hr.hbp.common.constants.org.OrgTreeDynEnum;
import kd.hr.hbp.common.util.org.model.OrgTreeModel;
import kd.hr.hbp.formplugin.web.org.template.AdminOrgTreeListTemplate;
import kd.sdk.hr.hspm.common.utils.PropertyHelper;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.base.orm.helper.QFilterCommonHelper;

import java.util.EventObject;
import java.util.HashSet;
import java.util.Set;

/**
* 【优秀生】-综合测评问卷左树右表
* @author W.Y.C
* @date 2025/7/15 20:33
* @version 1.0
*/
public class EvalQuestTreeListPlugin extends AdminOrgTreeListTemplate {

    public EvalQuestTreeListPlugin() {
        super(new OrgTreeModel(OrgTreeDynEnum.ADMIN_STRUCT.getDynEntity(), OrgTreeDynEnum.ADMIN_MAIN_ENTITY.getDynEntity(), Boolean.TRUE, Boolean.TRUE, Boolean.FALSE));
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        this.getView().setVisible(false, FormConstant.SEARCH_DATE_KEY);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        //设置默认开启包含子部门
        DynamicObject dynamicObject = this.getModel().getDataEntity();
        if(PropertyHelper.existProperty(dynamicObject, FormConstant.CHKINCLUDECHILD)) {
            this.getModel().setValue(FormConstant.CHKINCLUDECHILD, Boolean.TRUE);
        }
    }

    @Override
    protected QFilter buildNodeClickFilter(BuildTreeListFilterEvent buildTreeListFilterEvent) {
        String focusNodeId = buildTreeListFilterEvent.getNodeId().toString();
        QFilter nodeClickFilter = null;
        if(!this.getTreeModel().getRoot().getId().equalsIgnoreCase(focusNodeId)){

            QFilter focusNodeIdFilter = new QFilter("adminorg.id", QCP.equals, Long.valueOf(focusNodeId));
            Boolean chkIncludeChild = ConvertUtil.toBoolean(this.getModel().getValue(FormConstant.CHKINCLUDECHILD));
            //是否包含子部门
            if(chkIncludeChild) {
                DynamicObject structlongnumber = HRBaseDaoFactory.getInstance(this.getEntityName()).queryOne("structlongnumber", new QFilter[]{QFilterCommonHelper.getCurrentVersionFilter(), QFilterCommonHelper.getInitStatusFinishFilter(),
                        QFilterCommonHelper.getDataStatusFilter(), focusNodeIdFilter});
                String focusNodeLongNumber = (String) structlongnumber.get("structlongnumber");
                QFilter filter = new QFilter("structlongnumber", QCP.like, focusNodeLongNumber + "%");
                DynamicObjectCollection dynamicObjects = HRBaseDaoFactory.getInstance(this.getEntityName()).queryColl("adminorg.id adminorg", new QFilter[]{filter, QFilterCommonHelper.getCurrentVersionFilter(), QFilterCommonHelper.getInitStatusFinishFilter()}, null);
                Set<Long> ids = new HashSet<>(dynamicObjects.size());
                dynamicObjects.forEach(dynamicObject -> ids.add(dynamicObject.getLong("adminorg")));
                nodeClickFilter = new QFilter("nckd_evalperson.adminorg.id", QCP.in, ids);
            }else{
                nodeClickFilter = new QFilter("nckd_evalperson.adminorg.id", QCP.in, ConvertUtil.toLong(focusNodeId));
            }
        }
        return nodeClickFilter;
    }
}