package nckd.jxccl.opmc.pm.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 nckd.jxccl.base.orm.helper.QFilterCommonHelper;

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

/**
* 人员考评管理列表左树右表
* @author W.Y.C
* @date 2025/6/9 17:55
* @version 1.0
*/
public class PerfManagerTreeListPlugin extends AdminOrgTreeListTemplate {

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

    @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));
            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_person.adminorg.id", QCP.in, ids);
        }
        return nodeClickFilter;
    }
}