| | |
| | | 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; |
| | |
| | | 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; |
| | | |
| | |
| | | private final IDeptService deptService; |
| | | private final BladeTenantProperties tenantProperties; |
| | | private final IPoliceAffairsGridService policeAffairsGridService; |
| | | |
| | | @Autowired |
| | | private BladeRedis redisTemplate; |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | 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()) { |