From 09027ed09bbf02f8f03b983a6aadd2bcbfa50460 Mon Sep 17 00:00:00 2001 From: hshansha Date: Mon, 14 Jul 2025 08:47:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=B1=82=E7=BA=A7=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5=E6=B1=87=E6=80=BB=E6=95=B0=E6=8D=AE=E3=80=81=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0json=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kaohe/controller/KhGatherController.java | 117 +++++++++++------- .../service/impl/KhVoteEmpServiceImpl.java | 2 +- .../com/ruoyi/kaohe/util/JsonDeepMerge.java | 36 +++++- 3 files changed, 111 insertions(+), 44 deletions(-) diff --git a/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/controller/KhGatherController.java b/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/controller/KhGatherController.java index a998500..396679f 100644 --- a/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/controller/KhGatherController.java +++ b/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/controller/KhGatherController.java @@ -2,14 +2,10 @@ package com.ruoyi.kaohe.controller; import java.math.BigDecimal; import java.util.*; -import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; -import com.ruoyi.common.core.domain.model.LoginUser; -import com.ruoyi.common.utils.LogUtils; +import com.fasterxml.jackson.core.JsonProcessingException; 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.IKhVoteEmpService; import com.ruoyi.kaohe.service.IKhVoteService; @@ -59,7 +55,7 @@ public class KhGatherController extends BaseController { */ //@PreAuthorize("@ss.hasPermi('kaohe:gather:list')") @PostMapping("/generateSum") - public AjaxResult generateSum(@RequestBody String json) { + public AjaxResult generateSum(@RequestBody String json) throws JsonProcessingException { // 将String转换为JSONObject JSONObject jsonObject = new JSONObject(json); String hz_name = jsonObject.getString("title"); @@ -68,7 +64,7 @@ public class KhGatherController extends BaseController { JSONArray header = new JSONArray();//存放表头的最终数据 //---------------------------------------------------------------------------------------------- //循环主标题下的每一个子节点 - Map nodes = parseJson(datas, hz_name,null); + Map nodes = parseJson(datas, hz_name, null); //获取并拼接表头 JSONObject btKhdx = (JSONObject) nodes.get("btKhdx"); JSONObject head = (JSONObject) nodes.get("head"); @@ -83,29 +79,35 @@ public class KhGatherController extends BaseController { //获取并整合数据 JSONArray tabledata = new JSONArray((JSONArray) nodes.get("alldatas")); + JSONArray erjiTtitle_df = new JSONArray((JSONArray) nodes.get("title_df")); + tabledata.putAll(erjiTtitle_df); //根据被考核对象名分组数据到map中 得到表格内容的最终数据 JSONArray endTableData = JsonDeepMerge.mergeByKey(tabledata, "khdx"); -/* KhGather gather = new KhGather(); + + KhGather gather = new KhGather(); gather.setHzName(hz_name); gather.setTableHeader(header.toString(2)); gather.setTableData(endTableData.toString(2)); - khGatherService.insertKhGather(gather);*/ + khGatherService.insertKhGather(gather); return AjaxResult.success(); } /** * 递归解析json + * * @param datas * @param titleP * @param z_zb * @return */ - private Map parseJson(JSONArray datas, String titleP,BigDecimal z_zb) { + private Map parseJson(JSONArray datas, String titleP, BigDecimal z_zb) throws JsonProcessingException { + Map result = new HashMap<>(); - BigDecimal title_zsfs=BigDecimal.ZERO; + JSONArray endtTitle_df = new JSONArray();//存储标题占比时的得分 + JSONArray title_df = new JSONArray();//处理标题占比时的得分 String pyTitle = PinYinUtil.getPinyinInitials(titleP);//父级表头 - JSONObject btKhdx = new JSONObject(); - JSONObject btTitle = new JSONObject(); + JSONObject btKhdx = new JSONObject(true); //表头考核对象存储 + JSONObject btTitle = new JSONObject(true); btTitle.put("name", pyTitle); btTitle.put("label", titleP); JSONArray btchildren = new JSONArray(); @@ -127,54 +129,67 @@ public class KhGatherController extends BaseController { if (type.equals("0")) { JSONArray data2 = jobj.getJSONArray("data"); //递归获取下一级数据 - Map childmap = parseJson(data2, title,zb); + Map childmap = parseJson(data2, title, zb); JSONObject head = (JSONObject) childmap.get("head"); - JSONObject defen = new JSONObject(); + JSONObject defen = new JSONObject(true); defen.put("name", erjiPyTitle + "_df"); defen.put("label", "得分"); - JSONObject zb1 = new JSONObject(); + JSONObject zb1 = new JSONObject(true); zb1.put("name", erjiPyTitle + "_zb"); zb1.put("label", "占比"); - JSONObject zsfs = new JSONObject(); + JSONObject zsfs = new JSONObject(true); zsfs.put("name", erjiPyTitle + "_zsfs"); zsfs.put("label", "折算分数"); btchildren.put(head); btchildren.put(defen); btchildren.put(zb1); btchildren.put(zsfs); + //-------------------------获取表格数据------------------------------ 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) { JSONObject obj = (JSONObject) tabledatum; - JSONObject yiji = new JSONObject(); + JSONObject yiji = new JSONObject(true); 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.put(pyTitle, yiji); tabledatum = obj; - - - - - } alldatas.putAll(alldatas1); - } else { //引用考核任务 Long pcId = jobj.getLong("id"); @@ -193,27 +208,42 @@ public class KhGatherController extends BaseController { btchildren.put(head1); btchildren.put(head2); if (zb != null) { //当考核任务不占比时 - JSONObject head3 = new JSONObject(); //占比 + JSONObject head3 = new JSONObject(true); //占比 head3.put("name", pykhrwName + "_zb"); head3.put("label", "占比"); - JSONObject head4 = new JSONObject(); //折算分数 + JSONObject head4 = new JSONObject(true); //折算分数 head4.put("name", pykhrwName + "_zsfs"); head4.put("label", "折算分数"); btchildren.put(head3); btchildren.put(head4); } + //---------------组合表格数据------------------------------------------------ JSONArray tabledata = new JSONArray(khrw.get(0).getTableData()); String pinyinKhrw = PinYinUtil.getPinyinInitials(khrwName); for (Object tabledatum : tabledata) { JSONObject obj = (JSONObject) tabledatum; - JSONObject erji = new JSONObject(); + JSONObject erji = new JSONObject(true); erji.put(pinyinKhrw, obj.get(pinyinKhrw)); erji.put(pinyinKhrw + "_df", obj.get(pinyinKhrw + "_df")); - if (zb != null) { + if (zb != null) { //代表当前引用考核任务有占比 BigDecimal zsfs = zb.multiply((BigDecimal) obj.get(pinyinKhrw + "_df")); erji.put(pinyinKhrw + "_zb", zb); 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 + "_df");//删除原来的得分 @@ -224,11 +254,14 @@ public class KhGatherController extends BaseController { } } } + + btTitle.put("children", btchildren); result.put("head", btTitle); result.put("data", bd); result.put("alldatas", alldatas); result.put("btKhdx", btKhdx); + result.put("title_df", title_df); return result; } diff --git a/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/service/impl/KhVoteEmpServiceImpl.java b/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/service/impl/KhVoteEmpServiceImpl.java index 0c9eb2d..f0fbe61 100644 --- a/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/service/impl/KhVoteEmpServiceImpl.java +++ b/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/service/impl/KhVoteEmpServiceImpl.java @@ -160,7 +160,7 @@ public class KhVoteEmpServiceImpl implements IKhVoteEmpService { ).divide(voteEmp.getVoteNum()); voteEmp.setAvgScore(avgScore);//投票后计算平均分 if (oldvoteEmp.getPercentage() != null) { //计算最后得分 - voteEmp.setEndScore(avgScore.multiply(voteEmp.getPercentage())); + voteEmp.setEndScore(avgScore.multiply(oldvoteEmp.getPercentage())); }else{ voteEmp.setEndScore(avgScore); } diff --git a/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/util/JsonDeepMerge.java b/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/util/JsonDeepMerge.java index b30bc68..829506d 100644 --- a/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/util/JsonDeepMerge.java +++ b/ruoyi-kaohe/src/main/java/com/ruoyi/kaohe/util/JsonDeepMerge.java @@ -1,9 +1,12 @@ package com.ruoyi.kaohe.util; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.json.JSONArray; import org.json.JSONObject; -import java.util.Iterator; +import java.math.BigDecimal; +import java.util.*; public class JsonDeepMerge { /** @@ -57,4 +60,35 @@ public class JsonDeepMerge { JSONArray merged = mergeByKey(array, "khdx"); System.out.println(merged.toString(2)); } + + public static String jsonGroupSum(JSONArray jsonInput,String groupKey,String sumKey) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + // 解析JSON数组为List + List> inputList = mapper.readValue(jsonInput.toString(2), List.class); + + // 使用Map按khdx分组并累加值 + Map sumMap = new HashMap<>(); + for (Map 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 + List> result = new ArrayList<>(); + sumMap.forEach((khdx, sum) -> { + Map 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; + } }