|
@@ -1,8 +1,12 @@
|
|
package com.backendsys.modules.system.service.impl;
|
|
package com.backendsys.modules.system.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
|
+import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
|
+import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
import com.backendsys.modules.system.dao.SysUserIntegralDao;
|
|
import com.backendsys.modules.system.dao.SysUserIntegralDao;
|
|
import com.backendsys.modules.system.dao.SysUserIntegralLogDao;
|
|
import com.backendsys.modules.system.dao.SysUserIntegralLogDao;
|
|
import com.backendsys.modules.system.entity.SysUserIntegral;
|
|
import com.backendsys.modules.system.entity.SysUserIntegral;
|
|
|
|
+import com.backendsys.modules.system.entity.SysUserIntegralLog;
|
|
import com.backendsys.modules.system.service.SysUserIntegralService;
|
|
import com.backendsys.modules.system.service.SysUserIntegralService;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -14,11 +18,24 @@ import java.util.Map;
|
|
@Service
|
|
@Service
|
|
public class SysUserIntegralServiceImpl implements SysUserIntegralService {
|
|
public class SysUserIntegralServiceImpl implements SysUserIntegralService {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private HttpRequestUtil httpRequestUtil;
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private SysUserIntegralDao sysUserIntegralDao;
|
|
private SysUserIntegralDao sysUserIntegralDao;
|
|
@Autowired
|
|
@Autowired
|
|
private SysUserIntegralLogDao sysUserIntegralLogDao;
|
|
private SysUserIntegralLogDao sysUserIntegralLogDao;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 初始化用户积分
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void init(Long user_id) {
|
|
|
|
+ SysUserIntegral entity = new SysUserIntegral();
|
|
|
|
+ entity.setUser_id(user_id);
|
|
|
|
+ sysUserIntegralDao.insert(entity);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 查询积分 (如果不存在记录,则创建)
|
|
* 查询积分 (如果不存在记录,则创建)
|
|
*/
|
|
*/
|
|
@@ -45,16 +62,27 @@ public class SysUserIntegralServiceImpl implements SysUserIntegralService {
|
|
public Map<String, Object> increase(Long user_id, Integer integral) {
|
|
public Map<String, Object> increase(Long user_id, Integer integral) {
|
|
|
|
|
|
// 查询当前积分 (如果不存在记录,则创建)
|
|
// 查询当前积分 (如果不存在记录,则创建)
|
|
- Integer currentIntegral = selectIntegralByUserId(user_id);
|
|
|
|
|
|
+ Integer current_integral = selectIntegralByUserId(user_id);
|
|
|
|
+ // 计算结果积分 (当前 + 增量)
|
|
|
|
+ int target_integral = NumberUtil.add(current_integral, integral).intValue();
|
|
|
|
|
|
- Integer operation = 1; // 操作 (1:增加, -1:减少)
|
|
|
|
|
|
+ // 更新用户积分
|
|
|
|
+ SysUserIntegral entity = new SysUserIntegral();
|
|
|
|
+ entity.setIntegral(target_integral);
|
|
|
|
+ sysUserIntegralDao.update(entity, new LambdaQueryWrapper<SysUserIntegral>().eq(SysUserIntegral::getUser_id, user_id));
|
|
|
|
|
|
|
|
+ // 创建用户积分日志
|
|
|
|
+ SysUserIntegralLog entityLog = new SysUserIntegralLog();
|
|
|
|
+ entityLog.setUser_id(user_id);
|
|
|
|
+ entityLog.setContent("手动增加积分");
|
|
|
|
+ entityLog.setIntegral(integral);
|
|
|
|
+ entityLog.setOperation(1); // 操作 (1:增加, -1:减少)
|
|
|
|
+ entityLog.setOperation_user_id(SecurityUtil.getUserId()); // 操作人 (手动操作必填)
|
|
|
|
+ entityLog.setOrigin(1); // 来源 (1:手动操作, 2:..)
|
|
|
|
+ entityLog.setOperation_ip(httpRequestUtil.getIpAddr()); // IP
|
|
|
|
+ sysUserIntegralLogDao.insert(entityLog);
|
|
|
|
|
|
- Map<String, Object> resp = new LinkedHashMap<>();
|
|
|
|
- resp.put("user_id", user_id);
|
|
|
|
- resp.put("integral", integral);
|
|
|
|
- resp.put("operation", operation);
|
|
|
|
- return resp;
|
|
|
|
|
|
+ return Map.of("user_id", user_id);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -64,14 +92,26 @@ public class SysUserIntegralServiceImpl implements SysUserIntegralService {
|
|
public Map<String, Object> decrease(Long user_id, Integer integral) {
|
|
public Map<String, Object> decrease(Long user_id, Integer integral) {
|
|
|
|
|
|
// 查询当前积分 (如果不存在记录,则创建)
|
|
// 查询当前积分 (如果不存在记录,则创建)
|
|
- Integer currentIntegral = selectIntegralByUserId(user_id);
|
|
|
|
|
|
+ Integer current_integral = selectIntegralByUserId(user_id);
|
|
|
|
+ // 计算结果积分 (当前 - 增量)
|
|
|
|
+ int target_integral = NumberUtil.sub(current_integral, integral).intValue();
|
|
|
|
+
|
|
|
|
+ // 更新用户积分
|
|
|
|
+ SysUserIntegral entity = new SysUserIntegral();
|
|
|
|
+ entity.setIntegral(target_integral);
|
|
|
|
+ sysUserIntegralDao.update(entity, new LambdaQueryWrapper<SysUserIntegral>().eq(SysUserIntegral::getUser_id, user_id));
|
|
|
|
|
|
- Integer operation = -1; // 操作 (1:增加, -1:减少)
|
|
|
|
|
|
+ // 创建用户积分日志
|
|
|
|
+ SysUserIntegralLog entityLog = new SysUserIntegralLog();
|
|
|
|
+ entityLog.setUser_id(user_id);
|
|
|
|
+ entityLog.setContent("手动减少积分");
|
|
|
|
+ entityLog.setIntegral(integral);
|
|
|
|
+ entityLog.setOperation(-1); // 操作 (1:增加, -1:减少)
|
|
|
|
+ entityLog.setOperation_user_id(SecurityUtil.getUserId()); // 操作人 (手动操作必填)
|
|
|
|
+ entityLog.setOrigin(1); // 来源 (1:手动操作, 2:..)
|
|
|
|
+ entityLog.setOperation_ip(httpRequestUtil.getIpAddr()); // IP
|
|
|
|
+ sysUserIntegralLogDao.insert(entityLog);
|
|
|
|
|
|
- Map<String, Object> resp = new LinkedHashMap<>();
|
|
|
|
- resp.put("user_id", user_id);
|
|
|
|
- resp.put("integral", integral);
|
|
|
|
- resp.put("operation", operation);
|
|
|
|
- return resp;
|
|
|
|
|
|
+ return Map.of("user_id", user_id);
|
|
}
|
|
}
|
|
}
|
|
}
|