From a194540edeca91a570b52bc3095ffef390522d6b Mon Sep 17 00:00:00 2001
From: wuzeyu <1223318623@qq.com>
Date: 星期一, 19 八月 2024 15:36:19 +0800
Subject: [PATCH] 优化地区初始化等级 地区查全部从省开始

---
 pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/district/DistrictCtrl.java |  269 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 240 insertions(+), 29 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/district/DistrictCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/district/DistrictCtrl.java
index 3377625..033a2f9 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/district/DistrictCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/district/DistrictCtrl.java
@@ -1,13 +1,18 @@
 package com.dy.pipIrrBase.district;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
 import com.dy.common.aop.SsoAop;
+import com.dy.common.mybatis.envm.Deleted;
 import com.dy.common.webUtil.BaseResponse;
 import com.dy.common.webUtil.BaseResponseUtils;
+import com.dy.common.webUtil.QueryResultVo;
 import com.dy.common.webUtil.ResultCodeMsg;
+import com.dy.pipIrrGlobal.pojoBa.BaClient;
 import com.dy.pipIrrGlobal.pojoBa.BaDistrict;
 import com.dy.pipIrrGlobal.util.DistrictLevel;
 import io.swagger.v3.oas.annotations.Operation;
-import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.media.Content;
 import io.swagger.v3.oas.annotations.media.Schema;
 import io.swagger.v3.oas.annotations.responses.ApiResponse;
@@ -18,31 +23,27 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.validation.BindingResult;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 
 @Slf4j
 @Tag(name = "琛屾斂鍖哄垝绠$悊", description = "琛屾斂鍖哄垝澧炲垹鏀规煡绛夋搷浣�")
 @RestController
-@RequestMapping(path="district")
+@RequestMapping(path = "district")
 @SuppressWarnings("unchecked")//java鐗堟湰瓒婇珮锛屽娉涘瀷绾︽潫瓒婁弗锛屾墍浠ラ厤缃甋uppressWarnings("unchecked")
 public class DistrictCtrl {
 
-    private DistrictSv sv ;
+    private DistrictSv sv;
 
     @Autowired
-    private void setSv(DistrictSv sv){
-        this.sv = sv ;
+    private void setSv(DistrictSv sv) {
+        this.sv = sv;
     }
 
     /**
      * 瀹㈡埛绔姹傚緱鍒版墍鏈夎鏀垮尯鍒掔骇鍒暟鎹�
+     *
      * @return 鎵�鏈夎鏀垮尯鍒掔骇鍒暟鎹�
      */
     @Operation(summary = "琛屾斂鍖哄垝绾у埆", description = "杩斿洖鎵�鏈夎鏀垮尯鍒掔骇鍒暟鎹�")
@@ -55,32 +56,55 @@
             )
     })
     @GetMapping(path = "allLevel")
-    public BaseResponse<List<DistrictLevel>> allDistrictLevels(){
-        List<DistrictLevel> list = Arrays.asList(DistrictLevel.City, DistrictLevel.County, DistrictLevel.Town, DistrictLevel.Village, DistrictLevel.Group) ;
-        return BaseResponseUtils.buildSuccess(list);
+    public BaseResponse<List<DistrictLevel>> allDistrictLevels() {
+        //List<DistrictLevel> list = Arrays.asList(DistrictLevel.City, DistrictLevel.County, DistrictLevel.Town, DistrictLevel.Village) ;
+        return BaseResponseUtils.buildSuccess(DistrictLevel.LevelList);
     }
 
     /**
      * 瀹㈡埛绔姹傚緱鍒版墍鏈夎鏀垮尯鏁版嵁
+     *
      * @return 鎵�鏈夎鏀垮尯鏁版嵁
      */
     @Operation(summary = "鑾峰緱鎵�鏈夎鏀垮尯", description = "杩斿洖鎵�鏈夎鏀垮尯鏁版嵁")
     @ApiResponses(value = {
             @ApiResponse(
                     responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
-                    description = "杩斿洖鎵�鏈夎鏀垮尯鏁版嵁锛圔aseResponse鏄熀绫伙級",
+                    description = "杩斿洖鎵�鏈夎鏀垮尯鏁版嵁锛圔aseResponse.content:[{}]锛�",
                     content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                             schema = @Schema(implementation = BaDistrict.class))}
             )
     })
-    @GetMapping(path = "allDistricts")
-    public BaseResponse<List<BaDistrict>> allDistricts(){
-        List<BaDistrict> list = this.sv.getAll(DistrictLevel.City.code) ;
+    @GetMapping(path = "all")
+    @SsoAop()
+    public BaseResponse<List<BaDistrict>> all() {
+        List<BaDistrict> list = this.sv.selectAllByLevel(DistrictLevel.Province.code);
         return BaseResponseUtils.buildSuccess(list);
     }
 
     /**
+     * 寰楀埌涓�涓鏀垮尯鏁版嵁
+     *
+     * @return 涓�涓鏀垮尯鏁版嵁
+     */
+    @Operation(summary = "涓�涓鏀垮尯", description = "寰楀埌涓�涓鏀垮尯鏁版嵁")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "杩斿洖涓�涓鏀垮尯鏁版嵁锛圔aseResponse.content:{}锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = BaDistrict.class))}
+            )
+    })
+    @GetMapping(path = "one", consumes = MediaType.TEXT_PLAIN_VALUE)
+    @SsoAop()
+    public BaseResponse<BaDistrict> one(Long id) {
+        return BaseResponseUtils.buildSuccess(this.sv.selectById(id));
+    }
+
+    /**
      * 淇濆瓨琛屾斂鍖�
+     *
      * @param vo 淇濆瓨琛屾斂鍖篺orm琛ㄥ崟瀵硅薄
      * @return 鏄惁鎴愬姛
      */
@@ -88,29 +112,216 @@
     @ApiResponses(value = {
             @ApiResponse(
                     responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
-                    description = "淇濆瓨琛屾斂鍖鸿繑鍥烇細true锛氭垚鍔燂紝false锛氬け璐ワ紙鏁版嵁鍩虹被鐨刢ontent锛�",
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
                     content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                             schema = @Schema(implementation = Boolean.class))}
             )
     })
-    @PostMapping(path = "save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
-    @SsoAop("-1")//@SsoAop(power = "-1")
-    public BaseResponse<Boolean> save(@Parameter(description = "form琛ㄥ崟鏁版嵁", required = true) @Valid DistrictVo vo, @Parameter(hidden = true) BindingResult bindingResult){
-        if(bindingResult != null && bindingResult.hasErrors()){
+    @PostMapping(path = "save", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @SsoAop()
+    public BaseResponse<Boolean> save(@RequestBody @Valid DistrictVo vo, BindingResult bindingResult) {
+        if (bindingResult != null && bindingResult.hasErrors()) {
             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
         }
         BaDistrict po = DistrictVoMapper.INSTANCT.convert(vo);
         int count;
         try {
+            BaDistrict supperPo = this.sv.selectById(po.supperId);
+            if (supperPo == null) {
+                return BaseResponseUtils.buildFail("鍑洪敊锛屾湭寰楀埌涓婄骇琛屾斂鍖�");
+            } else {
+                po.level = DistrictLevel.get((byte)(supperPo.level.code + 1)) ;
+            }
+            po.deleted = Deleted.NO;
             count = this.sv.save(po);
         } catch (Exception e) {
             log.error("淇濆瓨琛屾斂鍖哄紓甯�", e);
-            return BaseResponseUtils.buildException(e.getMessage()) ;
+            return BaseResponseUtils.buildException(e.getMessage());
         }
-        if(count <= 0){
-            return BaseResponseUtils.buildFail("鏁版嵁搴撳瓨鍌ㄥけ璐�") ;
-        }else{
-            return BaseResponseUtils.buildSuccess(true) ;
+        if (count <= 0) {
+            return BaseResponseUtils.buildFail("鏁版嵁搴撳瓨鍌ㄥけ璐�");
+        } else {
+            return BaseResponseUtils.buildSuccess(true);
         }
     }
+
+    /**
+     * 缂栬緫淇敼琛屾斂鍖�
+     *
+     * @param vo 淇濆瓨琛屾斂鍖篺orm琛ㄥ崟瀵硅薄
+     * @return 鏄惁鎴愬姛
+     */
+    @Operation(summary = "缂栬緫淇敼琛屾斂鍖�", description = "鎻愪氦琛屾斂鍖烘暟鎹紙form琛ㄥ崟锛夛紝杩涜淇敼")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @PostMapping(path = "update", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @SsoAop()
+    public BaseResponse<Boolean> update(@RequestBody @Valid DistrictVo vo, BindingResult bindingResult) {
+        if (bindingResult != null && bindingResult.hasErrors()) {
+            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+        }
+        BaDistrict po = DistrictVoMapper.INSTANCT.convert(vo);
+        int count;
+        try {
+            count = this.sv.update(po);
+        } catch (Exception e) {
+            log.error("淇濆瓨琛屾斂鍖哄紓甯�", e);
+            return BaseResponseUtils.buildException(e.getMessage());
+        }
+        if (count <= 0) {
+            return BaseResponseUtils.buildFail("鏁版嵁搴撳瓨鍌ㄥけ璐�");
+        } else {
+            return BaseResponseUtils.buildSuccess(true);
+        }
+    }
+
+
+    /**
+     * 鍒犻櫎琛屾斂鍖�
+     *
+     * @param id 琛屾斂鍖篒D
+     * @return 鏄惁鎴愬姛
+     */
+    @Operation(summary = "鍒犻櫎琛屾斂鍖�", description = "鎻愪氦琛屾斂鍖篒D锛岃繘琛岄�昏緫鍒犻櫎")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = Boolean.class))}
+            )
+    })
+    @GetMapping(path = "delete")
+    @SsoAop()
+    public BaseResponse<Boolean> delete(Long id) {
+        if (id == null) {
+            return BaseResponseUtils.buildFail("id涓嶈兘涓虹┖");
+        }
+        int count;
+        try {
+            count = this.sv.delete(id);
+        } catch (Exception e) {
+            log.error("淇濆瓨琛屾斂鍖哄紓甯�", e);
+            return BaseResponseUtils.buildException(e.getMessage());
+        }
+        if (count <= 0) {
+            return BaseResponseUtils.buildFail("鏁版嵁搴撳瓨鍌ㄥけ璐�");
+        } else {
+            return BaseResponseUtils.buildSuccess(true);
+        }
+    }
+
+    /**
+     * 鏍规嵁绾у埆鑾峰彇琛屾斂鍖哄垝鍒楄〃
+     *
+     * @param id
+     * @return
+     */
+    @Operation(summary = "鑾峰緱琛屾斂鍖哄垝鍒楄〃", description = "杩斿洖琛屾斂鍖哄垝鍒楄〃")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "杩斿洖涓�椤靛啘鎴锋暟鎹紙BaseResponse.content:QueryResultVo[{}]锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = BaClient.class))}
+            )
+    })
+    @GetMapping(path = "/level/{id}")
+    @SsoAop()
+    public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getDistrictgsByLevel(@PathVariable("id") Integer id) {
+        if (id == null || id <= 0) {
+            return BaseResponseUtils.buildFail("id涓嶈兘涓虹┖");
+        }
+
+        List<Map<String, Object>> map_Districts = Optional.ofNullable(sv.getDistrictgsByLevel(id)).orElse(new ArrayList<>());
+        if (map_Districts == null || map_Districts.size() == 0) {
+            return BaseResponseUtils.buildFail("鎮ㄨ緭鍏ョ殑灞傜骇鏈夎");
+        }
+        return BaseResponseUtils.buildSuccess(map_Districts);
+    }
+
+    /**
+     * 鏍规嵁鐖禝D鑾峰彇琛屾斂鍖哄垝鍒楄〃
+     *
+     * @param supperId
+     * @return
+     */
+    @Operation(summary = "鑾峰緱琛屾斂鍖哄垝鍒楄〃", description = "杩斿洖琛屾斂鍖哄垝鍒楄〃")
+    @ApiResponses(value = {
+            @ApiResponse(
+                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
+                    description = "杩斿洖涓�椤靛啘鎴锋暟鎹紙BaseResponse.content:QueryResultVo[{}]锛�",
+                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
+                            schema = @Schema(implementation = BaClient.class))}
+            )
+    })
+    @GetMapping(path = "/supperid/{supperId}")
+    @SsoAop()
+    public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getDistrictsBySupperId(@PathVariable("supperId") Long supperId) {
+        if (supperId == null || supperId <= 0) {
+            return BaseResponseUtils.buildFail("鐖禝D涓嶈兘涓虹┖");
+        }
+
+        List<Map<String, Object>> map_Districts = Optional.ofNullable(sv.getDistrictsBySupperId(supperId)).orElse(new ArrayList<>());
+        if (map_Districts == null || map_Districts.size() == 0) {
+            return BaseResponseUtils.buildFail("鎮ㄨ緭鍏ョ殑鐖剁骇ID鏈夎");
+        }
+        return BaseResponseUtils.buildSuccess(map_Districts);
+    }
+
+    /**
+     * 鑾峰彇涓夌骇琛屾斂鍖哄垝
+     *
+     * @return 鍘块晣鏉戜笁绾ц鏀垮尯鍒�
+     */
+    @GetMapping(path = "/three")
+    @SsoAop()
+    public BaseResponse<QueryResultVo<List<Map<String, Object>>>> getThreeDistrict() {
+        JSONArray array_counties = new JSONArray();
+        JSONArray array_towns = new JSONArray();
+        JSONArray array_villages = new JSONArray();
+
+        List<Map<String, Object>> list_counties = Optional.ofNullable(sv.getDistrictgsByLevel(2)).orElse(new ArrayList<>());
+        if (list_counties.size() > 0) {
+            array_counties = JSONArray.parseArray(JSON.toJSONString(list_counties));
+            // 閬嶅巻姣忎竴涓幙锛屽皢鍘夸笅鐨勯晣鏉戝姞鍒板幙鐨勫瓙闆�
+            for (int i = 0; i < array_counties.size(); i++) {
+                JSONObject job_county = array_counties.getJSONObject(i);
+                Long countyId = job_county.getLong("id");
+                List<Map<String, Object>> list_towns = Optional.ofNullable(sv.getDistrictsBySupperId(countyId)).orElse(new ArrayList<>());
+                if (list_towns.size() > 0) {
+                    array_towns = JSONArray.parseArray(JSON.toJSONString(list_towns));
+                    job_county.put("children", array_towns);
+                    // 閬嶅巻姣忎竴涓晣锛屽皢闀囦笅鏉戝垪琛ㄥ姞鍒伴晣鐨勫瓙闆�
+                    for (int j = 0; j < array_towns.size(); j++) {
+                        JSONObject job_town = array_towns.getJSONObject(j);
+                        Long townId = job_town.getLong("id");
+                        List<Map<String, Object>> list_villages = Optional.ofNullable(sv.getDistrictsBySupperId(townId)).orElse(new ArrayList<>());
+                        if (list_villages.size() > 0) {
+                            array_villages = JSONArray.parseArray(JSON.toJSONString(list_villages));
+                            job_town.put("children", array_villages);
+                        }
+                    }
+                    //System.out.println("----" + array_towns);
+                }
+                // 闀囨潙缁勮瀹屾瘯锛屽皢闀囨潙鍔犲埌鍘夸笅
+//                job_county.put("children", array_towns);
+            }
+            // 鍘跨粍瑁呭畬姣�
+            //String a = array_counties.toJSONString();
+            //System.out.println(a);
+            return BaseResponseUtils.buildSuccess(array_counties);
+        } else {
+            return BaseResponseUtils.buildFail("鍘跨骇琛屾斂鍖哄垝涓嶅瓨鍦�");
+        }
+
+
+        //return BaseResponseUtils.buildSuccess();
+    }
 }

--
Gitblit v1.8.0