WangHan
2025-04-03 a1b85ef72062ca80db35546e4216dd564f3e0f57
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
package com.iplatform.base.controller;
 
import com.iplatform.base.SystemController;
import com.iplatform.base.pojo.dict.DictParam;
import com.iplatform.base.service.CodeServiceImpl;
import com.iplatform.model.po.S_dict_data;
import com.iplatform.model.po.S_dict_type;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.DataStatus;
import com.walker.web.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.List;
 
@RestController
@RequestMapping("/system/dict")
public class CodeController extends SystemController {
 
    private CodeServiceImpl codeService;
 
    @Autowired
    public CodeController(CodeServiceImpl codeService){
        this.codeService = codeService;
    }
 
    /** 类型列表 */
    @RequestMapping("/type/list")
    public ResponseValue listType(DictParam dictParam){
        logger.debug(dictParam.toString());
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // 需要添加拦截器,统一销毁线程变量对象,马上要补充,2022-11-19
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//        this.preparePageSearch();
 
        long beginTime = -1;
        long endTime = -1;
        if(dictParam.getParams().get("beginTime") != null){
            beginTime = this.getParamsDateTime(dictParam.getParams().get("beginTime").toString(), false);
        }
        if(dictParam.getParams().get("endTime") != null){
            endTime = this.getParamsDateTime(dictParam.getParams().get("endTime").toString(), false);
        }
        logger.debug("beginTime = " + beginTime + ", endTime = " + endTime);
 
        GenericPager<S_dict_type> pager = this.codeService.queryPageDictType(dictParam.getDictName()
                , dictParam.getDictType(), dictParam.getStatus(), beginTime, endTime);
//        return this.acquireTablePage(pager.getDatas(), pager.getTotalRows());
        return ResponseValue.success(pager);
    }
 
    /** 新增类型 */
    @PostMapping("/type/add")
    public ResponseValue<?> addType(@RequestBody DictParam dictParam){
        S_user_core currentUser = getCurrentUser();
        S_dict_type byDictType = codeService.getByDictType(dictParam.getDictType());
        if (byDictType != null) {
            return ResponseValue.error("字典类型[" + byDictType.getDict_type() + "] 已存在!");
        }
        S_dict_type dictType = new S_dict_type();
        dictType.setDict_type(dictParam.getDictType());
        dictType.setDict_name(dictParam.getDictName());
        if (dictParam.getStatus() == null) {
            dictType.setStatus(DataStatus.CONST_NORMAL);
        } else {
            dictType.setStatus(dictParam.getStatus());
        }
        if (currentUser != null) {
            dictType.setCreate_by(currentUser.getUser_name());
            dictType.setCreate_time(DateUtils.getDateTimeNumber());
        }
        dictType.setRemark(dictParam.getRemark());
        this.codeService.save(dictType);
        return ResponseValue.success(null);
    }
 
    /** 类型修改 */
    @PostMapping("/type/upd")
    public ResponseValue<?> updType(@RequestBody DictParam dictParam){
        S_dict_type dictType = new S_dict_type();
        dictType.setDict_id(dictParam.getDict_id());
        dictType.setDict_name(dictParam.getDictName());
        dictType.setStatus(dictParam.getStatus());
        dictType.setRemark(dictParam.getRemark());
        this.codeService.update(dictType);
        return ResponseValue.success(null);
    }
 
    /** 类型删除 */
    @DeleteMapping("/type/del")
    public ResponseValue<?> delType(@RequestBody DictParam dictParam) {
        this.codeService.execDelType(dictParam.getDict_id());
        return ResponseValue.success(null);
    }
 
    /** 类型详情 */
    @RequestMapping("/type/{dictId}")
    public ResponseValue detailDictType(@PathVariable Long dictId){
        if(dictId == null || dictId.longValue() <= 0){
            return ResponseValue.error("字典id错误");
        }
        return ResponseValue.success(this.codeService.queryOneDictType(dictId));
    }
 
    /**
     * 在数据字典项管理界面,查询条件展示所有'字典类型'列表。<p></p>
     * 因为权限配置的是: /system/dict/data/** 开放,因此该权限也配置到data,否则还需要再加上一个权限点。
     * @return
     * @date 2022-11-19
     */
//    @GetMapping("/type/optionselect")
    @GetMapping("/data/optionselect")
    public ResponseValue showDictTypeListAll(){
        List<S_dict_type> list = this.codeService.selectAll(new S_dict_type());
        return ResponseValue.success(list);
    }
 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
 
    /**
     * 根据代码表名字,查询包含的代码项集合。
     * @param dictType
     * @return
     */
    @RequestMapping("/data/type/{dictType}")
    public ResponseValue<List<S_dict_data>> dictTypeList(@PathVariable String dictType){
        logger.debug("dictType = " + dictType);
        List<S_dict_data> list = this.codeService.queryDictDataByType(dictType);
        if(StringUtils.isEmptyList(list)){
            list = new ArrayList<S_dict_data>(2);
        }
        return ResponseValue.success(list);
    }
 
    /** 字典数据列表 */
    @RequestMapping("/data/list")
    public ResponseValue listData(DictParam dictParam){
//        this.preparePageSearch();
        if(StringUtils.isEmpty(dictParam.getDictType())){
            return ResponseValue.error("必须提供字典类型参数");
        }
        GenericPager<S_dict_data> pager = this.codeService.queryPageDictData(dictParam.getDictType(), dictParam.getDictLabel());
//        return this.acquireTablePage(pager.getDatas(), pager.getTotalRows());
        return ResponseValue.success(pager);
    }
 
    /** 字典数据新增 */
    @PostMapping("/add")
    public ResponseValue<?> addDictData(@RequestBody S_dict_data s_dict_data){
        logger.debug(s_dict_data.toString());
//        if(s_dict_data.getDict_code() == null){
//            return ResponseValue.error("字典id必须输入!");
//        }
        if(s_dict_data.getParent_id() == null){
            return ResponseValue.error("父id必须输入!");
        }
        S_dict_data dict_data = this.getDictCacheProvider().getCacheData(String.valueOf(s_dict_data.getDict_code()));
        if(dict_data != null){
            return ResponseValue.error("字典id已存在!");
        }
        String error = this.validateDictData(s_dict_data, true);
        if(error != null){
            return ResponseValue.error(error);
        }
        s_dict_data.setIs_default("N");
        if(StringUtils.isEmpty(s_dict_data.getDict_value())){
            // 字典值如果不填,必须和id一致
            s_dict_data.setDict_value(String.valueOf(s_dict_data.getDict_code()));
        }
//        s_dict_data.setDict_code(NumberGenerator.getSequenceNumber());
        s_dict_data.setCreate_time(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
 
        this.codeService.save(s_dict_data);
        this.getDictCacheProvider().putCacheData(String.valueOf(s_dict_data.getDict_code()), s_dict_data);
        return ResponseValue.success();
    }
 
 
    /** 字典数据详情 */
    @RequestMapping("/data/{dictCode}")
    public ResponseValue getDictDataInfo(@PathVariable Long dictCode){
        S_dict_data e = this.codeService.queryOneDictData(dictCode);
        return ResponseValue.success(e);
    }
 
    /** 字典数据修改 */
    @RequestMapping("/edit")
    public ResponseValue updateDictData(@RequestBody S_dict_data s_dict_data){
        String error = this.validateDictData(s_dict_data, false);
        if(error != null){
            return ResponseValue.error(error);
        }
        this.codeService.save(s_dict_data);
        this.getDictCacheProvider().updateCacheData(String.valueOf(s_dict_data.getDict_code()), s_dict_data);
        return ResponseValue.success();
    }
 
    /** 字典数据删除 */
    @RequestMapping("/data/remove/{dictCodes}")
    public ResponseValue removeDictData(@PathVariable Long[] dictCodes){
        this.codeService.execDeleteDictData(dictCodes);
        return ResponseValue.success();
    }
 
    /**
     * 校验
     * @param s_dict_data 数据
     * @param checkExist 是否检查存在
     * @return 错误信息
     */
    private String validateDictData(S_dict_data s_dict_data, boolean checkExist){
        if(s_dict_data == null){
            return "提交字典内容为空";
        }
        if(StringUtils.isEmpty(s_dict_data.getDict_type())){
            return "请选择字典类型";
        }
        if(StringUtils.isEmpty(s_dict_data.getDict_label())){
            return "请输入字典标签";
        }
//        if(StringUtils.isEmpty(s_dict_data.getDict_value())){
//            return "请输入字典值";
//        }
 
        if(checkExist){
            S_dict_data exist = this.codeService.queryOneDictData(s_dict_data.getDict_type(), s_dict_data.getDict_value());
            if(exist != null){
                return "已经存在该字典值,请重新输入";
            }
        }
        return null;
    }
}