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

优化代码

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