|
|
@@ -1,26 +1,32 @@
|
|
|
package com.zhongshu.card.server.core.service.devices.permiss;
|
|
|
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
+import com.zhongshu.card.client.model.devices.permiss.PermissSettingListSearch;
|
|
|
import com.zhongshu.card.client.model.devices.permiss.TimeSlotModel;
|
|
|
import com.zhongshu.card.client.model.devices.permiss.TimeSlotWeekModel;
|
|
|
import com.zhongshu.card.client.type.DataState;
|
|
|
import com.zhongshu.card.client.type.UserState;
|
|
|
-import com.zhongshu.card.client.type.device.PermissDataType;
|
|
|
import com.zhongshu.card.client.type.device.TimeSlotType;
|
|
|
import com.zhongshu.card.client.type.device.WeekDayType;
|
|
|
import com.zhongshu.card.client.utils.DateUtils;
|
|
|
-import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
|
|
|
-import com.zhongshu.card.server.core.dao.devices.DevicePermissDao;
|
|
|
-import com.zhongshu.card.server.core.dao.devices.PermissSettingListDao;
|
|
|
+import com.zhongshu.card.server.core.dao.devices.*;
|
|
|
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.UserGroupToUserDao;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.DeviceGroup;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.DeviceGroupToDevice;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
|
|
|
import com.zhongshu.card.server.core.domain.devices.permiss.DevicePermiss;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.permiss.PermissSettingList;
|
|
|
import com.zhongshu.card.server.core.domain.devices.permiss.PermissTimeSlot;
|
|
|
-import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
+import com.zhongshu.card.server.core.domain.org.*;
|
|
|
+import com.zhongshu.card.server.core.service.org.OrganizationUserServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StopWatch;
|
|
|
|
|
|
@@ -53,6 +59,18 @@ public class DevicePermissVerifyService {
|
|
|
@Autowired
|
|
|
private PermissSettingListDao permissSettingListDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PermissTimeSlotDao permissTimeSlotDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceGroupToDeviceDao deviceGroupToDeviceDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrganizationUserServiceImpl organizationUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserGroupToUserDao userGroupToUserDao;
|
|
|
+
|
|
|
/**
|
|
|
* 验证用户是否有设备的使用权限
|
|
|
*
|
|
|
@@ -86,17 +104,82 @@ public class DevicePermissVerifyService {
|
|
|
|
|
|
boolean hasPermission = false;
|
|
|
String projectOid = deviceInfo.getProjectOid();
|
|
|
- DevicePermiss devicePermiss = devicePermissDao.findTopByUserIdAndDeviceIdAndProjectOid(userId, deviceId, projectOid);
|
|
|
- if (ObjectUtils.isNotEmpty(devicePermiss) && devicePermiss.getPermissDataType() == PermissDataType.UserSetting) {
|
|
|
- // 手动管理的设备权限
|
|
|
- if (devicePermiss.getDataState() == DataState.Enable) {
|
|
|
- hasPermission = true;
|
|
|
- }
|
|
|
+
|
|
|
+ // 验证手动管理的设备权限
|
|
|
+ DevicePermiss devicePermiss = devicePermissDao.findTopByUserIdAndDeviceIdAndProjectOidAndDataState(userId, deviceId, projectOid, DataState.Enable);
|
|
|
+ if (ObjectUtils.isNotEmpty(devicePermiss)) {
|
|
|
+ hasPermission = true;
|
|
|
}
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
- // 查看权限规则 是否包含该设备
|
|
|
+ // 查找当前时间对应的时段信息
|
|
|
+ List<PermissTimeSlot> permissTimeSlots = getCurrentTimeSlot(projectOid);
|
|
|
+ List<OrganizationUser> organizationUsers = organizationUserService.getUserOrgList(userId, projectOid);
|
|
|
|
|
|
+ if (ObjectUtils.isNotEmpty(permissTimeSlots) && ObjectUtils.isNotEmpty(organizationUsers)) {
|
|
|
+ // 查看权限规则 是否包含该设备
|
|
|
+ PermissSettingListSearch search = new PermissSettingListSearch();
|
|
|
+ search.setProjectOid(projectOid);
|
|
|
+ search.setTimeSlotIds(permissTimeSlots.stream().map(PermissTimeSlot::getId).collect(Collectors.toList()));
|
|
|
+ if (userAccount.getSex() != null) {
|
|
|
+ search.setSex(userAccount.getSex());
|
|
|
+ }
|
|
|
+ List<String> dataIds = new ArrayList<>();
|
|
|
+ // 用户id
|
|
|
+ dataIds.add(userId);
|
|
|
+
|
|
|
+ // 用户所属角色、机构
|
|
|
+ organizationUsers.parallelStream().forEach(organizationUser -> {
|
|
|
+ dataIds.add(organizationUser.getOrganization().getOid());
|
|
|
+ List<Role> roles = organizationUser.getRoles();
|
|
|
+ if (ObjectUtils.isNotEmpty(roles)) {
|
|
|
+ dataIds.addAll(roles.stream().map(Role::getId).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 用户分组
|
|
|
+ List<UserGroupToUser> userGroupToUsers = userGroupToUserDao.findByUserIdAndProjectOid(userId, projectOid);
|
|
|
+ if (ObjectUtils.isNotEmpty(userGroupToUsers)) {
|
|
|
+ userGroupToUsers.parallelStream().forEach(userGroupToUser -> {
|
|
|
+ UserGroup userGroup = userGroupToUser.getUserGroup();
|
|
|
+ if (ObjectUtils.isNotEmpty(userGroup) && userGroup.getState() == DataState.Enable) {
|
|
|
+ dataIds.add(userGroup.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ search.setDataIds(dataIds);
|
|
|
+
|
|
|
+ // 设备属性
|
|
|
+ List<String> deviceIds = new ArrayList<>();
|
|
|
+ deviceIds.add(deviceId);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo.getArea())) {
|
|
|
+ // 区域的下级怎么办?
|
|
|
+ deviceIds.add(deviceInfo.getArea().getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设备分组信息
|
|
|
+ List<DeviceGroupToDevice> deviceAbouts = deviceGroupToDeviceDao.findByProjectOidAndDeviceId(projectOid, deviceId);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceAbouts)) {
|
|
|
+ List<String> groupIds = new ArrayList<>();
|
|
|
+ deviceAbouts.parallelStream().forEach(it -> {
|
|
|
+ DeviceGroup deviceGroup = it.getDeviceGroup();
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceGroup) && deviceGroup.getState() == DataState.Enable) {
|
|
|
+ groupIds.add(deviceGroup.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (ObjectUtils.isNotEmpty(groupIds)) {
|
|
|
+ deviceIds.addAll(groupIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 设备所在的分组
|
|
|
+ search.setDeviceIds(deviceIds);
|
|
|
+
|
|
|
+ Pageable pageable = PageRequest.of(0, 1);
|
|
|
+ Page<PermissSettingList> page = permissSettingListDao.page(pageable, search);
|
|
|
+ if (ObjectUtils.isNotEmpty(page.getContent())) {
|
|
|
+ hasPermission = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
@@ -106,6 +189,42 @@ public class DevicePermissVerifyService {
|
|
|
return ResultContent.buildSuccess(msg);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询当前时间所在的时间段信息
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<PermissTimeSlot> getCurrentTimeSlot(String projectOid) {
|
|
|
+ List<PermissTimeSlot> list = new ArrayList<>();
|
|
|
+ List<PermissTimeSlot> allList = permissTimeSlotDao.findByProjectOidAndStateAndType(projectOid, DataState.Enable, TimeSlotType.Week);
|
|
|
+ if (ObjectUtils.isNotEmpty(allList)) {
|
|
|
+ Long currentTime = System.currentTimeMillis();
|
|
|
+ WeekDayType weekDayType = DateUtils.getTimeWeekData(currentTime);
|
|
|
+ int timeNumber = Integer.parseInt(DateUtils.paresTime(currentTime, "HHmm"));
|
|
|
+
|
|
|
+ allList.parallelStream().forEach(it -> {
|
|
|
+ List<TimeSlotWeekModel> timeSlotWeekModels = it.getTimeSlotWeekModels();
|
|
|
+ if (ObjectUtils.isNotEmpty(timeSlotWeekModels)) {
|
|
|
+
|
|
|
+ // 筛选星期时间和是否有时间段
|
|
|
+ List<TimeSlotWeekModel> _tempWeek = timeSlotWeekModels.parallelStream().filter(time -> {
|
|
|
+ return time.getWeekDayType() == weekDayType && ObjectUtils.isNotEmpty(time.getTimeSlots());
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ _tempWeek.parallelStream().forEach(time -> {
|
|
|
+ boolean exits = time.getTimeSlots().parallelStream().anyMatch(slotModel -> {
|
|
|
+ return slotModel.getStartTimeNumber() < timeNumber && timeNumber < slotModel.getEndTimeNumber();
|
|
|
+ });
|
|
|
+ if (exits) {
|
|
|
+ list.add(it);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证当前时间是否在时间段里
|
|
|
*
|
|
|
@@ -134,10 +253,13 @@ public class DevicePermissVerifyService {
|
|
|
});
|
|
|
|
|
|
if (ObjectUtils.isNotEmpty(timeSlots)) {
|
|
|
- Long time = System.currentTimeMillis();
|
|
|
- timeSlots.parallelStream().filter(it -> {
|
|
|
+ int time = Integer.parseInt(DateUtils.paresTime(System.currentTimeMillis(), "HHmm"));
|
|
|
+ List<TimeSlotModel> temp = timeSlots.parallelStream().filter(it -> {
|
|
|
return it.getStartTimeNumber() < time && time < it.getEndTimeNumber();
|
|
|
}).collect(Collectors.toUnmodifiableList());
|
|
|
+ if (ObjectUtils.isNotEmpty(temp)) {
|
|
|
+ b = true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return b;
|