wuzeyu
2024-08-19 a194540edeca91a570b52bc3095ffef390522d6b
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/district/DistrictCtrl.java
@@ -1,14 +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;
@@ -21,26 +25,25 @@
import org.springframework.validation.BindingResult;
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版本越高,对泛型约束越严,所以配置SuppressWarnings("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 = "返回所有行政区划级别数据")
@@ -53,13 +56,14 @@
            )
    })
    @GetMapping(path = "allLevel")
    public BaseResponse<List<DistrictLevel>> allDistrictLevels(){
        List<DistrictLevel> list = Arrays.asList(DistrictLevel.City, DistrictLevel.County, DistrictLevel.Town, DistrictLevel.Village) ;
        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 = "返回所有行政区数据")
@@ -72,14 +76,15 @@
            )
    })
    @GetMapping(path = "all")
    @SsoAop("-1")
    public BaseResponse<List<BaDistrict>> all(){
        List<BaDistrict> list = this.sv.selectAllByLevel(DistrictLevel.City.code) ;
    @SsoAop()
    public BaseResponse<List<BaDistrict>> all() {
        List<BaDistrict> list = this.sv.selectAllByLevel(DistrictLevel.Province.code);
        return BaseResponseUtils.buildSuccess(list);
    }
    /**
     * 得到一个行政区数据
     *
     * @return 一个行政区数据
     */
    @Operation(summary = "一个行政区", description = "得到一个行政区数据")
@@ -91,14 +96,15 @@
                            schema = @Schema(implementation = BaDistrict.class))}
            )
    })
    @GetMapping(path = "one")
    @SsoAop("-1")
    public BaseResponse<BaDistrict> one(@Parameter(description = "实体id", required = true) Long id){
    @GetMapping(path = "one", consumes = MediaType.TEXT_PLAIN_VALUE)
    @SsoAop()
    public BaseResponse<BaDistrict> one(Long id) {
        return BaseResponseUtils.buildSuccess(this.sv.selectById(id));
    }
    /**
     * 保存行政区
     *
     * @param vo 保存行政区form表单对象
     * @return 是否成功
     */
@@ -111,36 +117,37 @@
                            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{
            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 ;
            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 保存行政区form表单对象
     * @return 是否成功
     */
@@ -153,10 +160,10 @@
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @SsoAop("-1")//@SsoAop(power = "-1")
    public BaseResponse<Boolean> update(@Parameter(description = "form表单数据", required = true) @Valid DistrictVo vo, @Parameter(hidden = true) BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
    @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);
@@ -165,18 +172,19 @@
            count = this.sv.update(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 id 行政区ID
     * @return 是否成功
     */
@@ -189,23 +197,131 @@
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @GetMapping(path = "delete", consumes = MediaType.TEXT_PLAIN_VALUE)
    @SsoAop("-1")//@SsoAop(power = "-1")
    public BaseResponse<Boolean> delete(@Parameter(description = "实体id", required = true) Long id){
        if(id == null){
            return BaseResponseUtils.buildFail("id不能为空") ;
    @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()) ;
            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 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);
    }
    /**
     * 根据父ID获取行政区划列表
     *
     * @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("父ID不能为空");
        }
        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();
    }
}