TRX 1 год назад
Родитель
Сommit
10ac0a5988

+ 2 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/dao/devices/DevicePermissDao.java

@@ -31,4 +31,6 @@ public interface DevicePermissDao extends MongoDao<DevicePermiss>, DevicePermiss
 
     List<DevicePermiss> findByDeviceIdInAndDataState(List<String> deviceIds, DataState dataState);
 
+    List<DevicePermiss> findByUserIdInAndProjectOidAndDataState(List<String> userIds, String projectOid, DataState dataState);
+
 }

+ 38 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/devices/DevicePermissEventService.java

@@ -17,6 +17,7 @@ import com.zhongshu.card.server.core.service.openAPI.OpenApiRequestService;
 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.ApplicationContext;
 import org.springframework.context.event.EventListener;
@@ -51,6 +52,9 @@ public class DevicePermissEventService {
     //线程池
     private ExecutorService executorService = Executors.newFixedThreadPool(SystemUtil.getCpuCoreCount() * 2);
 
+    @Autowired
+    private DevicePermissService devicePermissService;
+
     @Autowired
     private void init(ApplicationContext applicationContext) {
         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
@@ -110,10 +114,43 @@ public class DevicePermissEventService {
         });
     }
 
+    /**
+     * 用户信息发生变化
+     *
+     * @param event
+     */
     @EventListener(classes = UserInfoChangeSyncEvent.class)
     @SneakyThrows
     public void userInfoChange(UserInfoChangeSyncEvent event) {
-
+        String projectOid = event.getProjectOid();
+        List<String> userIds = event.getUserIds();
+        if (StringUtils.isNotEmpty(projectOid) && ObjectUtils.isNotEmpty(userIds)) {
+            executorService.execute(() -> {
+                List<String> deviceIds = devicePermissService.getUserAllDeviceIds(projectOid, userIds);
+                HashMap<String, List<String>> map = deviceGroupByGateWayId(deviceIds);
+                if (!map.isEmpty()) {
+                    // 遍历发送
+                    map.keySet().stream().forEach(gateWayId -> {
+                        List<String> _deviceIds = map.get(gateWayId);
+                        try {
+                            while (_deviceIds.size() > 0) {
+                                // 统计网关管理的所有设备
+                                int index = Math.min(_deviceIds.size(), IotIdentifierConfig.maxNoticeSize);
+                                List<String> subList = _deviceIds.subList(0, index);
+                                _deviceIds = _deviceIds.subList(index, _deviceIds.size());
+                                // 下发用户信息
+                                sendDeviceAboutUserInfo(gateWayId, subList, projectOid);
+                                if (!_deviceIds.isEmpty()) {
+                                    TimeUnit.SECONDS.sleep(10);
+                                }
+                            }
+                        } catch (Exception e) {
+                            throw new RuntimeException(e);
+                        }
+                    });
+                }
+            });
+        }
     }
 
     /**

+ 12 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/devices/DevicePermissService.java

@@ -370,6 +370,18 @@ public class DevicePermissService extends SuperService {
         return Lists.newArrayList();
     }
 
+    public List<String> getUserAllDeviceIds(String projectOid, List<String> userIds) {
+        List<String> deviceIds = Lists.newArrayList();
+        if (StringUtils.isNotEmpty(projectOid) && ObjectUtils.isNotEmpty(userIds)) {
+            List<DevicePermiss> list = devicePermissDao.findByUserIdInAndProjectOidAndDataState(
+                    userIds, projectOid, DataState.Enable);
+            if (ObjectUtils.isNotEmpty(list)) {
+                deviceIds = list.stream().map(it -> it.getDeviceId()).collect(Collectors.toList());
+            }
+        }
+        return deviceIds;
+    }
+
     public DevicePermissModel toModel(DevicePermiss entity) {
         DevicePermissModel model = null;
         if (ObjectUtils.isNotEmpty(entity)) {

+ 25 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/projectAbout/CardInfoServiceImpl.java

@@ -30,6 +30,8 @@ import com.zhongshu.card.server.core.domain.org.UserAccount;
 import com.zhongshu.card.server.core.domain.school.CardInfo;
 import com.zhongshu.card.server.core.domain.school.CardInfoLossRecord;
 import com.zhongshu.card.server.core.domain.school.CardInfoPool;
+import com.zhongshu.card.server.core.event.DevicePermissChangeEvent;
+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.OrganizationServiceImpl;
 import com.zhongshu.card.server.core.service.org.OrganizationUserServiceImpl;
@@ -45,6 +47,7 @@ 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.ApplicationContext;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
@@ -105,6 +108,9 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
     @Autowired
     CardInfoPoolService cardInfoPoolService;
 
+    @Autowired
+    ApplicationContext applicationContext;
+
     /**
      * 添加或修改卡片信息
      *
@@ -159,6 +165,7 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
         cardInfo.setAboutOid(oidAboutInfo.getOid());
         cardInfo.setAboutAuthType(oidAboutInfo.getAuthType());
         cardInfoDao.save(cardInfo);
+
         return ResultContent.buildSuccess();
     }
 
@@ -288,6 +295,11 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
             cardInfoPool.setUseTime(null);
             cardInfoPoolDao.save(cardInfoPool);
         }
+
+        // 用户可用卡片减少
+        UserInfoChangeSyncEvent event = new UserInfoChangeSyncEvent(this,
+                List.of(cardInfo.getUserId()), cardInfo.getProjectOid());
+        applicationContext.publishEvent(event);
         return ResultContent.buildSuccess();
     }
 
@@ -549,6 +561,10 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
         cardInfoLossRecord.setOperationType(CardOperationType.Enable);
         cardInfoLossRecordDao.save(cardInfoLossRecord);
 
+        // 用户可用卡片发生变化
+        UserInfoChangeSyncEvent event = new UserInfoChangeSyncEvent(this,
+                List.of(cardInfo.getUserId()), cardInfo.getProjectOid());
+        applicationContext.publishEvent(event);
         return ResultContent.buildSuccess();
     }
 
@@ -658,6 +674,9 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
         cardInfoLossRecord.setOperationType(CardOperationType.Enable);
         cardInfoLossRecordDao.save(cardInfoLossRecord);
 
+        UserInfoChangeSyncEvent event = new UserInfoChangeSyncEvent(this,
+                List.of(cardInfo.getUserId()), cardInfo.getProjectOid());
+        applicationContext.publishEvent(event);
         return ResultContent.buildSuccess();
     }
 
@@ -745,6 +764,7 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
             List<String> failDetails = new ArrayList<>();
             List<CardInfoPoolBind2OrgUserParam> cardInfoPools = new ArrayList<>();
             if (ObjectUtils.isNotEmpty(list)) {
+                List<String> userIds = new ArrayList<>();
                 for (CardInfoBindImportParam bindImportParam : list) {
                     startRow++;
                     CardInfoPoolBind2OrgUserParam bind2OrgUserParam = new CardInfoPoolBind2OrgUserParam();
@@ -800,11 +820,16 @@ public class CardInfoServiceImpl extends SuperService implements CardInfoService
                     codeMap.put(code, phone);
                     bind2OrgUserParam.setId(organizationUser.getId());
                     cardInfoPools.add(bind2OrgUserParam);
+                    userIds.add(userAccount.getUserId());
                 }
                 if (ObjectUtils.isNotEmpty(cardInfoPools)) {
                     for (CardInfoPoolBind2OrgUserParam bind2OrgUserParam : cardInfoPools) {
                         bindCardFromPoolByOrgId(bind2OrgUserParam.getPoolId(), bind2OrgUserParam.getId(), bind2OrgUserParam.getRemark());
                     }
+
+                    UserInfoChangeSyncEvent event = new UserInfoChangeSyncEvent(this,
+                            userIds, projectOid);
+                    applicationContext.publishEvent(event);
                 }
                 model.setSuccess(cardInfoPools.size());
                 model.setFailDetails(failDetails);