1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.dy.pipIrrBase.district;
 
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;
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(@Parameter(description = "实体id", required = true) 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 @Parameter(description = "form表单json数据", required = true) @Valid DistrictVo vo, @Parameter(hidden = true) 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 @Parameter(description = "form表单json数据", required = true) @Valid DistrictVo vo, @Parameter(hidden = true) 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(@Parameter(description = "实体id", required = true) 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);
    }
}