Commit f97bb099 f97bb099a2a5421194809d7a85e3f74bff4e1710 by xianghan

1.优化部分任务处理接口

1 parent 0f17a33a
package com.topdraw.business.module.coupon.service.impl;
import com.topdraw.business.module.coupon.domain.Coupon;
import com.topdraw.config.RedisKeyConstants;
import com.topdraw.exception.GlobeExceptionMsg;
import com.topdraw.utils.ValidationUtil;
import com.topdraw.business.module.coupon.repository.CouponRepository;
......@@ -8,6 +9,7 @@ import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.coupon.service.dto.CouponDTO;
import com.topdraw.business.module.coupon.service.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
......@@ -30,9 +32,9 @@ public class CouponServiceImpl implements CouponService {
private CouponRepository couponRepository;
@Override
@Cacheable(cacheNames = RedisKeyConstants.cacheCouponById, key = "#id", unless = "#result.id == null")
public CouponDTO findById(Long id) {
Assert.notNull(id, GlobeExceptionMsg.COUPON_ID_IS_NULL);
Coupon coupon = this.couponRepository.findById(id).orElseGet(Coupon::new);
// ValidationUtil.isNull(coupon.getId(),"Coupon","id",id);
return this.couponMapper.toDto(coupon);
......
......@@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
......@@ -55,6 +56,7 @@ public class MemberServiceImpl implements MemberService {
}
@Override
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO findByCode(String code) {
Member member = this.memberRepository.findFirstByCode(code).orElseGet(Member::new);
return this.memberMapper.toDto(member);
......@@ -92,12 +94,20 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO doUpdateMemberExpAndLevel(Member resource) {
try {
this.redisUtils.doLock(RedisKeyConstants.updateCacheMemberById + resource.getId());
Integer count = this.memberRepository.updateExpAndLevel(resource);
if (Objects.nonNull(count) && count > 0) {
Member member = this.memberRepository.findById(resource.getId()).orElseGet(Member::new);
return this.memberMapper.toDto(member);
Integer count = this.memberRepository.updateExpAndLevel(resource);
if (Objects.nonNull(count) && count > 0) {
Member member = this.memberRepository.findById(resource.getId()).orElseGet(Member::new);
return this.memberMapper.toDto(member);
}
} catch (Exception e) {
log.info("修改会员优惠券,"+e.getMessage());
} finally {
this.redisUtils.doUnLock(RedisKeyConstants.updateCacheMemberById + resource.getId());
}
return this.memberMapper.toDto(resource);
......@@ -105,6 +115,7 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO doUpdateMemberCoupon(Member resource) {
log.info("修改会员优惠券 =>> {}", resource);
try {
......@@ -134,6 +145,7 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO doUpdateMemberVipAndVipExpireTime(Member resource) {
log.info("修改会员vip和vip过期时间 ==>> {}", resource);
try {
......@@ -156,6 +168,7 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO doUpdateMemberUserIptvIdAndBindIptvPlatformAndBindIptvTime(Member resource) {
log.info("修改会员绑定大屏信息 ==>> {}", resource);
try {
......@@ -178,6 +191,7 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO doUpdateMemberAvatarUrlAndNicknameAndGender(Member resource) {
log.info("修改会员头像、昵称、性别 ==>> {}", resource);
try {
......@@ -218,6 +232,7 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO update(Member resources) {
log.info("修改会员信息 ==>> {}", resources);
try {
......@@ -246,12 +261,21 @@ public class MemberServiceImpl implements MemberService {
@Override
@Transactional(rollbackFor = Exception.class)
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#code")
public MemberDTO doUpdateMemberPoints(Member resources) {
try {
this.redisUtils.doLock(RedisKeyConstants.updateCacheMemberById + resources.getId());
Integer count = this.memberRepository.updatePointAndDuePoint(resources);
if (Objects.nonNull(count) && count > 0) {
Member member = this.memberRepository.findById(resources.getId()).orElseGet(Member::new);
return this.memberMapper.toDto(member);
Integer count = this.memberRepository.updatePointAndDuePoint(resources);
if (Objects.nonNull(count) && count > 0) {
Member member = this.memberRepository.findById(resources.getId()).orElseGet(Member::new);
return this.memberMapper.toDto(member);
}
} catch (Exception e) {
log.info(e.getMessage());
} finally {
this.redisUtils.doUnLock(RedisKeyConstants.updateCacheMemberById + resources.getId());
}
return this.memberMapper.toDto(resources);
......
package com.topdraw.business.module.rights.service.impl;
import com.topdraw.business.module.rights.domain.Rights;
import com.topdraw.config.RedisKeyConstants;
import com.topdraw.utils.*;
import com.topdraw.business.module.rights.repository.RightsRepository;
import com.topdraw.business.module.rights.service.RightsService;
import com.topdraw.business.module.rights.service.dto.RightsDTO;
import com.topdraw.business.module.rights.service.mapper.RightsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
......@@ -25,9 +27,9 @@ public class RightsServiceImpl implements RightsService {
private RightsMapper rightsMapper;
@Override
@Cacheable(cacheNames = RedisKeyConstants.cacheRightById, key = "#id", unless = "#result.id == null")
public RightsDTO findById(Long id) {
Rights Rights = this.rightsRepository.findById(id).orElseGet(Rights::new);
ValidationUtil.isNull(Rights.getId(),"Rights","id",id);
return this.rightsMapper.toDto(Rights);
}
......
......@@ -151,6 +151,9 @@ public class Task implements Serializable {
@Transient
private String attr;
@Transient
private Integer event;
/** 创建时间 */
@CreatedDate
@Column(name = "create_time")
......
......@@ -72,7 +72,7 @@ public class TaskServiceImpl implements TaskService {
}
@Override
// @Cacheable(value = RedisKeyConstants.cacheTaskByTaskTemplateId, key = "#event" , unless = "#result.size() == 0")
@Cacheable(value = RedisKeyConstants.cacheTaskByEvent, key = "#event" , unless = "#result.size() == 0")
public List<Task> findByEvent(Integer event) {
log.info("从数据库查询事件列表 ==>> {}", event);
return Objects.nonNull(event) ? this.taskRepository.findByEvent(event) : new ArrayList<>();
......
......@@ -183,7 +183,6 @@ public class RightsOperationServiceImpl implements RightsOperationService {
}
});
// 其他权益
this.threadPoolTaskExecutor.execute(()-> {
log.info("发放其他权益开始 ==>> [{}]", tempRightsMap);
......@@ -247,7 +246,7 @@ public class RightsOperationServiceImpl implements RightsOperationService {
Long memberId = right.getMemberId();
Long userId = right.getUserId();
// 权益类型
RightsDTO rightsDTO = this.getRights(rightId);
RightsDTO rightsDTO = this.rightsService.findById(rightId);
// 权益的实体类型 1:积分;2成长值;3优惠券
Integer type = rightsDTO.getEntityType();
Long expireTime1 = rightsDTO.getExpireTime();
......@@ -261,7 +260,7 @@ public class RightsOperationServiceImpl implements RightsOperationService {
case RightTypeConstants
.DISCOUNT_COUPON:
Long entityId = rightsDTO.getEntityId();
CouponDTO couponDTO = this.findCouponById(entityId);
CouponDTO couponDTO = this.couponService.findById(entityId);
if (Objects.nonNull(couponDTO)) {
TempCoupon tempCoupon = new TempCoupon();
tempCoupon.setId(couponDTO.getId());
......@@ -294,27 +293,6 @@ public class RightsOperationServiceImpl implements RightsOperationService {
}
/**
* 获取优惠券信息
* @param id
* @return
*/
private CouponDTO findCouponById(Long id) {
CouponDTO couponDTO = this.couponService.findById(id);
return couponDTO;
}
/**
* 权益详情
* @param rightsId
* @return
*/
private RightsDTO getRights(Long rightsId) {
RightsDTO rightsDTO = this.rightsService.findById(rightsId);
return rightsDTO;
}
/**
* 添加权益领取记录
* @param rightsHistories
*/
......
......@@ -53,6 +53,7 @@ import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
......@@ -97,7 +98,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
private RightsOperationService rightsOperationService;
@Autowired
private RedisUtils redisUtils;
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Value("${uc.validPriorityMember}")
private int validPriorityMember;
......@@ -108,6 +109,8 @@ public class TaskOperationServiceImpl implements TaskOperationService {
private static final Integer POINTS_MIN = 1;
@AsyncMqSend
public void asyncCreateTask(Task task) {}
@AsyncMqSend
......@@ -116,8 +119,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
public void asyncDeleteTask(Task task) {}
@Override
/* @CachePut(cacheNames = RedisKeyConstants.cacheTaskByTaskTemplateId, key = "#task.taskTemplateId",
unless = "#result.id == null")*/
@CacheEvict(cacheNames = RedisKeyConstants.cacheTaskByEvent, key = "#task.event")
public TaskDTO createTask(Task task) {
Long taskTemplateId = task.getTaskTemplateId();
TaskTemplateDTO taskTemplateDTO = this.taskTemplateService.findById(taskTemplateId);
......@@ -147,8 +149,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
}
@Override
/*@CachePut(cacheNames = RedisKeyConstants.cacheTaskByTaskTemplateId, key = "#task.taskTemplateId",
unless = "#result.id == null")*/
@CacheEvict(cacheNames = RedisKeyConstants.cacheTaskByEvent, key = "#task.event")
public TaskDTO updateTask(Task task) {
Long taskTemplateId = task.getTaskTemplateId();
TaskTemplateDTO taskTemplateDTO = this.taskTemplateService.findById(taskTemplateId);
......@@ -179,7 +180,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
}
@Override
/*@CacheEvict(cacheNames = "", key = "")*/
@CacheEvict(cacheNames = RedisKeyConstants.cacheTaskByEvent, key = "#task.event")
public Integer deleteTask(Task task) {
Long id = task.getId();
TaskDTO taskDTO = this.findById(id);
......@@ -267,6 +268,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
String priorityMemberCode = userTvDTO.getPriorityMemberCode();
if (StringUtils.isNotBlank(priorityMemberCode)) {
// TODO 是否需要将code和id都进行缓存
memberDTO = this.memberService.findByCode(priorityMemberCode);
log.info("查询绑定的小屏的主会员信息, member ==>> {}", memberDTO);
} else if (Objects.nonNull(userTvDTO.getMemberId())) {
......@@ -336,11 +338,12 @@ public class TaskOperationServiceImpl implements TaskOperationService {
}
private void saveOrUpdateTaskProcess(Long memberId, Task task, Integer currentActionAmount) {
private void saveOrUpdateTaskProcess(Long id,Long memberId, Task task, Integer currentActionAmount, Integer status) {
TrTaskProgress trTaskProgress = new TrTaskProgress();
trTaskProgress.setId(id);
trTaskProgress.setCurrentActionAmount(currentActionAmount);
trTaskProgress.setCompletionTime(TimestampUtil.now());
trTaskProgress.setStatus(1);
trTaskProgress.setStatus(status);
trTaskProgress.setTaskId(task.getId());
trTaskProgress.setMemberId(memberId);
trTaskProgress.setTargetActionAmount(task.getActionAmount());
......@@ -393,7 +396,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
}*/
TrTaskProgressDTO trTaskProgressDTO =
this.trTaskProgressService.findByMemberIdAndTaskIdAndCompletionTime(memberDTO.getId(), task.getId(), LocalDateTimeUtil.todayStart());
this.trTaskProgressService.findByMemberIdAndTaskIdAndCompletionTime(memberDTO.getId(), task.getId(), LocalDateTimeUtil.todayStart());
log.info("当前任务完成的情况,trTaskProgressDTO ==>> {}", trTaskProgressDTO);
// 任务未完成
......@@ -414,7 +417,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存开机、登录任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, continueLogin);
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, continueLogin, TASK_FINISH_STATUS);
}
}
break;
......@@ -427,7 +430,8 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存观影任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, playDuration_);
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, playDuration_, TASK_FINISH_STATUS);
}
}
break;
......@@ -455,7 +459,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存签到天数任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, signDays);
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, signDays, TASK_FINISH_STATUS);
}
}
break;
......@@ -468,7 +472,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存完成用户信息设置任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, completeCount);
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, completeCount, TASK_FINISH_STATUS);
}
}
break;
......@@ -481,7 +485,9 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存用户播放总时长任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, playDuration);
threadPoolTaskExecutor.execute(() -> {
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, playDuration, TASK_FINISH_STATUS);
});
}
}
break;
......@@ -494,7 +500,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存跨屏绑定任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, bindCount);
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, bindCount, TASK_FINISH_STATUS);
}
}
break;
......@@ -511,7 +517,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
tasksResult.add(task);
log.info("保存积分兑换商品任务进度");
this.saveOrUpdateTaskProcess(memberDTO.getId(), task, exchangeCount);
this.saveOrUpdateTaskProcess(null, memberDTO.getId(), task, exchangeCount, TASK_FINISH_STATUS);
}
}
......@@ -702,7 +708,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
String nickname = memberDTO.getNickname();
String memberCode = memberDTO.getCode();
// 权益详情
RightsDTO rightsDTO = this.getRight(rightsId);
RightsDTO rightsDTO = this.rightsService.findById(rightsId);
if (Objects.nonNull(rightsDTO)){
// 用以保存权益历史
......@@ -720,7 +726,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
case RightTypeConstants.DISCOUNT_COUPON:
Long entityId1 = rightsDTO.getEntityId();
if (Objects.nonNull(entityId1)) {
CouponDTO couponDTO = this.findCouponById(entityId1);
CouponDTO couponDTO = this.couponService.findById(entityId1);
if (Objects.nonNull(couponDTO.getId())) {
// 优惠券
TempCoupon tempCoupon = this.tempCouponBuild(memberId, memberCode,rightsAmount, rightsSendStrategy, couponDTO, nickname);
......@@ -736,7 +742,7 @@ public class TaskOperationServiceImpl implements TaskOperationService {
case RightTypeConstants.VIEW_COUPON:
Long entityId2 = rightsDTO.getEntityId();
if (Objects.nonNull(entityId2)) {
CouponDTO couponDTO = this.findCouponById(entityId2);
CouponDTO couponDTO = this.couponService.findById(entityId2);
if (Objects.nonNull(couponDTO.getId())) {
// 优惠券
TempCoupon tempCoupon = this.tempCouponBuild(memberId, memberCode,rightsAmount, rightsSendStrategy, couponDTO, nickname);
......@@ -771,26 +777,6 @@ public class TaskOperationServiceImpl implements TaskOperationService {
}
/**
* 获取优惠券信息
* @param id
* @return
*/
private CouponDTO findCouponById(Long id) {
CouponDTO couponDTO = this.couponService.findById(id);
return couponDTO;
}
/**
*
* @param rightsId
* @return
*/
private RightsDTO getRight(Long rightsId) {
RightsDTO rightsDTO = this.rightsService.findById(rightsId);
return rightsDTO;
}
/**
* 成长值
* @param task
* @return
......
......@@ -84,7 +84,7 @@ public class MemberOperationServiceImpl implements MemberOperationService {
@Override
@CachePut(cacheNames = RedisKeyConstants.cacheMemberById, key = "#member.id")
@CachePut(cacheNames = RedisKeyConstants.cacheMemberByCode, key = "#member.code", unless = "#result.id == null")
public MemberDTO doUpdateMemberCoupon(Member member) {
return this.memberService.doUpdateMemberCoupon(member);
}
......
......@@ -12,6 +12,7 @@ package com.topdraw.config;
public interface RedisKeyConstants {
String cacheMemberById = "uce::member::id";
String cacheMemberByCode = "uce::member::code";
String updateCacheMemberById = "uce::updateMember::id";
String updateCachePointsByMemberId = "uce::updatePoints::memberId";
......@@ -21,4 +22,7 @@ public interface RedisKeyConstants {
String cacheUserTvByPlatformAccount = "uce::userTv::platformAccount";
String cacheTaskProcessByMemberId = "uce::taskProcess::memberId";
String cacheTaskByTaskTemplateId = "uce::task::taskTemplateId";
String cacheTaskByEvent = "uce::task::event";
String cacheCouponById = "uce::coupon::id";
String cacheRightById = "uce::right::id";
}
......