|
|
@@ -1,30 +1,27 @@
|
|
|
-package com.sckw.system.service;
|
|
|
+package com.sckw.system.service.flex;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
-import com.sckw.core.model.base.BaseModel;
|
|
|
+import com.mybatisflex.core.paginate.Page;
|
|
|
import com.sckw.core.utils.IdWorker;
|
|
|
import com.sckw.core.web.context.LoginUserHolder;
|
|
|
import com.sckw.core.web.response.result.PageDataResult;
|
|
|
-import com.sckw.system.model.SysDict;
|
|
|
+import com.sckw.system.entity.flex.SysDictEntity;
|
|
|
import com.sckw.system.model.vo.req.SysDictReqVo;
|
|
|
import com.sckw.system.model.vo.res.SysDictResp;
|
|
|
-import com.sckw.system.repository.SysDictRepository;
|
|
|
+import com.sckw.system.repository.flex.SysDictFlexService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.collections4.CollectionUtils;
|
|
|
-import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.*;
|
|
|
-import java.util.function.Function;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
- * 字典业务服务层(中间层) - MyBatis-Plus 版本
|
|
|
- * 在 SysDictService 基础上添加业务逻辑
|
|
|
+ * 字典业务服务层(中间层)
|
|
|
+ * 在 SysDictFlexService 基础上添加业务逻辑
|
|
|
* @author system
|
|
|
* @date 2024
|
|
|
*/
|
|
|
@@ -33,16 +30,16 @@ import java.util.stream.Collectors;
|
|
|
@RequiredArgsConstructor
|
|
|
public class SysDictFlexBusinessService {
|
|
|
|
|
|
- private final SysDictRepository sysDictRepository;
|
|
|
+ private final SysDictFlexService sysDictFlexService;
|
|
|
|
|
|
/**
|
|
|
* 新增字典
|
|
|
*/
|
|
|
- public Boolean insert(SysDictReqVo.SysDictInfo entity) {
|
|
|
+ public int insert(SysDictReqVo.SysDictInfo entity) {
|
|
|
log.info("新增字典,请求参数:{}", JSON.toJSONString(entity));
|
|
|
|
|
|
// 业务校验:检查是否存在相同的字典项
|
|
|
- SysDict existDict = sysDictRepository.selectUniqueDict(
|
|
|
+ SysDictEntity existDict = sysDictFlexService.selectUniqueDict(
|
|
|
entity.getDictId(),
|
|
|
entity.getValue(),
|
|
|
entity.getLabel(),
|
|
|
@@ -50,36 +47,31 @@ public class SysDictFlexBusinessService {
|
|
|
);
|
|
|
if (existDict != null) {
|
|
|
log.error("字典项已存在,请勿重复添加");
|
|
|
- return Boolean.FALSE;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
// 设置创建信息
|
|
|
Long userId = LoginUserHolder.getUserId();
|
|
|
long id = new IdWorker(1L).nextId();
|
|
|
- SysDict sysDict = getSysDict(entity, id, userId);
|
|
|
- return sysDictRepository.save(sysDict) ;
|
|
|
- }
|
|
|
-
|
|
|
- @NotNull
|
|
|
- private static SysDict getSysDict(SysDictReqVo.SysDictInfo entity, long id, Long userId) {
|
|
|
- SysDict sysDict = new SysDict();
|
|
|
- sysDict.setId(id);
|
|
|
- sysDict.setDictId(entity.getDictId());
|
|
|
- sysDict.setValue(entity.getValue());
|
|
|
- sysDict.setLabel(entity.getLabel());
|
|
|
- sysDict.setType(entity.getType());
|
|
|
- sysDict.setDescription(entity.getDescription());
|
|
|
- sysDict.setSort(entity.getSort());
|
|
|
- sysDict.setParentId(entity.getParentId());
|
|
|
- sysDict.setUrl(entity.getUrl());
|
|
|
- sysDict.setRemark(entity.getRemark());
|
|
|
- sysDict.setStatus(0);
|
|
|
- sysDict.setCreateBy(userId);
|
|
|
- sysDict.setUpdateBy(userId);
|
|
|
- sysDict.setCreateTime(new Date());
|
|
|
- sysDict.setUpdateTime(new Date());
|
|
|
- sysDict.setDelFlag(0);
|
|
|
- return sysDict;
|
|
|
+ SysDictEntity sysDictEntity = new SysDictEntity();
|
|
|
+ sysDictEntity.setId(id);
|
|
|
+ sysDictEntity.setDictId(entity.getDictId());
|
|
|
+ sysDictEntity.setValue(entity.getValue());
|
|
|
+ sysDictEntity.setLabel(entity.getLabel());
|
|
|
+ sysDictEntity.setType(entity.getType());
|
|
|
+ sysDictEntity.setDescription(entity.getDescription());
|
|
|
+ sysDictEntity.setSort(entity.getSort());
|
|
|
+ sysDictEntity.setParentId(entity.getParentId());
|
|
|
+ sysDictEntity.setUrl(entity.getUrl());
|
|
|
+ sysDictEntity.setRemark(entity.getRemark());
|
|
|
+ sysDictEntity.setStatus(0);
|
|
|
+ sysDictEntity.setCreateBy(userId);
|
|
|
+ sysDictEntity.setUpdateBy(userId);
|
|
|
+ sysDictEntity.setCreateTime(new Date());
|
|
|
+ sysDictEntity.setUpdateTime(new Date());
|
|
|
+ sysDictEntity.setDelFlag(0);
|
|
|
+
|
|
|
+ return sysDictFlexService.insert(sysDictEntity);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -88,32 +80,33 @@ public class SysDictFlexBusinessService {
|
|
|
*/
|
|
|
public int logicDeleteById(Long id) {
|
|
|
log.info("逻辑删除字典,ID:{}", id);
|
|
|
- return sysDictRepository.logicDeleteById(id);
|
|
|
+ // TODO: 可在此添加业务逻辑,如权限校验、关联数据检查等
|
|
|
+ return sysDictFlexService.logicDeleteById(id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新字典
|
|
|
*/
|
|
|
- public Boolean update(SysDictReqVo.SysDictInfo entity) {
|
|
|
+ public int update(SysDictReqVo.SysDictInfo entity) {
|
|
|
log.info("更新字典,ID:{}", entity.getId());
|
|
|
|
|
|
// 业务校验:检查是否存在相同的字典项(排除自己)
|
|
|
- SysDict existDict = sysDictRepository.selectById(entity.getId());
|
|
|
+ SysDictEntity existDict = sysDictFlexService.selectById(entity.getId());
|
|
|
if (Objects.isNull(existDict)) {
|
|
|
log.error("字典项不存在");
|
|
|
- return Boolean.FALSE;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
// 设置更新信息
|
|
|
Long userId = LoginUserHolder.getUserId();
|
|
|
- SysDict sysDict = new SysDict();
|
|
|
- sysDict.setId(existDict.getId());
|
|
|
- sysDict.setSort(entity.getSort());
|
|
|
- sysDict.setRemark(entity.getRemark());
|
|
|
- sysDict.setUpdateBy(userId);
|
|
|
- sysDict.setUpdateTime(new Date());
|
|
|
+ SysDictEntity sysDictEntity = new SysDictEntity();
|
|
|
+ sysDictEntity.setId(existDict.getId());
|
|
|
+ sysDictEntity.setSort(entity.getSort());
|
|
|
+ sysDictEntity.setRemark(entity.getRemark());
|
|
|
+ sysDictEntity.setUpdateBy(userId);
|
|
|
+ sysDictEntity.setUpdateTime(new Date());
|
|
|
|
|
|
- return sysDictRepository.update(sysDict);
|
|
|
+ return sysDictFlexService.update(sysDictEntity);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -127,27 +120,17 @@ public class SysDictFlexBusinessService {
|
|
|
*/
|
|
|
public PageDataResult<SysDictResp> selectPage(SysDictReqVo reqVo) {
|
|
|
log.info("分页查询字典,请求参数:{}", JSON.toJSONString(reqVo));
|
|
|
- int pageNum = reqVo.getPageNum();
|
|
|
+ int pageNum = reqVo.getPageNum() ;
|
|
|
int pageSize = reqVo.getPageSize();
|
|
|
|
|
|
- IPage<SysDict> page = sysDictRepository.selectPage(pageNum, pageSize, reqVo.getType(), reqVo.getParentId(), reqVo.getLabel());
|
|
|
+ Page<SysDictEntity> page = sysDictFlexService.selectPage(pageNum, pageSize, reqVo.getType(), reqVo.getParentId(),reqVo.getLabel());
|
|
|
|
|
|
- List<SysDict> records = page.getRecords();
|
|
|
+ List<SysDictEntity> records = page.getRecords();
|
|
|
if (records == null || records.isEmpty()) {
|
|
|
return PageDataResult.empty(reqVo.getPageNum(), reqVo.getPageSize());
|
|
|
}
|
|
|
- Set<String> parentIds = records.stream()
|
|
|
- .map(SysDict::getParentId)
|
|
|
- .collect(Collectors.toSet());
|
|
|
- //查询父字典
|
|
|
- List<SysDict> parentDicts = sysDictRepository.queryByIds(parentIds);
|
|
|
- Map<Long, SysDict> idAndParentDictMap = Maps.newHashMap();
|
|
|
- if (CollectionUtils.isNotEmpty(parentDicts)){
|
|
|
- idAndParentDictMap = parentDicts.stream()
|
|
|
- .collect(Collectors.toMap(BaseModel::getId, Function.identity(), (k1, k2) -> k1));
|
|
|
- }
|
|
|
-
|
|
|
- List<SysDictResp> sysDictResps = convertToRespList(records,idAndParentDictMap);
|
|
|
+
|
|
|
+ List<SysDictResp> sysDictResps = convertToRespList(records);
|
|
|
return PageDataResult.of(page, sysDictResps);
|
|
|
}
|
|
|
|
|
|
@@ -156,7 +139,7 @@ public class SysDictFlexBusinessService {
|
|
|
*/
|
|
|
public long count(String type) {
|
|
|
log.info("统计字典数量,type:{}", type);
|
|
|
- return sysDictRepository.count(type);
|
|
|
+ return sysDictFlexService.count(type);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -166,8 +149,9 @@ public class SysDictFlexBusinessService {
|
|
|
*/
|
|
|
public List<SysDictResp> searchParentDictForSelect(SysDictReqVo reqVo) {
|
|
|
log.info("查询上级字典下拉列表,请求参数:{}", JSON.toJSONString(reqVo));
|
|
|
- List<SysDict> list = sysDictRepository.searchParentDictForSelect(reqVo.getKeyword());
|
|
|
- return convertToRespList(list,new HashMap<>());
|
|
|
+ // TODO: 可在此添加缓存处理
|
|
|
+ List<SysDictEntity> list = sysDictFlexService.searchParentDictForSelect(reqVo.getKeyword());
|
|
|
+ return convertToRespList(list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -176,56 +160,54 @@ public class SysDictFlexBusinessService {
|
|
|
* @param status 状态(0正常/1锁定)
|
|
|
* @return 影响行数
|
|
|
*/
|
|
|
- public Boolean updateStatus(Long id, Integer status) {
|
|
|
+ public int updateStatus(Long id, Integer status) {
|
|
|
log.info("快捷更改字典状态,ID:{}, 新状态:{}", id, status);
|
|
|
|
|
|
// 业务校验
|
|
|
if (id == null) {
|
|
|
log.error("字典ID不能为空");
|
|
|
- return Boolean.FALSE;
|
|
|
+ return 0;
|
|
|
}
|
|
|
if (status == null || (status != 0 && status != 1)) {
|
|
|
log.error("状态值无效,只能为0(正常)或1(锁定)");
|
|
|
- return Boolean.FALSE;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
// 检查字典是否存在
|
|
|
- SysDict existDict = sysDictRepository.selectById(id);
|
|
|
+ SysDictEntity existDict = sysDictFlexService.selectById(id);
|
|
|
if (Objects.isNull(existDict)) {
|
|
|
log.error("字典项不存在,ID:{}", id);
|
|
|
- return Boolean.FALSE;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
// 更新状态
|
|
|
- return sysDictRepository.updateStatus(id, status);
|
|
|
+ return sysDictFlexService.updateStatus(id, status);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将实体转换为响应对象
|
|
|
*/
|
|
|
- private SysDictResp convertToResp(SysDict entity,Map<Long, SysDict> idAndParentDictMap) {
|
|
|
+ private SysDictResp convertToResp(SysDictEntity entity) {
|
|
|
if (entity == null) {
|
|
|
return null;
|
|
|
}
|
|
|
SysDictResp resp = new SysDictResp();
|
|
|
BeanUtils.copyProperties(entity, resp);
|
|
|
- resp.setStatusName(Objects.equals(entity.getStatus(), 0) ? " 正常" : "锁定");
|
|
|
+ resp.setStatusName(Objects.equals(entity.getStatus(), 0)?" 正常":"锁定");
|
|
|
// 设置父级字典名称
|
|
|
- Long parentId = Optional.ofNullable(entity.getParentId()).map(Long::parseLong).orElse(null);
|
|
|
- SysDict parentDict = idAndParentDictMap.getOrDefault(parentId, new SysDict());
|
|
|
- resp.setParentName(parentDict.getLabel());
|
|
|
+ resp.setParentName(entity.getPidName());
|
|
|
return resp;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将实体列表转换为响应对象列表
|
|
|
*/
|
|
|
- private List<SysDictResp> convertToRespList(List<SysDict> entities,Map<Long, SysDict> idAndParentDictMap ) {
|
|
|
+ private List<SysDictResp> convertToRespList(List<SysDictEntity> entities) {
|
|
|
if (entities == null || entities.isEmpty()) {
|
|
|
return List.of();
|
|
|
}
|
|
|
return entities.stream()
|
|
|
- .map(x->convertToResp(x,idAndParentDictMap))
|
|
|
+ .map(this::convertToResp)
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
}
|