Commit 3c1b3b18 authored by yuwenwen's avatar yuwenwen
parents 036cecd4 2dc614da
...@@ -93,7 +93,7 @@ export default { ...@@ -93,7 +93,7 @@ export default {
}, },
// 增加积分 // 增加积分
handleAddPoints(row) { handleAddPoints(row) {
this.$prompt('增加积分', '提示', { this.$prompt('增加积分,当前用户可用积分:' + row.currentPoints, '提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
cancelButtonText: '取消', cancelButtonText: '取消',
inputPattern: /^[1-9]\d*$/, inputPattern: /^[1-9]\d*$/,
...@@ -117,7 +117,7 @@ export default { ...@@ -117,7 +117,7 @@ export default {
}, },
// 扣减积分 // 扣减积分
handleDecreasePoints(row) { handleDecreasePoints(row) {
this.$prompt('扣减积分', '提示', { this.$prompt('扣减积分,当前用户可用积分:' + row.currentPoints, '提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
cancelButtonText: '取消', cancelButtonText: '取消',
inputPattern: /^[1-9]\d*$/, inputPattern: /^[1-9]\d*$/,
......
...@@ -9,6 +9,7 @@ import com.tangguo.common.enums.BusinessType; ...@@ -9,6 +9,7 @@ import com.tangguo.common.enums.BusinessType;
import com.tangguo.common.utils.poi.ExcelUtil; import com.tangguo.common.utils.poi.ExcelUtil;
import com.tangguo.domain.BbsUserPoints; import com.tangguo.domain.BbsUserPoints;
import com.tangguo.domain.BbsUserPointsDetail; import com.tangguo.domain.BbsUserPointsDetail;
import com.tangguo.domain.bo.BatchUserPointsBO;
import com.tangguo.service.IBbsUserPointsDetailService; import com.tangguo.service.IBbsUserPointsDetailService;
import com.tangguo.service.IBbsUserPointsService; import com.tangguo.service.IBbsUserPointsService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 用户积分Controller * 用户积分Controller
...@@ -115,4 +117,32 @@ public class BbsUserPointsController extends BaseController { ...@@ -115,4 +117,32 @@ public class BbsUserPointsController extends BaseController {
return AjaxResult.success(); return AjaxResult.success();
} }
/**
* 增加用户积分
*/
@RepeatSubmit(interval = 1000)
@ApiOperation("新增用户积分")
@PreAuthorize("@ss.hasPermi('points:points:incr')")
@Log(title = "用户积分", businessType = BusinessType.UPDATE)
@PutMapping("/batch/incr")
public AjaxResult batchIncrUserPoints(@RequestBody BatchUserPointsBO bo) {
List<Map<String, String>> results = this.bbsUserPointsService.batchAddUserPoints(bo);
return AjaxResult.success(results);
}
/**
* 扣减用户积分
*/
@RepeatSubmit(interval = 1000)
@ApiOperation("扣减用户积分")
@PreAuthorize("@ss.hasPermi('points:points:decr')")
@Log(title = "用户积分", businessType = BusinessType.UPDATE)
@PutMapping("/batch/decr")
public AjaxResult batchDecrUserPoints(@RequestBody BatchUserPointsBO bo) {
List<Map<String, String>> results = this.bbsUserPointsService.batchDeleteUserPoints(bo);
return AjaxResult.success(results);
}
} }
...@@ -8,6 +8,7 @@ import com.tangguo.common.core.page.TableDataInfo; ...@@ -8,6 +8,7 @@ import com.tangguo.common.core.page.TableDataInfo;
import com.tangguo.common.enums.BusinessType; import com.tangguo.common.enums.BusinessType;
import com.tangguo.common.utils.poi.ExcelUtil; import com.tangguo.common.utils.poi.ExcelUtil;
import com.tangguo.domain.BbsUserPointsExchange; import com.tangguo.domain.BbsUserPointsExchange;
import com.tangguo.domain.bo.UserPointsExchangeBO;
import com.tangguo.service.IBbsUserPointsExchangeService; import com.tangguo.service.IBbsUserPointsExchangeService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 用户积分兑换Controller * 用户积分兑换Controller
...@@ -69,10 +71,22 @@ public class BbsUserPointsExchangeController extends BaseController { ...@@ -69,10 +71,22 @@ public class BbsUserPointsExchangeController extends BaseController {
@Log(title = "用户积分兑换", businessType = BusinessType.INSERT) @Log(title = "用户积分兑换", businessType = BusinessType.INSERT)
@PostMapping("/exchange") @PostMapping("/exchange")
public AjaxResult exchange(@RequestBody BbsUserPointsExchange exchange) { public AjaxResult exchange(@RequestBody BbsUserPointsExchange exchange) {
synchronized (exchange.getUserName().intern()) { this.bbsUserPointsExchangeService.addUserPointsExchange(exchange);
this.bbsUserPointsExchangeService.addUserPointsExchange(exchange);
}
return AjaxResult.success(); return AjaxResult.success();
} }
/**
* 用户积分兑换
*/
@RepeatSubmit(interval = 1000)
@ApiOperation("用户积分兑换")
@PreAuthorize("@ss.hasPermi('points:exchange:exchange')")
@Log(title = "用户积分兑换", businessType = BusinessType.INSERT)
@PostMapping("/batch/exchange")
public AjaxResult batchExchange(@RequestBody UserPointsExchangeBO bo) {
List<Map<String, String>> results = this.bbsUserPointsExchangeService.batchAddUserPointsExchange(bo);
return AjaxResult.success(results);
}
} }
package com.tangguo.domain.bo;
import lombok.Data;
import java.util.List;
import java.util.Set;
/**
*
*
* @author 谈笑
* @createTime 2025-11-06 10:21:06 星期四
*/
@Data
public class BatchUserPointsBO {
/**
* 用户
*/
private List<User> users;
/**
* 积分
*/
private Integer points;
/**
* 备注
*/
private String remarks;
@Data
public static class User {
/**
* 用户姓名
*/
private String nickName;
/**
* 用户名
*/
private String userName;
}
}
package com.tangguo.domain.bo;
import lombok.Data;
import java.util.List;
/**
*
*
* @author 谈笑
* @createTime 2025-11-06 14:29:35 星期四
*/
@Data
public class UserPointsExchangeBO {
/**
* 用户名
*/
private List<User> users;
/**
* 商品编码
*/
private String goodsCode;
@Data
public static class User {
/**
* 用户姓名
*/
private String nickName;
/**
* 用户名
*/
private String userName;
}
}
...@@ -2,6 +2,7 @@ package com.tangguo.mapper; ...@@ -2,6 +2,7 @@ package com.tangguo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tangguo.domain.BbsGrade; import com.tangguo.domain.BbsGrade;
import org.apache.ibatis.annotations.Param;
/** /**
* 用户等级Mapper接口 * 用户等级Mapper接口
...@@ -10,4 +11,7 @@ import com.tangguo.domain.BbsGrade; ...@@ -10,4 +11,7 @@ import com.tangguo.domain.BbsGrade;
* @date 2025-08-28 * @date 2025-08-28
*/ */
public interface BbsGradeMapper extends BaseMapper<BbsGrade> { public interface BbsGradeMapper extends BaseMapper<BbsGrade> {
BbsGrade selectGradeByPoints(@Param("points") int points);
} }
...@@ -2,8 +2,10 @@ package com.tangguo.service; ...@@ -2,8 +2,10 @@ package com.tangguo.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.tangguo.domain.BbsUserPointsExchange; import com.tangguo.domain.BbsUserPointsExchange;
import com.tangguo.domain.bo.UserPointsExchangeBO;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 用户积分兑换Service接口 * 用户积分兑换Service接口
...@@ -23,10 +25,18 @@ public interface IBbsUserPointsExchangeService extends IService<BbsUserPointsExc ...@@ -23,10 +25,18 @@ public interface IBbsUserPointsExchangeService extends IService<BbsUserPointsExc
/** /**
* 添加用户积分兑换记录 * 用户积分兑换
* *
* @param exchange 兑换 * @param exchange 兑换
*/ */
void addUserPointsExchange(BbsUserPointsExchange exchange); void addUserPointsExchange(BbsUserPointsExchange exchange);
/**
* 批量用户积分兑换
*
* @param bo 兑换
*/
List<Map<String, String>> batchAddUserPointsExchange(UserPointsExchangeBO bo);
} }
...@@ -3,8 +3,10 @@ package com.tangguo.service; ...@@ -3,8 +3,10 @@ package com.tangguo.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.tangguo.common.domain.PointsDetail; import com.tangguo.common.domain.PointsDetail;
import com.tangguo.domain.BbsUserPoints; import com.tangguo.domain.BbsUserPoints;
import com.tangguo.domain.bo.BatchUserPointsBO;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 用户积分Service接口 * 用户积分Service接口
...@@ -66,6 +68,22 @@ public interface IBbsUserPointsService extends IService<BbsUserPoints> { ...@@ -66,6 +68,22 @@ public interface IBbsUserPointsService extends IService<BbsUserPoints> {
void deleteUserPoints(BbsUserPoints points); void deleteUserPoints(BbsUserPoints points);
/**
* 增加用户积分
*
* @param bo 积分
*/
List<Map<String, String>> batchAddUserPoints(BatchUserPointsBO bo);
/**
* 扣减用户积分
*
* @param bo 积分
*/
List<Map<String, String>> batchDeleteUserPoints(BatchUserPointsBO bo);
/** /**
* 增加用户积分 * 增加用户积分
* *
......
...@@ -192,11 +192,7 @@ public class BbsGradeServiceImpl extends ServiceImpl<BbsGradeMapper, BbsGrade> i ...@@ -192,11 +192,7 @@ public class BbsGradeServiceImpl extends ServiceImpl<BbsGradeMapper, BbsGrade> i
*/ */
@Override @Override
public BbsGrade getGradeByPoints(int points) { public BbsGrade getGradeByPoints(int points) {
return this.getOne( return this.baseMapper.selectGradeByPoints(points);
Wrappers.lambdaQuery(BbsGrade.class)
.le(BbsGrade::getMinPoints, points)
.ge(BbsGrade::getMaxPoints, points)
);
} }
} }
...@@ -254,7 +254,7 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap ...@@ -254,7 +254,7 @@ public class BbsPointsSettingServiceImpl extends ServiceImpl<BbsPointsSettingMap
pointsDetail.setDetailPoints(points); pointsDetail.setDetailPoints(points);
pointsDetail.setDetailName(operateName); pointsDetail.setDetailName(operateName);
pointsDetail.setDetailCode(operateCode); pointsDetail.setDetailCode(operateCode);
pointsDetail.setDescription(String.format("用户【%s】操作【%s】积分【%s】", userName, operateName, points)); pointsDetail.setDescription(String.format("用户【%s】操作【%s】%s【%s】", userName, operateName, settingType.getDesc(), points));
// 发送积分消息 // 发送积分消息
this.jmsTemplate.convertAndSend(pointsQueue, pointsDetail); this.jmsTemplate.convertAndSend(pointsQueue, pointsDetail);
......
...@@ -6,16 +6,18 @@ import com.tangguo.common.exception.ServiceException; ...@@ -6,16 +6,18 @@ import com.tangguo.common.exception.ServiceException;
import com.tangguo.domain.BbsPointsGoods; import com.tangguo.domain.BbsPointsGoods;
import com.tangguo.domain.BbsUserPoints; import com.tangguo.domain.BbsUserPoints;
import com.tangguo.domain.BbsUserPointsExchange; import com.tangguo.domain.BbsUserPointsExchange;
import com.tangguo.domain.bo.UserPointsExchangeBO;
import com.tangguo.mapper.BbsUserPointsExchangeMapper; import com.tangguo.mapper.BbsUserPointsExchangeMapper;
import com.tangguo.service.IBbsPointsGoodsService; import com.tangguo.service.IBbsPointsGoodsService;
import com.tangguo.service.IBbsUserPointsExchangeService; import com.tangguo.service.IBbsUserPointsExchangeService;
import com.tangguo.service.IBbsUserPointsService; import com.tangguo.service.IBbsUserPointsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.*;
import java.util.Objects;
/** /**
* 用户积分兑换Service业务层处理 * 用户积分兑换Service业务层处理
...@@ -23,18 +25,19 @@ import java.util.Objects; ...@@ -23,18 +25,19 @@ import java.util.Objects;
* @author ruoyi * @author ruoyi
* @date 2025-08-29 * @date 2025-08-29
*/ */
@Slf4j
@Service @Service
public class BbsUserPointsExchangeServiceImpl extends ServiceImpl<BbsUserPointsExchangeMapper, BbsUserPointsExchange> implements IBbsUserPointsExchangeService { public class BbsUserPointsExchangeServiceImpl extends ServiceImpl<BbsUserPointsExchangeMapper, BbsUserPointsExchange> implements IBbsUserPointsExchangeService {
@Resource
private BbsUserPointsExchangeMapper bbsUserPointsExchangeMapper;
@Resource @Resource
private IBbsUserPointsService userPointsService; private IBbsUserPointsService userPointsService;
@Resource @Resource
private IBbsPointsGoodsService pointsGoodsService; private IBbsPointsGoodsService pointsGoodsService;
@Resource
private TransactionTemplate transactionTemplate;
/** /**
* 查询用户积分兑换列表 * 查询用户积分兑换列表
...@@ -49,60 +52,105 @@ public class BbsUserPointsExchangeServiceImpl extends ServiceImpl<BbsUserPointsE ...@@ -49,60 +52,105 @@ public class BbsUserPointsExchangeServiceImpl extends ServiceImpl<BbsUserPointsE
/** /**
* 添加用户积分兑换记录 * 用户积分兑换
* *
* @param exchange 兑换 * @param exchange 兑换
*/ */
@Transactional(rollbackFor = Exception.class)
@Override @Override
public void addUserPointsExchange(BbsUserPointsExchange exchange) { public void addUserPointsExchange(BbsUserPointsExchange exchange) {
// 积分商品校验 this.pointsExchange(exchange.getUserName(), exchange.getNickName(), exchange.getGoodsCode());
BbsPointsGoods dbGoods = this.pointsGoodsService.getByGoodsCode(exchange.getGoodsCode());
if (Objects.isNull(dbGoods)) {
throw new ServiceException("兑换失败,未查询到当前兑换的商品数据。");
}
if (dbGoods.getStockNum() < 1) {
throw new ServiceException("兑换失败,当前兑换的商品库存数量不足。");
}
// 用户积分校验
String userName = exchange.getUserName();
Integer exchangePoints = dbGoods.getExchangePoints();
BbsUserPoints dbUserPoints = this.userPointsService.getAndInitUserPoints(userName);
int currentPoints = dbUserPoints.getCurrentPoints() - dbGoods.getExchangePoints();
if (currentPoints < 0) {
throw new ServiceException("兑换失败,当前用户可用积分不足。");
}
// 扣减商品库存
Long goodsId = dbGoods.getId();
boolean decrResult = this.pointsGoodsService.decrGoodsStock(goodsId, 1);
if (!decrResult) {
throw new ServiceException("兑换失败,当前兑换的商品库存数量不足。");
}
// 增加商品销量
this.pointsGoodsService.incrGoodsSales(goodsId, 1);
// 扣减用户积分
PointsDetail detail = new PointsDetail();
detail.setUserName(userName);
detail.setDetailPoints(exchangePoints);
detail.setDetailName(dbGoods.getName());
detail.setDetailCode(dbGoods.getCode());
detail.setDescription(String.format("用户【%s】兑换积分商品【%s-%s】", userName, dbGoods.getName(), dbGoods.getCode()));
this.userPointsService.decrUserPoints(detail);
// 添加兑换记录
BbsUserPointsExchange addExchange = new BbsUserPointsExchange();
addExchange.setNickName(exchange.getNickName());
addExchange.setUserName(dbUserPoints.getUserName());
addExchange.setUserGradeName(dbUserPoints.getGradeName());
addExchange.setGoodsName(dbGoods.getName());
addExchange.setGoodsCode(dbGoods.getCode());
addExchange.setGoodsExchangePoints(dbGoods.getExchangePoints());
addExchange.setUserRemainingPoints(currentPoints);
this.save(addExchange);
} }
/**
* 批量用户积分兑换
*
* @param bo 兑换
*/
@Override
public List<Map<String, String>> batchAddUserPointsExchange(UserPointsExchangeBO bo) {
List<Map<String, String>> results = new ArrayList<>(10);
for (UserPointsExchangeBO.User user : bo.getUsers()) {
String userName = user.getUserName();
String nickName = user.getNickName();
try {
this.pointsExchange(userName, nickName, bo.getGoodsCode());
} catch (Exception e) {
log.error("=> 批量用户积分兑换失败,用户:{} 商品:{} 失败原因:", userName, bo.getGoodsCode(), e);
Map<String, String> resultItem = new HashMap<>(2);
resultItem.put("nickName", nickName);
resultItem.put("userName", userName);
resultItem.put("errorMessage", e.getMessage());
results.add(resultItem);
}
}
return results;
}
/**
* 积分兑换
*
* @param userName 用户名
* @param goodsCode 商品编码
*/
public void pointsExchange(String userName, String nickName, String goodsCode) {
synchronized (userName.intern()) {
BbsPointsGoods dbGoods = this.pointsGoodsService.getByGoodsCode(goodsCode);
if (Objects.isNull(dbGoods)) {
throw new ServiceException("兑换失败,未查询到当前兑换的商品数据。");
}
if (dbGoods.getStockNum() < 1) {
throw new ServiceException("兑换失败,当前兑换的商品库存数量不足。");
}
this.transactionTemplate.executeWithoutResult(status -> {
try {
// 用户积分校验
Integer exchangePoints = dbGoods.getExchangePoints();
BbsUserPoints dbUserPoints = this.userPointsService.getAndInitUserPoints(userName);
int currentPoints = dbUserPoints.getCurrentPoints() - dbGoods.getExchangePoints();
if (currentPoints < 0) {
throw new ServiceException("兑换失败,当前用户可用积分不足。");
}
// 扣减商品库存
Long goodsId = dbGoods.getId();
boolean decrResult = this.pointsGoodsService.decrGoodsStock(goodsId, 1);
if (!decrResult) {
throw new ServiceException("兑换失败,当前兑换的商品库存数量不足。");
}
// 增加商品销量
this.pointsGoodsService.incrGoodsSales(goodsId, 1);
// 扣减用户积分
PointsDetail detail = new PointsDetail();
detail.setUserName(userName);
detail.setDetailPoints(exchangePoints);
detail.setDetailName(dbGoods.getName());
detail.setDetailCode(dbGoods.getCode());
detail.setDescription(String.format("用户【%s】兑换积分商品【%s-%s】", userName, dbGoods.getName(), dbGoods.getCode()));
this.userPointsService.decrUserPoints(detail);
// 添加兑换记录
BbsUserPointsExchange addExchange = new BbsUserPointsExchange();
addExchange.setNickName(nickName);
addExchange.setUserName(userName);
addExchange.setUserGradeName(dbUserPoints.getGradeName());
addExchange.setGoodsName(dbGoods.getName());
addExchange.setGoodsCode(dbGoods.getCode());
addExchange.setGoodsExchangePoints(dbGoods.getExchangePoints());
addExchange.setUserRemainingPoints(currentPoints);
this.save(addExchange);
} catch (Exception e) {
log.error("=> 用户积分兑换失败:", e);
status.setRollbackOnly();
throw new ServiceException(e.getMessage());
}
});
}
}
} }
package com.tangguo.service.impl; package com.tangguo.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.domain.PointsDetail; import com.tangguo.common.domain.PointsDetail;
import com.tangguo.common.exception.ServiceException; import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.domain.BbsGrade; import com.tangguo.domain.BbsGrade;
import com.tangguo.domain.BbsUserPoints; import com.tangguo.domain.BbsUserPoints;
import com.tangguo.domain.BbsUserPointsDetail; import com.tangguo.domain.BbsUserPointsDetail;
import com.tangguo.domain.bo.BatchUserPointsBO;
import com.tangguo.mapper.BbsUserPointsMapper; import com.tangguo.mapper.BbsUserPointsMapper;
import com.tangguo.service.IBbsGradeService; import com.tangguo.service.IBbsGradeService;
import com.tangguo.service.IBbsUserPointsDetailService; import com.tangguo.service.IBbsUserPointsDetailService;
import com.tangguo.service.IBbsUserPointsService; import com.tangguo.service.IBbsUserPointsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Objects;
/** /**
* 用户积分Service业务层处理 * 用户积分Service业务层处理
...@@ -26,6 +29,7 @@ import java.util.Objects; ...@@ -26,6 +29,7 @@ import java.util.Objects;
* @author ruoyi * @author ruoyi
* @date 2025-08-29 * @date 2025-08-29
*/ */
@Slf4j
@Service @Service
public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, BbsUserPoints> implements IBbsUserPointsService { public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, BbsUserPoints> implements IBbsUserPointsService {
...@@ -41,6 +45,10 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -41,6 +45,10 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
@Resource @Resource
private IBbsGradeService gradeService; private IBbsGradeService gradeService;
@Resource
private TransactionTemplate transactionTemplate;
/** /**
* 查询用户积分列表 * 查询用户积分列表
...@@ -119,7 +127,6 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -119,7 +127,6 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
* *
* @param points 积分 * @param points 积分
*/ */
@Transactional(rollbackFor = Exception.class)
@Override @Override
public void addUserPoints(BbsUserPoints points) { public void addUserPoints(BbsUserPoints points) {
String userName = points.getUserName(); String userName = points.getUserName();
...@@ -127,8 +134,11 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -127,8 +134,11 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
detail.setUserName(userName); detail.setUserName(userName);
detail.setDetailPoints(points.getIncrOrDecrPoints()); detail.setDetailPoints(points.getIncrOrDecrPoints());
detail.setDetailName("后台手动增加"); detail.setDetailName("后台手动增加");
// detail.setDescription(String.format("管理员【%s】后台手动增加用户【%s】积分", SecurityUtils.getUsername(), userName)); String remarks = points.getRemarks();
detail.setDescription(points.getRemarks()); if (StrUtil.isBlank(remarks)) {
remarks = String.format("管理员【%s】后台手动增加用户【%s】积分", SecurityUtils.getUsername(), userName);
}
detail.setDescription(remarks);
this.incrUserPoints(detail); this.incrUserPoints(detail);
} }
...@@ -138,7 +148,6 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -138,7 +148,6 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
* *
* @param points 积分 * @param points 积分
*/ */
@Transactional(rollbackFor = Exception.class)
@Override @Override
public void deleteUserPoints(BbsUserPoints points) { public void deleteUserPoints(BbsUserPoints points) {
String userName = points.getUserName(); String userName = points.getUserName();
...@@ -146,13 +155,86 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -146,13 +155,86 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
detail.setUserName(userName); detail.setUserName(userName);
detail.setDetailPoints(points.getIncrOrDecrPoints()); detail.setDetailPoints(points.getIncrOrDecrPoints());
detail.setDetailName("后台手动扣减"); detail.setDetailName("后台手动扣减");
// detail.setDescription(String.format("管理员【%s】后台手动扣减用户【%s】积分", SecurityUtils.getUsername(), userName)); String remarks = points.getRemarks();
detail.setDescription(points.getRemarks()); if (StrUtil.isBlank(remarks)) {
remarks = String.format("管理员【%s】后台手动扣减用户【%s】积分", SecurityUtils.getUsername(), userName);
}
detail.setDescription(remarks);
this.decrUserPoints(detail); this.decrUserPoints(detail);
} }
/** /**
* 增加用户积分
*
* @param bo 积分
*/
@Override
public List<Map<String, String>> batchAddUserPoints(BatchUserPointsBO bo) {
List<Map<String, String>> results = new ArrayList<>(10);
for (BatchUserPointsBO.User user : bo.getUsers()) {
String userName = user.getUserName();
try {
PointsDetail detail = new PointsDetail();
detail.setUserName(userName);
detail.setDetailPoints(bo.getPoints());
detail.setDetailName("后台手动增加");
String remarks = bo.getRemarks();
if (StrUtil.isBlank(remarks)) {
remarks = String.format("管理员【%s】后台手动增加用户【%s】积分", SecurityUtils.getUsername(), userName);
}
detail.setDescription(remarks);
this.incrUserPoints(detail);
} catch (Exception e) {
log.error("=> 用户:{} 增加积分:{} 失败:", userName, bo.getPoints(), e);
Map<String, String> resultItem = new HashMap<>(2);
resultItem.put("nickName", user.getNickName());
resultItem.put("userName", userName);
resultItem.put("errorMessage", e.getMessage());
results.add(resultItem);
}
}
return results;
}
/**
* 扣减用户积分
*
* @param bo 积分
*/
@Override
public List<Map<String, String>> batchDeleteUserPoints(BatchUserPointsBO bo) {
List<Map<String, String>> results = new ArrayList<>(10);
for (BatchUserPointsBO.User user : bo.getUsers()) {
String userName = user.getUserName();
try {
PointsDetail detail = new PointsDetail();
detail.setUserName(userName);
detail.setDetailPoints(bo.getPoints());
detail.setDetailName("后台手动增加");
String remarks = bo.getRemarks();
if (StrUtil.isBlank(remarks)) {
remarks = String.format("管理员【%s】后台手动扣减用户【%s】积分", SecurityUtils.getUsername(), userName);
}
detail.setDescription(remarks);
this.decrUserPoints(detail);
} catch (Exception e) {
log.error("=> 用户:{} 扣减积分:{} 失败:", userName, bo.getPoints(), e);
Map<String, String> resultItem = new HashMap<>(2);
resultItem.put("nickName", user.getNickName());
resultItem.put("userName", userName);
resultItem.put("errorMessage", e.getMessage());
results.add(resultItem);
}
}
return results;
}
/**
* 增加用户积分 * 增加用户积分
* *
* @param detail 积分明细 * @param detail 积分明细
...@@ -183,41 +265,49 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -183,41 +265,49 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
* *
* @param detail 积分明细 * @param detail 积分明细
*/ */
@Transactional(rollbackFor = Exception.class)
public void privIncrUserPoints(PointsDetail detail) { public void privIncrUserPoints(PointsDetail detail) {
int points = detail.getDetailPoints(); int points = detail.getDetailPoints();
if (points < 1) { if (points < 1) {
throw new ServiceException("增加用户积分失败,增加的积分分值不能小于0。"); throw new ServiceException("增加用户积分失败,增加的积分分值不能小于0。");
} }
// 更新用户积分 this.transactionTemplate.executeWithoutResult(status -> {
String userName = detail.getUserName(); try {
BbsUserPoints dbUserPoints = this.pointsServiceImpl.getAndInitUserPoints(userName); // 更新用户积分
int beforeCurrentPoints = dbUserPoints.getCurrentPoints(); String userName = detail.getUserName();
int afterCurrentPoints = beforeCurrentPoints + points; BbsUserPoints dbUserPoints = this.pointsServiceImpl.getAndInitUserPoints(userName);
int accumulatedPoints = dbUserPoints.getAccumulatedPoints() + points; int beforeCurrentPoints = dbUserPoints.getCurrentPoints();
dbUserPoints.setCurrentPoints(afterCurrentPoints); int afterCurrentPoints = beforeCurrentPoints + points;
dbUserPoints.setAccumulatedPoints(accumulatedPoints); int accumulatedPoints = dbUserPoints.getAccumulatedPoints() + points;
dbUserPoints.setCurrentPoints(afterCurrentPoints);
// 更新用户等级 dbUserPoints.setAccumulatedPoints(accumulatedPoints);
BbsGrade dbGrade = this.gradeService.getGradeByPoints(accumulatedPoints);
if (Objects.nonNull(dbGrade) && !dbGrade.getCode().equals(dbUserPoints.getGradeCode())) { // 更新用户等级
dbUserPoints.setGradeName(dbGrade.getName()); BbsGrade dbGrade = this.gradeService.getGradeByPoints(accumulatedPoints);
dbUserPoints.setGradeCode(dbGrade.getCode()); if (Objects.nonNull(dbGrade) && !dbGrade.getCode().equals(dbUserPoints.getGradeCode())) {
dbUserPoints.setLastUpgradeTime(new Date()); dbUserPoints.setGradeName(dbGrade.getName());
} dbUserPoints.setGradeCode(dbGrade.getCode());
this.updateById(dbUserPoints); dbUserPoints.setLastUpgradeTime(new Date());
}
// 添加用户积分明细 this.updateById(dbUserPoints);
BbsUserPointsDetail newDetail = new BbsUserPointsDetail();
newDetail.setUserName(userName); // 添加用户积分明细
newDetail.setDetailName(detail.getDetailName()); BbsUserPointsDetail newDetail = new BbsUserPointsDetail();
newDetail.setDetailCode(detail.getDetailCode()); newDetail.setUserName(userName);
newDetail.setDetailPoints(points); newDetail.setDetailName(detail.getDetailName());
newDetail.setBeforePoints(beforeCurrentPoints); newDetail.setDetailCode(detail.getDetailCode());
newDetail.setAfterPoints(afterCurrentPoints); newDetail.setDetailPoints(points);
newDetail.setDescription(detail.getDescription()); newDetail.setBeforePoints(beforeCurrentPoints);
this.pointsDetailService.save(newDetail); newDetail.setAfterPoints(afterCurrentPoints);
newDetail.setDescription(detail.getDescription());
this.pointsDetailService.save(newDetail);
} catch (Exception e) {
log.error("=> 处理增加用户积分失败:", e);
status.setRollbackOnly();
throw new ServiceException(e.getMessage());
}
});
} }
...@@ -226,37 +316,44 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B ...@@ -226,37 +316,44 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
* *
* @param detail 积分明细 * @param detail 积分明细
*/ */
@Transactional(rollbackFor = Exception.class)
public void privDecrUserPoints(PointsDetail detail) { public void privDecrUserPoints(PointsDetail detail) {
int points = detail.getDetailPoints(); int points = detail.getDetailPoints();
if (points < 1) { if (points < 1) {
throw new ServiceException("扣减用户积分失败,扣减的积分分值不能小于0。"); throw new ServiceException("扣减用户积分失败,扣减的积分分值不能小于0。");
} }
String userName = detail.getUserName(); this.transactionTemplate.executeWithoutResult(status -> {
BbsUserPoints dbUserPoints = this.pointsServiceImpl.getAndInitUserPoints(userName); try {
Integer beforeCurrentPoints = dbUserPoints.getCurrentPoints(); String userName = detail.getUserName();
if (points > beforeCurrentPoints) { BbsUserPoints dbUserPoints = this.pointsServiceImpl.getAndInitUserPoints(userName);
throw new ServiceException("扣减用户积分失败,当前用户可用积分不足。"); Integer beforeCurrentPoints = dbUserPoints.getCurrentPoints();
} if (points > beforeCurrentPoints) {
throw new ServiceException("扣减用户积分失败,当前用户可用积分不足。");
// 更新用户积分 }
int afterCurrentPoints = beforeCurrentPoints - points;
BbsUserPoints updUserPoints = new BbsUserPoints(); // 更新用户积分
updUserPoints.setId(dbUserPoints.getId()); int afterCurrentPoints = beforeCurrentPoints - points;
updUserPoints.setCurrentPoints(afterCurrentPoints); BbsUserPoints updUserPoints = new BbsUserPoints();
this.updateById(updUserPoints); updUserPoints.setId(dbUserPoints.getId());
updUserPoints.setCurrentPoints(afterCurrentPoints);
// 添加用户积分明细 this.updateById(updUserPoints);
BbsUserPointsDetail newDetail = new BbsUserPointsDetail();
newDetail.setUserName(userName); // 添加用户积分明细
newDetail.setDetailName(detail.getDetailName()); BbsUserPointsDetail newDetail = new BbsUserPointsDetail();
newDetail.setDetailCode(detail.getDetailCode()); newDetail.setUserName(userName);
newDetail.setDetailPoints(points); newDetail.setDetailName(detail.getDetailName());
newDetail.setBeforePoints(beforeCurrentPoints); newDetail.setDetailCode(detail.getDetailCode());
newDetail.setAfterPoints(afterCurrentPoints); newDetail.setDetailPoints(points);
newDetail.setDescription(detail.getDescription()); newDetail.setBeforePoints(beforeCurrentPoints);
this.pointsDetailService.save(newDetail); newDetail.setAfterPoints(afterCurrentPoints);
newDetail.setDescription(detail.getDescription());
this.pointsDetailService.save(newDetail);
} catch (Exception e) {
status.setRollbackOnly();
throw new ServiceException(e.getMessage());
}
});
} }
} }
...@@ -3,5 +3,18 @@ ...@@ -3,5 +3,18 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tangguo.mapper.BbsGradeMapper"> <mapper namespace="com.tangguo.mapper.BbsGradeMapper">
<select id="selectGradeByPoints" resultType="com.tangguo.domain.BbsGrade">
SELECT
*
FROM
bbs_grade g
WHERE
(g.min_points &lt;= #{points} AND g.max_points >= #{points}) OR (#{points} > g.max_points)
ORDER BY
g.max_points DESC
LIMIT
1
</select>
</mapper> </mapper>
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