根据层级拼接汇总数据、添加json工具类

main 28
hshansha 5 months ago
parent 7c3bdcb921
commit 09027ed09b

@ -2,14 +2,10 @@ package com.ruoyi.kaohe.controller;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.domain.model.LoginUser; import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.kaohe.domain.KhKhrwResult; import com.ruoyi.kaohe.domain.KhKhrwResult;
import com.ruoyi.kaohe.domain.KhVoteEmp;
import com.ruoyi.kaohe.domain.KhgatherParam;
import com.ruoyi.kaohe.service.IKhKhrwResultService; import com.ruoyi.kaohe.service.IKhKhrwResultService;
import com.ruoyi.kaohe.service.IKhVoteEmpService; import com.ruoyi.kaohe.service.IKhVoteEmpService;
import com.ruoyi.kaohe.service.IKhVoteService; import com.ruoyi.kaohe.service.IKhVoteService;
@ -59,7 +55,7 @@ public class KhGatherController extends BaseController {
*/ */
//@PreAuthorize("@ss.hasPermi('kaohe:gather:list')") //@PreAuthorize("@ss.hasPermi('kaohe:gather:list')")
@PostMapping("/generateSum") @PostMapping("/generateSum")
public AjaxResult generateSum(@RequestBody String json) { public AjaxResult generateSum(@RequestBody String json) throws JsonProcessingException {
// 将String转换为JSONObject // 将String转换为JSONObject
JSONObject jsonObject = new JSONObject(json); JSONObject jsonObject = new JSONObject(json);
String hz_name = jsonObject.getString("title"); String hz_name = jsonObject.getString("title");
@ -83,29 +79,35 @@ public class KhGatherController extends BaseController {
//获取并整合数据 //获取并整合数据
JSONArray tabledata = new JSONArray((JSONArray) nodes.get("alldatas")); JSONArray tabledata = new JSONArray((JSONArray) nodes.get("alldatas"));
JSONArray erjiTtitle_df = new JSONArray((JSONArray) nodes.get("title_df"));
tabledata.putAll(erjiTtitle_df);
//根据被考核对象名分组数据到map中 得到表格内容的最终数据 //根据被考核对象名分组数据到map中 得到表格内容的最终数据
JSONArray endTableData = JsonDeepMerge.mergeByKey(tabledata, "khdx"); JSONArray endTableData = JsonDeepMerge.mergeByKey(tabledata, "khdx");
/* KhGather gather = new KhGather();
KhGather gather = new KhGather();
gather.setHzName(hz_name); gather.setHzName(hz_name);
gather.setTableHeader(header.toString(2)); gather.setTableHeader(header.toString(2));
gather.setTableData(endTableData.toString(2)); gather.setTableData(endTableData.toString(2));
khGatherService.insertKhGather(gather);*/ khGatherService.insertKhGather(gather);
return AjaxResult.success(); return AjaxResult.success();
} }
/** /**
* json * json
*
* @param datas * @param datas
* @param titleP * @param titleP
* @param z_zb * @param z_zb
* @return * @return
*/ */
private Map<String, Object> parseJson(JSONArray datas, String titleP,BigDecimal z_zb) { private Map<String, Object> parseJson(JSONArray datas, String titleP, BigDecimal z_zb) throws JsonProcessingException {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
BigDecimal title_zsfs=BigDecimal.ZERO; JSONArray endtTitle_df = new JSONArray();//存储标题占比时的得分
JSONArray title_df = new JSONArray();//处理标题占比时的得分
String pyTitle = PinYinUtil.getPinyinInitials(titleP);//父级表头 String pyTitle = PinYinUtil.getPinyinInitials(titleP);//父级表头
JSONObject btKhdx = new JSONObject(); JSONObject btKhdx = new JSONObject(true); //表头考核对象存储
JSONObject btTitle = new JSONObject(); JSONObject btTitle = new JSONObject(true);
btTitle.put("name", pyTitle); btTitle.put("name", pyTitle);
btTitle.put("label", titleP); btTitle.put("label", titleP);
JSONArray btchildren = new JSONArray(); JSONArray btchildren = new JSONArray();
@ -129,52 +131,65 @@ public class KhGatherController extends BaseController {
//递归获取下一级数据 //递归获取下一级数据
Map<String, Object> childmap = parseJson(data2, title, zb); Map<String, Object> childmap = parseJson(data2, title, zb);
JSONObject head = (JSONObject) childmap.get("head"); JSONObject head = (JSONObject) childmap.get("head");
JSONObject defen = new JSONObject(); JSONObject defen = new JSONObject(true);
defen.put("name", erjiPyTitle + "_df"); defen.put("name", erjiPyTitle + "_df");
defen.put("label", "得分"); defen.put("label", "得分");
JSONObject zb1 = new JSONObject(); JSONObject zb1 = new JSONObject(true);
zb1.put("name", erjiPyTitle + "_zb"); zb1.put("name", erjiPyTitle + "_zb");
zb1.put("label", "占比"); zb1.put("label", "占比");
JSONObject zsfs = new JSONObject(); JSONObject zsfs = new JSONObject(true);
zsfs.put("name", erjiPyTitle + "_zsfs"); zsfs.put("name", erjiPyTitle + "_zsfs");
zsfs.put("label", "折算分数"); zsfs.put("label", "折算分数");
btchildren.put(head); btchildren.put(head);
btchildren.put(defen); btchildren.put(defen);
btchildren.put(zb1); btchildren.put(zb1);
btchildren.put(zsfs); btchildren.put(zsfs);
//-------------------------获取表格数据------------------------------
JSONArray alldatas1 = (JSONArray) childmap.get("alldatas"); JSONArray alldatas1 = (JSONArray) childmap.get("alldatas");
JSONArray title_df1 = (JSONArray) childmap.get("title_df");
/*if(title_df1==null){//代表它下面还是标题,没有数据 多级标题的
BigDecimal erjidf = BigDecimal.ZERO;
JSONObject outsideTitle = new JSONObject();
JSONObject newDf = new JSONObject();
newDf.put(erjiPyTitle+"_df", erjidf);
if (z_zb != null) { //如果传递过来的标题占比不为null
BigDecimal erjizsfs = z_zb.multiply(erjidf);
newDf.put(erjiPyTitle + "_zb", z_zb);
newDf.put(erjiPyTitle + "_zsfs", erjizsfs);
}
outsideTitle.put(pyTitle,newDf);
title_df.put(outsideTitle);
}else{*/
JSONArray title_df_cl = new JSONArray(JsonDeepMerge.jsonGroupSum(title_df1, "khdx", erjiPyTitle + "_df"));
//循环处理后的标题得分 加上层级及 占比、折算分数
for (Object df : title_df_cl) {
JSONObject dfobj = (JSONObject) df;
BigDecimal erjidf = dfobj.getBigDecimal(erjiPyTitle + "_df");
JSONObject newDf = new JSONObject();
newDf.put(erjiPyTitle + "_df", erjidf);
if (zb != null) { //如果传递过来的标题占比不为null
BigDecimal erjizsfs = zb.multiply(erjidf);
newDf.put(erjiPyTitle + "_zb", zb);
newDf.put(erjiPyTitle + "_zsfs", erjizsfs);
}
dfobj.remove(erjiPyTitle + "_df");//删除原来的数据项
dfobj.put(pyTitle, newDf);//删除原来的数据项
df = dfobj;
}
title_df=title_df_cl;
//循环改变内部层级关系 //循环改变内部层级关系
for (Object tabledatum : alldatas1) { for (Object tabledatum : alldatas1) {
JSONObject obj = (JSONObject) tabledatum; JSONObject obj = (JSONObject) tabledatum;
JSONObject yiji = new JSONObject(); JSONObject yiji = new JSONObject(true);
yiji.put(erjiPyTitle, obj.get(erjiPyTitle)); yiji.put(erjiPyTitle, obj.get(erjiPyTitle));
yiji.put(erjiPyTitle + "_df", title_zsfs);
if (z_zb != null) { //如果传递过来的标题占比不为null
BigDecimal erjizsfs = z_zb.multiply(title_zsfs);
yiji.put(erjiPyTitle + "_zb", z_zb);
yiji.put(erjiPyTitle + "_zsfs", erjizsfs);
}
obj.remove(erjiPyTitle);//删除原来的数据项 obj.remove(erjiPyTitle);//删除原来的数据项
obj.put(pyTitle, yiji); obj.put(pyTitle, yiji);
tabledatum = obj; tabledatum = obj;
} }
alldatas.putAll(alldatas1); alldatas.putAll(alldatas1);
} else { //引用考核任务 } else { //引用考核任务
Long pcId = jobj.getLong("id"); Long pcId = jobj.getLong("id");
@ -193,27 +208,42 @@ public class KhGatherController extends BaseController {
btchildren.put(head1); btchildren.put(head1);
btchildren.put(head2); btchildren.put(head2);
if (zb != null) { //当考核任务不占比时 if (zb != null) { //当考核任务不占比时
JSONObject head3 = new JSONObject(); //占比 JSONObject head3 = new JSONObject(true); //占比
head3.put("name", pykhrwName + "_zb"); head3.put("name", pykhrwName + "_zb");
head3.put("label", "占比"); head3.put("label", "占比");
JSONObject head4 = new JSONObject(); //折算分数 JSONObject head4 = new JSONObject(true); //折算分数
head4.put("name", pykhrwName + "_zsfs"); head4.put("name", pykhrwName + "_zsfs");
head4.put("label", "折算分数"); head4.put("label", "折算分数");
btchildren.put(head3); btchildren.put(head3);
btchildren.put(head4); btchildren.put(head4);
} }
//---------------组合表格数据------------------------------------------------ //---------------组合表格数据------------------------------------------------
JSONArray tabledata = new JSONArray(khrw.get(0).getTableData()); JSONArray tabledata = new JSONArray(khrw.get(0).getTableData());
String pinyinKhrw = PinYinUtil.getPinyinInitials(khrwName); String pinyinKhrw = PinYinUtil.getPinyinInitials(khrwName);
for (Object tabledatum : tabledata) { for (Object tabledatum : tabledata) {
JSONObject obj = (JSONObject) tabledatum; JSONObject obj = (JSONObject) tabledatum;
JSONObject erji = new JSONObject(); JSONObject erji = new JSONObject(true);
erji.put(pinyinKhrw, obj.get(pinyinKhrw)); erji.put(pinyinKhrw, obj.get(pinyinKhrw));
erji.put(pinyinKhrw + "_df", obj.get(pinyinKhrw + "_df")); erji.put(pinyinKhrw + "_df", obj.get(pinyinKhrw + "_df"));
if (zb != null) { if (zb != null) { //代表当前引用考核任务有占比
BigDecimal zsfs = zb.multiply((BigDecimal) obj.get(pinyinKhrw + "_df")); BigDecimal zsfs = zb.multiply((BigDecimal) obj.get(pinyinKhrw + "_df"));
erji.put(pinyinKhrw + "_zb", zb); erji.put(pinyinKhrw + "_zb", zb);
erji.put(pinyinKhrw + "_zsfs", zsfs); erji.put(pinyinKhrw + "_zsfs", zsfs);
if (z_zb != null) {//代表上一级标题有占比
JSONObject khdx = new JSONObject();
khdx.put("khdx", obj.get("khdx"));//存储当前考核对象名称
khdx.put(pyTitle + "_df", zsfs);//存储当前考核对象最后折算分数
title_df.put(khdx);
}
} else {
if (z_zb != null) {//代表上一级标题有占比
title_df.put(obj.getJSONObject("khdx"));//
JSONObject khdx = new JSONObject();
khdx.put("khdx", obj.get("khdx"));//存储当前考核对象名称
khdx.put(pyTitle + "_df", obj.get(pinyinKhrw + "_df"));///存储当前考核对象最后折算分数
title_df.put(khdx);
}
} }
obj.remove(pinyinKhrw);//删除原来的数据项 obj.remove(pinyinKhrw);//删除原来的数据项
obj.remove(pinyinKhrw + "_df");//删除原来的得分 obj.remove(pinyinKhrw + "_df");//删除原来的得分
@ -224,11 +254,14 @@ public class KhGatherController extends BaseController {
} }
} }
} }
btTitle.put("children", btchildren); btTitle.put("children", btchildren);
result.put("head", btTitle); result.put("head", btTitle);
result.put("data", bd); result.put("data", bd);
result.put("alldatas", alldatas); result.put("alldatas", alldatas);
result.put("btKhdx", btKhdx); result.put("btKhdx", btKhdx);
result.put("title_df", title_df);
return result; return result;
} }

@ -160,7 +160,7 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService {
).divide(voteEmp.getVoteNum()); ).divide(voteEmp.getVoteNum());
voteEmp.setAvgScore(avgScore);//投票后计算平均分 voteEmp.setAvgScore(avgScore);//投票后计算平均分
if (oldvoteEmp.getPercentage() != null) { //计算最后得分 if (oldvoteEmp.getPercentage() != null) { //计算最后得分
voteEmp.setEndScore(avgScore.multiply(voteEmp.getPercentage())); voteEmp.setEndScore(avgScore.multiply(oldvoteEmp.getPercentage()));
}else{ }else{
voteEmp.setEndScore(avgScore); voteEmp.setEndScore(avgScore);
} }

@ -1,9 +1,12 @@
package com.ruoyi.kaohe.util; package com.ruoyi.kaohe.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Iterator; import java.math.BigDecimal;
import java.util.*;
public class JsonDeepMerge { public class JsonDeepMerge {
/** /**
@ -57,4 +60,35 @@ public class JsonDeepMerge {
JSONArray merged = mergeByKey(array, "khdx"); JSONArray merged = mergeByKey(array, "khdx");
System.out.println(merged.toString(2)); System.out.println(merged.toString(2));
} }
public static String jsonGroupSum(JSONArray jsonInput,String groupKey,String sumKey) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// 解析JSON数组为List<Map>
List<Map<String, Object>> inputList = mapper.readValue(jsonInput.toString(2), List.class);
// 使用Map按khdx分组并累加值
Map<String, BigDecimal> sumMap = new HashMap<>();
for (Map<String, Object> item : inputList) {
String khdx = (String) item.get(groupKey);
BigDecimal value=BigDecimal.ZERO;
if(item.get(sumKey)!=null){ //非空判断
value = new BigDecimal(item.get(sumKey).toString());
}
sumMap.merge(khdx, value, BigDecimal::add);
}
// 转换为目标结构List<Map>
List<Map<String, Object>> result = new ArrayList<>();
sumMap.forEach((khdx, sum) -> {
Map<String, Object> entry = new HashMap<>();
entry.put(groupKey, khdx);
entry.put(sumKey, sum);
result.add(entry);
});
// 输出结果JSON
String jsonOutput = mapper.writeValueAsString(result);
// System.out.println(jsonOutput);
return jsonOutput;
}
} }

Loading…
Cancel
Save