| | |
| | | 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 org.apache.commons.lang3.StringUtils; |
| | | 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.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.property.service.IPropertyCompanyDistrictService; |
| | | import org.springblade.modules.property.service.IPropertyCompanyService; |
| | | import org.springblade.modules.system.entity.*; |
| | | import org.springblade.modules.system.excel.PoliceUserExcel; |
| | |
| | | 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.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | @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 IUserOauthService userOauthService; |
| | | private final IRoleService roleService; |
| | | private final IDeptService deptService; |
| | | private final BladeTenantProperties tenantProperties; |
| | | private final IPoliceAffairsGridService policeAffairsGridService; |
| | | |
| | | @Autowired |
| | | private SpinLockUtil spinLockUtil; |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | throw new ServiceException(StringUtil.format("当前用户 [{}] 已存在!", user.getAccount())); |
| | | } |
| | | boolean save = save(user); |
| | | // 更新警格或网格的绑定 |
| | | updateGridBind(user); |
| | | // 返回 |
| | | return save && submitUserDept(user); |
| | | } |
| | |
| | | user.setPassword(null); |
| | | boolean b = updateById(user); |
| | | // 更新警格或网格的绑定 |
| | | updateGridBind(user); |
| | | // updateGridBind(user); |
| | | return b; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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; |
| | | 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); |
| | | } |
| | | // 释放锁 |
| | | spinLockUtil.unlock(user.getId().toString()); |
| | | return result; |
| | | } catch (Exception e) { |
| | | // 释放锁 |
| | | spinLockUtil.unlock(user.getId().toString()); |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public IPage<User> selectUserPage(IPage<User> page, User user, String deptId, String tenantId) { |
| | |
| | | return buildUserInfo(user); |
| | | } |
| | | |
| | | /** |
| | | * 用户信息 |
| | | * |
| | | * @param tenantId |
| | | * @param account |
| | | * @param password |
| | | * @param type 登录类型 2:pc 3:app |
| | | * @return |
| | | */ |
| | | @Override |
| | | public UserInfo userInfo(String tenantId, String account, String password, Integer type) { |
| | | User user = baseMapper.getUser(tenantId, account, password); |
| | | return buildUserInfo(user, type); |
| | | } |
| | | |
| | | /** |
| | | * 用户信息 |
| | | * |
| | | * @param tenantId |
| | | * @param account |
| | | * @param password |
| | | * @param userEnum |
| | | * @return |
| | | */ |
| | | @Override |
| | | public UserInfo userInfo(String tenantId, String account, String password, UserEnum userEnum) { |
| | | User user = baseMapper.getUser(tenantId, account, password); |
| | |
| | | if (ObjectUtil.isNotEmpty(query)) { |
| | | detail.set("ext", query.getUserExt()); |
| | | } |
| | | } |
| | | userInfo.setDetail(detail); |
| | | return userInfo; |
| | | } |
| | | |
| | | /** |
| | | * @param user |
| | | * @param type 登录类型 2:pc 3:app |
| | | * @return |
| | | */ |
| | | private UserInfo buildUserInfo(User user, Integer type) { |
| | | if (ObjectUtil.isEmpty(user)) { |
| | | return null; |
| | | } |
| | | UserInfo userInfo = new UserInfo(); |
| | | userInfo.setUser(user); |
| | | if (Func.isNotEmpty(user)) { |
| | | // 判断登录类型设置对应的角色id |
| | | user.setRoleId(Strings.join(roleService.getRoleIdStrByRoleIdAndType(user.getRoleId(), type), ',')); |
| | | // 设置角色别名 |
| | | List<String> roleAlias = roleService.getRoleAliases(user.getRoleId()); |
| | | userInfo.setRoles(roleAlias); |
| | | } |
| | | // 根据每个用户平台,建立对应的detail表,通过查询将结果集写入到detail字段 |
| | | Kv detail = Kv.create().set("type", null); |
| | | UserOther userOther = new UserOther(); |
| | | UserOther query = userOther.selectOne(Wrappers.<UserOther>lambdaQuery().eq(UserOther::getUserId, user.getId())); |
| | | if (ObjectUtil.isNotEmpty(query)) { |
| | | detail.set("ext", query.getUserExt()); |
| | | } |
| | | userInfo.setDetail(detail); |
| | | return userInfo; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<UserExcel> exportUser(Wrapper<User> queryWrapper) { |
| | | List<UserExcel> userList = baseMapper.exportUser(queryWrapper); |
| | | public List<UserExcel> exportUser(Wrapper<User> queryWrapper, String deptId) { |
| | | if (Strings.isBlank(deptId) && !AuthUtil.isAdministrator() && !AuthUtil.isAdmin()) { |
| | | deptId = AuthUtil.getDeptId(); |
| | | } |
| | | List<Long> deptIdList = SysCache.getDeptChildIds(deptId); |
| | | List<UserExcel> userList = baseMapper.exportUser(queryWrapper, deptIdList); |
| | | userList.forEach(user -> { |
| | | user.setUserTypeName(DictCache.getValue(DictEnum.USER_TYPE, user.getUserType())); |
| | | user.setRoleName(StringUtil.join(SysCache.getRoleNames(user.getRoleId()))); |
| | |
| | | userOauth.setTenantId(user.getTenantId()); |
| | | boolean oauthTemp = userOauthService.updateById(userOauth); |
| | | return (userTemp && oauthTemp); |
| | | } |
| | | |
| | | @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 |
| | |
| | | IGridmanService bean = SpringUtil.getBean(IGridmanService.class); |
| | | return bean.getGridManByCode(houseCode); |
| | | } else { |
| | | // IPropertyCompanyDistrictService bean1 = SpringUtil.getBean(IPropertyCompanyDistrictService.class); |
| | | // return bean1.getDistictUserByCode(houseCode); |
| | | String deptId = AuthUtil.getDeptId(); |
| | | List<Long> deptIdList = SysCache.getDeptChildIds(deptId); |
| | | return baseMapper.selectUserByDept(deptIdList); |
| | | IPropertyCompanyDistrictService bean1 = SpringUtil.getBean(IPropertyCompanyDistrictService.class); |
| | | return bean1.getDistictUserByCode(houseCode); |
| | | // 通过huosecode 查询物业工作人员 |
| | | // String deptId = AuthUtil.getDeptId(); |
| | | // List<Long> deptIdList = SysCache.getDeptChildIds(deptId); |
| | | // return baseMapper.selectUserByDept(deptIdList); |
| | | } |
| | | } |
| | | |
| | |
| | | return baseMapper.getUserInfoByDistrictId(districtId, building); |
| | | } |
| | | |
| | | /** |
| | | * 通过小区id查询用户 |
| | | * |
| | | * @param districtIds |
| | | * @param building |
| | | * @param unit |
| | | * @param name |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<HouseholdVO> getUserInfoByDistrictIds(HouseholdVO householdVO, IPage<HouseholdVO> page) { |
| | | List<String> list = JSON.parseArray(householdVO.getDistrictIds()).toJavaList(String.class); |
| | | List<HouseholdVO> userInfoByDistrictIds = baseMapper.getUserInfoByDistrictIds(list, householdVO, page); |
| | | return page.setRecords(userInfoByDistrictIds); |
| | | } |
| | | |
| | | /** |
| | | * 处理漏绑定的user_dept |
| | |
| | | for (PoliceUserExcel userExcel : data) { |
| | | // 查询用户 |
| | | List<User> list = getUserListByPhoneOrAccount(userExcel.getPhone()); |
| | | |
| | | if (list.size() > 0) { |
| | | User userInfo = list.get(0); |
| | | // 判断角色 |
| | | if (!userInfo.getRoleId().contains("1727864411451359233")) { |
| | | userInfo.setRoleId(userInfo.getRoleId() + ",1727864411451359233"); |
| | | } |
| | | // 设置性别 |
| | | setSex(userInfo, userExcel); |
| | | // 更新机构 |
| | | setDeptId(userInfo, userExcel); |
| | | // 更新 |
| | | updateById(userInfo); |
| | | // 更新用户机构绑定 |
| | | submitUserDept(userInfo); |
| | | // 更新社区民警绑定信息 |
| | | updateGridBind(userInfo); |
| | | } else { |
| | | // 插入用户角色 |
| | | User user = new User(); |
| | |
| | | user.setRealName(userExcel.getRealName()); |
| | | user.setPhone(userExcel.getPhone()); |
| | | user.setAccount(userExcel.getPhone()); |
| | | // 设置性别 |
| | | setSex(user, userExcel); |
| | | // 更新机构 |
| | | setDeptId(user, userExcel); |
| | | // 设置初始密码 |
| | |
| | | submit(user); |
| | | // 更新用户机构绑定 |
| | | submitUserDept(user); |
| | | // 更新社区民警绑定信息 |
| | | updateGridBind(user); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置性别 |
| | | * |
| | | * @param user |
| | | * @param userExcel |
| | | */ |
| | | private void setSex(User user, PoliceUserExcel userExcel) { |
| | | if (userExcel.getSex().contains("男")) { |
| | | user.setSex(1); |
| | | } |
| | | if (userExcel.getSex().contains("女")) { |
| | | user.setSex(2); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public void setDeptId(User user, PoliceUserExcel userExcel) { |
| | | // 查询机构 |
| | | QueryWrapper<PoliceAffairsGridEntity> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("is_deleted", 0) |
| | | .eq("community_code", userExcel.getCommunityCode()) |
| | | .eq("pcs_name", userExcel.getPoliceStationName()); |
| | | PoliceAffairsGridEntity policeAffairsGridEntity = policeAffairsGridService.getOne(queryWrapper); |
| | | if (null != policeAffairsGridEntity) { |
| | | // 查询对应绑定的机构 |
| | | QueryWrapper<Dept> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("is_deleted", 0) |
| | | .eq("dept_nature", 1) |
| | | .eq("region_code", policeAffairsGridEntity.getJwGridCode()); |
| | | Dept dept = SpringUtil.getBean(IDeptService.class).getOne(wrapper); |
| | | if (null != dept) { |
| | | if (null != user.getId()) { |
| | | DeptNotHandle(user, dept); |
| | | } else { |
| | | user.setDeptId(dept.getId().toString()); |
| | | } |
| | | } |
| | | } |
| | | String deptIds = deptService.getDeptIds("000000", userExcel.getDeptName()); |
| | | user.setDeptId(deptIds); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (!user.getDeptId().contains(dept.getId().toString())) { |
| | | user.setDeptId(user.getDeptId() + "," + dept.getId()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新社区民警绑定信息 |
| | | * |
| | | * @param userExcel |
| | | * @param user |
| | | */ |
| | | public void updateCommunityInfo(PoliceUserExcel userExcel, User user) { |
| | | QueryWrapper<CommunityEntity> wrapper = new QueryWrapper<>(); |
| | | System.out.println("wrapper = " + userExcel.getCommunityCode()); |
| | | wrapper.eq("is_deleted", 0).eq("code", userExcel.getCommunityCode()); |
| | | CommunityEntity one = SpringUtil.getBean(ICommunityService.class).getOne(wrapper); |
| | | if (null != one) { |
| | | String userId = user.getId().toString(); |
| | | if (!Strings.isBlank(one.getResPoliceUserId())) { |
| | | if (!one.getResPoliceUserId().contains(userId)) { |
| | | one.setResPoliceUserId(one.getResPoliceUserId() + "," + userId); |
| | | // 更新 |
| | | SpringUtil.getBean(ICommunityService.class).updateById(one); |
| | | } |
| | | } else { |
| | | one.setResPoliceUserId(userId); |
| | | // 更新 |
| | | SpringUtil.getBean(ICommunityService.class).updateById(one); |
| | | } |
| | | } |
| | | } |