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;
private CouponHistoryService couponHistoryService;
@Autowired
RightsOperationService rightsOperationService;
private MemberOperationService memberOperationService;
@Autowired
MemberService memberService;
@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);
}
private MemberService memberService;
/**
* 更新当前用户优惠券信息
* @param memberId
* @param currentCoupon
* @param expireSoonCouponCount
*/
private void doUpdateMemberInfo(Long memberId, Long currentCoupon, Long expireSoonCouponCount) {
MemberDTO memberDTO = this.findMemberByMemberId(memberId);
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;
}
/**
* 当前优惠券数量 = 总优惠券-已过期的优惠券
* @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);
}
public void asyncCouponHistory(CouponHistory couponHistory) {
String memberCode = couponHistory.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
couponHistory.setUserId(memberDTO.getId());
/**
* 获取已过期的优惠券数量
* @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);
}
}
@Override
public void grantExpByManual(List<TempExp> tempExpList) {
for (TempExp tempExp : tempExpList) {
this.refresh(tempExp);
}
}
/**
*
* @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 asyncMemberExpAndLevel(Member member) {
String code = member.getCode();
MemberDTO memberDTO = this.memberService.findByCode(code);
member.setId(memberDTO.getId());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
this.memberService.doUpdateMemberExpAndLevel(member);
}
private long calculateTotalExp(long originalExp, TempExp tempExp) {
Long rewardExp = tempExp.getRewardExp();
return rewardExp + originalExp;
}
public void asyncExpDetail(ExpDetail expDetail) {
String code = expDetail.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(code);
expDetail.setMemberId(memberDTO.getId());
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);
}
}
......
......@@ -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;
......