| | |
| | | package org.springblade.modules.system.service.impl; |
| | | |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.ibatis.annotations.Param; |
| | | import org.apache.logging.log4j.util.Strings; |
| | | import org.flowable.idm.engine.impl.persistence.entity.UserEntity; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springblade.common.cache.DictCache; |
| | | import org.springblade.common.cache.ParamCache; |
| | | import org.springblade.common.cache.SysCache; |
| | |
| | | import org.springblade.common.constant.CommonConstant; |
| | | import org.springblade.common.constant.TenantConstant; |
| | | import org.springblade.common.enums.DictEnum; |
| | | import org.springblade.common.utils.SpinLockUtil; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | 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.core.tool.support.Kv; |
| | | import org.springblade.core.tool.utils.*; |
| | | import org.springblade.modules.auth.enums.UserEnum; |
| | | import org.springblade.modules.community.entity.CommunityEntity; |
| | | import org.springblade.modules.community.service.ICommunityService; |
| | | import org.springblade.modules.grid.service.IGridmanService; |
| | | import org.springblade.modules.house.entity.HouseholdEntity; |
| | | import org.springblade.modules.house.service.IHouseholdService; |
| | | import org.springblade.modules.house.vo.HouseholdVO; |
| | | import org.springblade.modules.police.entity.PoliceAffairsGridEntity; |
| | | import org.springblade.modules.police.service.IPoliceAffairsGridService; |
| | | import org.springblade.modules.property.entity.PropertyCompanyEntity; |
| | |
| | | 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; |
| | | |
| | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService { |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); |
| | | |
| | | private static final String GUEST_NAME = "guest"; |
| | | |
| | | private final IUserDeptService userDeptService; |
| | |
| | | private final IPoliceAffairsGridService policeAffairsGridService; |
| | | |
| | | @Autowired |
| | | private BladeRedis redisTemplate; |
| | | private SpinLockUtil spinLockUtil; |
| | | |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | private boolean submitUserDept(User user) { |
| | | List<Long> deptIdList = Func.toLongList(user.getDeptId()); |
| | | List<UserDept> userDeptList = new ArrayList<>(); |
| | | deptIdList.forEach(deptId -> { |
| | | UserDept userDept = new UserDept(); |
| | | userDept.setUserId(user.getId()); |
| | | userDept.setDeptId(deptId); |
| | | userDeptList.add(userDept); |
| | | }); |
| | | userDeptService.remove(Wrappers.<UserDept>update().lambda().eq(UserDept::getUserId, user.getId())); |
| | | boolean b = userDeptService.saveBatch(userDeptList); |
| | | 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 { |
| | | // 加锁 |
| | | boolean isLock = spinLockUtil.tryLock(user.getId().toString()); |
| | | boolean result = false; |
| | | List<Long> deptIdList = Func.toLongList(user.getDeptId()); |
| | | List<UserDept> userDeptList = new ArrayList<>(); |
| | | deptIdList.forEach(deptId -> { |
| | | UserDept userDept = new UserDept(); |
| | | userDept.setUserId(user.getId()); |
| | | userDept.setDeptId(deptId); |
| | | userDeptList.add(userDept); |
| | | }); |
| | | // 是否加锁成功 |
| | | logger.info("是否加锁成功:" + isLock); |
| | | if (isLock) { |
| | | userDeptService.remove(Wrappers.<UserDept>update().lambda().eq(UserDept::getUserId, user.getId())); |
| | | result = userDeptService.saveBatch(userDeptList); |
| | | } |
| | | try { |
| | | // 短暂休眠,防止CPU过度占用 |
| | | Thread.sleep(10); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | throw new RuntimeException(e); |
| | | } |
| | | // 释放锁 |
| | | spinLockUtil.unlock(user.getId().toString()); |
| | | return result; |
| | | } catch (Exception e) { |
| | | // 释放锁 |
| | | spinLockUtil.unlock(user.getId().toString()); |
| | | 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 |