|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.service.SysDictTypeService; | 
|---|
|  |  |  | import com.moral.api.util.CacheUtils; | 
|---|
|  |  |  | import com.moral.api.util.LogUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | 
|---|
|  |  |  | SysDictDataDTO dto = new SysDictDataDTO(); | 
|---|
|  |  |  | //取参 | 
|---|
|  |  |  | Integer id = form.getId(); | 
|---|
|  |  |  | //查询要删除的数据,用于插入日志 | 
|---|
|  |  |  | //查询要删除的数据和类型,用于插入日志 | 
|---|
|  |  |  | QueryWrapper existWrapper = new QueryWrapper(); | 
|---|
|  |  |  | existWrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | existWrapper.eq("id", id); | 
|---|
|  |  |  | SysDictData existData = sysDictDataMapper.selectOne(existWrapper); | 
|---|
|  |  |  | SysDictType type = sysDictTypeService.getDictTypeById(existData.getDictTypeId()); | 
|---|
|  |  |  | //执行删除 | 
|---|
|  |  |  | UpdateWrapper deleteWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | deleteWrapper.eq("id", id); | 
|---|
|  |  |  | sysDictDataMapper.update(null, deleteWrapper); | 
|---|
|  |  |  | //删除缓存 | 
|---|
|  |  |  | CacheUtils.clearDictionariesCache(); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | StringBuilder content = new StringBuilder("删除了字典数据;"); | 
|---|
|  |  |  | content.append("类型:" + type.getName() + "(" + type.getDesc() + ")" + ";"); | 
|---|
|  |  |  | content.append("key:" + existData.getDataKey() + ";"); | 
|---|
|  |  |  | content.append("value:" + existData.getDataValue() + ";"); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content.toString(), Constants.DELETE_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | existWrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | existWrapper.eq("id", sysDictData.getId()); | 
|---|
|  |  |  | SysDictData existData = sysDictDataMapper.selectOne(existWrapper); | 
|---|
|  |  |  | SysDictType type = sysDictTypeService.getDictTypeById(existData.getDictTypeId()); | 
|---|
|  |  |  | //判断key是否重复 | 
|---|
|  |  |  | String key = sysDictData.getDataKey(); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(key)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //执行更新 | 
|---|
|  |  |  | sysDictDataMapper.updateById(sysDictData); | 
|---|
|  |  |  | //清除缓存 | 
|---|
|  |  |  | CacheUtils.clearDictionariesCache(); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | StringBuilder content = new StringBuilder("修改了字典数据;"); | 
|---|
|  |  |  | content.append("类型:" + type.getName() + "(" + type.getDesc() + ")" + ";"); | 
|---|
|  |  |  | content.append("key:" + existData.getDataKey() + ";"); | 
|---|
|  |  |  | content.append("value:" + existData.getDataValue() + "->" + sysDictData.getDataValue() + ";"); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content.toString(), Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 根据字典类型id插入字典类型数据 | 
|---|
|  |  |  | * @Param: [form] | 
|---|
|  |  |  | * @return: com.moral.api.pojo.dto.sysDictData.SysDictDataDTO | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/16 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public SysDictDataDTO insertData(SysDictDataInsertForm form) { | 
|---|
|  |  |  | SysDictDataDTO dto = new SysDictDataDTO(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //执行插入 | 
|---|
|  |  |  | sysDictDataMapper.insert(sysDictData); | 
|---|
|  |  |  | //清除缓存 | 
|---|
|  |  |  | CacheUtils.clearDictionariesCache(); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | StringBuilder content = new StringBuilder("添加了字典数据;"); | 
|---|
|  |  |  | content.append("类型:" + type.getName() + "(" + type.getDesc() + ")" + ";"); | 
|---|
|  |  |  | content.append("key:" + sysDictData.getDataKey() + ";"); | 
|---|
|  |  |  | content.append("value:" + sysDictData.getDataValue() + ";"); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 根据类型名字查询对应字典的数据 | 
|---|
|  |  |  | * @Param: [typeName] | 
|---|
|  |  |  | * @return: java.util.Map<java.lang.String                               ,                               java.lang.Object> | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/16 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String, Object> queryDataByTypeName(String typeName) { | 
|---|
|  |  |  | Map<String, List<SysDictData>> datas = getAllDictData(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return datas; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String, SysDictData> getDictDatasByType(String type) { | 
|---|
|  |  |  | Map<String, List<SysDictData>> allDictData = getAllDictData(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void refreshCache() { | 
|---|
|  |  |  | redisTemplate.delete(RedisConstants.DICT_DATA_KEY); | 
|---|
|  |  |  | Map<String, List<SysDictData>> datas = getDictDataFromDB(); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(RedisConstants.DICT_DATA_KEY, datas); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 从数据库中根据字典类型读取字典数据,返回Map  Key:类型名称 value:数据集合 | 
|---|
|  |  |  | * @Param: [] | 
|---|
|  |  |  | * @return: java.util.Map<java.lang.String                                                               ,                                                               java.util.List                                                               <                                                               com.moral.api.entity.sysDictData>> | 
|---|
|  |  |  | * @return: java.util.Map<java.lang.String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               java.util.List                                                                                                                                                                                                                                               <               com.moral.api.entity.sysDictData>> | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/14 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<SysDictData> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | wrapper.eq("dict_type_id", type.getId()); | 
|---|
|  |  |  | wrapper.orderByAsc("id"); | 
|---|
|  |  |  | List<SysDictData> datas = sysDictDataMapper.selectList(wrapper); | 
|---|
|  |  |  | result.put(type.getName(), datas); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|