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.media.Content; 
 | 
import io.swagger.v3.oas.annotations.media.Schema; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponse; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponses; 
 | 
import io.swagger.v3.oas.annotations.tags.Tag; 
 | 
import jakarta.validation.Valid; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.MediaType; 
 | 
import org.springframework.validation.BindingResult; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.util.*; 
 | 
  
 | 
@Slf4j 
 | 
@Tag(name = "行政区划管理", description = "行政区划增删改查等操作") 
 | 
@RestController 
 | 
@RequestMapping(path="district") 
 | 
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") 
 | 
public class DistrictCtrl { 
 | 
  
 | 
    private DistrictSv sv ; 
 | 
  
 | 
    @Autowired 
 | 
    private void setSv(DistrictSv sv){ 
 | 
        this.sv = sv ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 客户端请求得到所有行政区划级别数据 
 | 
     * @return 所有行政区划级别数据 
 | 
     */ 
 | 
    @Operation(summary = "行政区划级别", description = "返回所有行政区划级别数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回所有行政区划级别数据(BaseResponse.content:[ { \"code\":1, \"name\":\"市\" }, { \"code\":2, \"name\":\"县\" }])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = DistrictLevel.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "allLevel") 
 | 
    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 = "返回所有行政区数据(BaseResponse.content:[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = BaDistrict.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "all") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<List<BaDistrict>> all(){ 
 | 
        List<BaDistrict> list = this.sv.selectAllByLevel(DistrictLevel.City.code) ; 
 | 
        return BaseResponseUtils.buildSuccess(list); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 得到一个行政区数据 
 | 
     * @return 一个行政区数据 
 | 
     */ 
 | 
    @Operation(summary = "一个行政区", description = "得到一个行政区数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回一个行政区数据(BaseResponse.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 保存行政区form表单对象 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "保存行政区", description = "提交行政区数据(form表单),进行保存") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = Boolean.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @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()) ; 
 | 
        } 
 | 
        if(count <= 0){ 
 | 
            return BaseResponseUtils.buildFail("数据库存储失败") ; 
 | 
        }else{ 
 | 
            return BaseResponseUtils.buildSuccess(true) ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 编辑修改行政区 
 | 
     * @param vo 保存行政区form表单对象 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "编辑修改行政区", description = "提交行政区数据(form表单),进行修改") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,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 行政区ID 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "删除行政区", description = "提交行政区ID,进行逻辑删除") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = Boolean.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "delete", consumes = MediaType.TEXT_PLAIN_VALUE) 
 | 
    @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); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据父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)); 
 | 
                        // 遍历每一个镇,将镇下村列表加到镇的子集 
 | 
                        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(); 
 | 
    } 
 | 
} 
 |