package com.sagacloud.dict;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sagacloud.endpoints.tagset.ObjectTag;

/**
 * Created by wo on 10/11/19.
 */
public class CsStaticInfo {
	public static JSONArray categoryTreeArray = null;
    private static final Logger log = LoggerFactory.getLogger(CsStaticInfo.class);
    //所有耗材编码
//    private static Set<String> codeSet = new HashSet<>();
    //code -> name
    private static Map<String, String> code2name = new HashMap<>();
    //code -> children codes
    private static Map<String, Set<String>> toChildren = new HashMap<>();


//    public static Set<String> getCodeSet() {
//        return codeSet;
//    }

    public static Map<String, String> getCode2name() {
    	try {
    		if(categoryTreeArray == null) {
    			synchronized (CsStaticInfo.class) {
    				categoryTreeArray = ObjectTag.comsumableCategoryTreeQuery();
				}
    		}
        	JSONArray arr = categoryTreeArray;
        	code2name.clear();
//            JSONArray arr = JSONObject.parseObject(OssUtils.getFileAsString("property/Dictionary/consumableRel.json")).getJSONArray("all");
            for (int i = 0; i < arr.size(); i++) {
                JSONObject item = arr.getJSONObject(i);
//                codeSet.add(item.getString("code"));
                code2name.put(item.getString("code"), item.getString("name"));
                if(item.get("content") != null){
                    JSONArray children = item.getJSONArray("content");
                    Set<String> subSet = new HashSet<>();
                    for (int j = 0; j < children.size(); j++) {
                        JSONObject child = children.getJSONObject(j);
                        //                          codeSet.add(child.getString("code"));
                        code2name.put(child.getString("code"), child.getString("name"));
                        subSet.add(child.getString("code"));
                        if (child.get("content") != null) {
                            JSONArray grChildren = child.getJSONArray("content");
                            Set<String> childSet = new HashSet<>();
                            for (int k = 0; k < grChildren.size(); k++) {
                                JSONObject grChild = grChildren.getJSONObject(k);
                                code2name.put(grChild.getString("code"), grChild.getString("name"));
                                childSet.add(grChild.getString("code"));
                            }
                            toChildren.put(child.getString("code"), childSet);
                        }
                    }
                    toChildren.put(item.getString("code"), subSet);
                }
            }
        } catch (Exception e) {
            log.error("",e);
        }
        return code2name;
    }

    public static Map<String, Set<String>> getToChildren() {
        return toChildren;
    }
}
