feat: 数据字典相关接口提交

This commit is contained in:
2025-12-01 21:05:19 +08:00
parent 991f6850c7
commit f56b903bcc
33 changed files with 391 additions and 508 deletions

View File

@ -0,0 +1,6 @@
package day.gitlab.dolphin.core.constants;
public class Exceptions {
public static final String FIELD_REQUIRED = "00020101";
}

View File

@ -1,18 +1,15 @@
package day.gitlab.dolphin.core.controller;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import day.gitlab.dolphin.core.entity.Dictionary;
import day.gitlab.dolphin.common.core.entity.PageResponse;
import day.gitlab.dolphin.common.core.entity.QueryPageRequest;
import day.gitlab.dolphin.common.core.entity.Result;
import day.gitlab.dolphin.common.security.annotation.AuthorityCheck;
import day.gitlab.dolphin.common.security.annotation.AuthorityType;
import day.gitlab.dolphin.core.entity.dto.DictionaryDTO;
import day.gitlab.dolphin.core.entity.vo.DictionaryVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import day.gitlab.dolphin.core.service.DictionaryService;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 字典 控制层。
@ -24,72 +21,45 @@ import java.util.List;
@RequestMapping("/dictionary")
public class DictionaryController {
@Autowired
@Resource
private DictionaryService dictionaryService;
/**
* 保存字典。
*
* @param dictionary 字典
* @return {@code true} 保存成功,{@code false} 保存失败
*/
@PostMapping("save")
public boolean save(@RequestBody Dictionary dictionary) {
return dictionaryService.save(dictionary);
}
/**
* 根据主键删除字典。
*
* @param id 主键
* @return {@code true} 删除成功,{@code false} 删除失败
*/
@DeleteMapping("remove/{id}")
public boolean remove(@PathVariable String id) {
return dictionaryService.removeById(id);
}
/**
* 根据主键更新字典。
*
* @param dictionary 字典
* @return {@code true} 更新成功,{@code false} 更新失败
*/
@PutMapping("update")
public boolean update(@RequestBody Dictionary dictionary) {
return dictionaryService.updateById(dictionary);
}
/**
* 查询所有字典。
*
* @return 所有数据
*/
@GetMapping("list")
public List<Dictionary> list() {
return dictionaryService.list();
}
/**
* 根据主键获取字典。
*
* @param id 字典主键
* @return 字典详情
*/
@GetMapping("getInfo/{id}")
public Dictionary getInfo(@PathVariable String id) {
return dictionaryService.getById(id);
}
/**
* 分页查询字典。
*
* @param page 分页对象
* @param pageRequest 分页请求参数
* @return 分页对象
*/
@GetMapping("page")
public Page<Dictionary> page(Page<Dictionary> page) {
return dictionaryService.page(page);
@GetMapping("paginate")
public Result<PageResponse<DictionaryVO>> page(@RequestBody QueryPageRequest<DictionaryDTO> pageRequest) {
return Result.success(dictionaryService.page(pageRequest));
}
@PostMapping("add")
@AuthorityCheck(type = AuthorityType.OR, value = {"ROLE00001"})
public Result<Void> add(@RequestBody DictionaryDTO dictionaryDTO) {
dictionaryService.add(dictionaryDTO);
return Result.success();
}
@PostMapping("edit")
@AuthorityCheck(type = AuthorityType.OR, value = {"ROLE00001"})
public Result<Void> edit(@RequestBody DictionaryDTO dictionaryDTO) {
dictionaryService.edit(dictionaryDTO);
return Result.success();
}
@PostMapping("delete")
@AuthorityCheck(type = AuthorityType.OR, value = {"ROLE00001"})
public Result<Void> delete(@RequestBody DictionaryDTO dictionaryDTO) {
dictionaryService.delete(dictionaryDTO.getId());
return Result.success();
}
@PostMapping("deleteBatch")
@AuthorityCheck(type = AuthorityType.OR, value = {"ROLE00001"})
public Result<Void> deleteBatch(@RequestBody DictionaryDTO dictionaryDTO) {
dictionaryService.deleteBatch(dictionaryDTO.getIds());
return Result.success();
}
}

View File

@ -1,18 +1,7 @@
package day.gitlab.dolphin.core.controller;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import day.gitlab.dolphin.core.entity.DictionaryItem;
import day.gitlab.dolphin.core.service.DictionaryItemService;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 字典项 控制层。
@ -23,73 +12,4 @@ import java.util.List;
@RestController
@RequestMapping("/dictionaryItem")
public class DictionaryItemController {
@Autowired
private DictionaryItemService dictionaryItemService;
/**
* 保存字典项。
*
* @param dictionaryItem 字典项
* @return {@code true} 保存成功,{@code false} 保存失败
*/
@PostMapping("save")
public boolean save(@RequestBody DictionaryItem dictionaryItem) {
return dictionaryItemService.save(dictionaryItem);
}
/**
* 根据主键删除字典项。
*
* @param id 主键
* @return {@code true} 删除成功,{@code false} 删除失败
*/
@DeleteMapping("remove/{id}")
public boolean remove(@PathVariable String id) {
return dictionaryItemService.removeById(id);
}
/**
* 根据主键更新字典项。
*
* @param dictionaryItem 字典项
* @return {@code true} 更新成功,{@code false} 更新失败
*/
@PutMapping("update")
public boolean update(@RequestBody DictionaryItem dictionaryItem) {
return dictionaryItemService.updateById(dictionaryItem);
}
/**
* 查询所有字典项。
*
* @return 所有数据
*/
@GetMapping("list")
public List<DictionaryItem> list() {
return dictionaryItemService.list();
}
/**
* 根据主键获取字典项。
*
* @param id 字典项主键
* @return 字典项详情
*/
@GetMapping("getInfo/{id}")
public DictionaryItem getInfo(@PathVariable String id) {
return dictionaryItemService.getById(id);
}
/**
* 分页查询字典项。
*
* @param page 分页对象
* @return 分页对象
*/
@GetMapping("page")
public Page<DictionaryItem> page(Page<DictionaryItem> page) {
return dictionaryItemService.page(page);
}
}

View File

@ -1,6 +1,7 @@
package day.gitlab.dolphin.core.entity;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.sql.Timestamp;

View File

@ -0,0 +1,27 @@
package day.gitlab.dolphin.core.entity.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DictionaryDTO {
private String id;
private String name;
private String code;
private String type;
private String description;
private List<String> ids;
}

View File

@ -0,0 +1,27 @@
package day.gitlab.dolphin.core.entity.dto;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DictionarySearchParamsDTO {
@Min(1)
@NotNull
private Long current;
@Min(1)
@NotNull
private Long size;
private String name;
private String code;
private String type;
}

View File

@ -0,0 +1,29 @@
package day.gitlab.dolphin.core.entity.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DictionaryVO {
private String id;
private String name;
private String code;
private String type;
private String description;
private Timestamp createTime;
private Timestamp updateTime;
}

View File

@ -1,8 +1,14 @@
package day.gitlab.dolphin.core.mapper;
import com.mybatisflex.core.paginate.Page;
import day.gitlab.dolphin.common.core.entity.QueryPageRequest;
import day.gitlab.dolphin.common.mybatis.Pages;
import day.gitlab.dolphin.core.entity.dto.DictionaryDTO;
import day.gitlab.dolphin.core.entity.table.DictionaryTableDef;
import org.apache.ibatis.annotations.Mapper;
import com.mybatisflex.core.BaseMapper;
import day.gitlab.dolphin.core.entity.Dictionary;
import org.springframework.util.StringUtils;
/**
* 字典 映射层。
@ -13,4 +19,19 @@ import day.gitlab.dolphin.core.entity.Dictionary;
@Mapper
public interface DictionaryMapper extends BaseMapper<Dictionary> {
default Page<Dictionary> page(QueryPageRequest<DictionaryDTO> pageRequest) {
return Pages.paginate(this, pageRequest, (dict, wrapper) -> {
if (StringUtils.hasText(dict.getName())) {
wrapper.and(DictionaryTableDef.DICTIONARY.NAME.like(dict.getName()));
}
if (StringUtils.hasText(dict.getCode())) {
wrapper.and(DictionaryTableDef.DICTIONARY.CODE.like(dict.getCode()));
}
if (StringUtils.hasText(dict.getType())) {
wrapper.and(DictionaryTableDef.DICTIONARY.TYPE.eq(dict.getType()));
}
return wrapper;
});
}
}

View File

@ -1,14 +1,11 @@
package day.gitlab.dolphin.core.service;
import com.mybatisflex.core.service.IService;
import day.gitlab.dolphin.core.entity.DictionaryItem;
/**
* 字典项 服务层。
*
* @author jiangyc
* @since 2025-11-28
*/
public interface DictionaryItemService extends IService<DictionaryItem> {
public interface DictionaryItemService {
}

View File

@ -1,7 +1,11 @@
package day.gitlab.dolphin.core.service;
import com.mybatisflex.core.service.IService;
import day.gitlab.dolphin.core.entity.Dictionary;
import day.gitlab.dolphin.common.core.entity.PageResponse;
import day.gitlab.dolphin.common.core.entity.QueryPageRequest;
import day.gitlab.dolphin.core.entity.dto.DictionaryDTO;
import day.gitlab.dolphin.core.entity.vo.DictionaryVO;
import java.util.List;
/**
* 字典 服务层。
@ -9,6 +13,15 @@ import day.gitlab.dolphin.core.entity.Dictionary;
* @author jiangyc
* @since 2025-11-28
*/
public interface DictionaryService extends IService<Dictionary> {
public interface DictionaryService {
PageResponse<DictionaryVO> page(QueryPageRequest<DictionaryDTO> pageRequest);
void add(DictionaryDTO dictionaryDTO);
void edit(DictionaryDTO dictionaryDTO);
void delete(String id);
void deleteBatch(List<String> ids);
}

View File

@ -1,8 +1,5 @@
package day.gitlab.dolphin.core.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import day.gitlab.dolphin.core.entity.DictionaryItem;
import day.gitlab.dolphin.core.mapper.DictionaryItemMapper;
import day.gitlab.dolphin.core.service.DictionaryItemService;
import org.springframework.stereotype.Service;
@ -13,6 +10,6 @@ import org.springframework.stereotype.Service;
* @since 2025-11-28
*/
@Service
public class DictionaryItemServiceImpl extends ServiceImpl<DictionaryItemMapper, DictionaryItem> implements DictionaryItemService{
public class DictionaryItemServiceImpl implements DictionaryItemService {
}

View File

@ -1,10 +1,24 @@
package day.gitlab.dolphin.core.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mybatisflex.core.paginate.Page;
import day.gitlab.dolphin.common.core.entity.PageResponse;
import day.gitlab.dolphin.common.core.entity.QueryPageRequest;
import day.gitlab.dolphin.common.core.i18n.MessagesHelper;
import day.gitlab.dolphin.common.mybatis.Pages;
import day.gitlab.dolphin.core.constants.Exceptions;
import day.gitlab.dolphin.core.entity.Dictionary;
import day.gitlab.dolphin.core.entity.dto.DictionaryDTO;
import day.gitlab.dolphin.core.entity.vo.DictionaryVO;
import day.gitlab.dolphin.core.mapper.DictionaryMapper;
import day.gitlab.dolphin.core.service.DictionaryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* 字典 服务层实现。
@ -13,6 +27,86 @@ import org.springframework.stereotype.Service;
* @since 2025-11-28
*/
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService{
public class DictionaryServiceImpl implements DictionaryService {
@Resource
private DictionaryMapper dictionaryMapper;
@Resource
private MessagesHelper messagesHelper;
@Override
public PageResponse<DictionaryVO> page(QueryPageRequest<DictionaryDTO> pageRequest) {
Page<Dictionary> page = dictionaryMapper.page(pageRequest);
return Pages.toPageResponse(page, (rec) -> {
DictionaryVO vo = new DictionaryVO();
vo.setId(rec.getId());
vo.setName(rec.getName());
vo.setCode(rec.getCode());
vo.setType(rec.getType());
vo.setDescription(rec.getDescription());
vo.setCreateTime(rec.getCreateTime());
vo.setUpdateTime(rec.getUpdateTime());
return vo;
});
}
@Override
public void add(DictionaryDTO dictionaryDTO) {
validate(dictionaryDTO);
Dictionary dict = Dictionary.builder()
.name(dictionaryDTO.getName())
.code(dictionaryDTO.getCode())
.type(dictionaryDTO.getType())
.description(dictionaryDTO.getDescription())
.createTime(Timestamp.from(Instant.now()))
.updateTime(Timestamp.from(Instant.now()))
.build();
dictionaryMapper.insert(dict);
}
@Override
public void edit(DictionaryDTO dictionaryDTO) {
validate(dictionaryDTO);
Dictionary dict = Dictionary.builder()
.id(dictionaryDTO.getId())
.name(dictionaryDTO.getName())
.code(dictionaryDTO.getCode())
.type(dictionaryDTO.getType())
.description(dictionaryDTO.getDescription())
.updateTime(Timestamp.from(Instant.now()))
.build();
dictionaryMapper.update(dict);
}
@Override
public void delete(String id) {
dictionaryMapper.deleteById(id);
}
@Override
public void deleteBatch(List<String> ids) {
dictionaryMapper.deleteBatchByIds(ids);
}
private void validate(DictionaryDTO dictionaryDTO) {
List<String> fields = new ArrayList<>();
if (!StringUtils.hasText(dictionaryDTO.getName())) {
fields.add("name");
}
if (!StringUtils.hasText(dictionaryDTO.getCode())) {
fields.add("code");
}
if (!StringUtils.hasText(dictionaryDTO.getType())) {
fields.add("type");
}
if (!fields.isEmpty()) {
throw messagesHelper.newBusinessException(Exceptions.FIELD_REQUIRED, String.join(",", fields));
}
}
}