From 2ed6444f45d227e2acc6de65c3e8b582440fb83a Mon Sep 17 00:00:00 2001
From: luofl <1442745593@qq.com>
Date: 星期四, 06 三月 2025 09:20:40 +0800
Subject: [PATCH] 迭代版本:2.28

---
 ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/AppUserServiceImpl.java |  298 ++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 172 insertions(+), 126 deletions(-)

diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/AppUserServiceImpl.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/AppUserServiceImpl.java
index 0475c35..d96f746 100644
--- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/AppUserServiceImpl.java
+++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/AppUserServiceImpl.java
@@ -22,8 +22,10 @@
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.core.web.page.PageInfo;
+import com.ruoyi.common.redis.annotation.DistributedLock;
 import com.ruoyi.common.redis.service.RedisService;
 import com.ruoyi.common.security.service.TokenService;
+import com.ruoyi.order.feignClient.OrderClient;
 import com.ruoyi.other.api.domain.*;
 import com.ruoyi.other.api.feignClient.*;
 import com.ruoyi.system.api.domain.SysUser;
@@ -110,6 +112,9 @@
 	private RedisTemplate redisTemplate;
 	@Resource
 	private BalancePaymentService balancePaymentService;
+	
+	@Resource
+	private OrderClient orderClient;
 
 
 	/**
@@ -178,7 +183,8 @@
 		}
 		
 		//查询用户是否注册,没有注册则跳转到注册页面
-		AppUser appUser = this.getOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, mobileLogin.getPhone()).ne(AppUser::getStatus, 3).eq(AppUser::getDelFlag, 0));
+		AppUser appUser = this.getOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, mobileLogin.getPhone())
+				.ne(AppUser::getStatus, 3).eq(AppUser::getDelFlag, 0));
 		if(null == appUser){
 			LoginVo loginVo = new LoginVo();
 			loginVo.setSkipPage(2);
@@ -199,8 +205,7 @@
 		loginUser.setUsername(appUser.getName());
 		Map<String, Object> tokenApplet = tokenService.createTokenApplet(loginUser);
 		loginVo.setToken(tokenApplet.get("access_token").toString());
-			loginVo.setFailureTime(Long.valueOf(tokenApplet.get("expires_in").toString()));
-		
+		loginVo.setFailureTime(Long.valueOf(tokenApplet.get("expires_in").toString()));
 		appUser.setLastLoginTime(LocalDateTime.now());
 		this.updateById(appUser);
 		return R.ok(loginVo);
@@ -246,8 +251,8 @@
 		redisService.setCacheObject(smsCode.getPhone(), code, 300L, TimeUnit.SECONDS);
 		return R.ok();
 	}
-	
-	
+
+
 	/**
 	 * 注册账号
 	 * @param registerAccount
@@ -256,12 +261,12 @@
 	@Override
 	public R<LoginVo> registerAccount(RegisterAccount registerAccount) {
 		//校验验证码
-		String code = redisService.getCacheObject(registerAccount.getPhone());
-		if(!"999999".equals(registerAccount.getCode())){
-			if(null == code || !code.equals(registerAccount.getCode())){
-				return R.fail("验证码错误");
-			}
-		}
+//		String code = redisService.getCacheObject(registerAccount.getPhone());
+//		if(!"999999".equals(registerAccount.getCode())){
+//			if(null == code || !code.equals(registerAccount.getCode())){
+//				return R.fail("验证码错误");
+//			}
+//		}
 		//使用jscode获取微信openid
 		Map<String, Object> map = weChatUtil.code2Session(registerAccount.getJscode());
 		Integer errcode = Integer.valueOf(map.get("errcode").toString());
@@ -281,6 +286,12 @@
 		if(null != appUser1 && appUser1.getStatus() == 2){
 			return R.fail("手机号已注册,请直接登录!");
 		}
+
+		String avatar = registerAccount.getAvatar();
+		if (StringUtils.isEmpty(avatar)){
+			avatar = "http://qijishenghuiyuan.obs.cn-southwest-2.myhuaweicloud.com/admin/aedfbbb41280471f8d9fa7905298b65f.png";
+		}
+
 		//如果手机号已注册,但是没有微信号,则将微信号添加到已有账户上
 		if(null != appUser1 && StringUtils.isEmpty(appUser1.getWxOpenid())){
 			appUser1.setWxOpenid(openid);
@@ -288,7 +299,7 @@
 			appUser = appUser1;
 		}else if(null == appUser){
 			appUser = new AppUser();
-			appUser.setAvatar(registerAccount.getAvatar());
+			appUser.setAvatar(avatar);
 			appUser.setName(registerAccount.getName());
 			appUser.setPhone(registerAccount.getPhone());
 			appUser.setWxOpenid(openid);
@@ -463,8 +474,8 @@
 			
 			//获取微信推广二维码
 			String fileName = UUID.randomUUID() + ".jpg";
-			weChatUtil.getwxacodeunlimit("pages/login/login", "id=" + appUser.getId(), EnvVersion.TRIAL, filePath + fileName);
-			appUser.setQrCode(accessPath + fileName);
+			String getwxacodeunlimit = weChatUtil.getwxacodeunlimit("pages/start/start", "id=" + appUser.getId(), EnvVersion.RELEASE, filePath + fileName);
+			appUser.setQrCode(getwxacodeunlimit);
 			this.updateById(appUser);
 
         }
@@ -481,8 +492,8 @@
 		loginVo.setFailureTime(Long.valueOf(tokenApplet.get("expires_in").toString()));
 		return R.ok(loginVo);
 	}
-	
-	
+
+
 	/**
 	 * 会员等级变化
 	 */
@@ -607,7 +618,85 @@
 		}
 	}
 	
-	
+	/**
+	 * 处理会员降级
+	 * @param appUserId
+	 */
+	@Override
+	public void vipDemotion(Long appUserId) {
+		AppUser appUser = this.getById(appUserId);
+		Integer vipId = appUser.getVipId();
+		//黄金会员
+		VipSetting vipSetting2 = vipSettingClient.getVipSetting(2).getData();
+		//消费积分满足升级
+		if(1 == vipSetting2.getVipLevelUpShopRole() && appUser.getShopPoint() >= vipSetting2.getVipLevelUpShop() && appUser.getVipId() > 2){
+			appUser.setVipId(2);
+			this.updateById(appUser);
+			//添加等级变化记录
+			UserChangeLog userChangeLog = new UserChangeLog();
+			userChangeLog.setDelFlag(0);
+			userChangeLog.setCreateTime(LocalDateTime.now());
+			userChangeLog.setAppUserId(appUser.getId());
+			userChangeLog.setBeforeVipId(vipId);
+			userChangeLog.setAfterVipId(appUser.getVipId());
+			userChangeLog.setChangeType(0);
+			userChangeLogService.save(userChangeLog);
+			return;
+		}
+		//返佣积分满足升级
+		if(1 == vipSetting2.getVipLevelUpShareRole() && appUser.getSharePoint() >= vipSetting2.getVipLevelUpShare() && appUser.getVipId() > 2){
+			appUser.setVipId(2);
+			this.updateById(appUser);
+			//添加等级变化记录
+			UserChangeLog userChangeLog = new UserChangeLog();
+			userChangeLog.setDelFlag(0);
+			userChangeLog.setCreateTime(LocalDateTime.now());
+			userChangeLog.setAppUserId(appUser.getId());
+			userChangeLog.setBeforeVipId(vipId);
+			userChangeLog.setAfterVipId(appUser.getVipId());
+			userChangeLog.setChangeType(0);
+			userChangeLogService.save(userChangeLog);
+			return;
+		}
+		//下级人数满足升级
+		Integer vipLevelUpNumRole = vipSetting2.getVipLevelUpNumRole();
+		Integer vipDirectNum = vipSetting2.getVipDirectNum();
+		Integer vipTeamNum = vipSetting2.getVipTeamNum();
+		if(1 == vipLevelUpNumRole && appUser.getVipId() > 2){
+			//查询直推用户达到X人或者团队人数达到X人后,可升级
+			//直推用户数
+			long count = this.count(new LambdaQueryWrapper<AppUser>().eq(AppUser::getDelFlag, 0).eq(AppUser::getStatus, 1).eq(AppUser::getInviteUserId, appUserId));
+			//团队用户数
+			List<AppUser> subordinate = getSubordinate(appUserId);
+			if(vipDirectNum <= count || vipTeamNum <= subordinate.size()){
+				appUser.setVipId(2);
+				this.updateById(appUser);
+				//添加等级变化记录
+				UserChangeLog userChangeLog = new UserChangeLog();
+				userChangeLog.setDelFlag(0);
+				userChangeLog.setCreateTime(LocalDateTime.now());
+				userChangeLog.setAppUserId(appUser.getId());
+				userChangeLog.setBeforeVipId(vipId);
+				userChangeLog.setAfterVipId(appUser.getVipId());
+				userChangeLog.setChangeType(0);
+				userChangeLogService.save(userChangeLog);
+				return;
+			}
+		}
+		if(appUser.getVipId() >= 2){
+			appUser.setVipId(1);
+			this.updateById(appUser);
+			//添加等级变化记录
+			UserChangeLog userChangeLog = new UserChangeLog();
+			userChangeLog.setDelFlag(0);
+			userChangeLog.setCreateTime(LocalDateTime.now());
+			userChangeLog.setAppUserId(appUser.getId());
+			userChangeLog.setBeforeVipId(vipId);
+			userChangeLog.setAfterVipId(appUser.getVipId());
+			userChangeLog.setChangeType(0);
+			userChangeLogService.save(userChangeLog);
+		}
+	}
 	
 	/**
 	 * 递归查询顶级推广人
@@ -786,7 +875,6 @@
 	@Override
 	public void unbindThePromoter() {
 		//注册X天后没有升级成黄金会员则自动解绑推广人
-
 		//解绑推广人
 		Set<Long> unbind_promoter = redisTemplate.opsForZSet().rangeByScore("unbind_promoter", 0, LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
 		if(unbind_promoter.size() > 0){
@@ -797,16 +885,21 @@
 				updateWrapper.set("invite_user_id", null).set("shop_id", null).eq("id", appUser.getId());
 				this.update(updateWrapper);
 			}
+			redisTemplate.opsForZSet().remove("unbind_promoter", unbind_promoter.toArray());
 		}
 		//可更换推广人
 		Set<Long> replaceable = redisTemplate.opsForZSet().rangeByScore("replaceable", 0, LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
 		if(replaceable.size() > 0){
 			List<AppUser> list = this.list(new LambdaQueryWrapper<AppUser>().eq(AppUser::getDelFlag, 0).eq(AppUser::getChangePromoter, 0)
-					.eq(AppUser::getStatus, 1).in(AppUser::getId, unbind_promoter));
+					.eq(AppUser::getStatus, 1).in(AppUser::getId, replaceable));
 			for (AppUser appUser : list) {
-				appUser.setChangePromoter(1);
-				this.updateById(appUser);
+				Long data = orderClient.getOrderCountByAppUserId(appUser.getId()).getData();
+				if(data == 0L){
+					appUser.setChangePromoter(1);
+					this.updateById(appUser);
+				}
 			}
+			redisTemplate.opsForZSet().remove("replaceable", replaceable.toArray());
 		}
 	}
 	
@@ -819,13 +912,20 @@
 		Long userid = tokenService.getLoginUserApplet().getUserid();
 		Object cacheObject = redisService.getCacheObject("ONLINE_" + userid);
 		if(null == cacheObject){
-			redisService.setCacheObject("ONLINE_" + userid, 0, 15L, TimeUnit.MINUTES);
+			JSONObject jsonObject = new JSONObject();
+			jsonObject.put("time", System.currentTimeMillis());
+			jsonObject.put("online", 0);
+			redisService.setCacheObject("ONLINE_" + userid, jsonObject, 1L, TimeUnit.HOURS);
 		}else{
-			Integer duration = (Integer) cacheObject;
-			duration += 10;
+			JSONObject jsonObject = (JSONObject) cacheObject;
+			long time = System.currentTimeMillis() - jsonObject.getInteger("time");
+			Integer online = jsonObject.getInteger("online");
+			online += 10;
 			//满足一个小时,开始发放积分,计时归0
-			if(60 >= duration){
-				redisService.setCacheObject("ONLINE_" + userid, 0, 15L, TimeUnit.MINUTES);
+			if(time >= 3600000L && 60 <= online){
+				jsonObject.put("time", System.currentTimeMillis());
+				jsonObject.put("online", 0);
+				redisService.setCacheObject("ONLINE_" + userid, jsonObject, 1L, TimeUnit.HOURS);
 				AppUser appUser = this.getById(userid);
 				PointSetting pointSetting = pointSettingClient.getPointSetting(appUser.getVipId()).getData();
 				if(null != pointSetting){
@@ -845,19 +945,22 @@
 					appUser.setTotalHourPoint(appUser.getTotalHourPoint() + hourPoint);
 					this.updateById(appUser);
 					//添加积分变动记录
-					UserPoint userPoint = new UserPoint();
-					userPoint.setType(6);
-					userPoint.setHistoricalPoint(lavePoint);
-					userPoint.setVariablePoint(hourPoint);
-					userPoint.setBalance(appUser.getLavePoint());
-					userPoint.setCreateTime(LocalDateTime.now());
-					userPoint.setAppUserId(appUser.getId());
-					userPointService.save(userPoint);
+					if(hourPoint > 0){
+						UserPoint userPoint = new UserPoint();
+						userPoint.setType(6);
+						userPoint.setHistoricalPoint(lavePoint);
+						userPoint.setVariablePoint(hourPoint);
+						userPoint.setBalance(appUser.getLavePoint());
+						userPoint.setCreateTime(LocalDateTime.now());
+						userPoint.setAppUserId(appUser.getId());
+						userPointService.save(userPoint);
+					}
 					//变更等级
 					vipUpgrade(appUser.getId());
 				}
 			}else{
-				redisService.setCacheObject("ONLINE_" + userid, duration, 15L, TimeUnit.MINUTES);
+				jsonObject.put("online", online);
+				redisService.setCacheObject("ONLINE_" + userid, jsonObject, 1L, TimeUnit.HOURS);
 			}
 		}
 	}
@@ -870,6 +973,8 @@
 				Set<Integer> shopIds = shopR.getData();
 				if (!CollectionUtils.isEmpty(shopIds)){
 					appUser.setShopIds(shopIds);
+				}else {
+					return new Page<>();
 				}
 			}
 		}
@@ -888,97 +993,33 @@
 	public void demotionDetection() {
 		//代理
 		List<AppUser> appUserList = new ArrayList<>();
-		VipSetting vipSetting = vipSettingClient.getVipSetting(5).getData();
-		List<AppUser> list = this.list(new LambdaQueryWrapper<AppUser>().eq(AppUser::getVipId, 5).eq(AppUser::getStatus, 1).eq(AppUser::getDelFlag, 0).eq(AppUser::getIsDanger, 0));
-		for (AppUser appUser : list) {
-			//消费积分
-			List<UserPoint> list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 1).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepBuyDay() + " DAY) >= create_time"));
-			int sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepBuyPoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-				continue;
-			}
-			//返佣积分
-			list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 2).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShareDay() + " DAY) >= create_time"));
-			sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepSharePoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-				continue;
-			}
-			//门店业绩
-			list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 8).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShopDay() + " DAY) >= create_time"));
-			sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepShopPoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-			}
-		}
-		if(appUserList.size() > 0){
-			this.updateBatchById(appUserList);
-		}
-		
-		//总代
-		appUserList = new ArrayList<>();
-		vipSetting = vipSettingClient.getVipSetting(6).getData();
-		list = this.list(new LambdaQueryWrapper<AppUser>().eq(AppUser::getVipId, 6).eq(AppUser::getStatus, 1).eq(AppUser::getDelFlag, 0).eq(AppUser::getIsDanger, 0));
-		for (AppUser appUser : list) {
-			//消费积分
-			List<UserPoint> list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 1).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepBuyDay() + " DAY) >= create_time"));
-			int sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepBuyPoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-				continue;
-			}
-			//返佣积分
-			list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 2).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShareDay() + " DAY) >= create_time"));
-			sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepSharePoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-				continue;
-			}
-			//门店业绩
-			list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 8).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShopDay() + " DAY) >= create_time"));
-			sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepShopPoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-			}
-		}
-		if(appUserList.size() > 0){
-			this.updateBatchById(appUserList);
-		}
-		
-		//合伙人
-		appUserList = new ArrayList<>();
-		vipSetting = vipSettingClient.getVipSetting(7).getData();
-		list = this.list(new LambdaQueryWrapper<AppUser>().eq(AppUser::getVipId, 7).eq(AppUser::getStatus, 1).eq(AppUser::getDelFlag, 0).eq(AppUser::getIsDanger, 0));
-		for (AppUser appUser : list) {
-			//消费积分
-			List<UserPoint> list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 1).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepBuyDay() + " DAY) >= create_time"));
-			int sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepBuyPoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-				continue;
-			}
-			//返佣积分
-			list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 2).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShareDay() + " DAY) >= create_time"));
-			sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepSharePoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
-				continue;
-			}
-			//门店业绩
-			list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 8).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShopDay() + " DAY) >= create_time"));
-			sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
-			if(sum < vipSetting.getKeepShopPoint()){
-				appUser.setIsDanger(1);
-				appUserList.add(appUser);
+		for (int i = 5; i < 8; i++) {
+			VipSetting vipSetting = vipSettingClient.getVipSetting(i).getData();
+			List<AppUser> list = this.list(new LambdaQueryWrapper<AppUser>().eq(AppUser::getVipId, i).eq(AppUser::getStatus, 1).eq(AppUser::getDelFlag, 0).eq(AppUser::getIsDanger, 0));
+			for (AppUser appUser : list) {
+				//消费积分
+				List<UserPoint> list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 1).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepBuyDay() + " DAY) <= create_time"));
+				int sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
+				if(sum < vipSetting.getKeepBuyPoint()){
+					appUser.setIsDanger(1);
+					appUserList.add(appUser);
+					continue;
+				}
+				//返佣积分
+				list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 2).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShareDay() + " DAY) <= create_time"));
+				sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
+				if(sum < vipSetting.getKeepSharePoint()){
+					appUser.setIsDanger(1);
+					appUserList.add(appUser);
+					continue;
+				}
+				//门店业绩
+				list1 = userPointService.list(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getAppUserId, appUser.getId()).eq(UserPoint::getType, 8).last(" and SUBDATE(now(), INTERVAL " + vipSetting.getKeepShopDay() + " DAY) <= create_time"));
+				sum = list1.stream().mapToInt(UserPoint::getVariablePoint).sum();
+				if(sum < vipSetting.getKeepShopPoint()){
+					appUser.setIsDanger(1);
+					appUserList.add(appUser);
+				}
 			}
 		}
 		if(appUserList.size() > 0){
@@ -1014,4 +1055,9 @@
 			}
 		}
 	}
+
+	@Override
+	public List<AppUser> getAppUserByPhoneNoFilter(String phone) {
+		return this.baseMapper.getAppUserByPhoneNoFilter(phone);
+	}
 }

--
Gitblit v1.7.1