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

优化代码

parent 72a253bb
package com.tangguo.controller;
import com.tangguo.common.annotation.Log;
import com.tangguo.common.annotation.RepeatSubmit;
import com.tangguo.common.core.controller.BaseController;
import com.tangguo.common.core.domain.AjaxResult;
import com.tangguo.common.core.page.TableDataInfo;
......@@ -80,6 +81,7 @@ public class BbsUserPointsController extends BaseController {
/**
* 增加用户积分
*/
@RepeatSubmit(interval = 1000)
@ApiOperation("新增用户积分")
@PreAuthorize("@ss.hasPermi('points:points:incr')")
@Log(title = "用户积分", businessType = BusinessType.UPDATE)
......@@ -93,6 +95,7 @@ public class BbsUserPointsController extends BaseController {
/**
* 扣减用户积分
*/
@RepeatSubmit(interval = 1000)
@ApiOperation("扣减用户积分")
@PreAuthorize("@ss.hasPermi('points:points:decr')")
@Log(title = "用户积分", businessType = BusinessType.UPDATE)
......
......@@ -3,6 +3,7 @@ package com.tangguo.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tangguo.common.annotation.Log;
import com.tangguo.common.annotation.RepeatSubmit;
import com.tangguo.common.core.controller.BaseController;
import com.tangguo.common.core.domain.AjaxResult;
import com.tangguo.common.core.page.TableDataInfo;
......@@ -100,15 +101,15 @@ public class BbsUserPointsExchangeController extends BaseController {
}
/**
* 用户积分兑换
*/
@RepeatSubmit(interval = 1000)
@ApiOperation("用户积分兑换")
@PreAuthorize("@ss.hasPermi('points:points:exchange')")
@Log(title = "用户积分兑换", businessType = BusinessType.INSERT)
@PostMapping("/exchange")
public synchronized AjaxResult exchange(@RequestBody BbsUserPointsExchange exchange) {
public AjaxResult exchange(@RequestBody BbsUserPointsExchange exchange) {
synchronized (exchange.getUserName().intern()) {
this.bbsUserPointsExchangeService.addUserPointsExchange(exchange);
}
......
......@@ -15,9 +15,9 @@ import java.util.List;
*/
public interface BbsUserPointsExchangeMapper extends BaseMapper<BbsUserPointsExchange> {
List<QwmhSysUserVO> selectPointsUsers(@Param("keyword") String keyword);
List<BbsUserPointsExchange> selectBbsUserPointsExchangeList(@Param("exchange") BbsUserPointsExchange exchange);
List<BbsUserPointsExchange> selectBbsUserPointsExchangeList(@Param("exchange") BbsUserPointsExchange exchange);
List<QwmhSysUserVO> selectPointsUsers(@Param("keyword") String keyword);
}
......@@ -18,9 +18,15 @@ public interface BbsUserPointsMapper extends BaseMapper<BbsUserPoints> {
List<BbsUserPoints> selectBbsUserPointsList(@Param("points") BbsUserPoints points);
QwmhSysUserVO selectQwmhUser(@Param("userName") String userName);
QwmhSysUserVO selectQwmhUsers(@Param("userName") String userName);
BbsUserPoints selectUserPoints(@Param("userName") String userName);
int initUserPointsGrade();
int refreshUserPointsGrade();
}
......@@ -61,4 +61,13 @@ public interface IBbsGradeService extends IService<BbsGrade> {
*/
BbsGrade getInitialGrade();
/**
* 通过积分查询等级
*
* @param points 积分
* @return 等级
*/
BbsGrade getGradeByPoints(int points);
}
......@@ -175,7 +175,26 @@ public class BbsGradeServiceImpl extends ServiceImpl<BbsGradeMapper, BbsGrade> i
@Override
public BbsGrade getInitialGrade() {
return this.getOne(
Wrappers.lambdaQuery(BbsGrade.class).eq(BbsGrade::getIsInitial, IsInitialGrade.YES.getValue())
Wrappers.lambdaQuery(BbsGrade.class)
.eq(BbsGrade::getIsInitial, IsInitialGrade.YES.getValue())
.orderByDesc(BbsGrade::getCreateTime)
.last("LIMIT 1")
);
}
/**
* 通过积分查询等级
*
* @param points 积分
* @return 等级
*/
@Override
public BbsGrade getGradeByPoints(int points) {
return this.getOne(
Wrappers.lambdaQuery(BbsGrade.class)
.le(BbsGrade::getMinPoints, points)
.ge(BbsGrade::getMaxPoints, points)
);
}
......
......@@ -96,8 +96,8 @@ public class BbsUserPointsExchangeServiceImpl extends ServiceImpl<BbsUserPointsE
// 添加兑换记录
BbsUserPointsExchange addExchange = new BbsUserPointsExchange();
addExchange.setNikeName(exchange.getNikeName());
addExchange.setUserName(exchange.getUserName());
addExchange.setUserGradeName(exchange.getUserGradeName());
addExchange.setUserName(dbUserPoints.getUserName());
addExchange.setUserGradeName(dbUserPoints.getGradeName());
addExchange.setGoodsName(dbGoods.getName());
addExchange.setGoodsCode(dbGoods.getCode());
addExchange.setGoodsExchangePoints(dbGoods.getExchangePoints());
......
......@@ -17,6 +17,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
......@@ -65,20 +66,19 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
public BbsUserPoints getUserPoints(String userName) {
BbsUserPoints dbUserPoints = this.baseMapper.selectUserPoints(userName);
if (Objects.isNull(dbUserPoints)) {
QwmhSysUserVO qwmhSysUser = this.baseMapper.selectQwmhUser(userName);
QwmhSysUserVO qwmhSysUser = this.baseMapper.selectQwmhUsers(userName);
if (Objects.isNull(qwmhSysUser)) {
throw new ServiceException("查询用户积分失败,未查询到当前用户数据。");
} else {
dbUserPoints = new BbsUserPoints();
// 用户初始积分
dbUserPoints.setUserName(userName);
dbUserPoints.setAccumulatedPoints(0);
dbUserPoints.setCurrentPoints(0);
// 用户初始等级
BbsGrade dbGrade = this.gradeService.getInitialGrade();
if (Objects.nonNull(dbGrade)) {
dbUserPoints.setGradeCode(dbGrade.getCode());
dbUserPoints.setGradeName(dbGrade.getName());
dbUserPoints.setLastUpgradeTime(new Date());
}
this.save(dbUserPoints);
}
......@@ -164,10 +164,19 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
// 更新用户积分
String userName = detail.getUserName();
BbsUserPoints userPoints = this.pointsServiceImpl.getUserPoints(userName);
Integer currentPoints = userPoints.getCurrentPoints();
int incrCurrentPoints = currentPoints + points;
userPoints.setCurrentPoints(incrCurrentPoints);
userPoints.setAccumulatedPoints(userPoints.getAccumulatedPoints() + points);
int beforeCurrentPoints = userPoints.getCurrentPoints();
int afterCurrentPoints = beforeCurrentPoints + points;
userPoints.setCurrentPoints(afterCurrentPoints);
int accumulatedPoints = userPoints.getAccumulatedPoints() + points;
userPoints.setAccumulatedPoints(accumulatedPoints);
// 更新用户等级
BbsGrade dbGrade = this.gradeService.getGradeByPoints(accumulatedPoints);
if (Objects.nonNull(dbGrade) && !dbGrade.getCode().equals(userPoints.getGradeCode())) {
userPoints.setGradeName(dbGrade.getName());
userPoints.setGradeCode(dbGrade.getCode());
userPoints.setLastUpgradeTime(new Date());
}
this.updateById(userPoints);
// 添加用户积分明细
......@@ -176,8 +185,8 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
newDetail.setDetailName(detail.getDetailName());
newDetail.setDetailCode(detail.getDetailCode());
newDetail.setDetailPoints(points);
newDetail.setBeforePoints(currentPoints);
newDetail.setAfterPoints(incrCurrentPoints);
newDetail.setBeforePoints(beforeCurrentPoints);
newDetail.setAfterPoints(afterCurrentPoints);
newDetail.setDescription(detail.getDescription());
this.pointsDetailService.save(newDetail);
}
......@@ -198,13 +207,13 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
// 更新用户积分
String userName = detail.getUserName();
BbsUserPoints userPoints = this.pointsServiceImpl.getUserPoints(userName);
Integer currentPoints = userPoints.getCurrentPoints();
if (points > currentPoints) {
Integer beforeCurrentPoints = userPoints.getCurrentPoints();
if (points > beforeCurrentPoints) {
throw new ServiceException("扣减用户积分失败,当前用户可用积分不足。");
}
int decrCurrentPoints = currentPoints - points;
userPoints.setCurrentPoints(decrCurrentPoints);
int afterCurrentPoints = beforeCurrentPoints - points;
userPoints.setCurrentPoints(afterCurrentPoints);
this.updateById(userPoints);
// 添加用户积分明细
......@@ -213,8 +222,8 @@ public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, B
newDetail.setDetailName(detail.getDetailName());
newDetail.setDetailCode(detail.getDetailCode());
newDetail.setDetailPoints(points);
newDetail.setBeforePoints(currentPoints);
newDetail.setAfterPoints(decrCurrentPoints);
newDetail.setBeforePoints(beforeCurrentPoints);
newDetail.setAfterPoints(afterCurrentPoints);
newDetail.setDescription(detail.getDescription());
this.pointsDetailService.save(newDetail);
}
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tangguo.mapper.BbsUserGradeMapper">
</mapper>
......@@ -4,30 +4,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tangguo.mapper.BbsUserPointsExchangeMapper">
<select id="selectPointsUsers" resultType="com.tangguo.domain.vo.QwmhSysUserVO">
SELECT
uv.user_name,
uv.nike_name,
IFNULL(p.current_points, 0) AS current_points,
IFNULL(p.accumulated_points, 0) AS accumulated_points,
IFNULL(p.grade_name, ug.name) AS grade_name
FROM
qwmh_sys_user_view uv
CROSS JOIN (
SELECT (SELECT name FROM bbs_grade WHERE is_initial = 1) AS name
) ug
LEFT JOIN
bbs_user_points p ON p.user_name = uv.user_name
<where>
<if test="keyword != null and keyword != ''">
nike_name LIKE CONCAT('%', #{keyword}, '%') OR user_name LIKE CONCAT('%', #{keyword}, '%')
</if>
</where>
ORDER BY
uv.create_time DESC
</select>
<select id="selectBbsUserPointsExchangeList" resultType="com.tangguo.domain.BbsUserPointsExchange">
SELECT
e.*
......@@ -57,4 +33,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
e.create_time DESC
</select>
<select id="selectPointsUsers" resultType="com.tangguo.domain.vo.QwmhSysUserVO">
SELECT
uv.nike_name,
uv.user_name,
uv.dept_name,
IFNULL(p.current_points, 0) AS current_points,
IFNULL(p.accumulated_points, 0) AS accumulated_points,
IFNULL(p.grade_name, ug.name) AS grade_name
FROM
qwmh_sys_user_view uv
LEFT JOIN
bbs_user_points p ON p.user_name = uv.user_name
CROSS JOIN (
SELECT (SELECT name FROM bbs_grade WHERE is_initial = 1 ORDER BY create_time DESC LIMIT 1) AS name
) ug
<where>
<if test="keyword != null and keyword != ''">
nike_name LIKE CONCAT('%', #{keyword}, '%') OR user_name LIKE CONCAT('%', #{keyword}, '%')
</if>
</where>
ORDER BY
uv.create_time DESC
</select>
</mapper>
......@@ -6,16 +6,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectBbsUserPointsList" resultType="com.tangguo.domain.BbsUserPoints">
SELECT
p.id,
IFNULL(p.accumulated_points, 0) AS accumulated_points,
IFNULL(p.current_points, 0) AS current_points,
uv.user_name,
uv.nike_name,
uv.dept_name
uv.user_name,
uv.dept_name,
IFNULL(p.current_points, 0) AS current_points,
IFNULL(p.accumulated_points, 0) AS accumulated_points,
IFNULL(p.grade_name, ug.name) AS grade_name
FROM
qwmh_sys_user_view uv
LEFT JOIN
bbs_user_points p ON p.user_name = uv.user_name
CROSS JOIN (
SELECT (SELECT name FROM bbs_grade WHERE is_initial = 1 ORDER BY create_time DESC LIMIT 1) AS name
) ug
<where>
<if test="points.nikeName != null and points.nikeName != ''">
uv.nike_name LIKE CONCAT('%', #{points.nikeName}, '%')
......@@ -31,11 +34,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</if>
</where>
ORDER BY
p.accumulated_points DESC
uv.create_time DESC
</select>
<select id="selectQwmhUser" resultType="com.tangguo.domain.vo.QwmhSysUserVO">
<select id="selectQwmhUsers" resultType="com.tangguo.domain.vo.QwmhSysUserVO">
SELECT * FROM qwmh_sys_user_view WHERE user_name = #{userName}
</select>
......@@ -44,4 +47,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
SELECT * FROM bbs_user_points WHERE user_name = #{userName}
</select>
<update id="initUserPointsGrade">
</update>
<update id="refreshUserPointsGrade">
UPDATE
bbs_user_points p
LEFT JOIN
(
SELECT * FROM bbs_grade ORDER BY create_time LIMIT 1
) g ON (g.min_points &lt;= p.accumulated_points AND g.max_points >= p.accumulated_points)
SET
p.grade_code = g.`code`, p.grade_name = g.`name`, p.last_upgrade_time = NOW()
WHERE
p.grade_code != g.code
</update>
</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