|
|
|
@ -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()];
|
|
|
|
|