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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
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 jnpf.base.controller.SuperController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.DictionaryDataApi;
import jnpf.base.vo.DownloadVO;
import jnpf.constant.MsgCode;
import jnpf.emnus.ModuleTypeEnum;
import jnpf.exception.DataException;
import jnpf.util.FileUtil;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import jnpf.base.ActionResult;
import jnpf.base.vo.ListVO;
import jnpf.base.model.dictionarydata.*;
import jnpf.base.model.dictionarytype.DictionaryTypeSelectModel;
import jnpf.base.model.dictionarytype.DictionaryTypeSelectVO;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.entity.DictionaryTypeEntity;
import jnpf.base.service.DictionaryDataService;
import jnpf.base.service.DictionaryTypeService;
import jnpf.util.treeutil.ListToTreeUtil;
import jnpf.util.treeutil.SumTree;
import jnpf.util.treeutil.TreeDotUtils;
import jnpf.util.type.StringNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import jakarta.validation.Valid;
import java.util.*;
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 = "DictionaryData")
@RestController
@RequestMapping("/DictionaryData")
public class DictionaryDataController extends SuperController<DictionaryDataService, DictionaryDataEntity> implements DictionaryDataApi {
 
    @Autowired
    private DictionaryDataService dictionaryDataService;
    @Autowired
    private DictionaryTypeService dictionaryTypeService;
 
    /**
     * 获取数据字典列表
     *
     * @param dictionaryTypeId 数据分类id
     * @param pageDictionaryData 分页模型
     * @return
     */
    @Operation(summary = "获取数据字典列表")
    @Parameters({
            @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true)
    })
    @GetMapping("/{dictionaryTypeId}")
    public ActionResult bindDictionary(@PathVariable("dictionaryTypeId") String dictionaryTypeId, PageDictionaryData pageDictionaryData) {
        List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId);
        List<DictionaryDataEntity> dataAll = data;
        if (StringUtil.isNotEmpty(pageDictionaryData.getKeyword())) {
            data = data.stream().filter(t -> t.getFullName().contains(pageDictionaryData.getKeyword()) || t.getEnCode().contains(pageDictionaryData.getKeyword())).collect(Collectors.toList());
        }
        if (pageDictionaryData.getIsTree() != null && StringNumber.ONE.equals(pageDictionaryData.getIsTree())) {
            List<DictionaryDataEntity> treeData = JsonUtil.getJsonToList(ListToTreeUtil.treeWhere(data, dataAll), DictionaryDataEntity.class);
            List<DictionaryDataModel> voListVO = JsonUtil.getJsonToList(treeData, DictionaryDataModel.class);
            List<SumTree<DictionaryDataModel>> sumTrees = TreeDotUtils.convertListToTreeDot(voListVO);
            List<DictionaryDataListVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryDataListVO.class);
            ListVO<DictionaryDataListVO> treeVo = new ListVO<>();
            treeVo.setList(list);
            return ActionResult.success(treeVo);
        }
        List<DictionaryDataModel> voListVO = JsonUtil.getJsonToList(data, DictionaryDataModel.class);
        ListVO<DictionaryDataModel> treeVo = new ListVO<>();
        treeVo.setList(voListVO);
        return ActionResult.success(treeVo);
    }
 
 
    /**
     * 获取数据字典列表
     *
     * @return
     */
    @Operation(summary = "获取数据字典列表(分类+内容)")
    @GetMapping("/All")
    public ActionResult<ListVO<Map<String, Object>>> allBindDictionary() {
        List<DictionaryTypeEntity> dictionaryTypeList = dictionaryTypeService.getList();
        List<Map<String, Object>> list = new ArrayList<>();
        for (DictionaryTypeEntity dictionaryTypeEntity : dictionaryTypeList) {
            List<DictionaryDataEntity> childNodeList = dictionaryDataService.getList(dictionaryTypeEntity.getId(), true);
            if (dictionaryTypeEntity.getIsTree() != null && dictionaryTypeEntity.getIsTree().compareTo(1) == 0) {
                List<Map<String, Object>> selectList = new ArrayList<>();
                for (DictionaryDataEntity item : childNodeList) {
                    Map<String, Object> ht = new HashMap<>();
                    ht.put("fullName", item.getFullName());
                    ht.put("id", item.getId());
                    ht.put("enCode", item.getEnCode());
                    ht.put("parentId", item.getParentId());
                    selectList.add(ht);
                }
                //==============转换树
                List<SumTree<DictionaryDataAllModel>> list1 = TreeDotUtils.convertListToTreeDot(JsonUtil.getJsonToList(selectList, DictionaryDataAllModel.class));
                List<DictionaryDataAllVO> list2 = JsonUtil.getJsonToList(list1, DictionaryDataAllVO.class);
                //==============
                Map<String, Object> ht_item = new HashMap<>();
                ht_item.put("id", dictionaryTypeEntity.getId());
                ht_item.put("enCode", dictionaryTypeEntity.getEnCode());
                ht_item.put("dictionaryList", list2);
                ht_item.put("isTree", 1);
                list.add(ht_item);
            } else {
                List<Map<String, Object>> selectList = new ArrayList<>();
                for (DictionaryDataEntity item : childNodeList) {
                    Map<String, Object> ht = new HashMap<>();
                    ht.put("enCode", item.getEnCode());
                    ht.put("id", item.getId());
                    ht.put("fullName", item.getFullName());
                    selectList.add(ht);
                }
                Map<String, Object> ht_item = new HashMap<>();
                ht_item.put("id", dictionaryTypeEntity.getId());
                ht_item.put("enCode", dictionaryTypeEntity.getEnCode());
                ht_item.put("dictionaryList", selectList);
                ht_item.put("isTree", 0);
                list.add(ht_item);
            }
        }
        ListVO<Map<String, Object>> vo = new ListVO<>();
        vo.setList(list);
        return ActionResult.success(vo);
    }
 
 
    /**
     * 获取数据字典下拉框数据
     *
     * @param dictionaryTypeId 类别主键
     * @param isTree 是否树形
     * @param id 主键
     * @return
     */
    @Operation(summary = "获取数据字典分类下拉框数据")
    @Parameters({
            @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true),
            @Parameter(name = "isTree", description = "是否树形"),
            @Parameter(name = "id", description = "主键", required = true)
    })
    @GetMapping("/{dictionaryTypeId}/Selector/{id}")
    public ActionResult<ListVO<DictionaryDataSelectVO>> treeView(@PathVariable("dictionaryTypeId") String dictionaryTypeId,
                                                                 @RequestParam(value = "isTree", required = false) String isTree, @PathVariable("id") String id) {
 
        DictionaryTypeEntity typeEntity = dictionaryTypeService.getInfo(dictionaryTypeId);
        List<DictionaryDataModel> treeList = new ArrayList<>();
        DictionaryDataModel treeViewModel = new DictionaryDataModel();
        treeViewModel.setId("0");
        treeViewModel.setFullName(typeEntity.getFullName());
        treeViewModel.setParentId("-1");
        treeViewModel.setIcon("fa fa-tags");
        treeList.add(treeViewModel);
        if (isTree != null && StringNumber.ONE.equals(isTree)) {
            List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId);
            //过滤子集
            if(!"0".equals(id)){
                data.remove(dictionaryDataService.getInfo(id));
            }
            for (DictionaryDataEntity entity : data) {
                DictionaryDataModel treeModel = new DictionaryDataModel();
                treeModel.setId(entity.getId());
                treeModel.setFullName(entity.getFullName());
                treeModel.setParentId("-1".equals(entity.getParentId()) ? entity.getDictionaryTypeId() : entity.getParentId());
                treeList.add(treeModel);
            }
        }
        List<SumTree<DictionaryDataModel>> sumTrees = TreeDotUtils.convertListToTreeDotFilter(treeList);
        List<DictionaryDataSelectVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryDataSelectVO.class);
        ListVO<DictionaryDataSelectVO> treeVo = new ListVO<>();
        treeVo.setList(list);
        return ActionResult.success(treeVo);
    }
 
    /**
     * 获取字典分类
     *
     * @param dictionaryTypeId 数据分类id
     * @return
     */
    @Operation(summary = "获取某个字典数据下拉框列表")
    @Parameters({
            @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true)
    })
    @GetMapping("/{dictionaryTypeId}/Data/Selector")
    public ActionResult<ListVO<DictionaryTypeSelectVO>> selectorOneTreeView(@PathVariable("dictionaryTypeId") String dictionaryTypeId) {
        List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId, true);
        if(data.isEmpty()){
            DictionaryTypeEntity typeEntity = dictionaryTypeService.getInfoByEnCode(dictionaryTypeId);
            if(typeEntity != null){
                data = dictionaryDataService.getList(typeEntity.getId(), true);
            }
 
        }
        List<DictionaryTypeSelectModel> voListVO = JsonUtil.getJsonToList(data, DictionaryTypeSelectModel.class);
        List<SumTree<DictionaryTypeSelectModel>> sumTrees = TreeDotUtils.convertListToTreeDot(voListVO);
        List<DictionaryTypeSelectVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryTypeSelectVO.class);
        ListVO<DictionaryTypeSelectVO> vo = new ListVO<>();
        vo.setList(list);
        return ActionResult.success(vo);
    }
 
 
    /**
     * 获取数据字典信息
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "获取数据字典信息")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @GetMapping("/{id}/Info")
    public ActionResult<DictionaryDataInfoVO> info(@PathVariable("id") String id) throws DataException {
        DictionaryDataEntity entity = dictionaryDataService.getInfo(id);
        DictionaryDataInfoVO vo = JsonUtil.getJsonToBeanEx(entity, DictionaryDataInfoVO.class);
        return ActionResult.success(vo);
    }
 
//    /**
//     * 重复验证(名称)
//     *
//     * @param dictionaryTypeId 类别主键
//     * @param fullName         名称
//     * @param id               主键值
//     * @return
//     */
//    @Operation(summary = "(待定)重复验证(名称)")
//    @GetMapping("/IsExistByFullName")
//    public ActionResult isExistByFullName(String dictionaryTypeId, String fullName, String id) {
//        boolean data = dictionaryDataService.isExistByFullName(dictionaryTypeId, fullName, id);
//        return ActionResult.success(data);
//    }
//
//    /**
//     * 重复验证(编码)
//     *
//     * @param dictionaryTypeId 类别主键
//     * @param enCode           编码
//     * @param id               主键值
//     * @return
//     */
//    @Operation(summary = "(待定)重复验证(编码)")
//    @GetMapping("/IsExistByEnCode")
//    public ActionResult isExistByEnCode(String dictionaryTypeId, String enCode, String id) {
//        boolean data = dictionaryDataService.isExistByEnCode(dictionaryTypeId, enCode, id);
//        return ActionResult.success(data);
//    }
 
 
    /**
     * 添加数据字典
     *
     * @param dictionaryDataCrForm 实体对象
     * @return
     */
    @Operation(summary = "添加数据字典")
    @Parameters({
            @Parameter(name = "dictionaryDataCrForm", description = "实体对象", required = true)
    })
    @SaCheckPermission("sysData.dictionary")
    @PostMapping
    public ActionResult create(@RequestBody @Valid DictionaryDataCrForm dictionaryDataCrForm) {
        DictionaryDataEntity entity = JsonUtil.getJsonToBean(dictionaryDataCrForm, DictionaryDataEntity.class);
        if (dictionaryDataService.isExistByFullName(entity.getDictionaryTypeId(), entity.getFullName(), entity.getId())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        if (dictionaryDataService.isExistByEnCode(entity.getDictionaryTypeId(), entity.getEnCode(), entity.getId())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        dictionaryDataService.create(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }
 
    /**
     * 修改数据字典
     *
     * @param dictionaryDataUpForm 实体对象
     * @param id                   主键值
     * @return
     */
    @Operation(summary = "修改数据字典")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true),
            @Parameter(name = "dictionaryDataUpForm", description = "实体对象", required = true)
    })
    @SaCheckPermission("sysData.dictionary")
    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid DictionaryDataUpForm dictionaryDataUpForm) {
        DictionaryDataEntity entity = JsonUtil.getJsonToBean(dictionaryDataUpForm, DictionaryDataEntity.class);
        if (dictionaryDataService.isExistByFullName(entity.getDictionaryTypeId(), entity.getFullName(), id)) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        if (dictionaryDataService.isExistByEnCode(entity.getDictionaryTypeId(), entity.getEnCode(), id)) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        boolean flag = dictionaryDataService.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.dictionary")
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        DictionaryDataEntity entity = dictionaryDataService.getInfo(id);
        if (entity != null) {
            if (dictionaryDataService.isExistSubset(entity.getId())) {
                return ActionResult.fail(MsgCode.SYS014.get());
            }
            dictionaryDataService.delete(entity);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }
 
    /**
     * 更新字典状态
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "更新字典状态")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission("sysData.dictionary")
    @PutMapping("/{id}/Actions/State")
    public ActionResult update(@PathVariable("id") String id) {
        DictionaryDataEntity entity = dictionaryDataService.getInfo(id);
        if (entity != null) {
            if (entity.getEnabledMark() == 1) {
                entity.setEnabledMark(0);
            } else {
                entity.setEnabledMark(1);
            }
            boolean flag = dictionaryDataService.update(entity.getId(), entity);
            if (!flag) {
                return ActionResult.success(MsgCode.FA002.get());
            }
        }
        return ActionResult.success(MsgCode.SU004.get());
    }
 
    /**
     * 数据字典导出功能
     *
     * @param id 接口id
     * @return ignore
     */
    @Operation(summary = "导出数据字典数据")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission("sysData.dictionary")
    @GetMapping("/{id}/Actions/Export")
    public ActionResult exportFile(@PathVariable("id") String id) {
        DownloadVO downloadVO = dictionaryDataService.exportData(id);
        return ActionResult.success(downloadVO);
    }
 
    /**
     * 数据字典导入功能
     *
     * @param multipartFile 文件
     * @return
     * @throws DataException
     */
    @Operation(summary = "数据字典导入功能")
    @SaCheckPermission("sysData.dictionary")
    @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ActionResult importFile(@RequestPart("file") MultipartFile multipartFile,
                                   @RequestParam("type") Integer type) throws DataException {
        //判断是否为.json结尾
        if (FileUtil.existsSuffix(multipartFile, ModuleTypeEnum.SYSTEM_DICTIONARYDATA.getTableName())) {
            return ActionResult.fail(MsgCode.IMP002.get());
        }
        try {
            //获取文件内容
            String fileContent = FileUtil.getFileContent(multipartFile);
            DictionaryExportModel exportModel = JsonUtil.getJsonToBean(fileContent, DictionaryExportModel.class);
            List<DictionaryTypeEntity> list = exportModel.getList();
            //父级分类id不存在的话,直接抛出异常
            //如果分类只有一个
            if (list.size() == 1 && !"-1".equals(list.get(0).getParentId()) && dictionaryTypeService.getInfo(list.get(0).getParentId()) == null) {
                return ActionResult.fail(MsgCode.IMP010.get());
            }
            //如果有多个需要验证分类是否存在
            if (list.stream().filter(t -> "-1".equals(t.getParentId())).count() < 1) {
                boolean exist = false;
                for (DictionaryTypeEntity dictionaryTypeEntity : list) {
                    //判断父级是否存在
                    if (dictionaryTypeService.getInfo(dictionaryTypeEntity.getParentId()) != null) {
                        exist = true;
                    }
                }
                if (!exist) {
                    return ActionResult.fail(MsgCode.IMP010.get());
                }
            }
            //判断数据是否存在
            return dictionaryDataService.importData(exportModel, type);
        } catch (Exception e) {
            throw new DataException(MsgCode.IMP004.get());
        }
    }
 
 
 
    /**
     * 获取字典数据信息列表
     */
    @Override
    @GetMapping("/getList/{dictionary}")
    public List<DictionaryDataEntity> getList(@PathVariable("dictionary") String dictionary) {
        return dictionaryDataService.getList(dictionary);
    }
 
    /**
     * 获取数据字典信息
     *
     * @param id 主键值
     * @return
     */
    @Override
    @GetMapping("/getInfo")
    public DictionaryDataEntity getInfo(@RequestParam(value = "id", required = false) String id) {
        return dictionaryDataService.getInfo(id);
    }
 
    @Override
    @GetMapping("/getDicList/{dictionaryTypeId}")
    public List<DictionaryDataEntity> getDicList(@PathVariable("dictionaryTypeId") String dictionaryTypeId) {
        return dictionaryDataService.getDicList(dictionaryTypeId);
    }
 
    @Override
    @GetMapping("/getDicList/{dictionaryTypeId}/{enable}")
    public List<DictionaryDataEntity> getList(@PathVariable("dictionaryTypeId") String dictionaryTypeId, @PathVariable("enable") String enable) {
        if ("true".equals(enable)) {
            return dictionaryDataService.getList(dictionaryTypeId, true);
        }
        return dictionaryDataService.getList(dictionaryTypeId, false);
    }
 
    /**
     * 获取字典数据信息列表
     *
     * @param typeCode 字典分类code
     * @return
     */
    @Override
    @GetMapping("/getListByTypeDataCode/{typeCode}")
    public ActionResult<List<DictionaryDataEntity>> getListByTypeDataCode(@PathVariable("typeCode") String typeCode) {
        List<DictionaryDataEntity> list = dictionaryDataService.getListByTypeDataCode(typeCode);
        return ActionResult.success(list);
    }
 
    //获取启用字典
    @Override
    @GetMapping("/getListByCode")
    public List<DictionaryDataEntity> getListByCode(@RequestParam("typeCode") String typeCode) {
        List<DictionaryDataEntity> list = dictionaryDataService.getByTypeCodeEnable(typeCode);
        return list;
    }
 
    @Override
    @PostMapping("/getDicList")
    public List<DictionaryDataEntity> getDictionName(@RequestBody List<String> id){
        return dictionaryDataService.getDictionName(id);
    }
 
    @Override
    @GetMapping("/getSwapInfo/{value}/{parentId}")
    public DictionaryDataEntity getSwapInfo(@PathVariable("value") String value,@PathVariable("parentId") String parentId){
        return dictionaryDataService.getSwapInfo(value,parentId);
    }
 
}