后台查询到前台所需,数据结构转换的通用方法(三) 地图-色斑图的对接

背景:

前台所需的数据结构和后台sql查询的数据结构一般不一致,该情况就涉及到了数据结构的转换,往往很多接口的查询都是固定两种数据结构的转化,这个时候就可以把这种转化关系抽离成一个通用的方法,避免大量的重复代码

前台数据结构:

min和max用来确定色域范围

{
year:[2010,2011,2012],
dates:[
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value}
     ]
}

后台数据结构:

id    year    column1    areaCode    areaName
1     2010    value      111         北京
2     2011    value      111         北京
3     2012    value      111         北京
4     2013    value      111         北京
1     2010    value      222         上海
2     2011    value      222         上海
3     2012    value      222         上海
4     2013    value      222         上海
1     2010    value      333         广州
2     2011    value      333         广州
3     2012    value      333         广州
4     2013    value      333         广州

实现:

使用List<Map<String,Object>> 接收后台sql查询数据

sql:select year,column1,areaCode,areaName from test where parentCode= 110000 and year>=2010 and year <=2013

year和areaCode联合唯一

代码:

/**
	 * 
	 * @param results 原始数据结构列
	 * @param valueColumn 需要取值的列名
	 * @return {year:[...],datas:[{data:[{name:地区名,value:数值,areaCode:地区码}....],minValue:最大值,maxValue:最小值}...]}
	 */
	public Map<String,Object> transfDataStructTwo(List<Map<String, Object>> results,String valueColumn){
		//管理引用的数据结构  year:[{columnName:columnValue}...]
		Map<Integer,List<Map<String,Object>>> managerObject = new HashMap<Integer,List<Map<String,Object>>>();
		//管理引用的数据结构 year:{minValue,maxValue};
		Map<Integer,Map<String,Float>> managerMinMax = new HashMap<Integer,Map<String,Float>>();
		//年份数组 从小到大排列
		Set<Integer> years = new TreeSet<Integer>();
		Integer year;
		Float value;
		Float minValue;
		Float maxValue;
		List<Map<String, Object>>  objM;
		Map<String,Float> minMaxM;
		for (Map<String, Object> map : results) {
			year = Integer.parseInt(map.get("static_year").toString());
			value = Float.parseFloat(map.get(valueColumn).toString());
			years.add(year);
			//替换map中valueColumn的名字,去掉年份
			map.put("value", value);
			map.remove(valueColumn);
			map.remove("static_year");
			//管理引用
			objM = managerObject.get(year);
			
			if(objM==null) {
				objM = new ArrayList<Map<String, Object>>();
				managerObject.put(year, objM);
			}
			objM.add(map);
			//求对应年份内的最大最小值
			minMaxM = managerMinMax.get(year);
			if(minMaxM==null) {
				minMaxM = new HashMap<String,Float>();
				managerMinMax.put(year, minMaxM);
				minMaxM.put("minValue", value);
				minMaxM.put("maxValue", value);
			}else {
				minValue = minMaxM.get("minValue");
				maxValue = minMaxM.get("maxValue");
				if(minValue>value) {
					minMaxM.put("minValue", value);
				}else if(maxValue<value) {
					minMaxM.put("maxValue", value);
				}
			}
		}
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("static_year", years);
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		result.put("datas", datas);
		Map<String,Object> data;
		//封装数据
		for (Integer yearKey : years) {
			objM = managerObject.get(yearKey);
			minValue = managerMinMax.get(yearKey).get("minValue");
			maxValue = managerMinMax.get(yearKey).get("maxValue");
			data = new HashMap<String,Object>();
			datas.add(data);
			data.put("data", objM);
			data.put("minValue", minValue);
			data.put("maxValue", maxValue);
		}
		return result;
	}

测试:

public static void main(String[] args) {
		MainTest test = new MainTest();
		List<Map<String,Object>> results = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> result1 = new HashMap<String,Object>();
		results.add(result1);
		result1.put("static_year", 2010);
		result1.put("column1", 100);
		result1.put("areaName", "北京");
		result1.put("areaCode", "111");
		Map<String,Object> result2 = new HashMap<String,Object>();
		results.add(result2);
		result2.put("static_year", 2010);
		result2.put("column1", 90);
		result2.put("areaName", "上海");
		result2.put("areaCode", "222");
		Map<String,Object> result3 = new HashMap<String,Object>();
		results.add(result3);
		result3.put("static_year", 2010);
		result3.put("column1", 80);
		result3.put("areaName", "广州");
		result3.put("areaCode", "333");
		Map<String,Object> result4 = new HashMap<String,Object>();
		results.add(result4);
		result4.put("static_year", 2011);
		result4.put("column1", 105);
		result4.put("areaName", "北京");
		result4.put("areaCode", "111");
		Map<String,Object> result5 = new HashMap<String,Object>();
		results.add(result5);
		result5.put("static_year", 2011);
		result5.put("column1", 95);
		result5.put("areaName", "上海");
		result5.put("areaCode", "222");
		Map<String,Object> result6 = new HashMap<String,Object>();
		results.add(result6);
		result6.put("static_year", 2011);
		result6.put("column1", 85);
		result6.put("areaName", "广州");
		result6.put("areaCode", "333");
		
		Map<String, Object> transfDataStruct = test.transfDataStructTwo(results,"column1");
		System.out.println(transfDataStruct);
	}

结果:

{static_year=[2010, 2011], datas=[{minValue=80.0, data=[{areaCode=111, areaName=北京, value=100.0}, {areaCode=222, areaName=上海, value=90.0}, {areaCode=333, areaName=广州, value=80.0}], maxValue=100.0}, {minValue=85.0, data=[{areaCode=111, areaName=北京, value=105.0}, {areaCode=222, areaName=上海, value=95.0}, {areaCode=333, areaName=广州, value=85.0}], maxValue=105.0}]}

后言:

static_year可以替换为排序字段,本通用型方法只能支持对单一数据列数据的数据结构转化,如果有需要可以往后扩展增加对多个数据列的数据结构转换

扩展后前台接收数据结构:

{
year:[2010,2011,2012],
column1:[
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value}
     ]
column2:[
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value}
     ]
column3:[
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value},
{data:[{areaName:地区名,areaCode:地区码,value:值},...],min:value,max:value}
     ]

}

转载自:https://blog.csdn.net/a13871125303/article/details/81216318

You may also like...