From 427fc9e4d17a9b524e2ad41dd9774a8b1fb14dbb Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 6 May 2024 19:02:45 +0800 Subject: [PATCH 1/7] 1.fix empty list in cache --- .../luoo/music/service/JournalService.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index 60e971c..b0062e1 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -1,6 +1,7 @@ package com.luoo.music.service; import java.util.*; +import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.persistence.criteria.CriteriaBuilder; @@ -14,6 +15,8 @@ import constants.Constants; import enums.JournalPublishEnum; import enums.JournalStatusEnum; import net.oschina.j2cache.CacheChannel; +import net.oschina.j2cache.CacheObject; + import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; @@ -354,18 +357,33 @@ public class JournalService { return journalSongDao.orderByJournalIdField(jouranlIds); } - @SuppressWarnings("unchecked") public List queryJournalByPage(JournalQueryReq queryReq) { if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) { - return (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTOWithComment(queryReq) , false).getValue() ; + return queryJournalByPage(queryReq, key -> getJournalRespDTOWithComment(queryReq)); } - List list= (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTO(queryReq) , false).getValue() ; + List list= queryJournalByPage(queryReq,key -> getJournalRespDTO(queryReq)) ; list.parallelStream().forEach(j->{ updateComment(j); }); return list; } + @SuppressWarnings("unchecked") + private List queryJournalByPage(JournalQueryReq queryReq, + Function> function) { + CacheObject cacheObject=this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString()); + if(null==cacheObject||null==cacheObject.getValue()) { + List result = function.apply(queryReq); + if(CollectionUtils.isEmpty(result)) { + return Collections.emptyList(); + } + this.cacheChannel.set(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), result); + return result; + } + return (List) cacheObject.getValue(); + } + + private void updateComment(JournalRespDTO journalRespDTO) { CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, redisTemplate,userClient,cacheChannel); From 456c87d1581d1d0378962d3e81c00649c0c764ec Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 8 May 2024 11:06:02 +0800 Subject: [PATCH 2/7] 1.add more info for error log --- .../controller/BaseExceptionHandler.java | 16 +++++ .../AbstractBaseExceptionHandler.java | 58 +++++++++++++++++++ .../controller/BaseExceptionHandler.java | 26 ++------- .../user/controller/BaseExceptionHandler.java | 28 ++------- 4 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java create mode 100644 luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java new file mode 100644 index 0000000..196e251 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java @@ -0,0 +1,16 @@ +package com.luoo.comment.controller; + +import api.StatusCode; +import controller.AbstractBaseExceptionHandler; +import org.springframework.web.bind.annotation.ControllerAdvice; + +/** + * 统一异常处理类 + */ +@ControllerAdvice +public class BaseExceptionHandler extends AbstractBaseExceptionHandler { + @Override + protected StatusCode getCommonFailedStatusCode() { + return StatusCode.COMMENT_COMMON_FAILED; + } +} diff --git a/luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java b/luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java new file mode 100644 index 0000000..02aec9a --- /dev/null +++ b/luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java @@ -0,0 +1,58 @@ +package controller; + +import java.util.Arrays; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import api.Result; +import api.StatusCode; +import dto.UserLoginDto; +import exception.BizException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public abstract class AbstractBaseExceptionHandler extends BaseController { + @ExceptionHandler(value = Exception.class) + @ResponseBody + public Result error(Exception e, HttpServletRequest request) { + log.error("{}user: {} 请求错误,请求地址: {}, 请求参数: {}, 错误信息: ", getDeviceType(request.getHeader("device_type")), + getUserId(request.getHeader("Authorization")), request.getRequestURL(), + getRequestParameter(request.getParameterMap()), e); + if (e instanceof BizException) { + BizException biz = (BizException) e; + StatusCode statusCode = null == biz.getCodeEnum() ? getCommonFailedStatusCode() : biz.getCodeEnum(); + return Result.failed(statusCode, biz.getMessage()); + } + return Result.failed(getCommonFailedStatusCode(), e.getMessage()); + } + + protected abstract StatusCode getCommonFailedStatusCode(); + + private String getRequestParameter(Map parameterMap) { + if (CollectionUtils.isEmpty(parameterMap)) { + return ""; + } + return parameterMap.entrySet().stream().map(this::getParameter).collect(Collectors.joining("|")); + } + + private String getParameter(Entry e) { + return e.getKey() + ":" + + (null == e.getValue() ? "" : Arrays.stream(e.getValue()).collect(Collectors.joining(","))); + } + + private String getDeviceType(String deviceType) { + return null == deviceType ? "" : (deviceType + " "); + } + + private String getUserId(String authorization) { + UserLoginDto userLoginDto = getUserLoginDto(authorization); + return null == userLoginDto ? "" : (userLoginDto.getUserId() + "_" + userLoginDto.getNickName()); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java index d34876c..75bf1a6 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java @@ -1,32 +1,16 @@ package com.luoo.music.controller; -import api.Result; import api.StatusCode; -import exception.BizException; -import lombok.extern.slf4j.Slf4j; - +import controller.AbstractBaseExceptionHandler; import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; /** * 统一异常处理类 */ -@Slf4j @ControllerAdvice -public class BaseExceptionHandler { - @ExceptionHandler(value = Exception.class) - @ResponseBody - public Result error(Exception e) { - log.error("执行出错", e); - return Result.failed(StatusCode.MUSIC_COMMON_FAILED); - } - - @ExceptionHandler(value = BizException.class) - @ResponseBody - public Result error(BizException e) { - log.info("业务错误:{}", e.getMessage()); - StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum(); - return Result.failed(statusCode, e.getMessage()); +public class BaseExceptionHandler extends AbstractBaseExceptionHandler { + @Override + protected StatusCode getCommonFailedStatusCode() { + return StatusCode.MUSIC_COMMON_FAILED; } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java b/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java index 08f4aef..97e8cbd 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java @@ -1,32 +1,16 @@ package com.luoo.user.controller; -import exception.BizException; -import api.Result; import api.StatusCode; -import lombok.extern.slf4j.Slf4j; +import controller.AbstractBaseExceptionHandler; import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; /** * 统一异常处理类 */ -@Slf4j @ControllerAdvice -public class BaseExceptionHandler { - - @ExceptionHandler(value = Exception.class) - @ResponseBody - public Result error(Exception e) { - log.error("执行出错", e); - return Result.failed(StatusCode.USER_COMMON_FAILED, e.getMessage()); - } - - @ExceptionHandler(value = BizException.class) - @ResponseBody - public Result error(BizException e) { - log.info("业务错误:{}", e.getMessage()); - StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.USER_COMMON_FAILED : e.getCodeEnum(); - return Result.failed(statusCode, e.getMessage()); +public class BaseExceptionHandler extends AbstractBaseExceptionHandler { + @Override + protected StatusCode getCommonFailedStatusCode() { + return StatusCode.USER_COMMON_FAILED; } -} +} \ No newline at end of file From 6554b871e723115547e7c3313ccf3adfc83a27c6 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 8 May 2024 11:18:29 +0800 Subject: [PATCH 3/7] 1.fix sql issue --- .../src/main/java/com/luoo/music/service/JournalService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index b0062e1..deed7cf 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -352,9 +352,9 @@ public class JournalService { return journalDao.orderByField(recommendIds); } - public List orderByJournalIdField(List jouranlIds) { - return journalSongDao.orderByJournalIdField(jouranlIds); + return CollectionUtils.isEmpty(jouranlIds) ? Collections.emptyList() + : journalSongDao.orderByJournalIdField(jouranlIds); } public List queryJournalByPage(JournalQueryReq queryReq) { From e414ad8b12c3f9c7c4561995ee798cc16e568a4a Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 8 May 2024 11:30:43 +0800 Subject: [PATCH 4/7] =?UTF-8?q?1.add=20'=E9=BB=91=E6=9A=97'=20for=20split?= =?UTF-8?q?=20word?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- luoo_music/src/main/resources/ext.dic | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/luoo_music/src/main/resources/ext.dic b/luoo_music/src/main/resources/ext.dic index 1fb130f..ee3c2e8 100644 --- a/luoo_music/src/main/resources/ext.dic +++ b/luoo_music/src/main/resources/ext.dic @@ -2,4 +2,5 @@ 诛仙2 梦幻诛仙 梦幻诛仙2 -大罗法咒 \ No newline at end of file +大罗法咒 +黑暗 \ No newline at end of file From a7fd80ab9750e2e340b44f8c6e220e5c92d9b846 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 10 May 2024 14:13:31 +0800 Subject: [PATCH 5/7] 1.add journal filter by tag --- .../src/main/java/com/luoo/music/dao/JournalTagDao.java | 2 ++ .../java/com/luoo/music/dto/request/JournalQueryReq.java | 5 ++++- .../src/main/java/com/luoo/music/service/JournalService.java | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java index 52f352d..e80742c 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java @@ -32,6 +32,8 @@ public interface JournalTagDao extends JpaRepository, JpaSpec @Query(value = "select journal_id FROM tb_journal_tag where tag_id=?1 or tag_id in (select id from tb_tag_info where parent_id=?1)", nativeQuery = true) List getJournalIdByTagId(String tagId); + @Query(value = "select tb_journal_tag.journal_id FROM tb_journal_tag, tb_tag_info where tb_tag_info.name_ch=?1 and (tb_journal_tag.tag_id=tb_tag_info.id or tb_journal_tag.tag_id in (select tb_tag_info.id from tb_tag_info where tb_tag_info.parent_id=tb_tag_info.id))", nativeQuery = true) + List getJournalIdByTagName(String tag); @Query(value = "select journal_id from tb_journal_tag where tag_id in (select tag_id from tb_journal_tag where journal_id=?1) and not journal_id=?1 order by rand() limit ?2", nativeQuery = true) List recommendByJournalId(String journalId, Integer limit); diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java index 1032948..c5bd2dc 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java @@ -18,6 +18,8 @@ public class JournalQueryReq implements Serializable { private static final long serialVersionUID = -1198060864891902188L; @ApiModelProperty(value = "筛选条件:类目Id", example = "民谣/国语 id") private String categoryId; + @ApiModelProperty(value = "筛选条件:期刊标签", example = "民谣/国语") + private String tag; @ApiModelProperty(value = "筛选条件:期刊号范围", example = "900~800") private String journalNoRange; @ApiModelProperty(value = "分页: 页码,以1开始,即获取最新的几期", example = "1") @@ -31,8 +33,9 @@ public class JournalQueryReq implements Serializable { } public String countString() { - return getString(categoryId) + "_" + getString(journalNoRange); + return getString(categoryId)+ "_" + getString(tag) + "_" + getString(journalNoRange); } + private String getString(String value) { return StringTools.isEmpty(value) ? null : value; } diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index deed7cf..6d09047 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -228,6 +228,9 @@ public class JournalService { if(StringUtils.isNotBlank(queryReq.getCategoryId())){ return journalTagDao.getJournalIdByTagId(queryReq.getCategoryId()); } + if(StringUtils.isNotBlank(queryReq.getTag())){ + return journalTagDao.getJournalIdByTagName(queryReq.getTag()); + } return Collections.emptyList(); } From 7cf67658d3c38d9a572eb2d74d307235952f3612 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 10 May 2024 17:32:07 +0800 Subject: [PATCH 6/7] 1.add tag infos in jouranl response --- .../luoo/music/config/SaticScheduleTask.java | 1 + .../music/controller/JournalController.java | 10 ++-- .../music/controller/SearchController.java | 5 +- .../com/luoo/music/dao/JournalTagDao.java | 3 - .../main/java/com/luoo/music/dao/TagDao.java | 3 + .../luoo/music/dto/mapper/JournalMapper.java | 56 +++++++++++++++++-- .../music/dto/request/JournalQueryReq.java | 4 +- .../music/dto/response/JournalRespDTO.java | 2 + .../luoo/music/service/JournalService.java | 16 +++--- 9 files changed, 74 insertions(+), 26 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java b/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java index 65f955a..4dcac37 100644 --- a/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java +++ b/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java @@ -124,6 +124,7 @@ public class SaticScheduleTask { private void updateJouranlTag(Journal journal) { List tags = tagDao.getTagNameChByJournalId(journal.getId()); if (!tags.isEmpty()) { + cacheChannel.clear("tag"); String tagStr = tags.stream().distinct().sorted().collect(Collectors.joining(Constants.COMMA)); if (!tagStr.equals(journal.getNameChTags())) { journal.setNameChTags(tagStr); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 0a55915..99d800a 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -12,7 +12,6 @@ import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.SongRespDTO; import io.swagger.annotations.*; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.redis.core.RedisTemplate; @@ -50,6 +49,9 @@ public class JournalController { @Autowired private JournalService journalService; + + @Autowired + private JournalMapper journalMapper; @Autowired private SongController songController; @@ -124,7 +126,7 @@ public class JournalController { List pageList = journalService.orderByField(objectIds); UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Set journalCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds); - List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)) + List list = pageList.stream().map(a -> journalMapper.getJournalRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); // list = list.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList()); return Result.success(new PageResult(count, list)); @@ -278,7 +280,7 @@ public class JournalController { List ids=journals.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); - List list = journals.stream().sorted(Comparator.comparing(Journal::getUserCollectCount).reversed()).map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)) + List list = journals.stream().sorted(Comparator.comparing(Journal::getUserCollectCount).reversed()).map(a -> journalMapper.getJournalRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); return Result.success(list); } @@ -288,7 +290,7 @@ public class JournalController { @GlobalInterceptor public Result> recommend() { List journals = journalService.recommend(); - List list = journals.stream().sorted(Comparator.comparing(Journal::getUserCollectCount).reversed()).map(a -> JournalMapper.getJournalRespDTO(a)) + List list = journals.stream().sorted(Comparator.comparing(Journal::getUserCollectCount).reversed()).map(a -> journalMapper.getJournalRespDTO(a)) .collect(Collectors.toList()); return Result.success(list); } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index 05fb856..e4cb7e7 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -43,6 +43,9 @@ public class SearchController { @Autowired private JournalService journalService; + @Autowired + private JournalMapper journalMapper; + @Autowired private JournalSongService journalSongService; @@ -89,7 +92,7 @@ public class SearchController { @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); List pageResults=journalService.fuzzySearch(query.getKeyword(),pageRequest); - List list = pageResults.stream().map(JournalMapper::getJournalRespDTO) + List list = pageResults.stream().map(journalMapper::getJournalRespDTO) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java index e80742c..19704f4 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java @@ -32,9 +32,6 @@ public interface JournalTagDao extends JpaRepository, JpaSpec @Query(value = "select journal_id FROM tb_journal_tag where tag_id=?1 or tag_id in (select id from tb_tag_info where parent_id=?1)", nativeQuery = true) List getJournalIdByTagId(String tagId); - @Query(value = "select tb_journal_tag.journal_id FROM tb_journal_tag, tb_tag_info where tb_tag_info.name_ch=?1 and (tb_journal_tag.tag_id=tb_tag_info.id or tb_journal_tag.tag_id in (select tb_tag_info.id from tb_tag_info where tb_tag_info.parent_id=tb_tag_info.id))", nativeQuery = true) - List getJournalIdByTagName(String tag); - @Query(value = "select journal_id from tb_journal_tag where tag_id in (select tag_id from tb_journal_tag where journal_id=?1) and not journal_id=?1 order by rand() limit ?2", nativeQuery = true) List recommendByJournalId(String journalId, Integer limit); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java index 77dca1a..0ae5238 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java @@ -33,4 +33,7 @@ public interface TagDao extends JpaRepository, JpaSpecificationExec @Query(value = "select name_ch from tb_journal_tag,tb_tag_info where journal_id = ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true) List getTagNameChByJournalId(String journalId); + + @Query(value = "select * from tb_tag_info where name_ch=?1 limit 1", nativeQuery = true) + Tag findByTagName(String tagName); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java index a6f3dd1..04f1396 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java @@ -10,31 +10,60 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import com.luoo.music.client.UserClient; import net.oschina.j2cache.CacheChannel; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; import com.apifan.common.random.RandomSource; import com.apifan.common.random.entity.Poem; import com.luoo.music.dao.CommentDao; +import com.luoo.music.dao.TagDao; import com.luoo.music.dto.response.CommentDTO; +import com.luoo.music.dto.response.JournalFilterDTO; import com.luoo.music.dto.response.JournalRespDTO; +import com.luoo.music.dto.response.TagDTO; import com.luoo.music.pojo.Journal; +import com.luoo.music.pojo.Tag; +import com.luoo.music.service.TagService; + import constants.Constants; import enums.DateTimePatternEnum; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + import util.DateUtil; import util.StringTools; - +@Component public class JournalMapper { - public static JournalRespDTO getJournalRespDTO(Journal journal) { + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private CacheChannel cacheChannel; + + @Autowired + private MongoTemplate mongoTemplate; + + @Autowired + private CommentDao commentDao; + + @Autowired + private TagDao tagDao; + + @Autowired + private UserClient userClient; + + public JournalRespDTO getJournalRespDTO(Journal journal) { return getJournalRespDTO(journal, Collections.emptySet()); } - public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { + public JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(journal.getId()); journalRespDTO.setJournalNo(journal.getJournalNo()); @@ -48,6 +77,8 @@ public class JournalMapper { String nameChTags=journal.getNameChTags(); if(!StringTools.isEmpty(nameChTags)) { List tags=Arrays.stream(nameChTags.split(",")).collect(Collectors.toList()); + List tagInfos = tags.stream().map(this::getTagDto).filter(Objects::nonNull).collect(Collectors.toList()); + journalRespDTO.setTagInfos(tagInfos); journalRespDTO.setTags(tags); } journalRespDTO.setContent(journal.getContent()); @@ -56,9 +87,22 @@ public class JournalMapper { return journalRespDTO; } + private TagDTO getTagDto(String tagName) { + return (TagDTO) this.cacheChannel.get("tag", tagName, key -> getTagDtoByName(tagName), false).getValue(); + + } + private TagDTO getTagDtoByName(String tagName) { + Tag tag=tagDao.findByTagName(tagName); + if(null==tag) { + return null; + } + TagDTO tagDTO=new TagDTO(); + tagDTO.setName(tag.getNameCh()); + tagDTO.setId(tag.getId()); + return tagDTO; + } - public static JournalRespDTO getJournalRespDTO(Journal journal, - MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, UserClient userClient, CacheChannel cacheChannel) { + public JournalRespDTO getJournalRespDTOWithComment(Journal journal) { JournalRespDTO journalRespDTO = getJournalRespDTO(journal); CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, redisTemplate,userClient,cacheChannel); @@ -70,7 +114,7 @@ public class JournalMapper { } - private static String getPublishDate(Journal journal) { + private String getPublishDate(Journal journal) { LocalDateTime date = null == journal.getPubTime() ? journal.getCreateTime() : journal.getPubTime(); return DateUtil.format(date, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern()); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java index c5bd2dc..372f0fa 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java @@ -18,8 +18,6 @@ public class JournalQueryReq implements Serializable { private static final long serialVersionUID = -1198060864891902188L; @ApiModelProperty(value = "筛选条件:类目Id", example = "民谣/国语 id") private String categoryId; - @ApiModelProperty(value = "筛选条件:期刊标签", example = "民谣/国语") - private String tag; @ApiModelProperty(value = "筛选条件:期刊号范围", example = "900~800") private String journalNoRange; @ApiModelProperty(value = "分页: 页码,以1开始,即获取最新的几期", example = "1") @@ -33,7 +31,7 @@ public class JournalQueryReq implements Serializable { } public String countString() { - return getString(categoryId)+ "_" + getString(tag) + "_" + getString(journalNoRange); + return getString(categoryId) + "_" + getString(journalNoRange); } private String getString(String value) { diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java index bc50964..e3d9aa8 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java @@ -20,6 +20,8 @@ public class JournalRespDTO implements Serializable { private String image; @ApiModelProperty(value = "期刊标签") private List tags; + @ApiModelProperty(value = "期刊标签信息") + private List tagInfos; @ApiModelProperty(value = "概要") private String summary; @ApiModelProperty(value = "文案") diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index 6d09047..17af6d3 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -60,6 +60,9 @@ public class JournalService { @Autowired private JournalSongDao journalSongDao; + @Autowired + private JournalMapper journalMapper; + @Autowired private JournalTagDao journalTagDao; @Autowired @@ -228,14 +231,9 @@ public class JournalService { if(StringUtils.isNotBlank(queryReq.getCategoryId())){ return journalTagDao.getJournalIdByTagId(queryReq.getCategoryId()); } - if(StringUtils.isNotBlank(queryReq.getTag())){ - return journalTagDao.getJournalIdByTagName(queryReq.getTag()); - } return Collections.emptyList(); } - - public boolean isLatest10ByJournalNo(String journalNo) { return null!=journalDao.isLatest10ByJournalNo(journalNo); } @@ -399,12 +397,12 @@ public class JournalService { private List getJournalRespDTO(JournalQueryReq queryReq) { List pageList = queryPage(queryReq); return pageList.stream().map(a -> - JournalMapper.getJournalRespDTO(a)).collect(Collectors.toList()); + journalMapper.getJournalRespDTO(a)).collect(Collectors.toList()); } private List getJournalRespDTOWithComment(JournalQueryReq queryReq) { List pageList = queryPage(queryReq); - return pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient,cacheChannel)).collect(Collectors.toList()); + return pageList.stream().map(journal -> journalMapper.getJournalRespDTOWithComment(journal)).collect(Collectors.toList()); } @@ -418,7 +416,7 @@ public class JournalService { if(null==journal) { return null; } - return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient,cacheChannel); + return journalMapper.getJournalRespDTOWithComment(journal); } public JournalRespDTO queryJournalByJournalNo(String journalNo) { @@ -430,7 +428,7 @@ public class JournalService { if(null==journal) { return null; } - return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient,cacheChannel); + return journalMapper.getJournalRespDTOWithComment(journal); } public JournalFilterDTO getJournalFilterDTO() { From 2bae4136746c65763ebba9b8ef19ead37571fbdd Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 10 May 2024 17:48:50 +0800 Subject: [PATCH 7/7] 1.do not clean tag cache --- .../src/main/java/com/luoo/music/config/SaticScheduleTask.java | 1 - 1 file changed, 1 deletion(-) diff --git a/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java b/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java index 4dcac37..65f955a 100644 --- a/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java +++ b/luoo_music/src/main/java/com/luoo/music/config/SaticScheduleTask.java @@ -124,7 +124,6 @@ public class SaticScheduleTask { private void updateJouranlTag(Journal journal) { List tags = tagDao.getTagNameChByJournalId(journal.getId()); if (!tags.isEmpty()) { - cacheChannel.clear("tag"); String tagStr = tags.stream().distinct().sorted().collect(Collectors.joining(Constants.COMMA)); if (!tagStr.equals(journal.getNameChTags())) { journal.setNameChTags(tagStr);