Browse Source

处理集合数据中精度丢失

buzhanyi 1 year ago
parent
commit
b3e11bbae3

+ 9 - 8
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/controller/SysDeptController.java

@@ -380,8 +380,8 @@ public class SysDeptController {
     @GetMapping("/listDict")
     public ResMsg listDict(SysDept filter) {
         List<SysDept> resultList = sysDeptService.getListByFilter(filter);
-        return ResMsg.newInstance(CodeEnum.OK, BeanQuery.select(
-                "parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList));
+        List<Map<String, Object>> mapList = BeanQuery.select("parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList);
+        return ResMsg.newInstance(CodeEnum.OK, sysDeptService.lossOfAccuracy(mapList));
     }
 
     /**
@@ -391,10 +391,10 @@ public class SysDeptController {
      * @return 应答结果对象,包含字典形式的数据集合。
      */
     @PostMapping("/listDictByIds")
-    public ResponseResult<List<Map<String, Object>>> listDictByIds(@MyRequestBody List<Long> dictIds) {
+    public ResponseResult<List<Map<String, String>>> listDictByIds(@MyRequestBody List<Long> dictIds) {
         List<SysDept> resultList = sysDeptService.getInList(new HashSet<>(dictIds));
-        return ResponseResult.success(BeanQuery.select(
-                "parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList));
+        List<Map<String, Object>> mapList = BeanQuery.select("parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList);
+        return ResponseResult.success(sysDeptService.lossOfAccuracy(mapList));
     }
 
     /**
@@ -405,12 +405,13 @@ public class SysDeptController {
      * @return 按照字典的形式返回下级数据列表。
      */
     @GetMapping("/listDictByParentId")
-    public ResponseResult<List<Map<String, Object>>> listDictByParentId(@RequestParam(required = false) Long parentId) {
+    public ResponseResult<List<Map<String, String>>> listDictByParentId(@RequestParam(required = false) Long parentId) {
         List<SysDept> resultList = sysDeptService.getListByParentId("parentId", parentId);
-        return ResponseResult.success(BeanQuery.select(
-                "parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList));
+        List<Map<String, Object>> mapList = BeanQuery.select("parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList);
+        return ResponseResult.success(sysDeptService.lossOfAccuracy(mapList));
     }
 
+
     /**
      * 根据父主键Id列表,获取当前部门Id及其所有下级部门Id列表。
      * 白名单接口,登录用户均可访问。

+ 8 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/service/SysDeptService.java

@@ -165,4 +165,12 @@ public interface SysDeptService extends IBaseService<SysDept, Long> {
      * @return 获取当前部门Id及其所有下级部门Id列表。
      */
     List<Long> getAllChildDeptIdByParentIds(List<Long> parentIds);
+
+    /**
+     * 处理集合数据中精度丢失
+     *
+     * @param mapList
+     * @return
+     */
+    List<Map<String, String>> lossOfAccuracy(List<Map<String, Object>> mapList);
 }

+ 31 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/service/impl/SysDeptServiceImpl.java

@@ -2,6 +2,8 @@ package com.ankaibei.workFlow.webadmin.upms.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.jimmyshi.beanquery.BeanQuery;
+import com.ankaibei.workFlow.common.core.object.ResponseResult;
 import com.baomidou.mybatisplus.core.conditions.query.*;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.ankaibei.workFlow.webadmin.upms.service.*;
@@ -346,4 +348,33 @@ public class SysDeptServiceImpl extends BaseService<SysDept, Long> implements Sy
         return sysDeptRelationMapper.selectList(queryWrapper)
                 .stream().map(SysDeptRelation::getDeptId).collect(Collectors.toList());
     }
+
+    /**
+     * 处理集合数据中精度丢失
+     *
+     * @param mapList
+     * @return
+     */
+    @Override
+    public List<Map<String, String>> lossOfAccuracy(List<Map<String, Object>> mapList) {
+        //object 转为string避免精度丢失
+        List<Map<String, String>> newList = new ArrayList<>();
+        for (Map<String, Object> map : mapList) {
+            Map<String, String> newMap = new HashMap<>();
+
+            for (Map.Entry<String, Object> entry : map.entrySet()) {
+                String key = entry.getKey();
+                Object value = entry.getValue();
+
+                if (value != null) {
+                    String stringValue = value.toString();
+                    newMap.put(key, stringValue);
+                }
+            }
+
+            newList.add(newMap);
+        }
+        return newList;
+    }
+
 }