Commit 10561abf 10561abf37d707350efe4a302a762b13290bfb29 by xianghan

1.优化

1 parent 3b8ac7e5
......@@ -23,6 +23,9 @@ import java.time.LocalDateTime;
@Table(name="m_coupon_history")
public class CouponHistory implements Serializable {
@Transient
private String memberCode;
/** 主键 */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
......
......@@ -23,6 +23,9 @@ import java.sql.Timestamp;
@Table(name="uc_exp_detail")
public class ExpDetail implements Serializable {
@Transient
private String memberCode;
/** 主键 */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
......
......@@ -72,4 +72,10 @@ public interface MemberService {
* @param member 会员
*/
MemberDTO checkMember(Member member);
/**
*
* @param resources
*/
MemberDTO doUpdateMemberExpAndLevel(Member resources);
}
......
......@@ -105,6 +105,12 @@ public class MemberServiceImpl implements MemberService {
}
@Override
public MemberDTO doUpdateMemberExpAndLevel(Member resources) {
MemberDTO memberDTO = this.update(resources);
return memberDTO;
}
@Override
@Transactional(rollbackFor = Exception.class)
public MemberDTO create(Member resources) {
......
package com.topdraw.business.process.service;
import com.topdraw.business.process.domian.TempCoupon;
import java.util.List;
/**
* @description 权益操作接口
* @author XiangHan
......@@ -12,15 +8,4 @@ import java.util.List;
public interface CouponOperationService {
/**
* 基于已完成的任务发放优惠券
* @param tempCouponList
*/
void grantCouponThroughTempCoupon(List<TempCoupon> tempCouponList);
/**
* 系统手动发放优惠券
* @param tempCouponList
*/
void grantCouponByManual(List<TempCoupon> tempCouponList);
}
......
......@@ -11,20 +11,5 @@ import java.util.List;
*/
public interface ExpOperationService {
/**
* 任务完成后自动发放成长值
* @param tempExpList
*/
void grantPointsThroughTempExp(List<TempExp> tempExpList);
/**
* 系统手动发放优惠券
* @param memberId
* @param userId
* @param tempExpList
*/
void grantExpByManual(Long memberId, Long userId, List<TempExp> tempExpList);
void grantExpByManual(List<TempExp> tempExpList);
}
......
......@@ -11,38 +11,4 @@ import java.util.List;
*/
public interface PointsOperationService {
/**
* 手动发放积分
* @param memberId 会员id
* @param tempPoints 积分详情
*/
void grantPointsByManual(Long memberId , TempPoints tempPoints);
/**
*
* @param tempPoints
*/
void grantPointsByManualByTempPoints(TempPoints tempPoints);
/**
* 积分扣减
* @param tempPoints 积分对象
*/
boolean customPoints(TempPoints tempPoints);
/**
* 积分发放,基于已获得的权益
* @param tempPointsList 已获得的权益
*/
void grantPointsThroughTempRightsList(List<TempPoints> tempPointsList);
/**
* 清理过期并计算可用总积分
* @param memberId
* @return
*/
Long cleanInvalidPointsAndCalculateCurrentPoints(Long memberId);
}
......
package com.topdraw.business.process.service;
import com.topdraw.business.module.rights.history.domain.RightsHistory;
import com.topdraw.business.process.domian.RightType;
import java.util.List;
import java.util.Map;
/**
* @description 权益操作接口
* @author XiangHan
* @date 2021.10.22
*/
public interface RightsOperationService {
/**
* 系统手动发放权益
* @param rightsHistories
*/
void grantRightsByManual(List<RightsHistory> rightsHistories);
/**
* 任务完成自动发放权益
* @param tempRightsMap
*/
void grantRights(Map<RightType, Object> tempRightsMap);
}
......@@ -6,193 +6,33 @@ import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.member.domain.Member;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.process.domian.TempCoupon;
import com.topdraw.business.process.service.CouponOperationService;
import com.topdraw.business.process.service.RightsOperationService;
import com.topdraw.business.process.service.member.MemberOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
@Service
public class CouponOperationServiceImpl implements CouponOperationService {
private static final Logger LOG = LoggerFactory.getLogger(CouponOperationServiceImpl.class);
@Autowired
CouponService couponService;
@Autowired
CouponHistoryService couponHistoryService;
@Autowired
MemberOperationService memberOperationService;
@Autowired
RightsOperationService rightsOperationService;
private CouponHistoryService couponHistoryService;
@Autowired
MemberService memberService;
private MemberOperationService memberOperationService;
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
// 过期阀值(默认一个月)
private static final Integer EXPIRE_FACTOR_DAY = 30;
@Override
public void grantCouponThroughTempCoupon(List<TempCoupon> tempCouponList) {
// 优惠券领取、使用历史记录表
for (TempCoupon tempCoupon : tempCouponList) {
this.refresh(tempCoupon);
}
}
/**
* 手动发放优惠券
* @param tempCouponList
*/
@Override
public void grantCouponByManual(List<TempCoupon> tempCouponList) {
// 优惠券领取、使用历史记录表
for (TempCoupon tempCoupon : tempCouponList) {
this.refresh(tempCoupon);
}
}
/**
* 优惠券领取历史记录表
*
* @param tempCoupon 领取的优惠券
*/
private void refresh(TempCoupon tempCoupon) {
// 1.更新会员优惠券数量
this.refreshMemberCoupon(tempCoupon);
// 2.保存优惠券领取、使用历史记录表
this.doInsertCouponHistory(tempCoupon);
}
/**
* 更新会员优惠券信息
* @param tempCoupon 账号id
*/
private void refreshMemberCoupon(TempCoupon tempCoupon) {
// Long userId = tempCoupon.getUserId();
Long memberId = tempCoupon.getMemberId();
Integer rightsAmount = tempCoupon.getRightsAmount();
try {
// 1.历史总优惠券数量
Long historyCouponCount = this.getTotalHistoryCoupon(memberId);
// 1.当前总优惠券数量
Long totalCouponCount = this.getTotalCoupon(historyCouponCount,rightsAmount);
// 2.获取已过期的优惠券数量
Long expireCouponCount = this.getTotalExpireCoupon(memberId);
// 3.即将过期的优惠券数量
Long expireSoonCouponCount = this.getTotalExpireSoonCoupon(memberId,EXPIRE_FACTOR_DAY);
// 4.当前优惠券数量 = 总优惠券-已过期的优惠券
Long currentCoupon = this.getCurrentCoupon(totalCouponCount,expireCouponCount);
// 5.更新用户信息(优惠券数量、即将过期的优惠券数量)
this.doUpdateMemberInfo(memberId,currentCoupon,expireSoonCouponCount);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private Long getTotalCoupon(Long historyCouponCount, Integer rightsAmount) {
return (Objects.nonNull(historyCouponCount) ? historyCouponCount: 0L) + (Objects.nonNull(rightsAmount) ? rightsAmount: 0L);
}
/**
* 更新当前用户优惠券信息
* @param memberId
* @param currentCoupon
* @param expireSoonCouponCount
*/
private void doUpdateMemberInfo(Long memberId, Long currentCoupon, Long expireSoonCouponCount) {
MemberDTO memberDTO = this.findMemberByMemberId(memberId);
private MemberService memberService;
Member member = new Member();
BeanUtils.copyProperties(memberDTO,member);
member.setCouponAmount(currentCoupon);
member.setDueCouponAmount(expireSoonCouponCount);
member.setUpdateTime(LocalDateTime.now());
public void asyncMemberCoupon(Member member) {
String memberCode = member.getCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
member.setId(memberDTO.getId());
this.memberOperationService.doUpdateMember(member);
}
private MemberDTO findMemberByMemberId(Long memberId) {
MemberDTO memberDTO = this.memberService.findById(memberId);
return memberDTO;
}
public void asyncCouponHistory(CouponHistory couponHistory) {
String memberCode = couponHistory.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
couponHistory.setUserId(memberDTO.getId());
/**
* 当前优惠券数量 = 总优惠券-已过期的优惠券
* @param totalCouponCount 总数
* @param expireCouponCount 已过期总数
* @return
*/
private Long getCurrentCoupon(Long totalCouponCount, Long expireCouponCount) {
return (Objects.nonNull(totalCouponCount)?totalCouponCount:0L)-(Objects.nonNull(expireCouponCount)?expireCouponCount:0L);
}
/**
* 即将过期的优惠券数量
* @param expireFactor
* @return
*/
private Long getTotalExpireSoonCoupon(Long userId, Integer expireFactor) {
LocalDateTime expireTime = LocalDateTime.now().plusDays(expireFactor);
return this.couponHistoryService.countByUserIdAndExpireTimeBetween(userId,LocalDateTime.now(),expireTime);
}
/**
* 获取已过期的优惠券数量
* @param userId
* @return
*/
private Long getTotalExpireCoupon(Long userId) {
return this.couponHistoryService.countByUserIdAndExpireTimeBefore(userId,LocalDateTime.now());
}
/**
* 获取用户领取的总优惠券
* @param userId
* @return
*/
private Long getTotalHistoryCoupon(Long userId) {
return this.couponHistoryService.countByUserId(userId);
}
/**
* 优惠券领取、使用历史记录表
* @param tempCoupon 优惠券
*/
private void doInsertCouponHistory(TempCoupon tempCoupon) {
CouponHistory couponHistory = new CouponHistory();
BeanUtils.copyProperties(tempCoupon,couponHistory);
couponHistory.setId(null);
couponHistory.setCouponId(tempCoupon.getId());
couponHistory.setUserId(tempCoupon.getMemberId());
couponHistory.setCouponCode(tempCoupon.getCode());
couponHistory.setUserNickname(tempCoupon.getUserNickname());
couponHistory.setOrderDetailId(tempCoupon.getOrderId());
couponHistory.setReceiveTime(LocalDateTime.now());
couponHistory.setUseStatus(Objects.nonNull(couponHistory.getUseStatus()) ? couponHistory.getUseStatus():0);
this.couponHistoryService.create(couponHistory);
}
}
......
......@@ -4,25 +4,13 @@ import com.topdraw.business.module.exp.detail.domain.ExpDetail;
import com.topdraw.business.module.exp.detail.service.ExpDetailService;
import com.topdraw.business.module.member.domain.Member;
import com.topdraw.business.module.member.level.service.MemberLevelService;
import com.topdraw.business.module.member.level.service.dto.MemberLevelDTO;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.process.domian.TempExp;
import com.topdraw.business.process.service.ExpOperationService;
import com.topdraw.business.process.service.member.MemberOperationService;
import com.topdraw.util.IdWorker;
import com.topdraw.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
/**
*
......@@ -30,180 +18,30 @@ import java.util.Objects;
@Service
public class ExpOperationServiceImpl implements ExpOperationService {
private static final Logger LOG = LoggerFactory.getLogger(ExpOperationServiceImpl.class);
@Autowired
ExpDetailService expDetailService;
private ExpDetailService expDetailService;
@Autowired
MemberOperationService memberOperationService;
private MemberOperationService memberOperationService;
@Autowired
MemberLevelService memberLevelService;
private MemberLevelService memberLevelService;
@Autowired
MemberService memberService;
private MemberService memberService;
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
public void grantPointsThroughTempExp(List<TempExp> tempExpList) {
for (TempExp tempExp : tempExpList) {
this.refresh(tempExp);
}
}
@Override
public void grantExpByManual(Long memberId, Long userId, List<TempExp> tempExpList) {
for (TempExp tempExp : tempExpList) {
this.refresh(tempExp);
}
}
public void asyncMemberExpAndLevel(Member member) {
String code = member.getCode();
MemberDTO memberDTO = this.memberService.findByCode(code);
member.setId(memberDTO.getId());
@Override
public void grantExpByManual(List<TempExp> tempExpList) {
for (TempExp tempExp : tempExpList) {
this.refresh(tempExp);
this.memberService.doUpdateMemberExpAndLevel(member);
}
}
/**
*
* @param tempExp
*/
private void refresh(TempExp tempExp) {
String memberCode = tempExp.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
Long id = memberDTO.getId();
tempExp.setId(id);
try {
// 原始积分
long originExp = this.getExpByMemberId(tempExp);
// 总积分
long totalExp = this.calculateTotalExp(originExp, tempExp);
// 1.添加成长值记录
// this.threadPoolTaskExecutor.execute(() -> this.doInsertExpDetail(tempExp, originExp, totalExp));
// 2.更新成长值与等级
this.refreshMemberExpAndLevel(tempExp,totalExp);
this.doInsertExpDetail(tempExp, originExp, totalExp);
public void asyncExpDetail(ExpDetail expDetail) {
String code = expDetail.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(code);
expDetail.setMemberId(memberDTO.getId());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private long calculateTotalExp(long originalExp, TempExp tempExp) {
Long rewardExp = tempExp.getRewardExp();
return rewardExp + originalExp;
}
private long getExpByMemberId(TempExp tempExp) {
Long memberId = tempExp.getMemberId();
MemberDTO memberDTO = this.memberOperationService.findById(memberId);
if (Objects.nonNull(memberDTO)) {
Long exp = memberDTO.getExp();
return Objects.isNull(exp) ? 0L : exp;
}
return 0L;
}
/**
* 更新成长值与等级
*
* @param tempExp 成长值列表
*/
private void refreshMemberExpAndLevel(TempExp tempExp, long totalExp) {
Long memberId = tempExp.getMemberId();
// 1.获取当前成长值
MemberDTO memberDTO = this.getMemberInfoByMemberId(memberId);
// 2.获取下一级需要的成长值
MemberLevelDTO memberLevelDTO = this.getNextLevelExp(memberDTO.getLevel() + 1, 1);
// 4.成长值比较,判断是否升级
Integer level = this.compareExp(totalExp, memberLevelDTO,memberDTO);
// 5.更新用户信息
this.updateMemberInfo(level, totalExp, memberId);
}
/**
*
* @param level
* @param totalExp 总积分
* @param memberId 会员id
*/
private void updateMemberInfo(Integer level,Long totalExp,Long memberId) {
MemberDTO memberDTO = this.findMemberByMemberId(memberId);
Member member = new Member();
BeanUtils.copyProperties(memberDTO,member);
member.setExp(totalExp);
member.setLevel(level);
member.setUpdateTime(LocalDateTime.now());
this.memberOperationService.doUpdateMember(member);
}
private MemberDTO findMemberByMemberId(Long memberId) {
MemberDTO memberDTO = this.memberService.findById(memberId);
return memberDTO;
}
private Integer compareExp(long newExp, MemberLevelDTO memberLevelDTO, MemberDTO memberDTO) {
if (Objects.nonNull(memberLevelDTO)) {
Long nextLevelExp = memberLevelDTO.getExpValue();
if (Objects.nonNull(nextLevelExp) && nextLevelExp > 0)
if(newExp - nextLevelExp >= 0){
return memberLevelDTO.getLevel();
}
}
return memberDTO.getLevel();
}
private MemberLevelDTO getNextLevelExp(Integer i, Integer status) {
List<MemberLevelDTO> memberLevelDTOList = this.memberLevelService.findLevelAndStatus(i,status);
if (!CollectionUtils.isEmpty(memberLevelDTOList)) {
return memberLevelDTOList.get(0);
}
return null;
}
/**
* 获取当前会员的成长值
* @param memberId 会员id
* @return Long 当前会员成长值
*/
private MemberDTO getMemberInfoByMemberId(Long memberId) {
MemberDTO memberDTO = this.memberOperationService.findById(memberId);
return memberDTO;
}
/**
* 添加成长值记录
*
* @param tempExp 成长值列表
*/
private void doInsertExpDetail(TempExp tempExp, long originalExp, long totalExp) {
// 获得的积分
Long rewardExp = tempExp.getRewardExp();
ExpDetail expDetail = new ExpDetail();
BeanUtils.copyProperties(tempExp,expDetail);
expDetail.setCode(String.valueOf(IdWorker.generator()));
// 原始积分
expDetail.setOriginalExp(originalExp);
// 总积分
expDetail.setResultExp(totalExp);
// 获得的积分
expDetail.setExp(rewardExp);
if (StringUtils.isEmpty(expDetail.getDescription())) {
expDetail.setDescription("#");
}
this.expDetailService.create(expDetail);
}
}
......
......@@ -26,6 +26,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
......@@ -39,532 +40,32 @@ import java.util.stream.Collectors;
public class PointsOperationServiceImpl implements PointsOperationService {
@Autowired
PointsService pointsService;
private PointsDetailService pointsDetailService;
@Autowired
PointsDetailService pointsDetailService;
private PointsAvailableService pointsAvailableService;
@Autowired
PointsAvailableService pointsAvailableService;
@Autowired
PointsDetailHistoryService pointsDetailHistoryService;
@Autowired
MemberOperationService memberOperationService;
@Autowired
MemberService memberService;
// 过期阈值 30天
private static final Integer EXPIRE_FACTOR = 30;
private static final String DELETE_AVAILABLE_POINTS = "delete";
private static final String INSERT_AVAILABLE_POINTS = "insert";
private MemberService memberService;
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
@Transactional(rollbackFor = Exception.class)
public void grantPointsByManual(Long memberId,TempPoints tempPoints){
if (Objects.nonNull(tempPoints) && Objects.nonNull(tempPoints.getPoints())) {
this.refresh(tempPoints);
}
public void asyncMemberPoint(Member member) {
String code = member.getCode();
MemberDTO memberDTO = this.memberService.findByCode(code);
member.setId(memberDTO.getId());
this.memberService.doUpdateMemberPoints(member);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void grantPointsByManualByTempPoints(TempPoints tempPoints) {
if (Objects.nonNull(tempPoints) && Objects.nonNull(tempPoints.getPoints())) {
String memberCode = tempPoints.getMemberCode();
public void asyncPointsAvailable(PointsAvailable pointsAvailable) {
String memberCode = pointsAvailable.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
if (Objects.nonNull(memberDTO)) {
Long memberId = memberDTO.getId();
tempPoints.setMemberId(memberId);
this.refresh(tempPoints);
}
}
}
/**
* 积分消耗
* @param tempPoints 任务对象
* @return true: 满足 false:不满足
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean customPoints(TempPoints tempPoints) {
String memberCode = tempPoints.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
if (Objects.nonNull(memberDTO)) {
Long memberId = memberDTO.getId();
tempPoints.setMemberId(memberId);
try {
//1.删除过期的积分
this.cleanInvalidAvailablePointsByMemberId(memberId);
// 1.判断可用积分是否够用
boolean b = this.checkAvailablePoints(tempPoints);
log.info("customPoints-->>> 当前可用总积分大于待兑换的积分--->>" + b);
if (b) {
// 2.可用积分表,按照过期时间进行升序排列
List<PointsAvailableDTO> pointsAvailableDTOS = this.findByMemberIdOrderByExpireTime(tempPoints);
// 3.当前可用总积分
long currentPoints = this.findAvailablePointsByMemberId(memberId);
// 2.优先使用即将过期的积分,累加到超过需兑换积分时,需要进行拆分
Map<String, List<PointsAvailableDTO>> customAvailablePointsMap = this.customAvailablePoints(tempPoints, pointsAvailableDTOS);
// 3.添加积分明细
this.doInsertTrPointsDetailByAvailablePointsMap(tempPoints, customAvailablePointsMap, currentPoints);
// 4.更新可用积分表,超过的删除,剩余的新增
long totalPoints = this.doFreshTrPointsAvailableByAvailablePointsMap(customAvailablePointsMap, currentPoints);
// 5.即将过期的积分
long soonExpirePoints = this.getSoonExpirePoints(memberId, tempPoints);
// 6.更新会员积分信息
this.freshMemberCurrentPoints(memberId, totalPoints,soonExpirePoints,tempPoints);
return true;
}
}catch (Exception e) {
e.printStackTrace();
throw e;
}
}
return false;
}
/**
* 更新可用积分表
* @param customAvailablePointsMap
*/
private long doFreshTrPointsAvailableByAvailablePointsMap(
Map<String, List<PointsAvailableDTO>> customAvailablePointsMap,long currentPoints) {
long totalCustomAvailablePoints = 0L;
if (customAvailablePointsMap != null) {
// 需要删除的
List<PointsAvailableDTO> pointsAvailableDTOS1 = customAvailablePointsMap.get(DELETE_AVAILABLE_POINTS);
if (!CollectionUtils.isEmpty(pointsAvailableDTOS1)) {
for (PointsAvailableDTO pointsAvailableDTO : pointsAvailableDTOS1) {
totalCustomAvailablePoints = (totalCustomAvailablePoints+pointsAvailableDTO.getPoints());
this.pointsAvailableService.delete(pointsAvailableDTO.getId());
}
}
// 需要添加的
List<PointsAvailableDTO> pointsAvailableDTOS2 = customAvailablePointsMap.get(INSERT_AVAILABLE_POINTS);
if (!CollectionUtils.isEmpty(pointsAvailableDTOS2)) {
PointsAvailableDTO pointsAvailableDTO = pointsAvailableDTOS2.get(0);
PointsAvailable pointsAvailable = new PointsAvailable();
BeanUtils.copyProperties(pointsAvailableDTO,pointsAvailable);
pointsAvailable.setId(null);
pointsAvailable.setCode(String.valueOf(IdWorker.generator()));
pointsAvailable.setMemberId(memberDTO.getId());
this.pointsAvailableService.create4Custom(pointsAvailable);
}
}
return currentPoints - totalCustomAvailablePoints;
}
/**
* 优先使用即将过期的积分,注意拆分
* @param customAvailablePointsMap
*/
private void doInsertTrPointsDetailByAvailablePointsMap(TempPoints tempPoints,
Map<String, List<PointsAvailableDTO>> customAvailablePointsMap,
long currentPoints) {
// 兑换的积分
Long points = tempPoints.getPoints();
List<PointsAvailableDTO> pointsAvailableDTOS = customAvailablePointsMap.get(DELETE_AVAILABLE_POINTS);
if (!CollectionUtils.isEmpty(pointsAvailableDTOS)) {
PointsAvailableDTO pointsAvailableDTO = pointsAvailableDTOS.get(0);
Long memberId = pointsAvailableDTO.getMemberId();
TempPoints tempPoints1 = new TempPoints();
BeanUtils.copyProperties(pointsAvailableDTO,tempPoints1);
BeanUtils.copyProperties(tempPoints,tempPoints1);
tempPoints1.setPoints(-(Math.abs(points)));
Long totalPoints = this.calculateTotalPoints(tempPoints1, currentPoints);
this.doInsertTrPointsDetail(memberId,tempPoints1,currentPoints,totalPoints);
}
}
/**
* 消耗过期的积分
* @param tempPoints 需要消耗的积分
* @param pointsAvailableDTOS 可用积分列表
* @return List<PointsAvailableDTO> 已消耗的可用积分列表
*/
private Map<String,List<PointsAvailableDTO>> customAvailablePoints(TempPoints tempPoints, List<PointsAvailableDTO> pointsAvailableDTOS) {
// 兑换的积分
Long points = tempPoints.getPoints();
Long points1_ = 0L;
List<PointsAvailableDTO> pointsAvailableDTOS1 = new ArrayList<>();
List<PointsAvailableDTO> pointsAvailableDTOS2 = new ArrayList<>();
Map<String,List<PointsAvailableDTO>> map = new HashMap<>();
for (PointsAvailableDTO pointsAvailableDTO : pointsAvailableDTOS) {
Long points1 = pointsAvailableDTO.getPoints();
points1_ = (points1_ + points1);
// 不够
if (points1_ < points) {
pointsAvailableDTOS1.add(pointsAvailableDTO);
continue;
}
// 刚好
if (points1_ == points) {
pointsAvailableDTOS1.add(pointsAvailableDTO);
break;
}
// 超了,拆分
if (points1_ > points) {
// 超过的
long beyond = points1_ - points;
// 新增
PointsAvailableDTO pointsAvailableDTO2 = new PointsAvailableDTO();
BeanUtils.copyProperties(pointsAvailableDTO,pointsAvailableDTO2);
pointsAvailableDTO2.setPoints(beyond);
pointsAvailableDTOS2.add(pointsAvailableDTO2);
// 剩余的
long suit = points1 - beyond;
// 扣除
PointsAvailableDTO pointsAvailableDTO1 = new PointsAvailableDTO();
BeanUtils.copyProperties(pointsAvailableDTO,pointsAvailableDTO1);
pointsAvailableDTO1.setPoints(suit);
pointsAvailableDTOS1.add(pointsAvailableDTO1);
break;
}
}
map.put(DELETE_AVAILABLE_POINTS,pointsAvailableDTOS1);
map.put(INSERT_AVAILABLE_POINTS,pointsAvailableDTOS2);
return map;
}
/**
* 可用积分表,按照过期时间进行升序排列
* @param tempPoints
* @return
*/
private List<PointsAvailableDTO> findByMemberIdOrderByExpireTime(TempPoints tempPoints) {
return this.pointsAvailableService.findByMemberIdOrderByExpireTime(tempPoints.getMemberId());
}
/**
* 检查当前用户可用积分是否足够
* @param tempPoints
* @return true : 满足 false: 不满足
*/
private boolean checkAvailablePoints(TempPoints tempPoints) {
Long memberId = tempPoints.getMemberId();
// 可用积分
long currentPoints = this.findAvailablePointsByMemberId(memberId);
long points1 = tempPoints.getPoints();
if (currentPoints > 0 && points1 > 0 && (currentPoints - Math.abs(points1) >= 0)) {
return true;
}
return false;
}
/**
* 积分发放,基于已获得的权益
*
* @param tempPointsList 已获得的权益
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void grantPointsThroughTempRightsList(List<TempPoints> tempPointsList){
log.info("------->>grantPointsThroughTempRightsList start1");
for (TempPoints tempPoints : tempPointsList){
log.info("------->>grantPointsThroughTempRightsList start");
this.refresh(tempPoints);
}
}
/**
* 清理过期积分
* @param memberId
*/
private void cleanInvalidAvailablePointsByMemberId(Long memberId) {
List<PointsAvailableDTO> pointsAvailableDTOS =
pointsAvailableService.findByMemberIdAndExpireTimeBefore(memberId,LocalDateTime.now());
if (!CollectionUtils.isEmpty(pointsAvailableDTOS)) {
//1.获取原始积分
for (PointsAvailableDTO pointsAvailableDTO : pointsAvailableDTOS) {
// 添加积分明细 uc_points_detail
this.doCreatePointsDetail(pointsAvailableDTO);
// 删除已过期的积分
this.doDeleteInvalidAvailablePoints(pointsAvailableDTO);
}
}
}
@Override
public Long cleanInvalidPointsAndCalculateCurrentPoints(Long memberId) {
// 清理当前用户的过期积分
this.cleanInvalidAvailablePointsByMemberId(memberId);
// 获取当前用户的可用总积分
long currentPoints = this.findAvailablePointsByMemberId(memberId);
// 即将过期的积分
long soonExpirePoints = this.getSoonExpirePoints(memberId, null);
// 更新会员信息
this.doUpdateMemberPoints(memberId,currentPoints,soonExpirePoints);
return currentPoints;
}
/**
* 获取可用总积分
* @param memberId
* @return
*/
private long findAvailablePointsByMemberId(Long memberId){
return this.pointsAvailableService.findAvailablePointsByMemberId(memberId);
}
/**
* 修改会员信息
* @param memberId
* @param currentPoints
*/
private void doUpdateMemberPoints(Long memberId, long currentPoints,long soonExpirePoints) {
Member member = new Member();
member.setId(memberId);
member.setPoints(currentPoints);
member.setDuePoints(soonExpirePoints);
this.memberOperationService.doUpdateMemberPoints(member);
}
/**
*
* @param pointsAvailableDTOS
*/
private void doDeleteBatchInvalidAvailablePoints(List<PointsAvailableDTO> pointsAvailableDTOS) {
List<Long> collect = pointsAvailableDTOS.stream().map(pointsAvailableDTO -> pointsAvailableDTO.getId()).collect(Collectors.toList());
this.pointsAvailableService.deleteBatchByIds(collect);
}
/**
*
* @param pointsAvailableDTO
*/
private void doDeleteInvalidAvailablePoints(PointsAvailableDTO pointsAvailableDTO) {
this.pointsAvailableService.delete(pointsAvailableDTO.getId());
}
/**
* 可用积分
* @param pointsAvailableDTO
*/
private void doCreatePointsDetail(PointsAvailableDTO pointsAvailableDTO) {
Long memberId = pointsAvailableDTO.getMemberId();
// 原始积分
long availablePoints = this.pointsAvailableService.findTotalPointsByMemberId(memberId);//this.findAvailablePointsByMemberId(memberId);
// 过期积分
long l = pointsAvailableDTO.getPoints();
// 结果积分
long resultPoints = availablePoints - l;
PointsDetail pointsDetail = new PointsDetail();
BeanUtils.copyProperties(pointsAvailableDTO,pointsDetail);
pointsDetail.setId(null);
pointsDetail.setPoints(-Math.abs(l));
pointsDetail.setCode(String.valueOf(IdWorker.generator()));
pointsDetail.setOriginalPoints(availablePoints);
pointsDetail.setResultPoints(resultPoints);
pointsDetail.setDescription("过期积分");
pointsDetail.setEvtType(99);
pointsDetail.setCreateTime(TimestampUtil.now());
pointsDetail.setUpdateTime(TimestampUtil.now());
this.doInsertPointsDetail(pointsDetail);
}
/**
* 模板方法,提供更新积分的总体流程
*
* @param tempPoints 积分
*/
private void refresh(TempPoints tempPoints) {
Long memberId = tempPoints.getMemberId();
log.info("----------->> points refresh start");
log.info("----------->> rLock --->> start" );
try {
log.info("----------->> refresh findAvailablePointsByMemberId start");
// 1.可用总积分
Long currentPoints = this.findAvailablePointsByMemberId(memberId);
log.info("----------->> refresh findAvailablePointsByMemberId currentPoints " + currentPoints);
// 2.计算总积分
Long totalPoints = this.calculateTotalPoints(tempPoints, currentPoints);
log.info("----------->> refresh findAvailablePointsByMemberId totalPoints " + totalPoints);
// 3.添加积分明细,并计算总积分
this.doInsertTrPointsDetail(memberId, tempPoints, currentPoints, totalPoints);
log.info(Thread.currentThread().getName() + "----------->> refresh doInsertTrPointsDetail end ");
// 4.添加可用积分
log.info("----------->> refresh doInsertTrPointsAvailable start ");
this.doInsertTrPointsAvailable(tempPoints);
log.info("----------->> refresh doInsertTrPointsAvailable end ");
// 即将过期的积分
long soonExpirePoints = this.getSoonExpirePoints(memberId, tempPoints);
// 6.更新会员的总积分
log.info("----------->> refresh freshMemberCurrentPoints start ");
this.freshMemberCurrentPoints(memberId, totalPoints,soonExpirePoints,tempPoints);
log.info("----------->> refresh freshMemberCurrentPoints end ");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 获取总积分
* @param tempPoints
* @param currentPoints
* @return
*/
private Long calculateTotalPoints(TempPoints tempPoints, Long currentPoints) {
// 获取的积分
Long rewardPoints = tempPoints.getPoints();
// 总积分
Long totalPoints = currentPoints + rewardPoints;
return totalPoints;
}
/**
* 获取即将过期的积分
* @param memberId
* @return
*/
private long getSoonExpirePoints(Long memberId,TempPoints tempPoints) {
// 计算即将过期的天数
Integer factor = this.calculateExpireFactor(tempPoints);
if (Objects.isNull(factor)) {
factor = EXPIRE_FACTOR;
}
Long soonExpireTime = this.pointsAvailableService.findSoonExpireTime(memberId, factor);
return Objects.nonNull(soonExpireTime) ? soonExpireTime : 0L;
}
/**
* 如果过期时间和过期天数同时存在,则以过期时间为准
* @param tempPoints
* @return
*/
private Integer calculateExpireFactor(TempPoints tempPoints) {
// TODO 计算过期的相对时间
return null;
}
/**
* 更新会员总积分
* @param memberId 会员Id
* @param currentPoints 当前总积分
*/
private void freshMemberCurrentPoints(Long memberId, Long currentPoints,long duePoints,TempPoints tempPoints) {
Member member = new Member();
member.setId(memberId);
member.setPoints(Objects.nonNull(currentPoints)?currentPoints:0);
member.setDuePoints(duePoints);
member.setUpdateTime(LocalDateTime.now());
member.setCode(tempPoints.getMemberCode());
try {
this.memberOperationService.doUpdateMemberPoints(member);
} catch (Exception e){
throw e;
}
}
/**
* 计算当前总积分
* @param memberId 会员id
* @return
*/
private Long findAvailablePointsByMemberId(long memberId){
Long availablePoints = this.pointsAvailableService.findAvailablePointsByMemberId(memberId);
return Objects.nonNull(availablePoints) ? availablePoints : 0L;
}
/**
* 更新可用积分表
* @param tempPoints
*/
private void doInsertTrPointsAvailable(TempPoints tempPoints){
PointsAvailable pointsAvailable = new PointsAvailable();
BeanUtils.copyProperties(tempPoints,pointsAvailable);
String description = pointsAvailable.getDescription();
pointsAvailable.setCode(String.valueOf(IdWorker.generator()));
pointsAvailable.setDescription(StringUtils.isEmpty(description)?"#":description);
LocalDateTime timestamp = tempPoints.getExpireTime();
if (Objects.nonNull(timestamp)) {
pointsAvailable.setExpireTime(timestamp);
}
// 添加可用积分记录
this.doInsertTrPointsAvailable(pointsAvailable);
}
/**
* 添加可用积分记录
* @param pointsAvailable 可用积分
*/
private void doInsertTrPointsAvailable(PointsAvailable pointsAvailable) {
this.pointsAvailableService.create(pointsAvailable);
}
/**
* 添加积分明细
* @param memberId 会员Id
* @param tempPoints 积分
* @return Integer 总积分
*/
private void doInsertTrPointsDetail(Long memberId, TempPoints tempPoints,Long currentPoints,Long totalPoints){
PointsDetail pointsDetail = new PointsDetail();
BeanUtils.copyProperties(tempPoints,pointsDetail);
pointsDetail.setId(null);
pointsDetail.setMemberId(memberId);
pointsDetail.setCode(String.valueOf(IdWorker.generator()));
pointsDetail.setPoints(tempPoints.getPoints());
pointsDetail.setOriginalPoints(currentPoints);
pointsDetail.setResultPoints(totalPoints);
pointsDetail.setCreateTime(null);
pointsDetail.setUpdateTime(null);
String description = pointsDetail.getDescription();
if (StringUtils.isEmpty(description)) {
pointsDetail.setDescription("#");
}
// 保存积分流水
this.doInsertPointsDetail(pointsDetail);
}
/**
*
* @param pointsDetail
*/
private void doInsertPointsDetail(PointsDetail pointsDetail){
public void asyncPointsDetail(PointsDetail pointsDetail) {
String memberCode = pointsDetail.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
pointsDetail.setMemberId(memberDTO.getId());
this.pointsDetailService.create4Custom(pointsDetail);
}
}
......
package com.topdraw.business.process.service.impl;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.coupon.service.dto.CouponDTO;
import com.topdraw.business.module.rights.history.domain.RightsHistory;
import com.topdraw.business.module.rights.history.service.RightsHistoryService;
import com.topdraw.business.module.rights.service.RightsService;
import com.topdraw.business.module.rights.service.dto.RightsDTO;
import com.topdraw.business.process.domian.*;
import com.topdraw.business.process.service.CouponOperationService;
import com.topdraw.business.process.service.ExpOperationService;
import com.topdraw.business.process.service.PointsOperationService;
import com.topdraw.business.process.service.RightsOperationService;
import com.topdraw.util.TimestampUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 权益处理
*
* @author XiangHan
* @date 2021.10.23
*/
@Service
@Slf4j
public class RightsOperationServiceImpl implements RightsOperationService {
@Autowired
RightsHistoryService rightsHistoryService;
@Autowired
CouponOperationService couponOperationService;
@Autowired
RightsService rightsService;
@Autowired
ExpOperationService expOperationService;
@Autowired
PointsOperationService pointsOperationService;
@Autowired
CouponService couponService;
// @Autowired
// ThreadPoolTaskExecutor threadPoolTaskExecutor;
private ExecutorService threadPoolTaskExecutor = Executors.newFixedThreadPool(10);
/**
* 系统手动发放
* 实现步骤:
* 1.当前权益只有实体券
* @param rightsList
*/
@Override
public void grantRightsByManual(List<RightsHistory> rightsList) {
// 1.权益区分
Map<RightType,Object> tempRightsMap = this.distinguishRight(rightsList);
// 2.权益下发
this.refresh(tempRightsMap);
// 3.保存权益历史
this.doInsertTrRightHistory(rightsList);
}
/**
* 权益发放
* @param tempRightsMap 权益类型
*/
@Override
public void grantRights(Map<RightType, Object> tempRightsMap) {
// this.threadPoolTaskExecutor.execute(()-> {
// 2.创建权益历史对象
List<RightsHistory> rightsList = this.getRightHistory(tempRightsMap);
// 3.保存权益历史
this.doInsertTrRightHistory(rightsList);
// });
// 1.权益下发
this.refresh(tempRightsMap);
}
/**
*
* @param tempRightsMap
* @return
*/
private List<RightsHistory> getRightHistory(Map<RightType, Object> tempRightsMap) {
List<TempRights> values = (List<TempRights>)tempRightsMap.get(RightType.RIGHTS);
List<RightsHistory> rightsHistoryList = new ArrayList<>();
values.forEach(value -> {
RightsHistory rightsHistory = new RightsHistory();
rightsHistory.setSendTime(TimestampUtil.now());
rightsHistory.setRightsId(value.getId());
rightsHistory.setMemberId(value.getMemberId());
rightsHistory.setExpireTime(value.getExpireTime());
String memberCode = value.getMemberCode();
rightsHistory.setMemberCode(memberCode);
rightsHistoryList.add(rightsHistory);
});
return rightsHistoryList;
}
/**
* 成长值发放,基于已获得的权益
* @param tempExpList 权益列表
*/
private void grantExp(List<TempExp> tempExpList) {
if (!CollectionUtils.isEmpty(tempExpList))
this.expOperationService.grantPointsThroughTempExp(tempExpList);
}
/**
* 发放积分,基于已获得的权益
*
* @param tempPointsList 权益列表
*/
private void grantPoint(List<TempPoints> tempPointsList) {
if (!CollectionUtils.isEmpty(tempPointsList))
this.pointsOperationService.grantPointsThroughTempRightsList(tempPointsList);
}
/**
* 发放优惠券,基于已获得的权益
*
* @param tempCouponList 优惠券
*/
private void grantCoupon(List<TempCoupon> tempCouponList) {
// 发放优惠券
if (!CollectionUtils.isEmpty(tempCouponList))
this.couponOperationService.grantCouponThroughTempCoupon(tempCouponList);
}
/**
* 权益发放
* @param tempRightsMap
*/
private void refresh(Map<RightType, Object> tempRightsMap) {
/*FutureTask<Map<Long,Long>> futureTask1 = new FutureTask(()->{
log.info(Thread.currentThread().getName() + "=========>> start");
// 积分
this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
log.info(Thread.currentThread().getName() + "=========>>grantPoint end");
// 成长值
// this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
// this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
return null;
});
FutureTask<Map<Long,Long>> futureTask2 = new FutureTask(()->{
// 积分
// this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
// 成长值
this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
// this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
return null;
});
FutureTask<Map<Long,Long>> futureTask3 = new FutureTask(()->{
// 积分
// this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
// 成长值
// this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
return null;
});
this.threadPoolTaskExecutor.execute(futureTask1);
this.threadPoolTaskExecutor.execute(futureTask2);
this.threadPoolTaskExecutor.execute(futureTask3);*/
/*this.threadPoolTaskExecutor.execute(() -> {
// 积分
this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
// 成长值
this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
});*/
/*this.threadPoolTaskExecutor.execute(() -> {
log.info(Thread.currentThread().getName() + "=========>> start");
// 积分
this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
log.info(Thread.currentThread().getName() + "=========>> end");
});*/
List<TempPoints> tempPointsList = (List<TempPoints>) tempRightsMap.get(RightType.POINTS);
if (!CollectionUtils.isEmpty(tempPointsList)) {
// 积分
this.grantPoint(tempPointsList);
}
List<TempExp> tempExpList = (List<TempExp>) tempRightsMap.get(RightType.EXP);
if (!CollectionUtils.isEmpty(tempExpList)) {
// 成长值
this.grantExp(tempExpList);
}
List<TempCoupon> tempCouponList = (List<TempCoupon>) tempRightsMap.get(RightType.COUPON);
if (!CollectionUtils.isEmpty(tempCouponList)) {
// 优惠券
this.grantCoupon(tempCouponList);
}
}
/**
* 区分权益
* @param rightsList
* @return
*/
private Map<RightType, Object> distinguishRight(List<RightsHistory> rightsList) {
Map<RightType,Object> map = new HashMap<>();
// 优惠券
List<TempCoupon> tempCouponList = new ArrayList<>();
for (RightsHistory right : rightsList) {
Long rightId = right.getRightsId();
Long memberId = right.getMemberId();
Long userId = right.getUserId();
// 权益类型
RightsDTO rightsDTO = this.getRights(rightId);
// 权益的实体类型 1:积分;2成长值;3优惠券
String type = rightsDTO.getEntityType();
Long expireTime = rightsDTO.getExpireTime();
switch (type) {
// 优惠券
case "1":
Long entityId = rightsDTO.getEntityId();
CouponDTO couponDTO = this.findCouponById(entityId);
if (Objects.nonNull(couponDTO)) {
TempCoupon tempCoupon = new TempCoupon();
tempCoupon.setId(couponDTO.getId());
tempCoupon.setMemberId(memberId);
tempCoupon.setUserId(userId);
tempCoupon.setRightsAmount(1);
tempCoupon.setRightsSendStrategy(0);
tempCoupon.setCode(couponDTO.getCode());
if (Objects.nonNull(expireTime))
tempCoupon.setExpireTime(LocalDateTimeUtil.of(expireTime));
tempCouponList.add(tempCoupon);
}
break;
// 观影券
case "2":
break;
// 活动参与机会
case "3":
break;
default:
break;
}
}
// 优惠券
if (!CollectionUtils.isEmpty(tempCouponList))
map.put(RightType.COUPON,tempCouponList);
return map;
}
/**
* 获取优惠券信息
* @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
*/
private void doInsertTrRightHistory(List<RightsHistory> rightsHistories) {
if (!CollectionUtils.isEmpty(rightsHistories)) {
for (RightsHistory rightsHistory : rightsHistories) {
Long operatorId = rightsHistory.getOperatorId();
String operatorName = rightsHistory.getOperatorName();
rightsHistory.setSendTime(TimestampUtil.now());
rightsHistory.setOperatorId(Objects.nonNull(operatorId)?operatorId:0);
rightsHistory.setOperatorName(!StringUtils.isEmpty(operatorName)?operatorName:"系统发放");
this.rightsHistoryService.create(rightsHistory);
}
}
}
}
......@@ -138,7 +138,8 @@ public class RabbitMqConfig {
public static final String UC_DIRECT_MANAGEMENT = "uc.direct.management";
// uc-service-management 2 uc-consumer-iptv
public static final String ENGINE_TO_SERVICE_DIRECT = "uc.engine.service.direct";
// public static final String ENGINE_TO_SERVICE_DIRECT = "uc.engine.service.direct";
public static final String ENGINE_TO_SERVICE_DIRECT = "uc.engine.service.direct.local";
@Bean
public Queue managementDirect() {
......@@ -184,18 +185,18 @@ public class RabbitMqConfig {
/**************************************************数据基座*************************************************************/
public static final String UC_EVENTBUS = "uc.eventbus";
public static final String UC_EVENTBUS_EXCHANGE = "uc.eventbus.local";
public static final String UC_EVENTBUS_KEY = "uc.eventbus.*.topic";
public static final String UC_EVENTBUS_TOPIC = "uc.eventbus";
public static final String UC_EVENTBUS_QUEUE = "uc.eventbus.local";
@Bean
public Queue eventBusQueue() {
return new Queue(UC_EVENTBUS_TOPIC);
return new Queue(UC_EVENTBUS_QUEUE);
}
@Bean
TopicExchange eventBusExchange() {
return ExchangeBuilder.topicExchange(UC_EVENTBUS)
return ExchangeBuilder.topicExchange(UC_EVENTBUS_EXCHANGE)
.durable(true).build();
}
......
......@@ -2,34 +2,35 @@ package com.topdraw.mq.consumer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
import com.topdraw.config.RabbitMqConfig;
import com.topdraw.exception.BadRequestException;
import com.topdraw.exception.EntityNotFoundException;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.util.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.sql.Timestamp;
import java.util.Objects;
@Component
@Slf4j
public class UcEventBusIptv2IptvConsumer {
public class UcEventBusIptv2ManagementUcEngine {
@Autowired
RestTemplateClient restTemplateClient;
private RestTemplateClient restTemplateClient;
@Autowired
private UserTvService userTvService;
@Autowired
private MemberService memberService;
/**
* 事件
......@@ -39,7 +40,7 @@ public class UcEventBusIptv2IptvConsumer {
* @date 2021/9/7 11:26 上午
*/
@RabbitHandler
@RabbitListener(queues = RabbitMqConfig.UC_EVENTBUS_TOPIC,
@RabbitListener(queues = RabbitMqConfig.UC_EVENTBUS_QUEUE,
containerFactory = "managementRabbitListenerContainerFactory")
public void ucEventConsumer(String content) {
log.info(" receive dataSync msg , content is : {} ", content);
......@@ -53,18 +54,6 @@ public class UcEventBusIptv2IptvConsumer {
* @param content
* @return
*/
/*{
"evt": "play”, // 表示播放
"deviceType": 1, // 1代表大屏
"time": "2022-04-01 00:10:09”,
"msgData": {
"platformAccount": "itv094430@“, // 大屏用户账号
"playDuration": 60, // 播放时长,单位分钟
"mediaId": 3433, // 节目id,此次是累计计算,可不传
"mediaCode": "media_123”, // 节目标识,同上
"mediaName": "白宫陷落” // 节目名称,同上
}
}*/
private DataSyncMsg parseContent(String content) {
CommonMsg commonMsg = JSONUtil.parseMsg2Object(content, CommonMsg.class);
......@@ -91,22 +80,35 @@ public class UcEventBusIptv2IptvConsumer {
DataSyncMsg.MsgData msgData1 = new DataSyncMsg.MsgData();
if (StringUtils.isNotBlank(platformAccount)) {
MemberDTO memberDTO = this.userTvService.findMemberByPlatformAccount(platformAccount);
Long memberId = memberDTO.getId();
String memberCode = memberDTO.getCode();
UserTvDTO userTvDTO = this.userTvService.findByPlatformAccount(platformAccount);
String priorityMemberCode = userTvDTO.getPriorityMemberCode();
String memberCode = "";
if (StringUtils.isNotBlank(priorityMemberCode)) {
memberCode = priorityMemberCode;
} else {
memberCode = this.memberService.findById(userTvDTO.getMemberId()).getCode();
}
if (StringUtils.isBlank(memberCode))
throw new EntityNotFoundException(MemberDTO.class, "memberCode", "memberCode is null");
msgData1.setMemberCode(memberCode);
}
msgData1.setEvent(8);
msgData1.setDeviceType(deviceType);
msgData1.setMediaId(mediaId);
JSONObject jsonObject = new JSONObject();
jsonObject.put("mediaId", mediaId);
jsonObject.put("mediaName", mediaName);
jsonObject.put("playDuration", playDuration);
jsonObject.put("mediaCode", mediaCode);
jsonObject.put("time", time);
msgData1.setDescription(JSON.toJSONString(jsonObject));
JSONObject param = new JSONObject();
param.put("playDuration", playDuration);
msgData1.setParam(JSON.toJSONString(param));
JSONObject description = new JSONObject();
description.put("mediaId", mediaId);
description.put("mediaName", mediaName);
description.put("playDuration", playDuration);
description.put("mediaCode", mediaCode);
description.put("time", time);
msgData1.setDescription(JSON.toJSONString(description));
dataSyncMsg.setMsg(msgData1);
return dataSyncMsg;
......