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 java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* 调薪情况列表左树右表
* @author W.Y.C
* @date 2025/6/9 17:55
* @version 1.0
*/
public class SalAdjTrackerTreeListPlugin extends AdminOrgTreeListTemplate {

    public SalAdjTrackerTreeListPlugin() {
        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 isCurrentVersionFilter = new QFilter("iscurrentversion", QCP.equals, "1");
            QFilter initStatusFilter = new QFilter("initstatus", QCP.equals, "2");
            QFilter dataStatusFilter = new QFilter("datastatus", QCP.in, Arrays.asList("1", "2"));
            QFilter focusNodeIdFilter = new QFilter("adminorg.id", QCP.equals, Long.valueOf(focusNodeId));
            DynamicObject structlongnumber = HRBaseDaoFactory.getInstance(this.getEntityName()).queryOne("structlongnumber", new QFilter[]{isCurrentVersionFilter, initStatusFilter, dataStatusFilter, 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, isCurrentVersionFilter, initStatusFilter}, 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;
    }
}