com.ape9527.core.controller.DictController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ape-core Show documentation
Show all versions of ape-core Show documentation
Ape low code platform core module
The newest version!
package com.ape9527.core.controller;
import com.alibaba.fastjson.JSONObject;
import com.ape9527.auth.annotation.LoginAuth;
import com.ape9527.core.constant.Const;
import com.ape9527.core.controller.BaseController;
import com.ape9527.core.controller.vo.SaveDictDetailVo;
import com.ape9527.core.controller.vo.SaveDictVo;
import com.ape9527.core.model.AjaxResult;
import com.ape9527.core.service.BaseDataService;
import com.ape9527.utils.string.StringUtil;
import com.ape9527.core.entity.SysDict;
import com.ape9527.core.entity.SysDictDetail;
import com.ape9527.core.model.base.BaseDelVo;
import com.ape9527.core.model.base.BaseQueryVo;
import com.ape9527.core.model.base.BaseSaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author YuanShuai[[email protected]]
*/
@Api(tags = "DictController", description = "字典信息控制层")
@RestController
@RequestMapping("/api/dict")
public class DictController extends BaseController {
private final RedisTemplate redisTemplate;
private final BaseDataService baseDataService;
public DictController(RedisTemplate redisTemplate, BaseDataService baseDataService) {
this.redisTemplate = redisTemplate;
this.baseDataService = baseDataService;
}
@ApiOperation("查询所有字典信息")
@GetMapping("/all")
public AjaxResult getAll(){
BaseQueryVo queryVo = new BaseQueryVo("sys_dict_detail");
List list = baseDataService.queryList(queryVo, SysDictDetail.class).getList();
JSONObject dict = buildDict(list);
return AjaxResult.success(dict);
}
private JSONObject buildDict(List list) {
JSONObject dict = new JSONObject();
list.forEach(e -> {
String dictCode = e.getDictName();
String dictLabel = e.getDictLabel();
String dictValue = e.getDictValue();
JSONObject dictDetail = dict.getJSONObject(dictCode);
if(dictDetail != null){
dictDetail.put(dictLabel, dictValue);
}else{
dictDetail = new JSONObject();
dictDetail.put(dictLabel, dictValue);
dict.put(dictCode, dictDetail);
}
});
return dict;
}
@ApiOperation("查询一个字典键值对")
@GetMapping("/get/{dictCode}/{dictLabel}")
public AjaxResult getDictValue(@PathVariable String dictCode, @PathVariable String dictLabel){
// 检查缓存
String redisKey = Const.DICT_PREFIX + dictCode;
String dictValue = (String) redisTemplate.opsForHash().get(redisKey, dictLabel);
if(StringUtil.isNotEmpty(dictValue)){
return AjaxResult.success(dictValue);
}
// 查询数据库
BaseQueryVo queryVo = new BaseQueryVo("sys_dict_detail");
queryVo.eq("dict_name", dictCode)
.eq("dict_label", dictValue);
SysDictDetail sysDictDetail = baseDataService.queryOne(queryVo, SysDictDetail.class);
redisTemplate.opsForHash().put(redisKey, dictLabel, sysDictDetail.getDictValue());
return AjaxResult.success(sysDictDetail.getDictValue());
}
@ApiOperation("查询一个字典")
@GetMapping("/get/{dictCode}")
public AjaxResult getDict(@PathVariable String dictCode) {
JSONObject dictResult = new JSONObject();
// 检索缓存
String redisKey = Const.DICT_PREFIX + dictCode;
Map