|
|
@@ -4,7 +4,10 @@ import com.alibaba.fastjson2.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.haha.common.vo.Result;
|
|
|
+import com.haha.common.constant.ResponseEnum;
|
|
|
+import com.haha.common.constant.RedisConstants;
|
|
|
+import com.haha.common.exception.BusinessException;
|
|
|
+import com.haha.common.utils.PageUtil;
|
|
|
import com.haha.entity.DictData;
|
|
|
import com.haha.entity.DictLog;
|
|
|
import com.haha.entity.DictType;
|
|
|
@@ -12,8 +15,8 @@ import com.haha.mapper.DictDataMapper;
|
|
|
import com.haha.mapper.DictLogMapper;
|
|
|
import com.haha.mapper.DictTypeMapper;
|
|
|
import com.haha.service.DictService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
@@ -24,6 +27,7 @@ import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
@Service
|
|
|
public class DictServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictService {
|
|
|
|
|
|
@@ -31,401 +35,270 @@ public class DictServiceImpl extends ServiceImpl<DictTypeMapper, DictType> imple
|
|
|
private static final String DICT_ALL_CACHE_KEY = "dict:data:all";
|
|
|
private static final long CACHE_EXPIRE_HOURS = 24;
|
|
|
|
|
|
- @Autowired
|
|
|
- private DictTypeMapper dictTypeMapper;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DictDataMapper dictDataMapper;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DictLogMapper dictLogMapper;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private StringRedisTemplate redisTemplate;
|
|
|
+ private final DictTypeMapper dictTypeMapper;
|
|
|
+ private final DictDataMapper dictDataMapper;
|
|
|
+ private final DictLogMapper dictLogMapper;
|
|
|
+ private final StringRedisTemplate redisTemplate;
|
|
|
|
|
|
@Override
|
|
|
- public Result<Map<String, Object>> getDictTypeList(Map<String, Object> params) {
|
|
|
- try {
|
|
|
- int page = Integer.parseInt(params.getOrDefault("page", 1).toString());
|
|
|
- int pageSize = Integer.parseInt(params.getOrDefault("pageSize", 10).toString());
|
|
|
- String dictCode = (String) params.get("dictCode");
|
|
|
- String dictName = (String) params.get("dictName");
|
|
|
- Integer status = params.get("status") != null ? Integer.parseInt(params.get("status").toString()) : null;
|
|
|
-
|
|
|
- LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
|
|
|
- if (StringUtils.hasText(dictCode)) {
|
|
|
- wrapper.like(DictType::getDictCode, dictCode);
|
|
|
- }
|
|
|
- if (StringUtils.hasText(dictName)) {
|
|
|
- wrapper.like(DictType::getDictName, dictName);
|
|
|
- }
|
|
|
- if (status != null) {
|
|
|
- wrapper.eq(DictType::getStatus, status);
|
|
|
- }
|
|
|
- wrapper.orderByAsc(DictType::getSortOrder);
|
|
|
- wrapper.orderByDesc(DictType::getCreateTime);
|
|
|
-
|
|
|
- Page<DictType> pageObj = new Page<>(page, pageSize);
|
|
|
- Page<DictType> result = dictTypeMapper.selectPage(pageObj, wrapper);
|
|
|
+ public Map<String, Object> getDictTypeList(Map<String, Object> params) {
|
|
|
+ int[] pageParams = PageUtil.parsePageParams(params);
|
|
|
+ int page = pageParams[0];
|
|
|
+ int pageSize = pageParams[1];
|
|
|
+ String dictCode = (String) params.get("dictCode");
|
|
|
+ String dictName = (String) params.get("dictName");
|
|
|
+ Integer status = params.get("status") != null ? Integer.parseInt(params.get("status").toString()) : null;
|
|
|
+
|
|
|
+ LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (StringUtils.hasText(dictCode)) {
|
|
|
+ wrapper.like(DictType::getDictCode, dictCode);
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(dictName)) {
|
|
|
+ wrapper.like(DictType::getDictName, dictName);
|
|
|
+ }
|
|
|
+ if (status != null) {
|
|
|
+ wrapper.eq(DictType::getStatus, status);
|
|
|
+ }
|
|
|
+ wrapper.orderByAsc(DictType::getSortOrder);
|
|
|
+ wrapper.orderByDesc(DictType::getCreateTime);
|
|
|
|
|
|
- Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("list", result.getRecords());
|
|
|
- data.put("total", result.getTotal());
|
|
|
- data.put("pageSize", pageSize);
|
|
|
- data.put("currentPage", page);
|
|
|
+ Page<DictType> pageObj = new Page<>(page, pageSize);
|
|
|
+ Page<DictType> result = dictTypeMapper.selectPage(pageObj, wrapper);
|
|
|
|
|
|
- return Result.success("查询成功", data);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询字典类型列表失败", e);
|
|
|
- return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ return PageUtil.buildPageResult(result, page, pageSize);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<DictType> getDictTypeById(Long id) {
|
|
|
- try {
|
|
|
- DictType dictType = dictTypeMapper.selectById(id);
|
|
|
- if (dictType == null) {
|
|
|
- return Result.error(404, "字典类型不存在");
|
|
|
- }
|
|
|
- return Result.success("查询成功", dictType);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询字典类型失败, id={}", id, e);
|
|
|
- return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ public DictType getDictTypeById(Long id) {
|
|
|
+ DictType dictType = dictTypeMapper.selectById(id);
|
|
|
+ ResponseEnum.DICT_TYPE_NOT_FOUND.assertNotNull(dictType);
|
|
|
+ return dictType;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<DictType> getDictTypeByCode(String dictCode) {
|
|
|
- try {
|
|
|
- LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(DictType::getDictCode, dictCode);
|
|
|
- DictType dictType = dictTypeMapper.selectOne(wrapper);
|
|
|
- if (dictType == null) {
|
|
|
- return Result.error(404, "字典类型不存在");
|
|
|
- }
|
|
|
- return Result.success("查询成功", dictType);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询字典类型失败, dictCode={}", dictCode, e);
|
|
|
- return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ public DictType getDictTypeByCode(String dictCode) {
|
|
|
+ LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(DictType::getDictCode, dictCode);
|
|
|
+ DictType dictType = dictTypeMapper.selectOne(wrapper);
|
|
|
+ ResponseEnum.DICT_TYPE_NOT_FOUND.assertNotNull(dictType);
|
|
|
+ return dictType;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> addDictType(DictType dictType) {
|
|
|
- try {
|
|
|
- LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(DictType::getDictCode, dictType.getDictCode());
|
|
|
- if (dictTypeMapper.selectCount(wrapper) > 0) {
|
|
|
- return Result.error(400, "字典编码已存在");
|
|
|
- }
|
|
|
-
|
|
|
- dictType.setCreateTime(LocalDateTime.now());
|
|
|
- dictType.setUpdateTime(LocalDateTime.now());
|
|
|
- dictTypeMapper.insert(dictType);
|
|
|
+ public void addDictType(DictType dictType) {
|
|
|
+ LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(DictType::getDictCode, dictType.getDictCode());
|
|
|
+ if (dictTypeMapper.selectCount(wrapper) > 0) {
|
|
|
+ throw new BusinessException(ResponseEnum.DICT_CODE_DUPLICATE);
|
|
|
+ }
|
|
|
|
|
|
- saveDictLog(dictType, null, "insert", "新增字典类型");
|
|
|
+ dictType.setCreateTime(LocalDateTime.now());
|
|
|
+ dictType.setUpdateTime(LocalDateTime.now());
|
|
|
+ dictTypeMapper.insert(dictType);
|
|
|
|
|
|
- return Result.success("添加成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("添加字典类型失败", e);
|
|
|
- return Result.error(500, "添加失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ saveDictLog(dictType, null, "insert", "新增字典类型");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> updateDictType(DictType dictType) {
|
|
|
- try {
|
|
|
- DictType oldDictType = dictTypeMapper.selectById(dictType.getId());
|
|
|
- if (oldDictType == null) {
|
|
|
- return Result.error(404, "字典类型不存在");
|
|
|
- }
|
|
|
+ public void updateDictType(DictType dictType) {
|
|
|
+ DictType oldDictType = dictTypeMapper.selectById(dictType.getId());
|
|
|
+ ResponseEnum.DICT_TYPE_NOT_FOUND.assertNotNull(oldDictType);
|
|
|
|
|
|
- dictType.setUpdateTime(LocalDateTime.now());
|
|
|
- dictTypeMapper.updateById(dictType);
|
|
|
+ dictType.setUpdateTime(LocalDateTime.now());
|
|
|
+ dictTypeMapper.updateById(dictType);
|
|
|
|
|
|
- saveDictLog(dictType, oldDictType, "update", "修改字典类型");
|
|
|
-
|
|
|
- return Result.success("修改成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("修改字典类型失败", e);
|
|
|
- return Result.error(500, "修改失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ saveDictLog(dictType, oldDictType, "update", "修改字典类型");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> deleteDictType(Long id) {
|
|
|
- try {
|
|
|
- DictType dictType = dictTypeMapper.selectById(id);
|
|
|
- if (dictType == null) {
|
|
|
- return Result.error(404, "字典类型不存在");
|
|
|
- }
|
|
|
+ public void deleteDictType(Long id) {
|
|
|
+ DictType dictType = dictTypeMapper.selectById(id);
|
|
|
+ ResponseEnum.DICT_TYPE_NOT_FOUND.assertNotNull(dictType);
|
|
|
|
|
|
- if (dictType.getIsSystem() != null && dictType.getIsSystem() == 1) {
|
|
|
- return Result.error(400, "系统内置字典不允许删除");
|
|
|
- }
|
|
|
-
|
|
|
- dictDataMapper.deleteByDictCode(dictType.getDictCode());
|
|
|
- dictTypeMapper.deleteById(id);
|
|
|
+ if (dictType.getIsSystem() != null && dictType.getIsSystem() == 1) {
|
|
|
+ throw new BusinessException(ResponseEnum.DICT_SYSTEM_NO_DELETE);
|
|
|
+ }
|
|
|
|
|
|
- clearDictCache(dictType.getDictCode());
|
|
|
+ dictDataMapper.deleteByDictCode(dictType.getDictCode());
|
|
|
+ dictTypeMapper.deleteById(id);
|
|
|
|
|
|
- saveDictLog(dictType, null, "delete", "删除字典类型");
|
|
|
+ clearDictCache(dictType.getDictCode());
|
|
|
|
|
|
- return Result.success("删除成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("删除字典类型失败", e);
|
|
|
- return Result.error(500, "删除失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ saveDictLog(dictType, null, "delete", "删除字典类型");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> batchDeleteDictType(List<Long> ids) {
|
|
|
- try {
|
|
|
- for (Long id : ids) {
|
|
|
- DictType dictType = dictTypeMapper.selectById(id);
|
|
|
- if (dictType != null && (dictType.getIsSystem() == null || dictType.getIsSystem() != 1)) {
|
|
|
- dictDataMapper.deleteByDictCode(dictType.getDictCode());
|
|
|
- dictTypeMapper.deleteById(id);
|
|
|
- clearDictCache(dictType.getDictCode());
|
|
|
- }
|
|
|
+ public void batchDeleteDictType(List<Long> ids) {
|
|
|
+ for (Long id : ids) {
|
|
|
+ DictType dictType = dictTypeMapper.selectById(id);
|
|
|
+ if (dictType != null && (dictType.getIsSystem() == null || dictType.getIsSystem() != 1)) {
|
|
|
+ dictDataMapper.deleteByDictCode(dictType.getDictCode());
|
|
|
+ dictTypeMapper.deleteById(id);
|
|
|
+ clearDictCache(dictType.getDictCode());
|
|
|
}
|
|
|
- return Result.success("批量删除成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("批量删除字典类型失败", e);
|
|
|
- return Result.error(500, "批量删除失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<List<DictData>> getDictDataList(String dictCode) {
|
|
|
- try {
|
|
|
- List<DictData> dataList = getDictDataByCode(dictCode);
|
|
|
- return Result.success("查询成功", dataList);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询字典数据列表失败, dictCode={}", dictCode, e);
|
|
|
- return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ public List<DictData> getDictDataList(String dictCode) {
|
|
|
+ return getDictDataByCode(dictCode);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<Map<String, Object>> getDictDataPage(Map<String, Object> params) {
|
|
|
- try {
|
|
|
- int page = Integer.parseInt(params.getOrDefault("page", 1).toString());
|
|
|
- int pageSize = Integer.parseInt(params.getOrDefault("pageSize", 10).toString());
|
|
|
- String dictCode = (String) params.get("dictCode");
|
|
|
- String itemLabel = (String) params.get("itemLabel");
|
|
|
- Integer status = params.get("status") != null ? Integer.parseInt(params.get("status").toString()) : null;
|
|
|
-
|
|
|
- LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
|
|
- if (StringUtils.hasText(dictCode)) {
|
|
|
- wrapper.eq(DictData::getDictCode, dictCode);
|
|
|
- }
|
|
|
- if (StringUtils.hasText(itemLabel)) {
|
|
|
- wrapper.like(DictData::getItemLabel, itemLabel);
|
|
|
- }
|
|
|
- if (status != null) {
|
|
|
- wrapper.eq(DictData::getStatus, status);
|
|
|
- }
|
|
|
- wrapper.orderByAsc(DictData::getSortOrder);
|
|
|
- wrapper.orderByDesc(DictData::getCreateTime);
|
|
|
-
|
|
|
- Page<DictData> pageObj = new Page<>(page, pageSize);
|
|
|
- Page<DictData> result = dictDataMapper.selectPage(pageObj, wrapper);
|
|
|
+ public Map<String, Object> getDictDataPage(Map<String, Object> params) {
|
|
|
+ int[] pageParams = PageUtil.parsePageParams(params);
|
|
|
+ int page = pageParams[0];
|
|
|
+ int pageSize = pageParams[1];
|
|
|
+ String dictCode = (String) params.get("dictCode");
|
|
|
+ String itemLabel = (String) params.get("itemLabel");
|
|
|
+ Integer status = params.get("status") != null ? Integer.parseInt(params.get("status").toString()) : null;
|
|
|
+
|
|
|
+ LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (StringUtils.hasText(dictCode)) {
|
|
|
+ wrapper.eq(DictData::getDictCode, dictCode);
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(itemLabel)) {
|
|
|
+ wrapper.like(DictData::getItemLabel, itemLabel);
|
|
|
+ }
|
|
|
+ if (status != null) {
|
|
|
+ wrapper.eq(DictData::getStatus, status);
|
|
|
+ }
|
|
|
+ wrapper.orderByAsc(DictData::getSortOrder);
|
|
|
+ wrapper.orderByDesc(DictData::getCreateTime);
|
|
|
|
|
|
- Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("list", result.getRecords());
|
|
|
- data.put("total", result.getTotal());
|
|
|
- data.put("pageSize", pageSize);
|
|
|
- data.put("currentPage", page);
|
|
|
+ Page<DictData> pageObj = new Page<>(page, pageSize);
|
|
|
+ Page<DictData> result = dictDataMapper.selectPage(pageObj, wrapper);
|
|
|
|
|
|
- return Result.success("查询成功", data);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询字典数据列表失败", e);
|
|
|
- return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ return PageUtil.buildPageResult(result, page, pageSize);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<DictData> getDictDataById(Long id) {
|
|
|
- try {
|
|
|
- DictData dictData = dictDataMapper.selectById(id);
|
|
|
- if (dictData == null) {
|
|
|
- return Result.error(404, "字典数据不存在");
|
|
|
- }
|
|
|
- return Result.success("查询成功", dictData);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询字典数据失败, id={}", id, e);
|
|
|
- return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ public DictData getDictDataById(Long id) {
|
|
|
+ DictData dictData = dictDataMapper.selectById(id);
|
|
|
+ ResponseEnum.DICT_DATA_NOT_FOUND.assertNotNull(dictData);
|
|
|
+ return dictData;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> addDictData(DictData dictData) {
|
|
|
- try {
|
|
|
- LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(DictData::getDictCode, dictData.getDictCode());
|
|
|
- wrapper.eq(DictData::getItemValue, dictData.getItemValue());
|
|
|
- if (dictDataMapper.selectCount(wrapper) > 0) {
|
|
|
- return Result.error(400, "该字典下已存在相同的字典项值");
|
|
|
- }
|
|
|
-
|
|
|
- dictData.setCreateTime(LocalDateTime.now());
|
|
|
- dictData.setUpdateTime(LocalDateTime.now());
|
|
|
- dictDataMapper.insert(dictData);
|
|
|
+ public void addDictData(DictData dictData) {
|
|
|
+ LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(DictData::getDictCode, dictData.getDictCode());
|
|
|
+ wrapper.eq(DictData::getItemValue, dictData.getItemValue());
|
|
|
+ if (dictDataMapper.selectCount(wrapper) > 0) {
|
|
|
+ throw new BusinessException(ResponseEnum.DICT_ITEM_VALUE_DUPLICATE);
|
|
|
+ }
|
|
|
|
|
|
- clearDictCache(dictData.getDictCode());
|
|
|
+ dictData.setCreateTime(LocalDateTime.now());
|
|
|
+ dictData.setUpdateTime(LocalDateTime.now());
|
|
|
+ dictDataMapper.insert(dictData);
|
|
|
|
|
|
- return Result.success("添加成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("添加字典数据失败", e);
|
|
|
- return Result.error(500, "添加失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ clearDictCache(dictData.getDictCode());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> updateDictData(DictData dictData) {
|
|
|
- try {
|
|
|
- DictData oldDictData = dictDataMapper.selectById(dictData.getId());
|
|
|
- if (oldDictData == null) {
|
|
|
- return Result.error(404, "字典数据不存在");
|
|
|
- }
|
|
|
+ public void updateDictData(DictData dictData) {
|
|
|
+ DictData oldDictData = dictDataMapper.selectById(dictData.getId());
|
|
|
+ ResponseEnum.DICT_DATA_NOT_FOUND.assertNotNull(oldDictData);
|
|
|
|
|
|
- if (!oldDictData.getItemValue().equals(dictData.getItemValue())) {
|
|
|
- LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(DictData::getDictCode, oldDictData.getDictCode());
|
|
|
- wrapper.eq(DictData::getItemValue, dictData.getItemValue());
|
|
|
- wrapper.ne(DictData::getId, dictData.getId());
|
|
|
- if (dictDataMapper.selectCount(wrapper) > 0) {
|
|
|
- return Result.error(400, "该字典下已存在相同的字典项值");
|
|
|
- }
|
|
|
+ if (!oldDictData.getItemValue().equals(dictData.getItemValue())) {
|
|
|
+ LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(DictData::getDictCode, oldDictData.getDictCode());
|
|
|
+ wrapper.eq(DictData::getItemValue, dictData.getItemValue());
|
|
|
+ wrapper.ne(DictData::getId, dictData.getId());
|
|
|
+ if (dictDataMapper.selectCount(wrapper) > 0) {
|
|
|
+ throw new BusinessException(ResponseEnum.DICT_ITEM_VALUE_DUPLICATE);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- dictData.setUpdateTime(LocalDateTime.now());
|
|
|
- dictDataMapper.updateById(dictData);
|
|
|
-
|
|
|
- clearDictCache(oldDictData.getDictCode());
|
|
|
+ dictData.setUpdateTime(LocalDateTime.now());
|
|
|
+ dictDataMapper.updateById(dictData);
|
|
|
|
|
|
- return Result.success("修改成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("修改字典数据失败", e);
|
|
|
- return Result.error(500, "修改失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ clearDictCache(oldDictData.getDictCode());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> deleteDictData(Long id) {
|
|
|
- try {
|
|
|
- DictData dictData = dictDataMapper.selectById(id);
|
|
|
- if (dictData == null) {
|
|
|
- return Result.error(404, "字典数据不存在");
|
|
|
- }
|
|
|
+ public void deleteDictData(Long id) {
|
|
|
+ DictData dictData = dictDataMapper.selectById(id);
|
|
|
+ ResponseEnum.DICT_DATA_NOT_FOUND.assertNotNull(dictData);
|
|
|
|
|
|
- dictDataMapper.deleteById(id);
|
|
|
+ dictDataMapper.deleteById(id);
|
|
|
|
|
|
- clearDictCache(dictData.getDictCode());
|
|
|
-
|
|
|
- return Result.success("删除成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("删除字典数据失败", e);
|
|
|
- return Result.error(500, "删除失败: " + e.getMessage());
|
|
|
- }
|
|
|
+ clearDictCache(dictData.getDictCode());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> batchDeleteDictData(List<Long> ids) {
|
|
|
- try {
|
|
|
- Set<String> dictCodes = new HashSet<>();
|
|
|
- for (Long id : ids) {
|
|
|
- DictData dictData = dictDataMapper.selectById(id);
|
|
|
- if (dictData != null) {
|
|
|
- dictCodes.add(dictData.getDictCode());
|
|
|
- dictDataMapper.deleteById(id);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for (String dictCode : dictCodes) {
|
|
|
- clearDictCache(dictCode);
|
|
|
+ public void batchDeleteDictData(List<Long> ids) {
|
|
|
+ Set<String> dictCodes = new HashSet<>();
|
|
|
+ for (Long id : ids) {
|
|
|
+ DictData dictData = dictDataMapper.selectById(id);
|
|
|
+ if (dictData != null) {
|
|
|
+ dictCodes.add(dictData.getDictCode());
|
|
|
+ dictDataMapper.deleteById(id);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- return Result.success("批量删除成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("批量删除字典数据失败", e);
|
|
|
- return Result.error(500, "批量删除失败: " + e.getMessage());
|
|
|
+ for (String dictCode : dictCodes) {
|
|
|
+ clearDictCache(dictCode);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<String, List<DictData>> getAllDictData() {
|
|
|
- try {
|
|
|
- String cachedJson = redisTemplate.opsForValue().get(DICT_ALL_CACHE_KEY);
|
|
|
- if (cachedJson != null) {
|
|
|
- return JSON.parseObject(cachedJson, Map.class);
|
|
|
- }
|
|
|
+ String cachedJson = redisTemplate.opsForValue().get(DICT_ALL_CACHE_KEY);
|
|
|
+ if (cachedJson != null) {
|
|
|
+ return JSON.parseObject(cachedJson, Map.class);
|
|
|
+ }
|
|
|
|
|
|
- LambdaQueryWrapper<DictType> typeWrapper = new LambdaQueryWrapper<>();
|
|
|
- typeWrapper.eq(DictType::getStatus, 1);
|
|
|
- typeWrapper.orderByAsc(DictType::getSortOrder);
|
|
|
- List<DictType> dictTypes = dictTypeMapper.selectList(typeWrapper);
|
|
|
+ LambdaQueryWrapper<DictType> typeWrapper = new LambdaQueryWrapper<>();
|
|
|
+ typeWrapper.eq(DictType::getStatus, 1);
|
|
|
+ typeWrapper.orderByAsc(DictType::getSortOrder);
|
|
|
+ List<DictType> dictTypes = dictTypeMapper.selectList(typeWrapper);
|
|
|
|
|
|
- Map<String, List<DictData>> cachedData = new LinkedHashMap<>();
|
|
|
+ Map<String, List<DictData>> cachedData = new LinkedHashMap<>();
|
|
|
|
|
|
- for (DictType dictType : dictTypes) {
|
|
|
- List<DictData> dataList = getDictDataByCode(dictType.getDictCode());
|
|
|
- cachedData.put(dictType.getDictCode(), dataList);
|
|
|
- }
|
|
|
+ for (DictType dictType : dictTypes) {
|
|
|
+ List<DictData> dataList = getDictDataByCode(dictType.getDictCode());
|
|
|
+ cachedData.put(dictType.getDictCode(), dataList);
|
|
|
+ }
|
|
|
|
|
|
- redisTemplate.opsForValue().set(DICT_ALL_CACHE_KEY, JSON.toJSONString(cachedData), CACHE_EXPIRE_HOURS, TimeUnit.HOURS);
|
|
|
+ redisTemplate.opsForValue().set(DICT_ALL_CACHE_KEY, JSON.toJSONString(cachedData), CACHE_EXPIRE_HOURS, TimeUnit.HOURS);
|
|
|
|
|
|
- return cachedData;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取所有字典数据失败", e);
|
|
|
- return new HashMap<>();
|
|
|
- }
|
|
|
+ return cachedData;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<DictData> getDictDataByCode(String dictCode) {
|
|
|
- try {
|
|
|
- String cacheKey = DICT_CACHE_KEY + dictCode;
|
|
|
- String cachedJson = redisTemplate.opsForValue().get(cacheKey);
|
|
|
- if (cachedJson != null) {
|
|
|
- return JSON.parseArray(cachedJson, DictData.class);
|
|
|
- }
|
|
|
+ String cacheKey = DICT_CACHE_KEY + dictCode;
|
|
|
+ String cachedJson = redisTemplate.opsForValue().get(cacheKey);
|
|
|
+ if (cachedJson != null) {
|
|
|
+ return JSON.parseArray(cachedJson, DictData.class);
|
|
|
+ }
|
|
|
|
|
|
- List<DictData> dataList = dictDataMapper.selectByDictCode(dictCode);
|
|
|
+ List<DictData> dataList = dictDataMapper.selectByDictCode(dictCode);
|
|
|
|
|
|
- redisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(dataList), CACHE_EXPIRE_HOURS, TimeUnit.HOURS);
|
|
|
+ redisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(dataList), CACHE_EXPIRE_HOURS, TimeUnit.HOURS);
|
|
|
|
|
|
- return dataList;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取字典数据失败, dictCode={}", dictCode, e);
|
|
|
- return new ArrayList<>();
|
|
|
- }
|
|
|
+ return dataList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void refreshCache() {
|
|
|
- try {
|
|
|
- Set<String> keys = redisTemplate.keys(DICT_CACHE_KEY + "*");
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- redisTemplate.delete(keys);
|
|
|
- }
|
|
|
- if (Boolean.TRUE.equals(redisTemplate.hasKey(DICT_ALL_CACHE_KEY))) {
|
|
|
- redisTemplate.delete(DICT_ALL_CACHE_KEY);
|
|
|
- }
|
|
|
- log.info("字典缓存已刷新");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("刷新字典缓存失败", e);
|
|
|
+ Set<String> keys = redisTemplate.keys(DICT_CACHE_KEY + "*");
|
|
|
+ if (keys != null && !keys.isEmpty()) {
|
|
|
+ redisTemplate.delete(keys);
|
|
|
}
|
|
|
+ if (Boolean.TRUE.equals(redisTemplate.hasKey(DICT_ALL_CACHE_KEY))) {
|
|
|
+ redisTemplate.delete(DICT_ALL_CACHE_KEY);
|
|
|
+ }
|
|
|
+ log.info("字典缓存已刷新");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -457,58 +330,47 @@ public class DictServiceImpl extends ServiceImpl<DictTypeMapper, DictType> imple
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<Void> exportDictData(String dictCode) {
|
|
|
- return Result.success("导出成功");
|
|
|
+ public void exportDictData(String dictCode) {
|
|
|
+ // 导出功能暂未实现
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<Void> importDictData(List<DictData> dataList) {
|
|
|
- try {
|
|
|
- if (dataList == null || dataList.isEmpty()) {
|
|
|
- return Result.error(400, "导入数据不能为空");
|
|
|
- }
|
|
|
-
|
|
|
- Set<String> dictCodes = new HashSet<>();
|
|
|
- for (DictData data : dataList) {
|
|
|
- data.setCreateTime(LocalDateTime.now());
|
|
|
- data.setUpdateTime(LocalDateTime.now());
|
|
|
- dictDataMapper.insert(data);
|
|
|
- dictCodes.add(data.getDictCode());
|
|
|
- }
|
|
|
+ public void importDictData(List<DictData> dataList) {
|
|
|
+ if (dataList == null || dataList.isEmpty()) {
|
|
|
+ throw new BusinessException(ResponseEnum.IMPORT_DATA_EMPTY);
|
|
|
+ }
|
|
|
|
|
|
- for (String code : dictCodes) {
|
|
|
- clearDictCache(code);
|
|
|
- }
|
|
|
+ Set<String> dictCodes = new HashSet<>();
|
|
|
+ for (DictData data : dataList) {
|
|
|
+ data.setCreateTime(LocalDateTime.now());
|
|
|
+ data.setUpdateTime(LocalDateTime.now());
|
|
|
+ dictDataMapper.insert(data);
|
|
|
+ dictCodes.add(data.getDictCode());
|
|
|
+ }
|
|
|
|
|
|
- return Result.success("导入成功");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("导入字典数据失败", e);
|
|
|
- return Result.error(500, "导入失败: " + e.getMessage());
|
|
|
+ for (String code : dictCodes) {
|
|
|
+ clearDictCache(code);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void clearDictCache(String dictCode) {
|
|
|
- try {
|
|
|
- String cacheKey = DICT_CACHE_KEY + dictCode;
|
|
|
- redisTemplate.delete(cacheKey);
|
|
|
- redisTemplate.delete(DICT_ALL_CACHE_KEY);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("清除字典缓存失败, dictCode={}", dictCode, e);
|
|
|
- }
|
|
|
+ String cacheKey = DICT_CACHE_KEY + dictCode;
|
|
|
+ redisTemplate.delete(cacheKey);
|
|
|
+ redisTemplate.delete(DICT_ALL_CACHE_KEY);
|
|
|
}
|
|
|
|
|
|
private void saveDictLog(DictType dictType, DictType oldDictType, String operationType, String operationDesc) {
|
|
|
try {
|
|
|
- DictLog log = new DictLog();
|
|
|
- log.setDictCode(dictType.getDictCode());
|
|
|
- log.setDictName(dictType.getDictName());
|
|
|
- log.setOperationType(operationType);
|
|
|
- log.setOperationDesc(operationDesc);
|
|
|
- log.setBeforeData(oldDictType != null ? JSON.toJSONString(oldDictType) : null);
|
|
|
- log.setAfterData(JSON.toJSONString(dictType));
|
|
|
- log.setCreateTime(LocalDateTime.now());
|
|
|
- dictLogMapper.insert(log);
|
|
|
+ DictLog logEntry = new DictLog();
|
|
|
+ logEntry.setDictCode(dictType.getDictCode());
|
|
|
+ logEntry.setDictName(dictType.getDictName());
|
|
|
+ logEntry.setOperationType(operationType);
|
|
|
+ logEntry.setOperationDesc(operationDesc);
|
|
|
+ logEntry.setBeforeData(oldDictType != null ? JSON.toJSONString(oldDictType) : null);
|
|
|
+ logEntry.setAfterData(JSON.toJSONString(dictType));
|
|
|
+ logEntry.setCreateTime(LocalDateTime.now());
|
|
|
+ dictLogMapper.insert(logEntry);
|
|
|
} catch (Exception e) {
|
|
|
log.error("保存字典日志失败", e);
|
|
|
}
|