fix: query and update bug, change page response

main
itao 10 months ago
parent d390e45f14
commit c044745bc0

@ -1,19 +1,19 @@
package com.luoo.tag.controller;
import api.PageResult;
import api.Result;
import com.luoo.tag.pojo.TagCreateReq;
import com.luoo.tag.pojo.TagDTO;
import com.luoo.tag.pojo.TagQueryReq;
import com.luoo.tag.pojo.TagUpdateReq;
import com.luoo.tag.service.TagService;
import api.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
* Web
@ -27,12 +27,8 @@ public class TagController {
private final TagService tagService;
@ApiOperation(value = "查询标签列表", notes = "查询标签列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "queryReq", value = "查询参数", required = true),
@ApiImplicitParam(name = "pageNum", value = "页码", required = true),
@ApiImplicitParam(name = "pageSize", value = "每页数", required = true) })
@GetMapping("/list")
public Result<Page<TagDTO>> getById(@Validated TagQueryReq queryReq) {
public Result<PageResult<TagDTO>> list(@Validated TagQueryReq queryReq) {
return Result.success(tagService.queryPage(queryReq));
}

@ -20,12 +20,14 @@ public interface TagDao extends JpaRepository<Tag, String>, JpaSpecificationExec
List<Tag> findByTagName(String nameCh, String nameEn);
@Modifying
@Query(value = "update tb_tag set state = ? where id = ?", nativeQuery = true)
void updateStateById(Integer state, String id);
@Query(value = "update Tag set state=:#{#updateTag.state}, updateTime=:#{#updateTag.updateTime}, " +
"updaterId=:#{#updateTag.updaterId} where id=:#{#updateTag.id}")
void updateStateById(Tag updateTag);
@Modifying
@Query(value = "update tb_tag set state = ? where parent_id = ?", nativeQuery = true)
void updateStateByParentId(Integer state, String parentId);
@Query(value = "update Tag set state=:#{#updateTag.state}, updateTime=:#{#updateTag.updateTime}, " +
"updaterId=:#{#updateTag.updaterId} where parentId=:#{#updateTag.id}")
void updateStateByParentId(Tag updateTag);
@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);

@ -0,0 +1,29 @@
package com.luoo.tag.enums;
import lombok.Getter;
/**
*
*/
@Getter
public enum TagLevelEnum {
L1(1, "一级"),
L2(2, "二级");
private final Integer code;
private final String desc;
TagLevelEnum(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public static TagLevelEnum getByCode(Integer code) {
for (TagLevelEnum tagLevelEnum : TagLevelEnum.values()) {
if (tagLevelEnum.getCode().equals(code)) {
return tagLevelEnum;
}
}
return null;
}
}

@ -0,0 +1,30 @@
package com.luoo.tag.enums;
import lombok.Getter;
/**
*
*/
@Getter
public enum TagStateEnum {
DISABLE(0, "禁用"),
ENABLE(1, "启用");
private final Integer code;
private final String desc;
TagStateEnum(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public static TagStateEnum getByCode(Integer code) {
for (TagStateEnum tagStateEnum : TagStateEnum.values()) {
if (tagStateEnum.getCode().equals(code)) {
return tagStateEnum;
}
}
return null;
}
}

@ -26,7 +26,12 @@ public class Tag implements Serializable {
private String id;
/**
* ID
* (1=2=)
*/
private Integer level;
/**
* ID(level=2, )
*/
private String parentId;
@ -48,7 +53,7 @@ public class Tag implements Serializable {
/**
* ID
*/
private Long creatorId;
private String creatorId;
/**
*
@ -56,6 +61,11 @@ public class Tag implements Serializable {
@CreatedDate
private LocalDateTime createTime;
/**
* ID
*/
private String updaterId;
/**
*
*/

@ -1,9 +1,11 @@
package com.luoo.tag.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@ -27,7 +29,7 @@ public class TagQueryReq implements Serializable {
@ApiModelProperty(value = "父标签ID")
private String parentId;
@ApiModelProperty(value = "标签名称")
@ApiModelProperty(value = "标签中英文名称")
private String name;
@ApiModelProperty(value = "标签状态 0=禁用1=启用")
@ -37,9 +39,13 @@ public class TagQueryReq implements Serializable {
private String creatorId;
@ApiModelProperty(value = "创建时间-开始")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime createTimeStart;
@ApiModelProperty(value = "创建时间-结束")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime createTimeEnd;
@ApiModelProperty(value = "分页: 页码", example = "1")

@ -1,7 +1,12 @@
package com.luoo.tag.service;
import api.PageResult;
import com.google.common.collect.Lists;
import com.luoo.tag.config.RequestContext;
import com.luoo.tag.enums.TagLevelEnum;
import com.luoo.tag.enums.TagStateEnum;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import util.AssertUtil;
import com.luoo.tag.dao.TagDao;
import com.luoo.tag.pojo.*;
@ -33,9 +38,9 @@ public class TagService {
/**
*
* @return
* @return PageResult<TagDTO>
*/
public Page<TagDTO> queryPage(TagQueryReq queryReq){
public PageResult<TagDTO> queryPage(TagQueryReq queryReq){
Specification<Tag> specification = createSpecification(queryReq);
PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize());
@ -46,7 +51,7 @@ public class TagService {
.distinct().collect(toList());
Map<String, Tag> parentTagMap = queryTagMap(parentIdList);
Map<String, TagStatistic> tagStatisticMap = queryTagStatistic(tagIdList);
return tagPage.map(tag -> {
Page<TagDTO> pageDTOPage = tagPage.map(tag -> {
TagDTO tagDTO = new TagDTO();
BeanUtils.copyProperties(tag, tagDTO);
TagStatistic tagStatistic = tagStatisticMap.get(tag.getId());
@ -54,16 +59,12 @@ public class TagService {
tagDTO.setArticleRefCount(tagStatistic.getColumnRefCount());
tagDTO.setSongRefCount(tagStatistic.getSongRefCount());
Tag parentTag = parentTagMap.get(tagDTO.getParentId());
if(Objects.nonNull(parentTag)){
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()));
return new PageResult<>(pageDTOPage.getTotalElements(), pageDTOPage.getContent());
}
/**
@ -81,13 +82,19 @@ public class TagService {
}else{
createReq.setParentId(StringUtils.EMPTY);
}
Tag tag = new Tag();
BeanUtils.copyProperties(createReq, tag);
tag.setId(idWorker.nextId()+"");
tag.setState(1);
tag.setLevel(StringUtils.isBlank(parentId) ? TagLevelEnum.L1.getCode() : TagLevelEnum.L2.getCode());
tag.setState(TagStateEnum.ENABLE.getCode());
LocalDateTime now = LocalDateTime.now();
tag.setCreateTime(now);
tag.setUpdateTime(now);
UserInfo userInfo = RequestContext.get();
tag.setCreatorId(userInfo.getId());
tag.setUpdaterId(userInfo.getId());
tagDao.save(tag);
}
@ -108,6 +115,7 @@ public class TagService {
tag.setNameCh(updateReq.getNameCh());
tag.setNameEn(updateReq.getNameEn());
tag.setUpdateTime(LocalDateTime.now());
tag.setUpdaterId(RequestContext.get().getId());
tagDao.save(tag);
}
@ -137,10 +145,14 @@ public class TagService {
*/
@Transactional(rollbackFor = Exception.class)
public void updateState(String id, Integer state){
LocalDateTime now = LocalDateTime.now();
tagDao.updateStateById(state, id);
Tag updateTag = new Tag();
updateTag.setId(id);
updateTag.setState(state);
updateTag.setUpdaterId(RequestContext.get().getId());
updateTag.setUpdateTime(LocalDateTime.now());
tagDao.updateStateById(updateTag);
// 级联更新子标签状态
tagDao.updateStateByParentId(state, id);
tagDao.updateStateByParentId(updateTag);
}
/**
@ -160,6 +172,16 @@ public class TagService {
tagDao.deleteById(id);
}
/**
* ID
* @param idList ID
* @return Map
*/
private Map<String, Tag> queryTagMap(List<String> idList){
List<Tag> tagList = tagDao.findAllById(idList);
return tagList.stream().collect(toMap(Tag::getId, Function.identity()));
}
/**
*
@ -179,7 +201,7 @@ public class TagService {
private Map<String, TagStatistic> queryTagStatistic(List<String> tagIdList){
Map<String, TagStatistic> tagStatisticMap = tagIdList.stream()
.collect(toMap(Function.identity(), TagStatistic::new));
List<TagStatistic> childTagStatisticList = tagDao.countByParentIds(tagIdList);
List<TagStatistic> childTagStatisticList = queryChildTagStatistic(tagIdList);
childTagStatisticList.forEach(childTagStatistic -> {
tagStatisticMap.get(childTagStatistic.getTagId()).setChildTagCount(childTagStatistic.getChildTagCount());
});
@ -187,6 +209,24 @@ public class TagService {
return tagStatisticMap;
}
private List<TagStatistic> queryChildTagStatistic(List<String> tagIdList){
if(CollectionUtils.isEmpty(tagIdList)){
return Collections.emptyList();
}
List<TagStatistic> childTagStatisticList = tagDao.countByParentIds(tagIdList);
Map<String, TagStatistic> childTagStatisticMap = childTagStatisticList.stream()
.collect(toMap(TagStatistic::getTagId, Function.identity()));
return tagIdList.stream().map(tagId -> {
TagStatistic tagStatistic = childTagStatisticMap.get(tagId);
if(Objects.isNull(tagStatistic)){
tagStatistic = new TagStatistic();
tagStatistic.setTagId(tagId);
tagStatistic.setChildTagCount(0L);
}
return tagStatistic;
}).collect(toList());
}
/**
*
* @param queryReq
@ -204,17 +244,17 @@ public class TagService {
if(Objects.nonNull(queryReq.getState())){
predicateList.add(criteriaBuilder.equal(root.get("state").as(Integer.class), queryReq.getState()));
}
if(Objects.nonNull(queryReq.getCreatorId())){
if(StringUtils.isNotBlank(queryReq.getCreatorId())){
predicateList.add(criteriaBuilder.equal(root.get("creatorId"), queryReq.getCreatorId()));
}
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("parentId"), ""));
if(Objects.nonNull(level)){
predicateList.add(criteriaBuilder.equal(root.get("level"), level));
}
if(level.compareTo(2) == 0 && StringUtils.isNotBlank(queryReq.getParentId())){
if(StringUtils.isNotBlank(queryReq.getParentId())){
predicateList.add(criteriaBuilder.equal(root.get("parentId"), queryReq.getParentId()));
}
if(Objects.nonNull(queryReq.getCreateTimeStart())){

Loading…
Cancel
Save