|
|
@@ -0,0 +1,135 @@
|
|
|
+package com.zhongshu.card.server.core.service.devices.permiss;
|
|
|
+
|
|
|
+import com.zhongshu.card.client.model.devices.userCache.UserCacheModel;
|
|
|
+import com.zhongshu.card.client.type.DataState;
|
|
|
+import com.zhongshu.card.client.type.UserState;
|
|
|
+import com.zhongshu.card.server.core.dao.devices.UserPermissDataCacheDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.OrganizationUserDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserGroupDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserGroupToUserDao;
|
|
|
+import com.zhongshu.card.server.core.dataConfig.TtlConfig;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.permiss.UserPermissDataCache;
|
|
|
+import com.zhongshu.card.server.core.domain.org.OrganizationUser;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserGroup;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserGroupToUser;
|
|
|
+import com.zhongshu.card.server.core.event.UserInfoChangeSyncEvent;
|
|
|
+import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
+import com.zhongshu.card.server.core.service.org.OrganizationUserServiceImpl;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.event.EventListener;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户权限数据 缓存
|
|
|
+ *
|
|
|
+ * @author TRX
|
|
|
+ * @date 2024/12/27
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UserPermissDataCacheService extends SuperService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrganizationUserDao organizationUserDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrganizationUserServiceImpl organizationUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserGroupDao userGroupDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserGroupToUserDao userGroupToUserDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserCountDao userCountDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserPermissDataCacheDao userPermissDataCacheDao;
|
|
|
+
|
|
|
+ @EventListener(classes = UserInfoChangeSyncEvent.class)
|
|
|
+ @SneakyThrows
|
|
|
+ public void userPermissDataChange(UserInfoChangeSyncEvent event) {
|
|
|
+ log.info("userPermissDataChange: {}", event.getFrom());
|
|
|
+ String projectOid = event.getProjectOid();
|
|
|
+ List<String> userIds = event.getUserIds();
|
|
|
+ if (ObjectUtils.isNotEmpty(userIds) && StringUtils.isNotEmpty(projectOid)) {
|
|
|
+ for (String userId : userIds) {
|
|
|
+ updateUserDevicePermissAboutInfo(userId, projectOid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缓存用户
|
|
|
+ *
|
|
|
+ * @param userId
|
|
|
+ * @param projectOid
|
|
|
+ */
|
|
|
+ private UserPermissDataCache updateUserDevicePermissAboutInfo(String userId, String projectOid) {
|
|
|
+ UserPermissDataCache dataCache = userPermissDataCacheDao.findTopByDataIdAndProjectOid(userId, projectOid);
|
|
|
+ if (ObjectUtils.isEmpty(dataCache)) {
|
|
|
+ dataCache = new UserPermissDataCache();
|
|
|
+ dataCache.setTTL(new Date(System.currentTimeMillis() + TtlConfig.userCacheTime));
|
|
|
+ }
|
|
|
+
|
|
|
+ UserCacheModel model = new UserCacheModel();
|
|
|
+ UserAccount userAccount = userCountDao.findTopByUserId(userId);
|
|
|
+ if (ObjectUtils.isNotEmpty(userAccount)) {
|
|
|
+ if (userAccount.getState() != UserState.Normal) {
|
|
|
+ model.setFailed("用户不可用");
|
|
|
+ } else {
|
|
|
+ List<OrganizationUser> organizationUsers = organizationUserService.getUserOrgList(userId, projectOid);
|
|
|
+ if (ObjectUtils.isNotEmpty(organizationUsers)) {
|
|
|
+ model.add(userId);
|
|
|
+ model.setSex(userAccount.getSex());
|
|
|
+
|
|
|
+ organizationUsers.parallelStream().forEach(organizationUser -> {
|
|
|
+ model.add(organizationUser.getOid());
|
|
|
+ if (ObjectUtils.isNotEmpty(organizationUser.getRoleIds())) {
|
|
|
+ model.addAll(organizationUser.getRoleIds());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 用户分组
|
|
|
+ List<UserGroupToUser> userGroupToUsers = userGroupToUserDao.findByUserIdAndProjectOid(userId, projectOid);
|
|
|
+ if (ObjectUtils.isNotEmpty(organizationUsers)) {
|
|
|
+ userGroupToUsers.parallelStream().forEach(userGroupToUser -> {
|
|
|
+ UserGroup userGroup = userGroupToUser.getUserGroup();
|
|
|
+ if (ObjectUtils.isNotEmpty(userGroup) && userGroup.getState() == DataState.Enable) {
|
|
|
+ model.add(userGroup.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ model.setSuccess();
|
|
|
+ } else {
|
|
|
+ model.setFailed("用户未加入项目");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ model.setFailed("用户不存在");
|
|
|
+ }
|
|
|
+ dataCache.setCacheData(model);
|
|
|
+ userPermissDataCacheDao.save(dataCache);
|
|
|
+ return dataCache;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserCacheModel getUserCacheModel(String userId, String projectOid) {
|
|
|
+ UserCacheModel model = null;
|
|
|
+ UserPermissDataCache dataCache = userPermissDataCacheDao.findTopByDataIdAndProjectOid(userId, projectOid);
|
|
|
+ if (ObjectUtils.isNotEmpty(dataCache)) {
|
|
|
+ model = (UserCacheModel) dataCache.getCacheData();
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|