新增字典
This commit is contained in:
parent
2ae7c39b9f
commit
90d9af2eb2
@ -0,0 +1,18 @@
|
|||||||
|
package net.rzdata.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据重复异常
|
||||||
|
*/
|
||||||
|
public class RepeatDataException extends ClientException {
|
||||||
|
|
||||||
|
public static final String ERROR_CODE = "A0428";
|
||||||
|
public static final String ERROR_MESSAGE = "数据已存在";
|
||||||
|
|
||||||
|
public RepeatDataException() {
|
||||||
|
super(ERROR_CODE, ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RepeatDataException(String message) {
|
||||||
|
super(ERROR_CODE, message);
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
package net.rzdata.demo.dict;
|
package net.rzdata.demo.dict;
|
||||||
|
|
||||||
import net.rzdata.demo.dict.domain.AddDictTypeReq;
|
import net.rzdata.demo.dict.domain.AddDictReq;
|
||||||
import net.rzdata.demo.dict.domain.Dict;
|
import net.rzdata.demo.dict.domain.Dict;
|
||||||
import net.rzdata.demo.dict.domain.UpdateDictReq;
|
|
||||||
import net.rzdata.domain.Id;
|
import net.rzdata.domain.Id;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.PutMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -18,4 +16,15 @@ public class DictController {
|
|||||||
public DictController(DictService dictService) {
|
public DictController(DictService dictService) {
|
||||||
this.dictService = dictService;
|
this.dictService = dictService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public Id addDictData(AddDictReq req) {
|
||||||
|
Dict dict = req.into();
|
||||||
|
if (dict.getParentId() == null) {
|
||||||
|
dict.setParentId(Dict.ROOT.getId());
|
||||||
|
dict.setType(Dict.ROOT.getType());
|
||||||
|
}
|
||||||
|
dictService.addDict(dict);
|
||||||
|
return Id.of(dict.getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
package net.rzdata.demo.dict;
|
package net.rzdata.demo.dict;
|
||||||
|
|
||||||
import net.rzdata.demo.dict.domain.Dict;
|
import net.rzdata.demo.dict.domain.Dict;
|
||||||
import net.rzdata.demo.dict.mapper.DictMapper;
|
|
||||||
import net.rzdata.demo.dict.mapper.DictRepository;
|
import net.rzdata.demo.dict.mapper.DictRepository;
|
||||||
|
import net.rzdata.exception.RepeatDataException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DictService {
|
public class DictService {
|
||||||
|
|
||||||
@ -13,4 +17,23 @@ public class DictService {
|
|||||||
public DictService(DictRepository dictRepository) {
|
public DictService(DictRepository dictRepository) {
|
||||||
this.dictRepository = dictRepository;
|
this.dictRepository = dictRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void addDict(Dict dict) {
|
||||||
|
Optional<Dict> exist = this.getDictByTypeAndValue(dict.getType(), dict.getValue());
|
||||||
|
if (exist.isPresent()) {
|
||||||
|
throw new RepeatDataException(String.format("字典类型:%s, 值:%s 已存在", dict.getType(), dict.getValue()));
|
||||||
|
}
|
||||||
|
dictRepository.get().insert(dict);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<Dict> getDictByType(String type) {
|
||||||
|
return dictRepository.findByTypeOrderBySortAsc(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Dict> getDictByTypeAndValue(String type, String value) {
|
||||||
|
List<Dict> dictList = this.getDictByType(type);
|
||||||
|
return dictList.parallelStream()
|
||||||
|
.filter(d -> Objects.equals(d.getValue(), value))
|
||||||
|
.findAny();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
package net.rzdata.demo.dict.domain;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
import net.rzdata.trait.IConverter;
|
||||||
|
import net.rzdata.trait.IQuery;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString
|
||||||
|
public class AddDictReq implements IQuery<Dict> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典标签
|
||||||
|
*/
|
||||||
|
private String label;
|
||||||
|
/**
|
||||||
|
* 字典值
|
||||||
|
*/
|
||||||
|
private String value;
|
||||||
|
/**
|
||||||
|
* 字典类型
|
||||||
|
*/
|
||||||
|
private String type;
|
||||||
|
/**
|
||||||
|
* 父级ID
|
||||||
|
* 如果是顶级字典 则为0
|
||||||
|
*/
|
||||||
|
private String parentId;
|
||||||
|
/**
|
||||||
|
* 排序
|
||||||
|
*/
|
||||||
|
private Integer sort;
|
||||||
|
/**
|
||||||
|
* 是否禁用
|
||||||
|
*/
|
||||||
|
private Boolean disabled;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dict into() {
|
||||||
|
return AddDictReqConverter.INSTANCE.convert(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
interface AddDictReqConverter extends IConverter<AddDictReq, Dict> {
|
||||||
|
|
||||||
|
AddDictReqConverter INSTANCE = Mappers.getMapper(AddDictReqConverter.class);
|
||||||
|
|
||||||
|
@Mapping(target = "id", ignore = true)
|
||||||
|
@Mapping(target = "children", ignore = true)
|
||||||
|
@Override
|
||||||
|
Dict convert(AddDictReq req);
|
||||||
|
}
|
@ -1,13 +1,23 @@
|
|||||||
package net.rzdata.demo.dict.mapper;
|
package net.rzdata.demo.dict.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import net.rzdata.demo.dict.domain.Dict;
|
import net.rzdata.demo.dict.domain.Dict;
|
||||||
import net.rzdata.demo.trait.BaseRepository;
|
import net.rzdata.demo.trait.BaseRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class DictRepository extends BaseRepository<Dict, DictMapper> {
|
public class DictRepository extends BaseRepository<Dict, DictMapper> {
|
||||||
|
|
||||||
public DictRepository(DictMapper mapper) {
|
public DictRepository(DictMapper mapper) {
|
||||||
super(mapper);
|
super(mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Dict> findByTypeOrderBySortAsc(String type) {
|
||||||
|
return this.get().selectList(new LambdaQueryWrapper<Dict>()
|
||||||
|
.eq(Dict::getType, type)
|
||||||
|
.orderByAsc(Dict::getSort)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,4 +9,12 @@ class DictServiceTest {
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private transient DictService dictService;
|
private transient DictService dictService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addDict() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getDictByType() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user