All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ape9527.core.controller.DictController Maven / Gradle / Ivy

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 entries = redisTemplate.opsForHash().entries(redisKey);
        if (StringUtil.isNotEmpty(entries)) {
            dictResult.put("indexes", entries.keySet());
            dictResult.put("dict", entries);
            return AjaxResult.success(dictResult);
        }

        // 查询数据库
        BaseQueryVo queryVo = new BaseQueryVo("sys_dict_detail");
        queryVo.eq("dict_name", dictCode);
        List list = baseDataService.queryList(queryVo, SysDictDetail.class).getList();

        // 循环构建返回对象,并添加缓存
        Map dict = new HashMap<>();
        list.forEach(e -> {
            dict.put(e.getDictLabel(), e.getDictValue());
        });
        redisTemplate.opsForHash().putAll(redisKey, dict);

        dictResult.put("indexes", dict.keySet());
        dictResult.put("dict", dict);
        return AjaxResult.success(dictResult);
    }

    @ApiOperation("添加或修改一个数据字典")
    @LoginAuth
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveDict(@RequestBody SaveDictVo vo) {
        // 更新前查询字典信息,用于更新字典明细
        SysDict oldDict = null;
        if (StringUtil.isNotEmpty(vo.getUuid())) {
            BaseQueryVo dictQueryVo = new BaseQueryVo("sys_dict");
            dictQueryVo.setUuid(vo.getUuid());
            oldDict = baseDataService.queryById(dictQueryVo, SysDict.class);
        }

        // 更新字典
        BaseSaveVo dictVo = new BaseSaveVo("sys_dict");
        dictVo.addField("dict_name", vo.getDictName())
                .addField("dict_code", vo.getDictCode());
        if (StringUtil.isNotEmpty(vo.getUuid())) {
            dictVo.eq("uuid", vo.getUuid());
        }
        int i = baseDataService.save(dictVo);
        if (i == 1) {
            // 更新字典明细
            if (oldDict != null) {
                BaseSaveVo saveDetailVo = new BaseSaveVo("sys_dict_detail");
                saveDetailVo.addField("dict_name", vo.getDictCode());
                saveDetailVo.eq("dict_name", oldDict.getDictCode());
                baseDataService.update(saveDetailVo);
                delCache(oldDict.getDictCode());
            }
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @LoginAuth
    @ApiOperation("删除一个数据字典")
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult del(@RequestBody BaseDelVo vo) {
        BaseQueryVo dictDetailQueryVo = new BaseQueryVo("sys_dict");
        dictDetailQueryVo.setConditions(vo.getConditions());
        List list = baseDataService.queryList(dictDetailQueryVo, SysDict.class).getList();

        int del = baseDataService.del(vo);
        if (del > 0) {
            List dictCodes = new ArrayList<>();
            list.forEach(e -> dictCodes.add(e.getDictCode()));
            if (dictCodes.size() > 0) {
                BaseDelVo delDetailVo = new BaseDelVo("sys_dict_detail");
                delDetailVo.in("dict_name", dictCodes);
                baseDataService.del(delDetailVo);
            }
        }
        list.forEach(e -> delCache(e.getDictCode()));
        return del > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("添加或修改一对数据字典键值")
    @LoginAuth
    @PostMapping("/detail/save")
    public AjaxResult saveDictDetail(@RequestBody SaveDictDetailVo vo) {
        BaseSaveVo dictDetailVo = new BaseSaveVo("sys_dict_detail");
        dictDetailVo.addField("dict_label", vo.getDictLabel())
                .addField("dict_value", vo.getDictValue())
                .addField("dict_name", vo.getDictCode());
        if (StringUtil.isNotEmpty(vo.getUuid())) {
            dictDetailVo.eq("uuid", vo.getUuid());
        }

        int i = baseDataService.save(dictDetailVo);
        if (i == 1) {
            delCache(vo.getDictCode());
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @LoginAuth
    @ApiOperation("删除一对数据字典键值")
    @PostMapping("/detail/del")
    public AjaxResult delDetail(@RequestBody BaseDelVo vo) {
        BaseQueryVo dictDetailQueryVo = new BaseQueryVo("sys_dict_detail");
        dictDetailQueryVo.setConditions(vo.getConditions());
        List list = baseDataService.queryList(dictDetailQueryVo, SysDictDetail.class).getList();

        int del = baseDataService.del(vo);

        list.forEach(e -> delCache(e.getDictName()));
        return del > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Async
    public void delCache(String dictCode, String dictLabel) {
        String redisKey = Const.DICT_PREFIX + dictCode;
        redisTemplate.opsForHash().delete(redisKey, dictLabel);
    }

    @Async
    public void delCache(String dictCode) {
        String redisKey = Const.DICT_PREFIX + dictCode;
        redisTemplate.delete(redisKey);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy