package com.app.hxqh.location;

import com.app.hxqh.udkbfl.UdKbflMbo;
import psdi.app.location.LocHierarchy;
import psdi.app.location.LocHierarchySet;
import psdi.mbo.*;
import psdi.server.MXServer;
import psdi.util.MXException;

import java.rmi.RemoteException;

/**
 * 位置MboSet
 * edit 2023-10-16
 */
public class LOCHIERARCHYMboSet extends LocHierarchySet implements MboSetRemote,HierarchicalMboSetRemote {
    public LOCHIERARCHYMboSet(MboServerInterface ms) throws MXException, RemoteException {
        super(ms);
    }

    @Override
    protected Mbo getMboInstance(MboSet mboSet) throws MXException, RemoteException {
        return new LOCHIERARCHYMbo(mboSet);
    }

    @Override
    public MboValueData[][] getChildren(String s, String s1, String[] strings, int i) throws MXException, RemoteException {
        String sql="PARENT IN(SELECT LOCATION FROM lochierarchy WHERE LOCHIERARCHYID="+s1.replace(",", "")+")";
        setWhere(sql);
        this.reset();
        if (!(isEmpty())){
            return getMboValueData(0, i + 1, strings);
        }else{
            return null;
        }
    }

    @Override
    public MboValueData[] getParent(String s, String s1, String[] strings) throws MXException, RemoteException {
        setWhere(" 1=1 ");
        if (!this.isEmpty()) {
            MboRemote mbo = this.getMbo(0);
            return mbo.getMboValueData(strings);
        } else {
            return null;
        }
    }
    @Override
    public MboValueData[][] getSiblings(String s, String s1, String[] strings, int i) throws MXException, RemoteException {
        return new MboValueData[0][];
    }

    @Override
    public MboValueData[][] getTop(String[] strings, int i) throws MXException, RemoteException {
        String userid=getUserInfo().getPersonId();
        MboSetRemote pset= MXServer.getMXServer().getMboSet("person", getUserInfo());
        pset.setWhere("personid='"+userid+"'");
        if(!pset.isEmpty() && pset.count()>0){
            MboRemote per=pset.getMbo(0);
            //权限等级
            String aut=per.getString("UDAUTGRADE");
            String orgid=per.getString("LOCATIONORG");
            String siteid=per.getString("LOCATIONSITE");

            if("Level 1".equalsIgnoreCase(aut)){
                setWhere(" PARENT IS null ");
            }else if("Level 2".equalsIgnoreCase(aut)){
                setWhere(" PARENT IS null  and ORGID='"+orgid+"' ");
            }else if("Level 3".equalsIgnoreCase(aut)){
                setWhere(" PARENT IS null and SITEID='"+siteid+"' ");
            }else{
                //没有等级
                setWhere("1=2");
            }
        }
        reset();
        if (!(isEmpty())){
            return getMboValueData(0, i + 1, strings);
        }else{
            return null;
        }

    }

    @Override
    public MboValueData[][] getPathToTop(String s, String s1, String[] strings, int i) throws MXException, RemoteException {
        return new MboValueData[0][];
    }

    @Override
    public MboValueData[][] getAllHierarchies(String s, String s1, String[] strings, int i) throws MXException, RemoteException {
        return new MboValueData[0][];
    }

    @Override
    public MboValueData[] getHierarchy(String s, String s1) throws MXException, RemoteException {
        return new MboValueData[0];
    }

    @Override
    public void setHierarchy(String s, String s1, String s2) throws MXException, RemoteException {

    }

    @Override
    public MboValueData getUniqueIDValue(String s, String[] strings, String[] strings1) throws MXException, RemoteException {
        return null;
    }


}
