package com.yem.wm.utils;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.list.OrgList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Date 2024/2/5 17:21
 * @Created by ZZSLL
 */

public class OrgFilterUtils
{
    /**
     * 过滤某一个组织下的所有组织
     * @param org
     * @return
     */
    public static QFilter getOrgFilter(DynamicObject org)
    {
        if (org == null) return null;
        List<Long> ids = new ArrayList<>();
        String number = org.getString("number");
        Object pkValue = org.getPkValue();
        long selfId = (long) pkValue;
        ids.add(selfId);
        QFilter qFilter = new QFilter("longnumber", QCP.like, "%" + number + "!%");
        qFilter.and(new QFilter("view.number", QCP.equals, "15"));
        DynamicObject[] entries = BusinessDataServiceHelper.load("bos_org_structure", "id, org.id", qFilter.toArray());
        for (DynamicObject dynamicObject : entries) {
            long orgId = dynamicObject.getLong("org.id");
            ids.add(orgId);
        }
        return new QFilter("id", QCP.in, ids);
    }

    public static List<Long> getOrgList(DynamicObject org) {
        List<Long> ids = new ArrayList<>();
        if (org == null) return ids;
        String number = org.getString("number");
        Object pkValue = org.getPkValue();
        long selfId = (long) pkValue;
        ids.add(selfId);
        QFilter qFilter = new QFilter("longnumber", QCP.like, "%" + number + "!%");
        qFilter.and(new QFilter("view.number", QCP.equals, "15"));
        DynamicObject[] entries = BusinessDataServiceHelper.load("bos_org_structure", "id, org.id", qFilter.toArray());
        for (DynamicObject dynamicObject : entries) {
            long orgId = dynamicObject.getLong("org.id");
            ids.add(orgId);
        }
        return ids;
    }
}
