package com.huatek.frame.base.util;
 

import org.springframework.data.redis.core.ValueOperations;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; 

/**
 * @author key_dong
 * 字典翻译
 */
public class DicTool {
	
	/**
	 * 字典翻译
	 * @param operations
	 * @param code
	 * @param dcode
	 * @return
	 */
	public  static String translate(@SuppressWarnings("rawtypes") ValueOperations operations,String code,String dcode){
		Object jsons = operations.get("dics_");
		 JSONArray dics = JSONArray.parseArray(jsons.toString());
		 for (Object obj : dics) {
			 JSONObject dic = JSONObject.parseObject(obj.toString());
			 String redisCode = dic.getString("code");
			 if(redisCode.equals(code)){
				 JSONArray details = dic.getJSONArray("details");
				 for (Object object : details) {
					 JSONObject detail = JSONObject.parseObject(object.toString());
					 String redisDcode = detail.getString("dcode");
					 if(redisDcode.equals(dcode)){
							return detail.getString("dname"); 
					 }
				}
			 }
		}
		return ""; 
	}

	

	/**
	 * 初始化下拉框
	 * @param operations
	 * @param code
	 * @param dcode
	 * @return
	 */
	public  static String select(@SuppressWarnings("rawtypes") ValueOperations operations,String code){
		Object jsons = operations.get("dics_");
		 JSONArray dics = JSONArray.parseArray(jsons.toString());
		 String result="";
		 dics:
		 for (Object obj : dics) {
			 JSONObject dic = JSONObject.parseObject(obj.toString());
			 String redisCode = dic.getString("code"); 
			 if(code.equals(redisCode)){
				 result=dic.toJSONString();
				 break dics;
			 }
		}
		return result; 
	}
}
