From cb734129b410c02275c0f2fc5e820d6ccc0e17c0 Mon Sep 17 00:00:00 2001
From: linwe <872216996@qq.com>
Date: Tue, 18 Jun 2024 18:12:20 +0800
Subject: [PATCH] 出租屋查询房东信息

---
 src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java |   83 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 75 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java b/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
index 0776471..7ed7f80 100644
--- a/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
+++ b/src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
@@ -39,6 +39,7 @@
 import org.springblade.core.mp.base.BaseServiceImpl;
 import org.springblade.core.mp.support.Condition;
 import org.springblade.core.mp.support.Query;
+import org.springblade.core.redis.cache.BladeRedis;
 import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tenant.BladeTenantProperties;
 import org.springblade.core.tool.constant.BladeConstant;
@@ -65,10 +66,15 @@
 import org.springblade.modules.system.vo.UserDetailVO;
 import org.springblade.modules.system.vo.UserVO;
 import org.springblade.modules.system.wrapper.UserWrapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.ReturnType;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 import static org.springblade.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD;
 
@@ -88,6 +94,10 @@
 	private final IDeptService deptService;
 	private final BladeTenantProperties tenantProperties;
 	private final IPoliceAffairsGridService policeAffairsGridService;
+
+	@Autowired
+	private BladeRedis redisTemplate;
+
 
 	@Override
 	@Transactional(rollbackFor = Exception.class)
@@ -215,6 +225,42 @@
 		return b;
 	}
 
+	/**
+	 * 尝试获取锁
+	 * @param timeout 超时时间(毫秒)
+	 * @return 是否成功获取锁
+	 */
+	public boolean lock(long timeout,String lockKey) {
+		long endTime = System.currentTimeMillis() + timeout;
+		ValueOperations<String, Object> ops = redisTemplate.getValueOps();
+		while (System.currentTimeMillis() < endTime) {
+			// 使用setIfAbsent命令尝试设置值,仅当key不存在时设置,类似于SETNX
+			Boolean result = ops.setIfAbsent(lockKey, String.valueOf(System.currentTimeMillis() + 5000), 5, TimeUnit.SECONDS);
+			if (result != null && result) {
+				return true;
+			}
+			try {
+				// 短暂休眠,防止CPU过度占用
+				Thread.sleep(10);
+			} catch (InterruptedException e) {
+				Thread.currentThread().interrupt();
+				throw new RuntimeException(e);
+			}
+		}
+		return false;
+	}
+
+	/**
+	 * 释放锁
+	 */
+	public void unlock(String lockKey) {
+		// 使用lua脚本保证操作的原子性,避免删除非本客户端创建的锁
+		String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
+		redisTemplate.getRedisTemplate().execute((RedisCallback<Object>) (connection) -> connection.eval(script.getBytes(), ReturnType.INTEGER, 1, lockKey.getBytes(), String.valueOf(redisTemplate.getValueOps().get(lockKey)).getBytes()));
+	}
+
+
+
 	@Override
 	public IPage<User> selectUserPage(IPage<User> page, User user, String deptId, String tenantId) {
 		if (Strings.isBlank(deptId) && !AuthUtil.isAdministrator() && !AuthUtil.isAdmin()) {
@@ -279,7 +325,7 @@
 	 * @param tenantId
 	 * @param account
 	 * @param password
-	 * @param type 登录类型 2:pc 3:app
+	 * @param type     登录类型 2:pc 3:app
 	 * @return
 	 */
 	@Override
@@ -343,7 +389,6 @@
 	}
 
 	/**
-	 *
 	 * @param user
 	 * @param type 登录类型 2:pc 3:app
 	 * @return
@@ -356,7 +401,7 @@
 		userInfo.setUser(user);
 		if (Func.isNotEmpty(user)) {
 			// 判断登录类型设置对应的角色id
-			user.setRoleId(Strings.join(roleService.getRoleIdStrByRoleIdAndType(user.getRoleId(),type), ','));
+			user.setRoleId(Strings.join(roleService.getRoleIdStrByRoleIdAndType(user.getRoleId(), type), ','));
 			// 设置角色别名
 			List<String> roleAlias = roleService.getRoleAliases(user.getRoleId());
 			userInfo.setRoles(roleAlias);
@@ -539,6 +584,28 @@
 	}
 
 	@Override
+	public boolean registerUser(UserVO user) {
+		if (StringUtils.isEmpty(user.getAccount())) {
+			throw new ServiceException("账号不能为空!");
+		}
+		if (StringUtils.isEmpty(user.getPassword())) {
+			throw new ServiceException("密码不能为空!");
+		}
+		if (!StringUtils.equals(user.getPassword(), user.getPassword2())) {
+			throw new ServiceException("两次的密码不一致!");
+		}
+		long count = count(Wrappers.<User>lambdaQuery().eq(User::getAccount, user.getAccount()));
+		if (count > 0) {
+			throw new ServiceException("改账号已存在,请更换其他账号!");
+		}
+
+		user.setPassword(DigestUtil.encrypt(user.getPassword()));
+		user.setRoleId("1717429059648606209");
+		boolean oauthTemp = save(user);
+		return (oauthTemp);
+	}
+
+	@Override
 	public boolean updatePlatform(Long userId, Integer userType, String userExt) {
 		if (userType.equals(UserEnum.WEB.getCategory())) {
 			UserWeb userWeb = new UserWeb();
@@ -684,7 +751,6 @@
 	}
 
 
-
 	/**
 	 * 处理漏绑定的user_dept
 	 */
@@ -753,7 +819,7 @@
 					userInfo.setRoleId(userInfo.getRoleId() + ",1727864411451359233");
 				}
 				// 设置性别
-				setSex(userInfo,userExcel);
+				setSex(userInfo, userExcel);
 				// 更新机构
 				setDeptId(userInfo, userExcel);
 				// 更新
@@ -771,7 +837,7 @@
 				user.setPhone(userExcel.getPhone());
 				user.setAccount(userExcel.getPhone());
 				// 设置性别
-				setSex(user,userExcel);
+				setSex(user, userExcel);
 				// 更新机构
 				setDeptId(user, userExcel);
 				// 设置初始密码
@@ -786,14 +852,15 @@
 
 	/**
 	 * 设置性别
+	 *
 	 * @param user
 	 * @param userExcel
 	 */
 	private void setSex(User user, PoliceUserExcel userExcel) {
-		if (userExcel.getSex().contains("男")){
+		if (userExcel.getSex().contains("男")) {
 			user.setSex(1);
 		}
-		if (userExcel.getSex().contains("女")){
+		if (userExcel.getSex().contains("女")) {
 			user.setSex(2);
 		}
 	}

--
Gitblit v1.9.3