Selaa lähdekoodia

分级选择地址 v1.1:新增CRUD

Tinger 2 vuotta sitten
vanhempi
commit
1fa94d679a

+ 16 - 3
admin/src/main/java/com/huimv/farm/damsubsidy/common/utils/AreaUtil.java

@@ -2,11 +2,24 @@ package com.huimv.farm.damsubsidy.common.utils;
 // Author: Tinger-XXX
 // Time: 2023/5/8 19:29
 
+import com.huimv.farm.damsubsidy.entity.AreaAll;
+
+import java.util.Arrays;
 import java.util.List;
 
 public class AreaUtil {
-    public static <T> boolean include(List<T> arr, T tar) {
-        for (T it : arr) if (tar == it) return true;
-        return false;
+    public static final List<Integer> idLengths = Arrays.asList(2, 4, 6, 9, 12);
+
+    public static <T> boolean isNotIn(T tar, List<T> arr) {
+        for (T it : arr) if (tar == it) return false;
+        return true;
+    }
+
+    public static boolean isError(AreaAll entity) {
+        String id = entity.getId(), name = entity.getName(), level = entity.getLevel();
+        if (id == null || name == null || level == null)
+            return true;
+
+        return isNotIn(id.length(), idLengths) || name.length() > 32 || level.length() != 2;
     }
 }

+ 37 - 0
admin/src/main/java/com/huimv/farm/damsubsidy/config/XssHttpServletRequestWrapper.java

@@ -2,6 +2,8 @@ package com.huimv.farm.damsubsidy.config;
 
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HtmlUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 
 import javax.servlet.ReadListener;
@@ -10,6 +12,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import java.io.*;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -93,6 +96,39 @@ public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
         reader.close();
         in.close();
 
+        // new, edit by Tinger to support <list> as JSON root node:
+        String data_str = body.toString().trim(), json_str;
+        switch (data_str.charAt(0)) {
+            case '[':  // list type
+                JSONArray array = JSONUtil.parseArray(data_str);
+                ArrayList<Object> res_list = new ArrayList<>();
+                for (Object item : array) {
+                    if (item instanceof String) {
+                        res_list.add(HtmlUtil.filter(item.toString()));
+                    } else {
+                        res_list.add(item);
+                    }
+                }
+                json_str = JSONUtil.toJsonStr(res_list);
+                break;
+            case '{':  // json type
+                JSONObject object = JSONUtil.parseObj(data_str);
+                HashMap<String, Object> res_map = new HashMap<>();
+                for (String key : object.keySet()) {
+                    Object value = object.get(key);
+                    if (value instanceof String) {
+                        res_map.put(key, HtmlUtil.filter(value.toString()));
+                    } else {
+                        res_map.put(key, value);
+                    }
+                }
+                json_str = JSONUtil.toJsonStr(res_map);
+                break;
+            default:
+                throw new RuntimeException("bad json data");
+        }
+        final ByteArrayInputStream bain = new ByteArrayInputStream(json_str.getBytes());
+        /* old: <map> as JSON root node only
         Map<String, Object> map = JSONUtil.parseObj(body.toString());
         Map<String, Object> resultMap = new HashMap(map.size());
         for (String key : map.keySet()) {
@@ -105,6 +141,7 @@ public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
         }
         String str = JSONUtil.toJsonStr(resultMap);
         final ByteArrayInputStream bain = new ByteArrayInputStream(str.getBytes());
+        */
         return new ServletInputStream() {
             @Override
             public int read() throws IOException {

+ 65 - 15
admin/src/main/java/com/huimv/farm/damsubsidy/controller/AreaByLevelController.java

@@ -2,14 +2,13 @@ package com.huimv.farm.damsubsidy.controller;
 
 import com.huimv.farm.damsubsidy.common.utils.AreaUtil;
 import com.huimv.farm.damsubsidy.common.utils.Result;
-import com.huimv.farm.damsubsidy.common.utils.ResultCode;
+import com.huimv.farm.damsubsidy.entity.AreaAll;
 import com.huimv.farm.damsubsidy.service.IAreaByLevelService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 
 
 // Author: Tinger-XXX
@@ -18,40 +17,91 @@ import java.util.Map;
 @RequestMapping("/area")
 public class AreaByLevelController {
     @Autowired
-    private IAreaByLevelService areaByLevelService;
-
-    private final List<Integer> idLengths = Arrays.asList(2, 4, 6, 9, 12);
+    private IAreaByLevelService service;
 
     @GetMapping("/types")
     public Result listTypes() {
-        return areaByLevelService.listTypes();
+        return service.listTypes();
     }
 
     @GetMapping("/levels")
     public Result listLevels() {
-        return areaByLevelService.listLevels();
+        return service.listLevels();
     }
 
     @GetMapping("/info")
     public Result getInfo() {
-        return areaByLevelService.getInfo();
+        return service.getInfo();
     }
 
     @GetMapping("/provinces")
     public Result listProvinces() {
-        return areaByLevelService.listProvinces();
+        return service.listProvinces();
     }
 
     @PostMapping("/children_of")
-    public Result listChildrenOf(@RequestBody Map<String, String> map) {
-        String curId = map.get("cur_id"), child = map.get("child");
+    public Result listChildrenOf(@RequestBody AreaAll cur) {
+        String curId = cur.getId(), child = cur.getChild();
         if (curId == null || child == null)
             return new Result(10002, "key param lost", false);
-        boolean include = AreaUtil.include(idLengths, curId.length());
-        if (!include || child.length() != 2)
+        if (AreaUtil.isNotIn(curId.length(), AreaUtil.idLengths) || child.length() != 2)
             return new Result(10002, "error params", false);
 
         if (curId.length() == 12) return new Result(10002, "no more children", false);
-        return areaByLevelService.listChildrenOf(curId, child);
+        return service.listChildrenOf(curId, child);
+    }
+
+    @PostMapping("/add")
+    public Result addArea(@RequestBody AreaAll record) {
+        if (AreaUtil.isError(record))
+            return new Result(10002, "参数错误,添加失败", false);
+        return service.insert(record);
+    }
+
+    @PostMapping("/add_batch")
+    public Result addAreaBatch(@RequestBody List<AreaAll> records) {
+        ArrayList<String> errId = new ArrayList<>();
+        for (AreaAll one: records) if (AreaUtil.isError(one)) errId.add(one.getId());
+
+        if (errId.size() > 0)
+            return new Result(10002, "操作未执行,以下id数据不规范:" + errId, false);
+
+        return service.insert(records);
+    }
+
+    @PostMapping("/remove")
+    public Result removeArea(@RequestBody AreaAll one) {
+        String id = one.getId();
+        if (AreaUtil.isNotIn(id.length(), AreaUtil.idLengths))
+            return new Result(10002, "参数错误,删除失败", false);
+        return service.delete(id);
+    }
+
+    @PostMapping("/remove_batch")
+    public Result removeAreaBatch(@RequestBody List<String> ids) {
+        ArrayList<String> errId = new ArrayList<>();
+        for (String id: ids) if (AreaUtil.isNotIn(id.length(), AreaUtil.idLengths)) errId.add(id);
+
+        if (errId.size() > 0)
+            return new Result(10002, "操作未执行,以下id不规范:" + errId, false);
+
+        return service.delete(ids);
+    }
+
+    @PostMapping("/update")
+    public Result updateArea(@RequestBody AreaAll one) {
+        if (AreaUtil.isError(one))
+            return new Result(10002, "参数错误,更新失败", false);
+        return service.update(one);
+    }
+
+    @PostMapping("/update_batch")
+    public Result updateArea(@RequestBody List<AreaAll> records) {
+        ArrayList<String> errId = new ArrayList<>();
+        for (AreaAll one: records) if (AreaUtil.isError(one)) errId.add(one.getId());
+
+        if (errId.size() > 0)
+            return new Result(10002, "操作未执行,以下id数据不规范:" + errId, false);
+        return service.update(records);
     }
 }

+ 0 - 1
admin/src/main/java/com/huimv/farm/damsubsidy/mapper/AreaByLevelMapper.java

@@ -4,7 +4,6 @@ package com.huimv.farm.damsubsidy.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.huimv.farm.damsubsidy.entity.*;
-import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;

+ 5 - 1
admin/src/main/java/com/huimv/farm/damsubsidy/service/IAreaByLevelService.java

@@ -24,5 +24,9 @@ public interface IAreaByLevelService {
 
     Result delete(String id);
 
-    Result update(AreaAll item);
+    Result delete(List<String> ids);
+
+    Result update(AreaAll one);
+
+    Result update(List<AreaAll> batch);
 }

+ 99 - 10
admin/src/main/java/com/huimv/farm/damsubsidy/service/impl/AreaByLevelServiceImpl.java

@@ -9,6 +9,7 @@ import com.huimv.farm.damsubsidy.service.IAreaByLevelService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 
@@ -60,26 +61,114 @@ public class AreaByLevelServiceImpl implements IAreaByLevelService {
 
     @Override
     public Result insert(AreaAll one) {
-        int res = dbMapper.insert(one);
-        return new Result(ResultCode.SUCCESS, "inserted <" + res + "> record");
+        try {
+            int res = dbMapper.insert(one);
+            return new Result(ResultCode.SUCCESS, "已添加 <" + res + "> 条记录");
+        } catch (Exception ignored) {
+        }
+
+        return new Result(10002, "添加失败", false);
     }
 
     @Override
     public Result insert(List<AreaAll> batch) {
-        int num = 0;
-        for (AreaAll one: batch) num += dbMapper.insert(one);
-        return new Result(ResultCode.SUCCESS, "inserted <" + num + "> record(s)");
+        ArrayList<String> errId = new ArrayList<>();
+        for (AreaAll one : batch) {
+            try {
+                dbMapper.insert(one);
+            } catch (Exception ignored) {
+                errId.add(one.getId());
+            }
+        }
+
+        if (errId.size() == 0)
+            return new Result(ResultCode.SUCCESS, "已添加 <" + batch.size() + "> 条记录");
+        if (errId.size() != batch.size()) {
+            int success = batch.size() - errId.size();
+            return new Result(
+                    ResultCode.SUCCESS,
+                    "已添加 <" + success + "> 条记录, 以下id添加失败: " + errId
+            );
+        }
+
+        return new Result(10002, "全部添加失败", false);
     }
 
     @Override
     public Result delete(String id) {
-        int res = dbMapper.deleteById(id);
-        return new Result(ResultCode.SUCCESS, "deleted <" + res + "> record(s)");
+        try {
+            int res = dbMapper.deleteById(id);
+            return new Result(ResultCode.SUCCESS, "已删除 <" + res + "> 条记录");
+        } catch (Exception ignored) {
+        }
+
+        return new Result(10002, "删除失败", false);
     }
 
     @Override
-    public Result update(AreaAll item) {
-        int res = dbMapper.updateById(item);
-        return new Result(ResultCode.SUCCESS, "updated <" + res + "> record(s)");
+    public Result delete(List<String> ids) {
+        try {
+            int res = dbMapper.deleteBatchIds(ids);
+            return new Result(ResultCode.SUCCESS, "已删除 <" + res + "> 条记录");
+        } catch (Exception ignored) {
+        }
+
+        return new Result(10002, "删除失败", false);
+    }
+
+    @Override
+    public Result update(AreaAll one) {
+        try {
+            int res = dbMapper.updateById(one);
+            return new Result(ResultCode.SUCCESS, "已更新 <" + res + "> 条记录");
+        } catch (Exception ignored) {
+        }
+
+        return new Result(10002, "更新失败", false);
+    }
+
+    @Override
+    public Result update(List<AreaAll> batch) {
+        ArrayList<String> errorId = new ArrayList<>();
+        ArrayList<String> notExistId = new ArrayList<>();
+        for (AreaAll one : batch) {
+            try {
+                int res = dbMapper.updateById(one);
+                if (res == 0) notExistId.add(one.getId());
+            } catch (Exception ignored) {
+                errorId.add(one.getId());
+            }
+        }
+
+        if (errorId.size() == 0 && notExistId.size() == 0)
+            return new Result(ResultCode.SUCCESS, "已更新 <" + batch.size() + "> 条记录");
+        if (errorId.size() != 0 && notExistId.size() != 0) {
+            int success = batch.size() - errorId.size() - notExistId.size();
+            if (success == 0)
+                return new Result(
+                        10002,
+                        "更新失败,以下id不存在:" + notExistId + ",以下id更新失败:" + errorId,
+                        false
+                );
+            return new Result(
+                    ResultCode.SUCCESS,
+                    "已更新 <" + success + "> 条记录,以下id不存在" + notExistId + ",以下id更新失败" + errorId
+            );
+        }
+        if (notExistId.size() != 0) {
+            int success = batch.size() - notExistId.size();
+            return new Result(
+                    ResultCode.SUCCESS,
+                    "已更新 <" + success + "> 条记录, 以下id不存在: " + notExistId
+            );
+        }
+        if (errorId.size() != batch.size()) {
+            int success = batch.size() - errorId.size();
+            return new Result(
+                    ResultCode.SUCCESS,
+                    "已更新 <" + success + "> 条记录, 以下id更新失败: " + errorId
+            );
+        }
+        return new Result(10002, "全部更新失败", false);
     }
 }