Commit effcab0d authored by 万成波's avatar 万成波

用户操作事件积分

parent 945df793
......@@ -4,6 +4,8 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 操作明细
*
......@@ -13,7 +15,9 @@ import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OperateDetail {
public class OperateDetail implements Serializable {
private static final long serialVersionUID = -5020237125816721320L;
/**
* 链路追踪Id
......
......@@ -30,9 +30,9 @@ public class OperateEventListener {
* @param event 事件对象
*/
@TransactionalEventListener(classes = OperateEvent.class, fallbackExecution = true)
public void handle(OperateEvent event) {
public void handleOperate(OperateEvent event) {
OperateDetail detail = new OperateDetail();
detail.setLinkTracId(IdUtil.fastUUID());
detail.setLinkTracId(IdUtil.fastSimpleUUID());
detail.setUserName(event.getUserName());
detail.setOperateCode(event.getOperateType().getCode());
log.info("=> 开始处理用户操作事件:{}", detail);
......
......@@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
......@@ -38,6 +39,20 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap
private JmsTemplate jmsTemplate;
/**
* 初始化积分规则配置缓存
*/
@PostConstruct
public void initCachePointsSetting() {
POINTS_SETTING.clear();
List<BbsPointsSetting> dbSettings = this.getEnableSettings();
for (BbsPointsSetting setting : dbSettings) {
POINTS_SETTING.put(setting.getOperateCode(), setting);
}
log.info("=> 初始化积分规则配置缓存完毕,缓存配置数量:{}", POINTS_SETTING.size());
}
/**
* 查询积分规则配置列表
*
......@@ -107,6 +122,7 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap
addSetting.setLimitCount(setting.getLimitCount());
addSetting.setEnableStatus(setting.getEnableStatus());
this.save(addSetting);
this.initCachePointsSetting();
}
......@@ -138,6 +154,7 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap
updSetting.setLimitCount(setting.getLimitCount());
updSetting.setEnableStatus(setting.getEnableStatus());
this.updateById(updSetting);
this.initCachePointsSetting();
}
......@@ -149,6 +166,7 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap
@Override
public void deleteSetting(Long settingId) {
this.removeById(settingId);
this.initCachePointsSetting();
}
......@@ -181,40 +199,42 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap
@Override
public void handleOperatePoints(OperateDetail detail) {
log.info("=> 处理用户操作积分:{}", detail);
BbsPointsSetting cacheSetting = POINTS_SETTING.get(detail.getOperateCode());
if (Objects.isNull(cacheSetting)) {
throw new ServiceException("处理失败,未查询到操作编码对应的积分配置。");
}
// 匹配积分规则
BbsPointsSetting setting = POINTS_SETTING.get(detail.getOperateCode());
String userName = detail.getUserName();
String operateCode = setting.getOperateCode();
String operateCode = cacheSetting.getOperateCode();
int pointsCount = this.baseMapper.selectPointsCount(userName, operateCode, 7);
if (pointsCount >= cacheSetting.getLimitCount()) {
return;
}
if (pointsCount < setting.getLimitCount()) {
// 积分消息明细
String operateName = setting.getOperateName();
Integer points = setting.getOperatePoints();
PointsDetail pointsDetail = new PointsDetail();
pointsDetail.setLinkTracId(detail.getLinkTracId());
pointsDetail.setUserName(userName);
pointsDetail.setDetailPoints(points);
pointsDetail.setDetailName(operateName);
pointsDetail.setDetailCode(operateCode);
pointsDetail.setDescription(String.format("用户【%s】操作【%s】积分【%s】", userName, operateName, points));
// 匹配积分队列
String pointsQueue;
PointsSettingType settingType = PointsSettingType.getSettingType(setting.getType());
if (PointsSettingType.ADD == settingType) {
pointsQueue = ActiveMQConstant.Points.INCR_USER_POINTS_QUEUE;
} else if (PointsSettingType.DEC == settingType) {
pointsQueue = ActiveMQConstant.Points.DECR_USER_POINTS_QUEUE;
} else {
log.error("=> 当前积分规则的设置类型错误:{}", setting);
return;
}
// 发送积分消息
this.jmsTemplate.convertAndSend(pointsQueue, pointsDetail);
// 匹配积分队列
String pointsQueue;
PointsSettingType settingType = PointsSettingType.getSettingType(cacheSetting.getType());
if (PointsSettingType.ADD == settingType) {
pointsQueue = ActiveMQConstant.Points.INCR_USER_POINTS_QUEUE;
} else if (PointsSettingType.DEC == settingType) {
pointsQueue = ActiveMQConstant.Points.DECR_USER_POINTS_QUEUE;
} else {
throw new ServiceException("处理失败,当前积分规则的设置类型错误。");
}
// 积分消息明细
String operateName = cacheSetting.getOperateName();
Integer points = cacheSetting.getOperatePoints();
PointsDetail pointsDetail = new PointsDetail();
pointsDetail.setLinkTracId(detail.getLinkTracId());
pointsDetail.setUserName(userName);
pointsDetail.setDetailPoints(points);
pointsDetail.setDetailName(operateName);
pointsDetail.setDetailCode(operateCode);
pointsDetail.setDescription(String.format("用户【%s】操作【%s】积分【%s】", userName, operateName, points));
// 发送积分消息
this.jmsTemplate.convertAndSend(pointsQueue, pointsDetail);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment