查询字典
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
package net.rzdata.demo.dict;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import net.rzdata.demo.dict.domain.AddDictReq;
|
||||
import net.rzdata.demo.dict.domain.Dict;
|
||||
import net.rzdata.demo.dict.domain.GetDictTypeListReq;
|
||||
import net.rzdata.demo.dict.domain.UpdateDictReq;
|
||||
import net.rzdata.domain.Id;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dict")
|
||||
public class DictController {
|
||||
@@ -16,6 +21,11 @@ public class DictController {
|
||||
this.dictService = dictService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字典
|
||||
* @param req 新增字典请求
|
||||
* @return 新增字典的ID
|
||||
*/
|
||||
@PostMapping()
|
||||
public Id addDict(@RequestBody AddDictReq req) {
|
||||
Dict dict = req.into();
|
||||
@@ -27,10 +37,49 @@ public class DictController {
|
||||
return Id.of(dict.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新字典
|
||||
* @param req 更新字典请求
|
||||
* @return 字典的ID
|
||||
*/
|
||||
@PutMapping()
|
||||
public Id updateDict(@RequestBody UpdateDictReq req) {
|
||||
Dict dict = req.into();
|
||||
dictService.updateDict(dict);
|
||||
return Id.of(dict.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典类型列表
|
||||
* @param req 获取字典类型列表请求
|
||||
* @return 字典类型列表(分页)
|
||||
*/
|
||||
@GetMapping("/type")
|
||||
public IPage<Dict> getDictTypePage(@RequestParam GetDictTypeListReq req) {
|
||||
return dictService.getDictTypePage(req);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典树
|
||||
* @param parentId 父ID
|
||||
* @param level 层级
|
||||
* @return 字典树
|
||||
*/
|
||||
@GetMapping("/tree")
|
||||
public List<Dict> getDictTree(
|
||||
@RequestParam(required = false, defaultValue = "0") String parentId,
|
||||
@RequestParam(required = false, defaultValue = "-1") Integer level
|
||||
) {
|
||||
return dictService.getDictTreeTopDown(Collections.singleton(parentId), level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典数据
|
||||
* @param type 字典类型
|
||||
* @return 字典数据
|
||||
*/
|
||||
@GetMapping("/data/{type}")
|
||||
public List<Dict> getDictData(@PathVariable String type) {
|
||||
return dictService.getDictByType(type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package net.rzdata.demo.dict;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import net.rzdata.demo.dict.domain.Dict;
|
||||
import net.rzdata.demo.dict.domain.GetDictTypeListReq;
|
||||
import net.rzdata.demo.dict.mapper.DictRepository;
|
||||
import net.rzdata.exception.NotExistDataException;
|
||||
import net.rzdata.exception.RepeatDataException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class DictService {
|
||||
@@ -43,6 +44,30 @@ public class DictService {
|
||||
}
|
||||
}
|
||||
|
||||
protected IPage<Dict> getDictTypePage(GetDictTypeListReq req) {
|
||||
return dictRepository.getDictTypePage(req);
|
||||
}
|
||||
|
||||
protected List<Dict> getDictTreeTopDown(Collection<String> parentId, int level) {
|
||||
if (level == 0 || level < Byte.MIN_VALUE) {
|
||||
return Collections.emptyList();
|
||||
} else if (parentId.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<Dict> children = dictRepository.findByParentIdIn(parentId);
|
||||
Set<String> childIds = children.parallelStream()
|
||||
.map(Dict::getId)
|
||||
.collect(Collectors.toSet());
|
||||
List<Dict> grandchildren = getDictTreeTopDown(childIds, level);
|
||||
for (Dict child : children) {
|
||||
List<Dict> grandchild = grandchildren.parallelStream()
|
||||
.filter(g -> Objects.equals(g.getParentId(), child.getId()))
|
||||
.toList();
|
||||
child.setChildren(grandchild);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
private Optional<Dict> getDictByTypeAndValue(String type, String value) {
|
||||
List<Dict> dictList = this.getDictByType(type);
|
||||
return dictList.parallelStream()
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.rzdata.demo.dict.domain;
|
||||
|
||||
import net.rzdata.demo.trait.GetReq;
|
||||
|
||||
public class GetDictTypeListReq extends GetReq<Dict> {
|
||||
}
|
||||
@@ -2,10 +2,13 @@ package net.rzdata.demo.dict.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import net.rzdata.demo.dict.domain.Dict;
|
||||
import net.rzdata.demo.dict.domain.GetDictTypeListReq;
|
||||
import net.rzdata.demo.trait.BaseRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
@@ -28,4 +31,14 @@ public class DictRepository extends BaseRepository<Dict, DictMapper> {
|
||||
.eq(Dict::getType, oldValue)
|
||||
);
|
||||
}
|
||||
|
||||
public List<Dict> findByParentIdIn(Collection<String> parentId) {
|
||||
return this.get().selectList(new LambdaQueryWrapper<Dict>()
|
||||
.in(Dict::getParentId, parentId)
|
||||
);
|
||||
}
|
||||
|
||||
public IPage<Dict> getDictTypePage(GetDictTypeListReq req) {
|
||||
return this.get().selectPage(req.into(), new LambdaQueryWrapper<>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user