From 9d163755914c72922f02af8f941bea8ebd255e9f Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Fri, 27 Sep 2024 16:21:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E7=AD=89?= =?UTF-8?q?=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hfkj/controller/UserGradeController.java | 31 +++++ .../com/hfkj/schedule/UserGradeSchedule.java | 113 ++++++++++++++++++ .../java/com/hfkj/common/utils/ListUtil.java | 56 +++++++++ .../com/hfkj/service/user/BsUserService.java | 7 ++ .../user/impl/BsUserAccountServiceImpl.java | 3 - .../user/impl/BsUserGradeServiceImpl.java | 3 - .../service/user/impl/BsUserServiceImpl.java | 13 +- 7 files changed, 217 insertions(+), 9 deletions(-) create mode 100644 schedule/src/main/java/com/hfkj/schedule/UserGradeSchedule.java create mode 100644 service/src/main/java/com/hfkj/common/utils/ListUtil.java diff --git a/cweb/src/main/java/com/hfkj/controller/UserGradeController.java b/cweb/src/main/java/com/hfkj/controller/UserGradeController.java index ca31e2b..b6297e8 100644 --- a/cweb/src/main/java/com/hfkj/controller/UserGradeController.java +++ b/cweb/src/main/java/com/hfkj/controller/UserGradeController.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; +import com.hfkj.common.security.SessionObject; import com.hfkj.common.security.UserCenter; import com.hfkj.common.utils.MemberValidateUtil; 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.BsUserGradeService; import com.hfkj.service.user.BsUserService; +import com.hfkj.service.user.impl.BsUserServiceImpl; import com.hfkj.sysenum.user.UserAccountRecordSourceTypeEnum; import com.hfkj.sysenum.user.UserGradeEnum; import io.swagger.annotations.Api; @@ -48,6 +50,29 @@ public class UserGradeController { @Resource 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) @ResponseBody @ApiOperation(value = "购买【优淘会员】等级") @@ -58,6 +83,12 @@ public class UserGradeController { // 购买 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("操作成功"); } catch (Exception e) { diff --git a/schedule/src/main/java/com/hfkj/schedule/UserGradeSchedule.java b/schedule/src/main/java/com/hfkj/schedule/UserGradeSchedule.java new file mode 100644 index 0000000..37fb154 --- /dev/null +++ b/schedule/src/main/java/com/hfkj/schedule/UserGradeSchedule.java @@ -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 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> splitNList = ListUtil.splitList(userList, 200); + // 记录单个任务的执行次数 + CountDownLatch countDownLatch = new CountDownLatch(splitNList.size()); + // 对拆分的集合进行批量处理, 先拆分的集合, 再多线程执行 + for (List 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("更新价格失败!!!"); + } + } + + + + +} diff --git a/service/src/main/java/com/hfkj/common/utils/ListUtil.java b/service/src/main/java/com/hfkj/common/utils/ListUtil.java new file mode 100644 index 0000000..706b902 --- /dev/null +++ b/service/src/main/java/com/hfkj/common/utils/ListUtil.java @@ -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 泛型对象 + * @param resList 需要拆分的集合 + * @param subListLength 每个子集合的元素个数 + * @return 返回拆分后的各个集合组成的列表 + **/ + public static List> splitList(List resList, int subListLength) { + if (CollectionUtils.isEmpty(resList) || subListLength <= 0) { + return new ArrayList<>(); + } + List> 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 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 itemList = new ArrayList<>(last); + for (int i = 0; i < last; i++) { + itemList.add(resList.get(pre * subListLength + i)); + } + ret.add(itemList); + } + } + return ret; + } +} diff --git a/service/src/main/java/com/hfkj/service/user/BsUserService.java b/service/src/main/java/com/hfkj/service/user/BsUserService.java index 965a949..8b701d5 100644 --- a/service/src/main/java/com/hfkj/service/user/BsUserService.java +++ b/service/src/main/java/com/hfkj/service/user/BsUserService.java @@ -4,6 +4,7 @@ import com.hfkj.common.security.SessionObject; import com.hfkj.entity.BsUser; import com.hfkj.sysenum.user.UserLoginType; +import java.util.List; import java.util.Map; /** @@ -54,6 +55,12 @@ public interface BsUserService { */ BsUser getUser(String userPhone); + /** + * 查询列表 + * @param param + * @return + */ + List getList(Map param); /** * 用户注册 * @param phone 手机号 diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserAccountServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserAccountServiceImpl.java index f0b6765..b9bbf57 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserAccountServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserAccountServiceImpl.java @@ -111,9 +111,6 @@ public class BsUserAccountServiceImpl implements BsUserAccountService { record.setSourceContent(MapUtils.getString(otherParam, "sourceContent")); userAccountRecordService.create(record); - // 是否达到升级条件 - userGradeService.promote(userId); - // 释放锁 redisTemplate.delete(lockKey); diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserGradeServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserGradeServiceImpl.java index adca966..8df4b3d 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserGradeServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserGradeServiceImpl.java @@ -84,9 +84,6 @@ public class BsUserGradeServiceImpl implements BsUserGradeService { otherParam.put("sourceContent", "升级优淘会员"); // 支付3元宝 userAccountService.consume(user.getId(),new BigDecimal("3"), UserAccountRecordSourceTypeEnum.type2, otherParam); - - // 等级晋升 - promote(userId); } @Override diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java index 8e40089..39846ae 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java @@ -144,9 +144,6 @@ public class BsUserServiceImpl implements BsUserService { userParentRel.setUserId(userId); userParentRelService.editData(userParentRel); - // 检查上级等级晋升 - userGradeService.promote(userParentRel.getParentUserId()); - // 更新session UserSessionObject session = new UserSessionObject(); session.setUser(user); @@ -178,6 +175,16 @@ public class BsUserServiceImpl implements BsUserService { return null; } + @Override + public List getList(Map param) { + BsUserExample example = new BsUserExample(); + example.createCriteria().andStatusNotEqualTo(0); + + + example.setOrderByClause("create_time desc"); + return userMapper.selectByExample(example); + } + @Override @Transactional(propagation= Propagation.REQUIRED,rollbackFor= {RuntimeException.class}) public BsUser register(String phone, Map other, Long inviteUseId) {