字典功能 #5

Closed
jiangdingxuan wants to merge 9 commits from rzdata:feature/dict into main
5 changed files with 116 additions and 5 deletions
Showing only changes of commit 711e20c040 - Show all commits

View File

@ -0,0 +1,18 @@
package net.rzdata.exception;
/**
* 操作不存在的数据异常
*/
public class NotExistDataException extends ClientException {
public static final String ERROR_CODE = "A0429";
public static final String ERROR_MESSAGE = "数据不存在";
public NotExistDataException() {
super(ERROR_CODE, ERROR_MESSAGE);
}
public NotExistDataException(String message) {
super(ERROR_CODE, message);
}
}

View File

@ -2,8 +2,10 @@ package net.rzdata.demo.dict;
import net.rzdata.demo.dict.domain.AddDictReq;
import net.rzdata.demo.dict.domain.Dict;
import net.rzdata.demo.dict.domain.UpdateDictReq;
import net.rzdata.domain.Id;
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.RestController;
@ -18,7 +20,7 @@ public class DictController {
}
@PostMapping()
public Id addDictData(AddDictReq req) {
public Id addDict(AddDictReq req) {
Dict dict = req.into();
if (dict.getParentId() == null) {
dict.setParentId(Dict.ROOT.getId());
@ -27,4 +29,11 @@ public class DictController {
dictService.addDict(dict);
return Id.of(dict.getId());
}
@PutMapping()
public Id updateDict(UpdateDictReq req) {
Dict dict = req.into();
dictService.updateDict(dict);
return Id.of(dict.getId());
}
}

View File

@ -2,6 +2,7 @@ package net.rzdata.demo.dict;
import net.rzdata.demo.dict.domain.Dict;
import net.rzdata.demo.dict.mapper.DictRepository;
import net.rzdata.exception.NotExistDataException;
import net.rzdata.exception.RepeatDataException;
import org.springframework.stereotype.Service;
@ -19,10 +20,7 @@ public class DictService {
}
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()));
}
check(dict);
dictRepository.get().insert(dict);
}
@ -30,10 +28,32 @@ public class DictService {
return dictRepository.findByTypeOrderBySortAsc(type);
}
protected void updateDict(Dict dict) {
Dict exist = dictRepository.get().selectById(dict.getId());
if (exist == null) {
throw new NotExistDataException("字典已被删除,无法更新");
}
if (!Objects.equals(dict.getValue(), exist.getValue())) {
check(dict);
}
dictRepository.get().updateById(dict);
// 如果是字典类 则需要更新该类下所有值的type字段
if (Objects.equals(exist.getParentId(), Dict.ROOT.getId())) {
dictRepository.updateType(exist.getType(), dict.getType());
}
}
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();
}
private void check(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()));
}
}
}

View File

@ -0,0 +1,56 @@
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 UpdateDictReq implements IQuery<Dict> {
/**
* 字典ID
*/
private String id;
/**
* 字典标签
*/
private String label;
/**
* 字典值
*/
private String value;
/**
* 排序
*/
private Integer sort;
/**
* 父级ID
*/
private String parentId;
/**
* 是否禁用
*/
private Boolean disabled;
public Dict into() {
return UpdateDictReqConverter.INSTANCE.convert(this);
}
}
@Mapper
interface UpdateDictReqConverter extends IConverter<UpdateDictReq, Dict> {
UpdateDictReqConverter INSTANCE = Mappers.getMapper(UpdateDictReqConverter.class);
@Mapping(target = "type", ignore = true)
@Mapping(target = "children", ignore = true)
@Override
Dict convert(UpdateDictReq req);
}

View File

@ -1,6 +1,7 @@
package net.rzdata.demo.dict.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import net.rzdata.demo.dict.domain.Dict;
import net.rzdata.demo.trait.BaseRepository;
import org.springframework.stereotype.Repository;
@ -20,4 +21,11 @@ public class DictRepository extends BaseRepository<Dict, DictMapper> {
.orderByAsc(Dict::getSort)
);
}
public int updateType(String oldValue, String newValue) {
return this.get().update(null, new LambdaUpdateWrapper<Dict>()
.set(Dict::getType, newValue)
.eq(Dict::getType, oldValue)
);
}
}