package com.kingdee.eas.custom.abk.dormitory.builder;

import com.google.common.collect.Maps;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.data.SortType;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.rabbitmq.guava.Lists;
import com.kingdee.bos.sql.ParserException;
import com.kingdee.eas.basedata.org.AdminOrgUnitCollection;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.IAdminOrgUnit;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryCollection;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryFactory;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryInfo;
import com.kingdee.eas.custom.dormitorysystem.dormitory.IDormitory;
import com.kingdee.eas.framework.TreeBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.DomainFilterFacadeFactory;
import com.kingdee.shr.base.syssetting.app.builder.CommonTreeBuilder;
import com.kingdee.shr.base.syssetting.app.builder.ITreeBuilder;
import com.kingdee.shr.perfweb.util.SHRPerfWebResourceUtil;
import com.kingdee.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DormitoryBuilder implements ITreeBuilder {


    @Override
    public Object getRootNode(Context context, Map<String, String> map) throws EASBizException, BOSException {
        Map<String, Object> root = new HashMap();
        root.put("id", "rootId");
        String mlFlag = SHRPerfWebResourceUtil.getMlFlag(context);
        String selectSQL = "select FName" + mlFlag + " from CT_DOR_Dormitory t where FParentID is null";
        IRowSet rs = DbUtil.executeQuery(context, selectSQL);

        try {
            if (rs.next()) {
                root.put("name", rs.getString("FName" + mlFlag));
            } else {
                root.put("name", map.get("rootAlias"));
            }
        } catch (SQLException var8) {
            var8.printStackTrace();
        }

        root.put("isLeaf", false);
        return root;
    }


    @Override
    public Object getChildren(Context context, Map<String, String> map) throws EASBizException, BOSException {
        String nodeId = (String)map.get("nodeId");
        String domain = (String)map.get("domain");
        String parentPropertyName = (String)map.get("parent");
        String parentKeyName = (String)map.get("parentKey");
        String sorters = (String)map.get("sorterItems");
        if (StringUtils.isEmpty(nodeId) || "rootId".equalsIgnoreCase(nodeId)) {
            nodeId = null;
        }

        if (StringUtils.isEmpty(parentPropertyName)) {
            parentPropertyName = "parent";
        }

        if (StringUtils.isEmpty(parentKeyName)) {
            parentKeyName = "id";
        }

        EntityViewInfo viewInfo = new EntityViewInfo();
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.getFilterItems().add(new FilterItemInfo(parentPropertyName + "." + parentKeyName, nodeId));
        FilterInfo domainFilter = this.getDomainFilter(context, domain);
        filterInfo.mergeFilter(domainFilter, "AND");
        viewInfo.setFilter(filterInfo);
        viewInfo.setSelector(this.getSelectorItems());
        if (!StringUtils.isEmpty(sorters)) {
            viewInfo.setSorter(this.getSorterItems(sorters));
        } else {
            SorterItemCollection sortCollection = new SorterItemCollection();
            SorterItemInfo sortInfo = new SorterItemInfo("longnumber");
            sortInfo.setSortType(SortType.ASCEND);
            sortCollection.add(sortInfo);
            viewInfo.setSorter(sortCollection);
        }

        List<Map<String, Object>> notes = this.getChilrenNotes(context, viewInfo);
        return notes;
    }
    /**
     * 获取排序列
     *
     * @param sorters
     * @return
     * @throws BOSException
     */
    private SorterItemCollection getSorterItems(String sorters) throws BOSException {
        SorterItemCollection sorterItemCollection = new SorterItemCollection();
        try {
            sorterItemCollection.getSorter().decode("order by " + sorters);
            return sorterItemCollection;
        } catch (ParserException var4) {
            throw new BOSException(var4.getMessage());
        }
    }
    /**
     * 获取查询列
     *
     * @return
     */
    private SelectorItemCollection getSelectorItems() {
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add(new SelectorItemInfo("id"));
        sic.add(new SelectorItemInfo("number"));
        sic.add(new SelectorItemInfo("name"));
        sic.add(new SelectorItemInfo("longNumber"));
        sic.add(new SelectorItemInfo("displayName"));
        sic.add(new SelectorItemInfo("parentId"));
        sic.add(new SelectorItemInfo("isLeaf"));
        sic.add(new SelectorItemInfo("level"));
        sic.add(new SelectorItemInfo("state"));
        sic.add(new SelectorItemInfo("parent"));
        return sic;
    }
    /**
     * 封装子节点数据
     *
     * @param ctx
     * @param viewInfo
     * @return
     * @throws BOSException
     */
    private List<Map<String, Object>> getChilrenNotes(Context ctx, EntityViewInfo viewInfo) throws BOSException {
        List<Map<String, Object>> notes = new ArrayList();
        IDormitory iDormitory = DormitoryFactory.getLocalInstance(ctx);
        DormitoryCollection dormitoryCollection = iDormitory.getDormitoryCollection(viewInfo);
        for (int i = 0; i < dormitoryCollection.size(); ++i) {
            DormitoryInfo info = dormitoryCollection.get(i);
            if (info != null) {
                HashMap<String, Object> item = new HashMap();

                if (info.getState() != null && info.getState().getValue() == 2) {
                    item.put("name", info.getName() + " (已禁用)");
                    item.put("state", info.getState().getValue());
                } else {
                    item.put("name", info.getName());
                    item.put("state", null);
                }
                item.put("id", info.getId().toString());
                item.put("isLeaf", info.isIsLeaf());
                item.put("number", info.getNumber());
                item.put("level", String.valueOf(info.getLevel()));
                item.put("longNumber", info.getLongNumber());
                item.put("displayName", info.getDisplayName());
                item.put("parentId", info.getParent() == null ? null : info.getParent().getId().toString());
                //item.put("subAdminlist", null);
                notes.add(item);
            }
        }
        return notes;
    }
    /**
     * 获取领域过滤器
     *
     * @param ctx
     * @param domain
     * @return
     * @throws BOSException
     */
    private FilterInfo getDomainFilter(Context ctx, String domain) throws BOSException {
        return StringUtils.isEmpty(domain) ?
                null :
                DomainFilterFacadeFactory.getLocalInstance(ctx).getDomainFilter("com.kingdee.eas.custom.dormitorysystem.dormitory.app.Dormitory", domain, (Map) null);
    }

    @Override
    public Object getAllNodes(Context context, Map<String, String> map) throws EASBizException, BOSException {
        String domain = map.get("domain");
        String sorters = map.get("sorterItems");
        EntityViewInfo viewInfo = new EntityViewInfo();
        FilterInfo domainFilter = this.getDomainFilter(context, domain);
        viewInfo.setFilter(domainFilter);
        viewInfo.setSelector(this.getSelectorItems());
        if (!StringUtils.isEmpty(sorters)) {
            viewInfo.setSorter(this.getSorterItems(sorters));
        } else {
            SorterItemCollection sortCollection = new SorterItemCollection();
            SorterItemInfo sortInfo = new SorterItemInfo("longNumber");
            sortInfo.setSortType(SortType.ASCEND);
            sortCollection.add(sortInfo);
            viewInfo.setSorter(sortCollection);
        }
        List<Map<String, Object>> notes = getChilrenNotes(context, viewInfo);
        return notes;
        //return null;
    }

}
