ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package jnpf.base.controller;
 
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.AreaApi;
import jnpf.base.Page;
import jnpf.base.entity.ProvinceEntity;
import jnpf.base.model.province.*;
import jnpf.base.service.ProvinceService;
import jnpf.base.vo.ListVO;
import jnpf.constant.MsgCode;
import jnpf.exception.DataException;
import jnpf.util.JsonUtil;
import jnpf.util.JsonUtilEx;
import jnpf.util.StringUtil;
import jnpf.util.treeutil.ListToTreeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import jakarta.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 行政区划
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
 * @date 2019年9月27日 上午9:18
 */
@Tag(name = "行政区划" , description = "Area" )
@RestController
@RequestMapping("/Area" )
public class AreaController extends SuperController<ProvinceService, ProvinceEntity> implements AreaApi {
 
    @Autowired
    private ProvinceService provinceService;
 
    /**
     * 列表(异步加载)
     *
     * @param nodeId 节点主键
     * @param page   关键字
     * @return
     */
    @Operation(summary = "列表(异步加载)" )
    @Parameters({
            @Parameter(name = "nodeId" , description = "节点主键" , required = true)
    })
    @SaCheckPermission("sysData.area" )
    @GetMapping("/{nodeId}" )
    public ActionResult<ListVO<ProvinceListVO>> list(@PathVariable("nodeId" ) String nodeId, PaginationProvince page) {
        List<ProvinceEntity> data = provinceService.getList(nodeId, page);
        List<ProvinceEntity> dataAll = data;
        List<ProvinceEntity> result = JsonUtil.getJsonToList(ListToTreeUtil.treeWhere(data, dataAll), ProvinceEntity.class);
        List<ProvinceListVO> treeList = JsonUtil.getJsonToList(result, ProvinceListVO.class);
        int i = 0;
        for (ProvinceListVO entity : treeList) {
            boolean childNode = provinceService.getList(entity.getId()).size() <= 0;
            ProvinceListVO provinceListVO = JsonUtil.getJsonToBean(entity, ProvinceListVO.class);
            provinceListVO.setIsLeaf(childNode);
            provinceListVO.setHasChildren(!childNode);
            treeList.set(i, provinceListVO);
            i++;
        }
        ListVO<ProvinceListVO> vo = new ListVO<>();
        vo.setList(treeList);
        return ActionResult.success(vo);
    }
 
    /**
     * 获取行政区划下拉框数据
     *
     * @param id  主键
     * @param ids 主键集合
     * @return
     */
    @Operation(summary = "获取行政区划下拉框数据" )
    @Parameters({
            @Parameter(name = "id" , description = "主键" , required = true),
            @Parameter(name = "ids" , description = "主键集合" , required = true)
    })
    @GetMapping("/{id}/Selector/{ids}" )
    public ActionResult<ListVO<ProvinceSelectListVO>> selectList(@PathVariable("id" ) String id, @PathVariable("ids" ) String ids) {
        List<ProvinceEntity> data = provinceService.getList(id);
        data = data.stream().filter(t -> t.getEnabledMark() == 1).collect(Collectors.toList());
        if (!"0".equals(ids)) {
            //排除子集
            filterData(data, new ArrayList<>(Arrays.asList(new String[]{ids})));
        }
        List<ProvinceSelectListVO> treeList = JsonUtil.getJsonToList(data, ProvinceSelectListVO.class);
        int i = 0;
        for (ProvinceSelectListVO entity : treeList) {
//            boolean childNode = provinceService.getList(entity.getId()).size() <= 0;
            ProvinceSelectListVO provinceListVO = JsonUtil.getJsonToBean(entity, ProvinceSelectListVO.class);
            provinceListVO.setIsLeaf(false);
            treeList.set(i, provinceListVO);
            i++;
        }
        ListVO<ProvinceSelectListVO> vo = new ListVO<>();
        vo.setList(treeList);
        return ActionResult.success(vo);
    }
 
    /**
     * 递归排除子集
     *
     * @param data
     * @param id
     */
    private void filterData(List<ProvinceEntity> data, List<String> id) {
        List<ProvinceEntity> collect = null;
        //获取子集信息
        for (String ids : id) {
            collect = data.stream().filter(t -> ids.equals(t.getParentId())).collect(Collectors.toList());
            data.removeAll(collect);
        }
        //递归移除子集的子集
        if (collect.size() > 0) {
            filterData(data, collect.stream().map(t -> t.getId()).collect(Collectors.toList()));
        }
    }
 
    /**
     * 信息
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "获取行政区划信息" )
    @Parameters({
            @Parameter(name = "id" , description = "主键" , required = true)
    })
    @SaCheckPermission("sysData.area" )
    @GetMapping("/{id}/Info" )
    public ActionResult<ProvinceInfoVO> info(@PathVariable("id" ) String id) throws DataException {
        ProvinceEntity entity = provinceService.getInfo(id);
        ProvinceInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, ProvinceInfoVO.class);
        if (!"-1".equals(entity.getParentId())) {
            ProvinceEntity parent = provinceService.getInfo(entity.getParentId());
            vo.setParentName(parent.getFullName());
        }
        return ActionResult.success(vo);
    }
 
    /**
     * 新建
     *
     * @param provinceCrForm 实体对象
     * @return
     */
    @Operation(summary = "添加行政区划" )
    @Parameters({
            @Parameter(name = "provinceCrForm" , description = "实体对象" , required = true)
    })
    @SaCheckPermission("sysData.area" )
    @PostMapping
    public ActionResult create(@RequestBody @Valid ProvinceCrForm provinceCrForm) {
        ProvinceEntity entity = JsonUtil.getJsonToBean(provinceCrForm, ProvinceEntity.class);
        if (provinceService.isExistByEnCode(provinceCrForm.getEnCode(), entity.getId())) {
            return ActionResult.fail(MsgCode.SYS001.get());
        }
        if (StringUtil.isEmpty(provinceCrForm.getParentId())) {
            entity.setParentId("-1" );
        }
        if (entity.getParentId().equals("-1" )) {
            entity.setType("1" );
        } else {
            ProvinceEntity info = provinceService.getInfo(provinceCrForm.getParentId());
            int i = Integer.valueOf(info.getType()) + 1;
            entity.setType(String.valueOf(i));
        }
        provinceService.create(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }
 
    /**
     * 更新
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "修改行政区划" )
    @Parameters({
            @Parameter(name = "id" , description = "主键值" , required = true),
            @Parameter(name = "provinceUpForm" , description = "实体对象" , required = true)
    })
    @SaCheckPermission("sysData.area" )
    @PutMapping("/{id}" )
    public ActionResult update(@PathVariable("id" ) String id, @RequestBody @Valid ProvinceUpForm provinceUpForm) {
        ProvinceEntity entity = JsonUtil.getJsonToBean(provinceUpForm, ProvinceEntity.class);
        if (provinceService.isExistByEnCode(provinceUpForm.getEnCode(), id)) {
            return ActionResult.fail(MsgCode.SYS001.get());
        }
        boolean flag = provinceService.update(id, entity);
        if (!flag) {
            return ActionResult.fail(MsgCode.FA002.get());
        }
        return ActionResult.success(MsgCode.SU004.get());
    }
 
    /**
     * 删除
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "删除" )
    @Parameters({
            @Parameter(name = "id" , description = "主键值" , required = true)
    })
    @SaCheckPermission("sysData.area" )
    @DeleteMapping("/{id}" )
    public ActionResult delete(@PathVariable("id" ) String id) {
        if (provinceService.getList(id).size() == 0) {
            ProvinceEntity entity = provinceService.getInfo(id);
            if (entity != null) {
                provinceService.delete(entity);
                return ActionResult.success(MsgCode.SU003.get());
            }
            return ActionResult.fail(MsgCode.FA003.get());
        } else {
            return ActionResult.fail(MsgCode.SYS002.get());
        }
    }
 
    /**
     * 更新行政区划状态
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "更新行政区划状态" )
    @Parameters({
            @Parameter(name = "id" , description = "主键值" , required = true)
    })
    @SaCheckPermission("sysData.area" )
    @PutMapping("/{id}/Actions/State" )
    public ActionResult upState(@PathVariable("id" ) String id) {
        ProvinceEntity entity = provinceService.getInfo(id);
        if (entity.getEnabledMark() == null || "1".equals(String.valueOf(entity.getEnabledMark()))) {
            entity.setEnabledMark(0);
        } else {
            entity.setEnabledMark(1);
        }
        boolean flag = provinceService.update(id, entity);
        if (!flag) {
            return ActionResult.fail(MsgCode.FA002.get());
        }
        return ActionResult.success(MsgCode.SU004.get());
    }
 
    /**
     * 行政区划id转名称
     *
     * @param model 二维数组
     * @return ignore
     */
    @Operation(summary = "行政区划id转名称" )
    @Parameters({
            @Parameter(name = "model" , description = "二维数组" , required = true)
    })
    @PostMapping("/GetAreaByIds" )
    public ActionResult<List<List<String>>> getAreaByIds(@RequestBody AreaModel model) {
        // 返回给前端的list
        List<List<String>> list = new LinkedList<>();
        for (List<String> idList : model.getIdsList()) {
            List<ProvinceEntity> proList = provinceService.getProList(idList);
            List<String> collect = proList.stream().map(ProvinceEntity::getFullName).collect(Collectors.toList());
            list.add(collect);
        }
        return ActionResult.success(list);
    }
 
 
    @Operation(summary = "获取行政区划下拉框数据-新" )
    @Parameters({
            @Parameter(name = "provinceSelectModel" , description = "省市区下拉参数模型" )
    })
    @PostMapping("/SelectorNew" )
    public ActionResult<ListVO<ProvinceSelectListVO>> selectListNew(@RequestBody ProvinceSelectModel provinceSelectModel) {
        List<ProvinceEntity> data = provinceService.getList(provinceSelectModel.getPid());
        data = data.stream().filter(t -> t.getEnabledMark() == 1).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(provinceSelectModel.getIds())) {
            List<List<String>> ids = provinceSelectModel.getIds();
            List<String> allId=new ArrayList<>();
            for (List<String> arr : ids) {
                allId.addAll(arr);
            }
            data=data.stream().filter(t -> allId.contains(t.getId())).collect(Collectors.toList());
        }
        List<ProvinceSelectListVO> treeList = JsonUtil.getJsonToList(data, ProvinceSelectListVO.class);
        int i = 0;
        for (ProvinceSelectListVO entity : treeList) {
            entity.setIsLeaf(false);
            treeList.set(i, entity);
            i++;
        }
        ListVO<ProvinceSelectListVO> vo = new ListVO<>();
        vo.setList(treeList);
        return ActionResult.success(vo);
    }
 
    /**
     * 获取行政区划列表
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/getList/{id}" )
    public List<ProvinceEntity> getList(@PathVariable("id" ) String id) {
        List<ProvinceEntity> list = provinceService.getList(id);
        return list;
    }
 
    @Override
    @PostMapping("/getByIdList" )
    public List<ProvinceEntity> getByIdList(@RequestBody List<String> ids) {
        List<ProvinceEntity> proList = provinceService.getProList(ids);
        return proList;
    }
 
    @Override
    @GetMapping("/getAllProList" )
    public List<ProvinceEntity> getAllProList() {
        List<ProvinceEntity> proList = provinceService.getAllProList();
        return proList;
    }
 
    @Override
    public List<ProvinceEntity> getProListBytype(String type) {
        return provinceService.getProListBytype(type);
    }
 
    @Override
    @PostMapping("/getProvinceByParent" )
    public ProvinceEntity getProListBytype(@RequestParam("id" ) String id, @RequestBody List<String> parentIds) {
        return provinceService.getInfo(id, parentIds);
    }
 
 
}