feat: redis点赞数据同步数据库
This commit is contained in:
@@ -4,10 +4,13 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.landaiqing.subject.application.convert.SubjectLikedDTOConverter;
|
||||
import com.landaiqing.subject.application.dto.SubjectLikedDTO;
|
||||
import com.landaiqing.subject.common.entity.PageResult;
|
||||
import com.landaiqing.subject.common.entity.Result;
|
||||
import com.landaiqing.subject.common.util.LoginUtil;
|
||||
import com.landaiqing.subject.domain.entity.SubjectLikedBO;
|
||||
import com.landaiqing.subject.domain.service.SubjectLikedDomainService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -38,17 +41,15 @@ public class SubjectLikedController {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(subjectLikedDTO));
|
||||
}
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getId(), "不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getSubjectId(), "不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getLikeUserId(), "不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getStatus(), "不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getCreatedBy(), "创建人不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getCreatedTime(), "创建时间不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getUpdateBy(), "更新人不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getUpdateTime(), "更新时间不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getIsDeleted(), "不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getSubjectId(), "题目id不能为空");
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getStatus(), "点赞状态 1点赞 0不点赞 不能为空");
|
||||
String loginId = LoginUtil.getLoginId();
|
||||
subjectLikedDTO.setLikeUserId(loginId);
|
||||
Preconditions.checkNotNull(subjectLikedDTO.getLikeUserId(), "点赞人不能为空");
|
||||
|
||||
SubjectLikedBO SubjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);
|
||||
return Result.ok(subjectLikedDomainService.add(SubjectLikedBO));
|
||||
subjectLikedDomainService.add(SubjectLikedBO);
|
||||
return Result.ok(true);
|
||||
} catch (Exception e) {
|
||||
log.error("SubjectLikedController.register.error:{}", e.getMessage(), e);
|
||||
return Result.fail("新增题目点赞表失败");
|
||||
@@ -112,4 +113,24 @@ public class SubjectLikedController {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询我的点赞列表
|
||||
*/
|
||||
@PostMapping("/getSubjectLikedPage")
|
||||
public Result<PageResult<SubjectLikedDTO>> getSubjectLikedPage(@RequestBody SubjectLikedDTO subjectLikedDTO) {
|
||||
try {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("SubjectController.getSubjectLikedPage.dto:{}", JSON.toJSONString(subjectLikedDTO));
|
||||
}
|
||||
SubjectLikedBO subjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);
|
||||
subjectLikedBO.setPageNo(subjectLikedDTO.getPageNo());
|
||||
subjectLikedBO.setPageSize(subjectLikedDTO.getPageSize());
|
||||
PageResult<SubjectLikedBO> boPageResult = subjectLikedDomainService.getSubjectLikedPage(subjectLikedBO);
|
||||
return Result.ok(boPageResult);
|
||||
} catch (Exception e) {
|
||||
log.error("SubjectCategoryController.getSubjectLikedPage.error:{}", e.getMessage(), e);
|
||||
return Result.fail("分页查询我的点赞失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,9 +2,12 @@ package com.landaiqing.subject.application.convert;
|
||||
|
||||
import com.landaiqing.subject.application.dto.SubjectLikedDTO;
|
||||
import com.landaiqing.subject.domain.entity.SubjectLikedBO;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectLiked;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 题目点赞表 dto转换器
|
||||
*
|
||||
@@ -17,5 +20,6 @@ public interface SubjectLikedDTOConverter {
|
||||
SubjectLikedDTOConverter INSTANCE = Mappers.getMapper(SubjectLikedDTOConverter.class);
|
||||
|
||||
SubjectLikedBO convertDTOToBO(SubjectLikedDTO subjectLikedDTO);
|
||||
List<SubjectLikedBO> convertListInfoToBO(List<SubjectLiked> subjectLikedList);
|
||||
|
||||
}
|
||||
|
@@ -85,6 +85,23 @@ public class SubjectInfoDTO extends PageInfo implements Serializable {
|
||||
* 题目数量
|
||||
*/
|
||||
private Integer subjectCount;
|
||||
/**
|
||||
* 是否被当前用户点赞
|
||||
*/
|
||||
private Boolean liked;
|
||||
|
||||
/**
|
||||
* 当前题目点赞的数量
|
||||
*/
|
||||
private Integer likedCount;
|
||||
/**
|
||||
* 下一题
|
||||
*/
|
||||
private Long nextSubjectId;
|
||||
/**
|
||||
* 上一题
|
||||
*/
|
||||
private Long lastSubjectId;
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.landaiqing.subject.application.dto;
|
||||
|
||||
import com.landaiqing.subject.common.entity.PageInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -12,7 +13,7 @@ import java.util.Date;
|
||||
* @since 2024-03-08 13:44:59
|
||||
*/
|
||||
@Data
|
||||
public class SubjectLikedDTO implements Serializable {
|
||||
public class SubjectLikedDTO extends PageInfo implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
|
@@ -62,5 +62,15 @@
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.27</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -0,0 +1,78 @@
|
||||
package com.landaiqing.subject.domain.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* xxl-job config
|
||||
*
|
||||
* @author landaiqing
|
||||
*/
|
||||
@Configuration
|
||||
public class XxlJobConfig {
|
||||
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
|
||||
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${xxl.job.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${xxl.job.executor.appname}")
|
||||
private String appname;
|
||||
|
||||
@Value("${xxl.job.executor.address}")
|
||||
private String address;
|
||||
|
||||
@Value("${xxl.job.executor.ip}")
|
||||
private String ip;
|
||||
|
||||
@Value("${xxl.job.executor.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${xxl.job.executor.logpath}")
|
||||
private String logPath;
|
||||
|
||||
@Value("${xxl.job.executor.logretentiondays}")
|
||||
private int logRetentionDays;
|
||||
|
||||
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
logger.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobSpringExecutor.setAppname(appname);
|
||||
xxlJobSpringExecutor.setAddress(address);
|
||||
xxlJobSpringExecutor.setIp(ip);
|
||||
xxlJobSpringExecutor.setPort(port);
|
||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||
xxlJobSpringExecutor.setLogPath(logPath);
|
||||
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
|
||||
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
|
||||
*
|
||||
* 1、引入依赖:
|
||||
* <dependency>
|
||||
* <groupId>org.springframework.cloud</groupId>
|
||||
* <artifactId>spring-cloud-commons</artifactId>
|
||||
* <version>${version}</version>
|
||||
* </dependency>
|
||||
*
|
||||
* 2、配置文件,或者容器启动变量
|
||||
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
|
||||
*
|
||||
* 3、获取IP
|
||||
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
|
||||
*/
|
||||
|
||||
|
||||
}
|
@@ -5,6 +5,8 @@ import com.landaiqing.subject.infra.basic.entity.SubjectLiked;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 题目点赞表 bo转换器
|
||||
*
|
||||
@@ -18,4 +20,7 @@ public interface SubjectLikedBOConverter {
|
||||
|
||||
SubjectLiked convertBOToEntity(SubjectLikedBO subjectLikedBO);
|
||||
|
||||
List<SubjectLikedBO> convertListInfoToBO(List<SubjectLiked> subjectLikedList);
|
||||
|
||||
|
||||
}
|
||||
|
@@ -88,6 +88,24 @@ public class SubjectInfoBO extends PageInfo implements Serializable {
|
||||
*/
|
||||
private Integer subjectCount;
|
||||
|
||||
/**
|
||||
* 是否被当前用户点赞
|
||||
*/
|
||||
private Boolean liked;
|
||||
|
||||
/**
|
||||
* 当前题目点赞的数量
|
||||
*/
|
||||
private Integer likedCount;
|
||||
|
||||
/**
|
||||
* 下一题
|
||||
*/
|
||||
private Long nextSubjectId;
|
||||
/**
|
||||
* 上一题
|
||||
*/
|
||||
private Long lastSubjectId;
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.landaiqing.subject.domain.entity;
|
||||
|
||||
import com.landaiqing.subject.common.entity.PageInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -12,7 +13,7 @@ import java.util.Date;
|
||||
* @since 2024-03-08 13:44:59
|
||||
*/
|
||||
@Data
|
||||
public class SubjectLikedBO implements Serializable {
|
||||
public class SubjectLikedBO extends PageInfo implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
|
@@ -0,0 +1,38 @@
|
||||
package com.landaiqing.subject.domain.job;
|
||||
|
||||
import com.landaiqing.subject.domain.service.SubjectLikedDomainService;
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 同步点赞数据
|
||||
*
|
||||
* @author landaiqing
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SyncLikedJob {
|
||||
@Resource
|
||||
private SubjectLikedDomainService subjectLikedDomainService;
|
||||
|
||||
|
||||
/**
|
||||
* 同步点赞任务
|
||||
*/
|
||||
@XxlJob("syncLikedJobHandler")
|
||||
public void syncLikedJobHandler() throws Exception {
|
||||
XxlJobHelper.log("syncLikedJobHandler.starter");
|
||||
try {
|
||||
subjectLikedDomainService.syncLiked();
|
||||
} catch (Exception e) {
|
||||
XxlJobHelper.log("syncLikedJobHandler.error", e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,11 +1,15 @@
|
||||
package com.landaiqing.subject.domain.redis;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.Cursor;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -105,10 +109,33 @@ public class RedisUtil {
|
||||
}
|
||||
|
||||
|
||||
public Set rankWithScore(String key,long start,long end){
|
||||
public Set rankWithScore(String key, long start, long end) {
|
||||
Set<ZSetOperations.TypedTuple<String>> set = redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
|
||||
return set;
|
||||
}
|
||||
|
||||
|
||||
public void putHash(String key, String hashKey, Object hashVal) {
|
||||
redisTemplate.opsForHash().put(key, hashKey, hashVal);
|
||||
}
|
||||
|
||||
public Integer getInt(String key) {
|
||||
return (Integer) redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public void increment(String key, Integer count) {
|
||||
redisTemplate.opsForValue().increment(key,count);
|
||||
}
|
||||
public Map<Object, Object> getHashAndDelete(String key) {
|
||||
Map<Object, Object> map = new HashMap<>();
|
||||
Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(key, ScanOptions.NONE);
|
||||
while (cursor.hasNext()) {
|
||||
Map.Entry<Object, Object> entry = cursor.next();
|
||||
Object hashKey = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
map.put(hashKey, value);
|
||||
redisTemplate.opsForHash().delete(key, hashKey);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.landaiqing.subject.domain.service;
|
||||
|
||||
import com.landaiqing.subject.common.entity.PageResult;
|
||||
import com.landaiqing.subject.domain.entity.SubjectLikedBO;
|
||||
|
||||
|
||||
@@ -14,7 +15,17 @@ public interface SubjectLikedDomainService {
|
||||
/**
|
||||
* 添加 题目点赞表 信息
|
||||
*/
|
||||
Boolean add(SubjectLikedBO subjectLikedBO);
|
||||
void add(SubjectLikedBO subjectLikedBO);
|
||||
|
||||
/**
|
||||
* 获取当前是否被点赞过
|
||||
*/
|
||||
Boolean isLiked(String subjectId, String userId);
|
||||
|
||||
/**
|
||||
* 获取点赞数量
|
||||
*/
|
||||
Integer getLikedCount(String subjectId);
|
||||
|
||||
/**
|
||||
* 更新 题目点赞表 信息
|
||||
@@ -25,5 +36,10 @@ public interface SubjectLikedDomainService {
|
||||
* 删除 题目点赞表 信息
|
||||
*/
|
||||
Boolean delete(SubjectLikedBO subjectLikedBO);
|
||||
/**
|
||||
* 同步点赞数据
|
||||
*/
|
||||
void syncLiked();
|
||||
|
||||
PageResult<SubjectLikedBO> getSubjectLikedPage(SubjectLikedBO subjectLikedBO);
|
||||
}
|
||||
|
@@ -12,6 +12,7 @@ import com.landaiqing.subject.domain.handler.subject.SubjectTypeHandler;
|
||||
import com.landaiqing.subject.domain.handler.subject.SubjectTypeHandlerFactory;
|
||||
import com.landaiqing.subject.domain.redis.RedisUtil;
|
||||
import com.landaiqing.subject.domain.service.SubjectInfoDomainService;
|
||||
import com.landaiqing.subject.domain.service.SubjectLikedDomainService;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectInfo;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectInfoEs;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectLabel;
|
||||
@@ -54,6 +55,9 @@ public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private SubjectLikedDomainService subjectLikedDomainService;
|
||||
|
||||
private static final String RANK_KEY = "subject_rank";
|
||||
|
||||
|
||||
@@ -148,6 +152,8 @@ public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
|
||||
List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
|
||||
List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
|
||||
bo.setLabelName(labelNameList);
|
||||
bo.setLiked(subjectLikedDomainService.isLiked(subjectInfoBO.getId().toString(), LoginUtil.getLoginId()));
|
||||
bo.setLikedCount(subjectLikedDomainService.getLikedCount(subjectInfoBO.getId().toString()));
|
||||
return bo;
|
||||
}
|
||||
|
||||
|
@@ -1,15 +1,25 @@
|
||||
package com.landaiqing.subject.domain.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.landaiqing.subject.common.entity.PageResult;
|
||||
import com.landaiqing.subject.common.enums.IsDeletedFlagEnum;
|
||||
import com.landaiqing.subject.common.enums.SubjectLikedStatusEnum;
|
||||
import com.landaiqing.subject.common.util.LoginUtil;
|
||||
import com.landaiqing.subject.domain.convert.SubjectLikedBOConverter;
|
||||
import com.landaiqing.subject.domain.entity.SubjectLikedBO;
|
||||
import com.landaiqing.subject.domain.redis.RedisUtil;
|
||||
import com.landaiqing.subject.domain.service.SubjectLikedDomainService;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectLiked;
|
||||
import com.landaiqing.subject.infra.basic.service.SubjectLikedService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 题目点赞表 领域service实现了
|
||||
@@ -23,12 +33,53 @@ public class SubjectLikedDomainServiceImpl implements SubjectLikedDomainService
|
||||
|
||||
@Resource
|
||||
private SubjectLikedService subjectLikedService;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
private static final String SUBJECT_LIKED_KEY = "subject.liked";
|
||||
private static final String SUBJECT_LIKED_COUNT_KEY = "subject.liked.count";
|
||||
private static final String SUBJECT_LIKED_DETAIL_KEY = "subject.liked.detail";
|
||||
|
||||
@Override
|
||||
public Boolean add(SubjectLikedBO subjectLikedBO) {
|
||||
SubjectLiked subjectLiked = SubjectLikedBOConverter.INSTANCE.convertBOToEntity(subjectLikedBO);
|
||||
subjectLiked.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
|
||||
return subjectLikedService.insert(subjectLiked) > 0;
|
||||
public void add(SubjectLikedBO subjectLikedBO) {
|
||||
Long subjectId = subjectLikedBO.getSubjectId();
|
||||
String likeUserId = subjectLikedBO.getLikeUserId();
|
||||
Integer status = subjectLikedBO.getStatus();
|
||||
String hashKey = buildSubjectLikedKey(subjectId.toString(), likeUserId);
|
||||
redisUtil.putHash(SUBJECT_LIKED_KEY, hashKey, status);
|
||||
String countKey = SUBJECT_LIKED_COUNT_KEY + "." + subjectId;
|
||||
String detailKey = SUBJECT_LIKED_DETAIL_KEY + "." + subjectId + "." + likeUserId;
|
||||
if (SubjectLikedStatusEnum.LIKED.getCode() == status) {
|
||||
redisUtil.increment(countKey, 1);
|
||||
redisUtil.set(detailKey, "1");
|
||||
} else {
|
||||
Integer count = redisUtil.getInt(countKey);
|
||||
if (Objects.isNull(count) || count <= 0) {
|
||||
return;
|
||||
}
|
||||
redisUtil.increment(countKey, -1);
|
||||
redisUtil.del(detailKey);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isLiked(String subjectId, String userId) {
|
||||
String detailKey = SUBJECT_LIKED_DETAIL_KEY + "." + subjectId + "." + userId;
|
||||
return redisUtil.exist(detailKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getLikedCount(String subjectId) {
|
||||
String countKey = SUBJECT_LIKED_COUNT_KEY + "." + subjectId;
|
||||
Integer count = redisUtil.getInt(countKey);
|
||||
if (Objects.isNull(count) || count <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return redisUtil.getInt(countKey);
|
||||
}
|
||||
|
||||
private String buildSubjectLikedKey(String subjectId, String userId) {
|
||||
return subjectId + ":" + userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,4 +96,52 @@ public class SubjectLikedDomainServiceImpl implements SubjectLikedDomainService
|
||||
return subjectLikedService.update(subjectLiked) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void syncLiked() {
|
||||
Map<Object, Object> subjectLikedMap = redisUtil.getHashAndDelete(SUBJECT_LIKED_KEY);
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("syncLiked.subjectLikedMap:{}", JSON.toJSONString(subjectLikedMap));
|
||||
}
|
||||
if (MapUtils.isEmpty(subjectLikedMap)) {
|
||||
return;
|
||||
}
|
||||
//批量同步到数据库
|
||||
List<SubjectLiked> subjectLikedList = new LinkedList<>();
|
||||
subjectLikedMap.forEach((key, val) -> {
|
||||
SubjectLiked subjectLiked = new SubjectLiked();
|
||||
String[] keyArr = key.toString().split(":");
|
||||
String subjectId = keyArr[0];
|
||||
String likedUser = keyArr[1];
|
||||
subjectLiked.setSubjectId(Long.valueOf(subjectId));
|
||||
subjectLiked.setLikeUserId(likedUser);
|
||||
subjectLiked.setStatus(Integer.valueOf(val.toString()));
|
||||
subjectLiked.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
|
||||
subjectLikedList.add(subjectLiked);
|
||||
});
|
||||
subjectLikedService.batchInsert(subjectLikedList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SubjectLikedBO> getSubjectLikedPage(SubjectLikedBO subjectLikedBO) {
|
||||
PageResult<SubjectLikedBO> pageResult = new PageResult<>();
|
||||
pageResult.setPageNo(subjectLikedBO.getPageNo());
|
||||
pageResult.setPageSize(subjectLikedBO.getPageSize());
|
||||
int start = (subjectLikedBO.getPageNo() - 1) * subjectLikedBO.getPageSize();
|
||||
SubjectLiked subjectLiked = SubjectLikedBOConverter.INSTANCE.convertBOToEntity(subjectLikedBO);
|
||||
subjectLiked.setLikeUserId(LoginUtil.getLoginId());
|
||||
int count = subjectLikedService.countByCondition(subjectLiked);
|
||||
if (count == 0) {
|
||||
return pageResult;
|
||||
}
|
||||
List<SubjectLiked> subjectLikedList = subjectLikedService.queryPage(subjectLiked, start,
|
||||
subjectLikedBO.getPageSize());
|
||||
List<SubjectLikedBO> subjectInfoBOS = SubjectLikedBOConverter.INSTANCE.convertListInfoToBO(subjectLikedList);
|
||||
|
||||
|
||||
|
||||
pageResult.setRecords(subjectInfoBOS);
|
||||
pageResult.setTotal(count);
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,8 +2,11 @@ package com.landaiqing.subject.infra.basic.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectLiked;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 题目点赞表 表数据库访问层
|
||||
*
|
||||
@@ -13,5 +16,12 @@ import org.springframework.stereotype.Repository;
|
||||
@Repository
|
||||
public interface SubjectLikedDao extends BaseMapper<SubjectLiked> {
|
||||
|
||||
void insertBatch(@Param("entities") List<SubjectLiked> subjectLikedList);
|
||||
|
||||
int countByCondition(SubjectLiked subjectLiked);
|
||||
|
||||
List<SubjectLiked> queryPage(@Param("entity") SubjectLiked subjectLiked,
|
||||
@Param("start") int start,
|
||||
@Param("pageSize") Integer pageSize);
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,8 @@ package com.landaiqing.subject.infra.basic.service;
|
||||
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectLiked;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 题目点赞表 表服务接口
|
||||
@@ -48,4 +50,10 @@ public interface SubjectLikedService {
|
||||
*/
|
||||
SubjectLiked queryByCondition(SubjectLiked subjectLiked);
|
||||
|
||||
void batchInsert(List<SubjectLiked> subjectLikedList);
|
||||
|
||||
|
||||
int countByCondition(SubjectLiked subjectLiked);
|
||||
|
||||
List<SubjectLiked> queryPage(SubjectLiked subjectLiked, int start, Integer pageSize);
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import com.landaiqing.subject.infra.basic.service.SubjectLikedService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -90,4 +91,19 @@ public class SubjectLikedServiceImpl implements SubjectLikedService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void batchInsert(List<SubjectLiked> subjectLikedList) {
|
||||
this.subjectLikedDao.insertBatch(subjectLikedList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countByCondition(SubjectLiked subjectLiked) {
|
||||
return this.subjectLikedDao.countByCondition(subjectLiked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SubjectLiked> queryPage(SubjectLiked subjectLiked, int start, Integer pageSize) {
|
||||
return this.subjectLikedDao.queryPage(subjectLiked, start, pageSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -13,5 +13,35 @@
|
||||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
|
||||
<result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
|
||||
</resultMap>
|
||||
<insert id="insertBatch">
|
||||
insert into subject_liked(id, subject_id, like_user_id, status, created_by, created_time, update_by,
|
||||
update_time, is_deleted)
|
||||
values
|
||||
<foreach collection="entities" item="entity" separator=",">
|
||||
(#{entity.id},
|
||||
#{entity.subjectId},
|
||||
#{entity.likeUserId},
|
||||
#{entity.status},
|
||||
#{entity.createdBy},
|
||||
#{entity.createdTime},
|
||||
#{entity.updateBy}, #{entity.updateTime}, #{entity.isDeleted})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<select id="countByCondition" resultType="java.lang.Integer">
|
||||
select count(1)
|
||||
from subject_liked
|
||||
where like_user_id = #{likeUserId} and status = 1
|
||||
and is_deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="queryPage" resultType="com.landaiqing.subject.infra.basic.entity.SubjectLiked">
|
||||
select *
|
||||
from subject_liked
|
||||
where like_user_id = #{entity.likeUserId}
|
||||
and is_deleted = 0 and status = 1
|
||||
limit #{start}
|
||||
, #{pageSize}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
@@ -57,5 +57,17 @@ es:
|
||||
es-cluster-configs[0]:
|
||||
name: 5fdd534ef33c
|
||||
nodes: 116.196.80.239:9200
|
||||
xxl:
|
||||
job:
|
||||
admin:
|
||||
addresses: http://127.0.0.1:8080/xxl-job-admin
|
||||
accessToken: default_token
|
||||
executor:
|
||||
appname: qing-yu-club-subject
|
||||
address:
|
||||
ip: 127.0.0.1
|
||||
port: 9999
|
||||
logpath: /data/applogs/xxl-job/jobhandler
|
||||
logretentiondays: 30
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user