feat: swagger文档和字段调整

main
itao 1 year ago
parent 9f9cc51bbe
commit bd66213130

@ -7,7 +7,7 @@ import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
@SpringBootApplication
@SpringBootApplication(scanBasePackages = "com.luoo")
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@ -19,7 +19,7 @@ public class TagApplication {
}
@Bean
public IdWorker idWorkker(){
public IdWorker idWorker(){
return new IdWorker(1, 1);
}

@ -24,7 +24,7 @@ public class BaseExceptionHandler {
@ExceptionHandler(value = BizException.class)
@ResponseBody
public Result error(BizException e){
log.info("业务错误", e);
return new Result(false, StatusCode.ERROR, "业务错误");
log.info("业务错误:{}", e.getMessage());
return new Result(false, StatusCode.ERROR, e.getMessage());
}
}

@ -39,10 +39,8 @@ public class TagController {
}
)
@GetMapping("/list")
public ResultVO<Page<TagDTO>> getById(@Validated @RequestBody TagQueryReq queryReq,
@RequestParam int pageNum,
@RequestParam int pageSize){
return ResultVO.success(tagService.queryPage(queryReq, pageNum, pageSize));
public ResultVO<Page<TagDTO>> getById(@Validated TagQueryReq queryReq){
return ResultVO.success(tagService.queryPage(queryReq));
}
@Operation(

@ -4,12 +4,10 @@ import com.luoo.tag.pojo.Tag;
import com.luoo.tag.pojo.TagStatistic;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Map;
/**
* 访
@ -19,7 +17,7 @@ import java.util.Map;
public interface TagDao extends JpaRepository<Tag, String>, JpaSpecificationExecutor<Tag>{
@Query(value = "select distinct id from tb_tag where name_ch = ? or name_en = ?", nativeQuery = true)
List<Tag> findByName(String nameCh, String nameEn);
List<Tag> findByTagName(String nameCh, String nameEn);
@Modifying
@Query(value = "update tb_tag set state = ? where id = ?", nativeQuery = true)
@ -29,9 +27,6 @@ public interface TagDao extends JpaRepository<Tag, String>, JpaSpecificationExec
@Query(value = "update tb_tag set state = ? where parent_id = ?", nativeQuery = true)
void updateStateByParentId(Integer state, String parentId);
@Query(value = "select count(*) from tb_tag where parent_id = ?", nativeQuery = true)
long countByParentId(String parentId);
@Query(value = "select new com.luoo.tag.pojo.TagStatistic(parent_id, count(*)) from tb_tag where parent_id in ? group by parent_id", nativeQuery = true)
@Query(value = "select new com.luoo.tag.pojo.TagStatistic(parentId, count(*)) from Tag where parentId in :parentIds group by parentId")
List<TagStatistic> countByParentIds(List<String> parentIds);
}

@ -14,11 +14,8 @@ import java.io.Serializable;
public class TagCreateReq implements Serializable {
private static final long serialVersionUID = -3431829126574541980L;
/**
* ID
*/
@Schema(description = "父标签ID")
private String parentId = "";
private String parentId;
@Schema(description = "标签中文名")
@NotBlank(message = "标签中文名称必填")

@ -19,7 +19,7 @@ public class TagDTO implements Serializable {
private String parentId;
@Schema(description = "父标签名称")
private String parentNameCh;
private String parentNameCh = "";
@Schema(description = "标签中文名称")
private String nameCh;

@ -40,4 +40,10 @@ public class TagQueryReq implements Serializable {
@Schema(description = "创建时间-结束")
private LocalDateTime createTimeEnd;
@Schema(description = "分页: 页码", defaultValue = "1")
private int pageNum = 1;
@Schema(description = "分页: 每页数量", defaultValue = "10")
private int pageSize = 10;
}

@ -29,13 +29,13 @@ public class TagStatistic implements Serializable {
private Long childTagCount = 0L;
/**
*
*
*/
@Builder.Default
private Long articleRefCount = 0L;
private Long columnRefCount = 0L;
/**
*
*
*/
@Builder.Default
private Long songRefCount = 0L;
@ -43,10 +43,15 @@ public class TagStatistic implements Serializable {
public TagStatistic(String tagId){
this.tagId = tagId;
this.childTagCount = 0L;
this.columnRefCount = 0L;
this.songRefCount = 0L;
}
public TagStatistic(String tagId, Long childTagCount){
this.tagId = tagId;
this.childTagCount = childTagCount;
this.columnRefCount = 0L;
this.songRefCount = 0L;
}
}

@ -1,6 +1,7 @@
package com.luoo.tag.service;
import com.google.common.collect.Lists;
import org.springframework.transaction.annotation.Transactional;
import util.AssertUtil;
import com.luoo.tag.dao.TagDao;
import com.luoo.tag.pojo.*;
@ -34,43 +35,59 @@ public class TagService {
*
* @return
*/
public Page<TagDTO> queryPage(TagQueryReq queryReq, int pageNum, int pageSize){
public Page<TagDTO> queryPage(TagQueryReq queryReq){
Specification<Tag> specification = createSpecification(queryReq);
PageRequest pageRequest = PageRequest.of(pageNum-1, pageSize);
PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize());
Page<Tag> tagPage = tagDao.findAll(specification, pageRequest);
List<String> tagIdList = tagPage.stream().map(Tag::getId).collect(toList());
List<String> parentIdList = tagPage.stream().map(Tag::getParentId)
.filter(StringUtils::isNotBlank)
.distinct().collect(toList());
Map<String, Tag> parentTagMap = queryTagMap(parentIdList);
Map<String, TagStatistic> tagStatisticMap = queryTagStatistic(tagIdList);
return tagPage.map(tag -> {
TagDTO tagDTO = new TagDTO();
BeanUtils.copyProperties(tag, tagDTO);
TagStatistic tagStatistic = tagStatisticMap.get(tag.getId());
tagDTO.setChildTagCount(tagStatistic.getChildTagCount());
tagDTO.setArticleRefCount(tagStatistic.getArticleRefCount());
tagDTO.setArticleRefCount(tagStatistic.getColumnRefCount());
tagDTO.setSongRefCount(tagStatistic.getSongRefCount());
//todo parentTagName
Tag parentTag = parentTagMap.get(tagDTO.getParentId());
if(Objects.nonNull(parentTag)){
tagDTO.setParentNameCh(parentTag.getNameCh());
}
return tagDTO;
});
}
private Map<String, Tag> queryTagMap(List<String> idList){
List<Tag> tagList = tagDao.findAllById(idList);
return tagList.stream().collect(toMap(Tag::getId, Function.identity()));
}
/**
*
* @param createReq
*/
public void create(TagCreateReq createReq){
List<Tag> tagListByName = tagDao.findByName(createReq.getNameCh(), createReq.getNameEn());
List<Tag> tagListByName = tagDao.findByTagName(createReq.getNameCh(), createReq.getNameEn());
AssertUtil.mustEmpty(tagListByName, "标签中英文名称不允许重复");
String parentId = createReq.getParentId();
Optional<Tag> tagOptional = tagDao.findById(parentId);
AssertUtil.mustTrue(tagOptional.isPresent(), "父标签记录不存在");
if(StringUtils.isNotBlank(parentId)){
Optional<Tag> tagOptional = tagDao.findById(parentId);
AssertUtil.mustTrue(tagOptional.isPresent(), "父标签记录不存在");
}else{
createReq.setParentId(StringUtils.EMPTY);
}
Tag tag = new Tag();
BeanUtils.copyProperties(createReq, tag);
tag.setId(idWorker.nextId()+"");
tag.setState(1);
LocalDateTime now = LocalDateTime.now();
tag.setCreateTime(now);
tag.setCreateTime(now);
tag.setUpdateTime(now);
tagDao.save(tag);
}
@ -83,12 +100,14 @@ public class TagService {
Optional<Tag> tagOptional = tagDao.findById(id);
AssertUtil.mustTrue(tagOptional.isPresent(), "标签记录不存在");
List<Tag> tagListByName = tagDao.findByName(updateReq.getNameCh(), updateReq.getNameEn());
List<Tag> tagListByName = tagDao.findByTagName(updateReq.getNameCh(), updateReq.getNameEn());
boolean nameDuplicateCheck = tagListByName.stream().map(Tag::getId).allMatch(id::equals);
AssertUtil.mustTrue(nameDuplicateCheck, "标签中英文名称不允许重复");
Tag tag = new Tag();
BeanUtils.copyProperties(updateReq, tag);
Tag tag = tagOptional.get();
tag.setNameCh(updateReq.getNameCh());
tag.setNameEn(updateReq.getNameEn());
tag.setUpdateTime(LocalDateTime.now());
tagDao.save(tag);
}
@ -104,9 +123,9 @@ public class TagService {
TagDTO tagDTO = new TagDTO();
BeanUtils.copyProperties(tag, tagDTO);
TagStatistic tagStatistic = queryTagStatistic(id);
TagStatistic tagStatistic = queryTagStatistic(tag.getId());
tagDTO.setChildTagCount(tagStatistic.getChildTagCount());
tagDTO.setArticleRefCount(tagStatistic.getArticleRefCount());
tagDTO.setArticleRefCount(tagStatistic.getColumnRefCount());
tagDTO.setSongRefCount(tagStatistic.getSongRefCount());
return tagDTO;
}
@ -116,7 +135,9 @@ public class TagService {
* @param id ID
* @param state
*/
@Transactional(rollbackFor = Exception.class)
public void updateState(String id, Integer state){
LocalDateTime now = LocalDateTime.now();
tagDao.updateStateById(state, id);
// 级联更新子标签状态
tagDao.updateStateByParentId(state, id);
@ -132,8 +153,8 @@ public class TagService {
return;
}
TagStatistic referenceStatistic = queryTagStatistic(id);
AssertUtil.mustEq(referenceStatistic.getArticleRefCount(), 0L,"无法执行删除, 该标签还存在关联的期刊");
TagStatistic referenceStatistic = queryTagStatistic(tagOptional.get().getId());
AssertUtil.mustEq(referenceStatistic.getColumnRefCount(), 0L,"无法执行删除, 该标签还存在关联的期刊");
AssertUtil.mustEq(referenceStatistic.getSongRefCount(), 0L,"无法执行删除, 该标签还存在关联的音乐");
AssertUtil.mustEq(referenceStatistic.getChildTagCount(), 0L,"无法执行删除, 该标签还存在关联的子标签");
tagDao.deleteById(id);
@ -176,32 +197,32 @@ public class TagService {
List<Predicate> predicateList = new ArrayList<>();
String tagName = queryReq.getName();
if(StringUtils.isNotBlank(tagName)){
Predicate tagNameChPredicate = criteriaBuilder.like(root.get("name_ch"), '%'+tagName+'%');
Predicate tagNameEnPredicate = criteriaBuilder.like(root.get("name_en"), '%'+tagName+'%');
Predicate tagNameChPredicate = criteriaBuilder.like(root.get("nameCh"), '%'+tagName+'%');
Predicate tagNameEnPredicate = criteriaBuilder.like(root.get("nameEn"), '%'+tagName+'%');
predicateList.add(criteriaBuilder.or(tagNameChPredicate, tagNameEnPredicate));
}
if(Objects.nonNull(queryReq.getState())){
predicateList.add(criteriaBuilder.equal(root.get("state").as(Integer.class), queryReq.getState()));
}
if(Objects.nonNull(queryReq.getCreatorId())){
predicateList.add(criteriaBuilder.equal(root.get("creator_id"), queryReq.getCreatorId()));
predicateList.add(criteriaBuilder.equal(root.get("creatorId"), queryReq.getCreatorId()));
}
if(Objects.nonNull(queryReq.getId())){
if(StringUtils.isNotBlank(queryReq.getId())){
predicateList.add(criteriaBuilder.equal(root.get("id"), queryReq.getId()));
}
Integer level = queryReq.getLevel();
if (level.compareTo(1) == 0){
predicateList.add(criteriaBuilder.equal(root.get("parent_id"), ""));
predicateList.add(criteriaBuilder.equal(root.get("parentId"), ""));
}
if(level.compareTo(2) == 0 && StringUtils.isNotBlank(queryReq.getParentId())){
predicateList.add(criteriaBuilder.equal(root.get("parent_id"), queryReq.getParentId()));
predicateList.add(criteriaBuilder.equal(root.get("parentId"), queryReq.getParentId()));
}
if(Objects.nonNull(queryReq.getCreateTimeStart())){
predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("create_time").as(LocalDateTime.class),
predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(LocalDateTime.class),
queryReq.getCreateTimeStart()));
}
if(Objects.nonNull(queryReq.getCreateTimeEnd())){
predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("create_time").as(LocalDateTime.class),
predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(LocalDateTime.class),
queryReq.getCreateTimeEnd()));
}
Predicate[] predicates = new Predicate[predicateList.size()];

@ -4,14 +4,14 @@ use indie_tag;
-- 创建表
drop table if exists indie_tag.tb_tag;
create table indie_tag.tb_tag (
id bigint unsigned not null primary key auto_increment comment '主键',
parent bigint unsigned not null default 0 comment '父类主键(0表示一级标签)',
name_ch varchar(100) not null default '' comment '标签中文名称',
name_en varchar(100) not null default '' comment '标签英文名称',
status tinyint not null default 1 comment '状态(0=禁用,1=启用)',
creator_id int default 0 comment '创建人用户ID',
create_time datetime default current_timestamp comment '创建时间',
update_time datetime default current_timestamp on update current_timestamp comment '更新时间',
key idx_update_time(update_time),
key idx_parent(parent)
id varchar(100) not null primary key comment '标签ID',
parent_id varchar(100) not null default '' comment '父标签ID(一级标签的父标签ID=空字符串)',
name_ch varchar(100) not null default '' comment '标签中文名称',
name_en varchar(100) not null default '' comment '标签英文名称',
state tinyint not null default 1 comment '状态(0=禁用,1=启用)',
creator_id int default 0 comment '创建人用户ID',
create_time datetime default current_timestamp comment '创建时间',
update_time datetime default current_timestamp on update current_timestamp comment '更新时间',
key idx_update_time(update_time),
key idx_parent_id(parent_id)
) character set utf8mb4 comment '标签表';
Loading…
Cancel
Save