需求:
查詢上個月每個人各個插件的總加分彙總爲一個json存儲到一個字段裏
查詢上個月每個組織機構插件的各個插件的總加分彙總爲一個json存儲到一個字段裏
流程:
- 查詢後返回結果是一個List集合,每一條記錄的字段(user_id,plugin_id,分數)
- 定義一個大Hashmap[key–userId,value–(plugin_id,integral)]
- 定義一個小Hashmap[key–plugin_id,value–ntegral]
流程圖:
最終的結果:
查詢上個月每個人各個插件的總加分彙總爲一個json存儲到一個字段裏
查詢上個月每個組織機構插件的各個插件的總加分彙總爲一個json存儲到一個字段裏
注意:在判斷的過程中注意一下邏輯既可以了
核心代碼:
//查詢本月插件加分
List<QueryMonthPluginModel> queryMonthPlugin = integralDetailDao.QueryMonthPlugin();
//更新本月插件加分
//定義一個HashMap用來存放queryMonthPlugin遍歷結果[key-userid value(plugin_id,integral)]
HashMap<String, HashMap> MonthPluginHashMaps = new HashMap<String, HashMap>();
//實例化一個model(plugin_id,integral)
MonthPluginSumModel monthPluginSumModel = new MonthPluginSumModel();
//定義一個HashMap用來存放遍歷內容 插件(key)-加分(value)
HashMap<String, Integer> MonthPluginHashMap = new HashMap<String, Integer>();
//實例化一個UserId
String USERID = "1";
//for(Iterator<QueryMonthPluginModel> it = queryMonthPlugin.iterator(); it.hasNext(); ) {}
for (QueryMonthPluginModel queryMonthPluginModel : queryMonthPlugin) {
//得帶外層Hashmap(MonthPluginHashMaps)的key值-userId
String userId = queryMonthPluginModel.getUserId();
//判斷外層HashMap裏有沒有這個人的UserId
boolean Istrue = userId.equals(USERID);
//boolean isFalse = USERID.equals(userId);
if (Istrue) {
MonthPluginHashMap.put(queryMonthPluginModel.getPluginId(), queryMonthPluginModel.getSumIntegral());
} else {
//nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'MonthHashmapModel' in 'class com.tfjybj.integral.model.MonthHashmapModel'
//如果外層HashMap裏不包含這個這個人的UserId
//將這個人的keyValue(userId,pluginId-integral)存到數據庫中
//將這個人的keyValue pluginId-integral轉爲Json格式
boolean Istrueoffalse = USERID.equals("1");
if (!USERID.equals("1")) {
String HashJson = JSONUtils.toJSONString(MonthPluginHashMap);
MonthHashmapModel monthHashmapModel = new MonthHashmapModel();
monthHashmapModel.setUserId(USERID);
monthHashmapModel.setPluginJson(HashJson);
monthIntegralDao.UpdateMonthPlugin(monthHashmapModel);
}
MonthPluginHashMap.clear();
MonthPluginHashMap.put(queryMonthPluginModel.getPluginId(), queryMonthPluginModel.getSumIntegral());
MonthPluginHashMaps.put(userId, MonthPluginHashMap);
USERID = userId;
}
}