| | |
| | | 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.setSysDictData(existData); |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | return dto; |
| | |
| | | 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(); |
| | | QueryWrapper<SysDictData> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | wrapper.eq("dataKey", key); |
| | | wrapper.eq("dict_type_id", existData.getDictTypeId()); |
| | | SysDictData existKeyData = sysDictDataMapper.selectOne(wrapper); |
| | | if (!ObjectUtils.isEmpty(existKeyData)) { |
| | | dto.setCode(ResponseCodeEnum.DICTDATA_KEY_EXIST.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.DICTDATA_KEY_EXIST.getMsg()); |
| | | return dto; |
| | | if (!ObjectUtils.isEmpty(key)) { |
| | | QueryWrapper<SysDictData> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | wrapper.eq("dataKey", key); |
| | | wrapper.eq("dict_type_id", existData.getDictTypeId()); |
| | | SysDictData existKeyData = sysDictDataMapper.selectOne(wrapper); |
| | | if (!ObjectUtils.isEmpty(existKeyData)) { |
| | | dto.setCode(ResponseCodeEnum.DICTDATA_KEY_EXIST.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.DICTDATA_KEY_EXIST.getMsg()); |
| | | return dto; |
| | | } |
| | | } |
| | | //执行更新 |
| | | 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.setSysDictData(sysDictData); |
| | | 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(); |
| | | //取参 |
| | | SysDictData sysDictData = form.formConvertEntity(); |
| | | //判断类型是否存在 |
| | | //取出类型用于插入日志使用 |
| | | SysDictType type = sysDictTypeService.getDictTypeById(sysDictData.getDictTypeId()); |
| | | if (ObjectUtils.isEmpty(type)) { |
| | | dto.setCode(ResponseCodeEnum.DICTTYPE_NOT_EXIST.getCode()); |
| | |
| | | } |
| | | //执行插入 |
| | | 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.setSysDictData(sysDictData); |
| | | dto.setSysDictType(type); |
| | | 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; |
| | | } |
| | | } |
| | | |