Merge remote-tracking branch 'origin/master'

master
袁野 2 months ago
commit d705c43194
  1. 31
      cweb/src/main/java/com/hfkj/controller/UserGradeController.java
  2. 113
      schedule/src/main/java/com/hfkj/schedule/UserGradeSchedule.java
  3. 56
      service/src/main/java/com/hfkj/common/utils/ListUtil.java
  4. 7
      service/src/main/java/com/hfkj/service/user/BsUserService.java
  5. 3
      service/src/main/java/com/hfkj/service/user/impl/BsUserAccountServiceImpl.java
  6. 3
      service/src/main/java/com/hfkj/service/user/impl/BsUserGradeServiceImpl.java
  7. 13
      service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode; import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter; import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.MemberValidateUtil;
import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.RedisUtil;
@ -15,6 +16,7 @@ import com.hfkj.service.user.BsUserAccountRecordService;
import com.hfkj.service.user.BsUserAccountService; import com.hfkj.service.user.BsUserAccountService;
import com.hfkj.service.user.BsUserGradeService; import com.hfkj.service.user.BsUserGradeService;
import com.hfkj.service.user.BsUserService; import com.hfkj.service.user.BsUserService;
import com.hfkj.service.user.impl.BsUserServiceImpl;
import com.hfkj.sysenum.user.UserAccountRecordSourceTypeEnum; import com.hfkj.sysenum.user.UserAccountRecordSourceTypeEnum;
import com.hfkj.sysenum.user.UserGradeEnum; import com.hfkj.sysenum.user.UserGradeEnum;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -48,6 +50,29 @@ public class UserGradeController {
@Resource @Resource
private BsUserGradeService userGradeService; private BsUserGradeService userGradeService;
@RequestMapping(value = "/promote", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "等级晋升")
public ResponseData promote() {
try {
UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
// 等级晋升
userGradeService.promote(userSession.getUser().getId());
// 更新session
UserSessionObject session = new UserSessionObject();
session.setUser(userService.getUser(userSession.getUser().getId()));
SessionObject sessionObject = new SessionObject(BsUserServiceImpl.userToken(userSession.getUser().getId()), session);
userCenter.save(sessionObject);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/payGrade2", method = RequestMethod.POST) @RequestMapping(value = "/payGrade2", method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "购买【优淘会员】等级") @ApiOperation(value = "购买【优淘会员】等级")
@ -58,6 +83,12 @@ public class UserGradeController {
// 购买 // 购买
userGradeService.payGrade2(userSession.getUser().getId()); userGradeService.payGrade2(userSession.getUser().getId());
// 更新session
UserSessionObject session = new UserSessionObject();
session.setUser(userService.getUser(userSession.getUser().getId()));
SessionObject sessionObject = new SessionObject(BsUserServiceImpl.userToken(userSession.getUser().getId()), session);
userCenter.save(sessionObject);
return ResponseMsgUtil.success("操作成功"); return ResponseMsgUtil.success("操作成功");
} catch (Exception e) { } catch (Exception e) {

@ -0,0 +1,113 @@
package com.hfkj.schedule;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ListUtil;
import com.hfkj.entity.BsUser;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.user.BsUserGradeService;
import com.hfkj.service.user.BsUserService;
import com.hfkj.service.user.impl.BsUserServiceImpl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @className: UserGradeSchedule
* @author: HuRui
* @date: 2024/9/27
**/
@Component
public class UserGradeSchedule {
@Resource
private BsUserService userService;
@Resource
private BsUserGradeService userGradeService;
@Resource
private UserCenter userCenter;
@Scheduled(cron = "0 0 5 * * ?") // 每日凌晨05:00:00 执行一次
public void promote() {
try {
// 用户数据
List<BsUser> userList = userService.getList(new HashMap<>());
// 设备核心数目
int processorsNum = Runtime.getRuntime().availableProcessors();
Long startTime = System.currentTimeMillis();
System.out.println("本次更新任务开始");
// 初始化线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
processorsNum * 2,
processorsNum * 2,
4,
TimeUnit.SECONDS,
new ArrayBlockingQueue(processorsNum * 2 * 10),
new ThreadPoolExecutor.DiscardPolicy());
// 大集合拆分成N个小集合,然后用多线程去处理数据,确保不会因为数据量过大导致执行过慢
List<List<BsUser>> splitNList = ListUtil.splitList(userList, 200);
// 记录单个任务的执行次数
CountDownLatch countDownLatch = new CountDownLatch(splitNList.size());
// 对拆分的集合进行批量处理, 先拆分的集合, 再多线程执行
for (List<BsUser> singleList : splitNList) {
// 线程池执行
threadPool.execute(new Thread(new Runnable(){
@Override
public void run() {
//模拟执行时间
System.out.println("当前线程:"+Thread.currentThread().getName());
try {
for (BsUser user : singleList) {
try {
// 等级晋升
userGradeService.promote(user.getId());
// 更新session
UserSessionObject session = new UserSessionObject();
session.setUser(userService.getUser(user.getId()));
SessionObject sessionObject = new SessionObject(BsUserServiceImpl.userToken(user.getId()), session);
userCenter.save(sessionObject);
} catch (Exception e) {
System.out.println("更新油站失败");
}
}
} catch (Exception e) {
System.out.println("更新油站出现异常");
System.out.println(e.getMessage());
} finally {
// 任务个数 - 1, 直至为0时唤醒await()
countDownLatch.countDown();
}
}
}));
}
try {
// 让当前线程处于阻塞状态,直到锁存器计数为零
countDownLatch.await();
} catch (Exception e) {
System.out.println("系统出现异常");
}
Long endTime = System.currentTimeMillis();
Long useTime = endTime - startTime;
System.out.println("本次更新任务结束,共计用时"+useTime+"毫秒");
} catch (Exception e) {
System.out.println("更新价格失败!!!");
}
}
}

@ -0,0 +1,56 @@
package com.hfkj.common.utils;
import com.alibaba.excel.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* 集合工具类
* @className: ListUtil
* @author: HuRui
* @date: 2024/9/27
**/
public class ListUtil {
/**
* 拆分集合
*
* @param <T> 泛型对象
* @param resList 需要拆分的集合
* @param subListLength 每个子集合的元素个数
* @return 返回拆分后的各个集合组成的列表
**/
public static <T> List<List<T>> splitList(List<T> resList, int subListLength) {
if (CollectionUtils.isEmpty(resList) || subListLength <= 0) {
return new ArrayList<>();
}
List<List<T>> ret = new ArrayList<>();
int size = resList.size();
if (size <= subListLength) {
// 数据量不足 subListLength 指定的大小
ret.add(resList);
} else {
int pre = size / subListLength;
int last = size % subListLength;
// 前面pre个集合,每个大小都是 subListLength 个元素
for (int i = 0; i < pre; i++) {
List<T> itemList = new ArrayList<>(subListLength);
for (int j = 0; j < subListLength; j++) {
itemList.add(resList.get(i * subListLength + j));
}
ret.add(itemList);
}
// last的进行处理
if (last > 0) {
List<T> itemList = new ArrayList<>(last);
for (int i = 0; i < last; i++) {
itemList.add(resList.get(pre * subListLength + i));
}
ret.add(itemList);
}
}
return ret;
}
}

@ -4,6 +4,7 @@ import com.hfkj.common.security.SessionObject;
import com.hfkj.entity.BsUser; import com.hfkj.entity.BsUser;
import com.hfkj.sysenum.user.UserLoginType; import com.hfkj.sysenum.user.UserLoginType;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -54,6 +55,12 @@ public interface BsUserService {
*/ */
BsUser getUser(String userPhone); BsUser getUser(String userPhone);
/**
* 查询列表
* @param param
* @return
*/
List<BsUser> getList(Map<String,Object> param);
/** /**
* 用户注册 * 用户注册
* @param phone 手机号 * @param phone 手机号

@ -111,9 +111,6 @@ public class BsUserAccountServiceImpl implements BsUserAccountService {
record.setSourceContent(MapUtils.getString(otherParam, "sourceContent")); record.setSourceContent(MapUtils.getString(otherParam, "sourceContent"));
userAccountRecordService.create(record); userAccountRecordService.create(record);
// 是否达到升级条件
userGradeService.promote(userId);
// 释放锁 // 释放锁
redisTemplate.delete(lockKey); redisTemplate.delete(lockKey);

@ -84,9 +84,6 @@ public class BsUserGradeServiceImpl implements BsUserGradeService {
otherParam.put("sourceContent", "升级优淘会员"); otherParam.put("sourceContent", "升级优淘会员");
// 支付3元宝 // 支付3元宝
userAccountService.consume(user.getId(),new BigDecimal("3"), UserAccountRecordSourceTypeEnum.type2, otherParam); userAccountService.consume(user.getId(),new BigDecimal("3"), UserAccountRecordSourceTypeEnum.type2, otherParam);
// 等级晋升
promote(userId);
} }
@Override @Override

@ -144,9 +144,6 @@ public class BsUserServiceImpl implements BsUserService {
userParentRel.setUserId(userId); userParentRel.setUserId(userId);
userParentRelService.editData(userParentRel); userParentRelService.editData(userParentRel);
// 检查上级等级晋升
userGradeService.promote(userParentRel.getParentUserId());
// 更新session // 更新session
UserSessionObject session = new UserSessionObject(); UserSessionObject session = new UserSessionObject();
session.setUser(user); session.setUser(user);
@ -178,6 +175,16 @@ public class BsUserServiceImpl implements BsUserService {
return null; return null;
} }
@Override
public List<BsUser> getList(Map<String, Object> param) {
BsUserExample example = new BsUserExample();
example.createCriteria().andStatusNotEqualTo(0);
example.setOrderByClause("create_time desc");
return userMapper.selectByExample(example);
}
@Override @Override
@Transactional(propagation= Propagation.REQUIRED,rollbackFor= {RuntimeException.class}) @Transactional(propagation= Propagation.REQUIRED,rollbackFor= {RuntimeException.class})
public BsUser register(String phone, Map<String, Object> other, Long inviteUseId) { public BsUser register(String phone, Map<String, Object> other, Long inviteUseId) {

Loading…
Cancel
Save