|
|
@@ -1,21 +1,41 @@
|
|
|
package com.zhongshu.card.server.core.service.devices;
|
|
|
|
|
|
+import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
-import com.zhongshu.card.client.model.devices.DevicePermissModel;
|
|
|
-import com.zhongshu.card.client.model.devices.DevicePermissParam;
|
|
|
+import com.github.microservice.net.ResultMessage;
|
|
|
+import com.zhongshu.card.client.model.base.IDsParam;
|
|
|
+import com.zhongshu.card.client.model.devices.*;
|
|
|
import com.zhongshu.card.client.model.org.OrganizationUserModel;
|
|
|
+import com.zhongshu.card.client.type.DataOperationType;
|
|
|
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.org.OrganizationDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.OrganizationUserDao;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DevicePermiss;
|
|
|
+import com.zhongshu.card.server.core.domain.org.Organization;
|
|
|
+import com.zhongshu.card.server.core.domain.org.OrganizationUser;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
+import com.zhongshu.card.server.core.event.OrgUserBindUpdateSyncEvent;
|
|
|
+import com.zhongshu.card.server.core.event.ProjectSyncEvent;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.card.server.core.service.user.UserAccountServiceImpl;
|
|
|
import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.event.EventListener;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.security.core.userdetails.User;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
/**
|
|
|
* 设备权限 (绑定用户)管理
|
|
|
*
|
|
|
@@ -38,9 +58,146 @@ public class DevicePermissService extends SuperService {
|
|
|
@Autowired
|
|
|
private DeviceInfoDao deviceInfoDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private OrganizationDao organizationDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrganizationUserDao organizationUserDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设备绑定用户
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public ResultContent saveBindInfo(DevicePermissParam param) {
|
|
|
+ String projectOid = param.getProjectOid();
|
|
|
+ if (ObjectUtils.isEmpty(projectOid)) {
|
|
|
+ return ResultContent.buildFail(String.format("projectOid不能为空"));
|
|
|
+ }
|
|
|
+
|
|
|
+ String deviceId = param.getDeviceId();
|
|
|
+ DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
|
|
|
+ if (ObjectUtils.isEmpty(deviceInfo)) {
|
|
|
+ return ResultContent.buildFail(String.format("设备信息不存在:%s", deviceId));
|
|
|
+ }
|
|
|
+ List<String> ids = param.getIds();
|
|
|
+ if (ObjectUtils.isEmpty(ids)) {
|
|
|
+ return ResultContent.buildFail("ids is null");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ObjectUtils.isNotEmpty(ids)) {
|
|
|
+ List<OrganizationUser> list = organizationUserDao.findByIdIn(ids);
|
|
|
+ if (ObjectUtils.isNotEmpty(list)) {
|
|
|
+ List<DevicePermiss> permisses = new ArrayList<DevicePermiss>();
|
|
|
+ for (OrganizationUser organizationUser : list) {
|
|
|
+ DevicePermiss permiss = devicePermissDao.findTopByDeviceIdAndOrganizationUser(deviceId, organizationUser);
|
|
|
+ if (ObjectUtils.isEmpty(permiss)) {
|
|
|
+ permiss = new DevicePermiss();
|
|
|
+ initEntity(permiss);
|
|
|
+ } else {
|
|
|
+ initUpdateEntity(permiss);
|
|
|
+ }
|
|
|
+ buildDeviceInfo(permiss, deviceInfo);
|
|
|
+ buildOrgUserInfo(permiss, organizationUser);
|
|
|
+ permisses.add(permiss);
|
|
|
+ }
|
|
|
+ devicePermissDao.saveAll(permisses);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void buildDeviceInfo(DevicePermiss permiss, DeviceInfo deviceInfo) {
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo)) {
|
|
|
+ permiss.setDeviceInfo(deviceInfo);
|
|
|
+ permiss.setDeviceId(deviceInfo.getDeviceId());
|
|
|
+ permiss.setDeviceName(deviceInfo.getDeviceName());
|
|
|
+ permiss.setDeviceType(deviceInfo.getDeviceType());
|
|
|
+ permiss.setArea(deviceInfo.getArea());
|
|
|
+ permiss.setAreaPaths(deviceInfo.getAreaPaths());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ private void buildOrgUserInfo(DevicePermiss permiss, OrganizationUser organizationUser) {
|
|
|
+ if (ObjectUtils.isNotEmpty(organizationUser)) {
|
|
|
+ UserAccount userAccount = organizationUser.getUser();
|
|
|
+ permiss.setOrganizationUser(organizationUser);
|
|
|
+ permiss.setUserAccount(userAccount);
|
|
|
+ permiss.setUserId(userAccount.getUserId());
|
|
|
+ permiss.setName(userAccount.getName());
|
|
|
+ permiss.setDepartment(organizationUser.getDepartment());
|
|
|
+ permiss.setRoles(organizationUser.getRoles());
|
|
|
+ permiss.setPosition(organizationUser.getPosition());
|
|
|
+ permiss.setState(organizationUser.getState());
|
|
|
+ permiss.setUserOid(organizationUser.getOid());
|
|
|
+ permiss.setCode(organizationUser.getCode());
|
|
|
+ permiss.setAuthType(organizationUser.getAuthType());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @EventListener(classes = OrgUserBindUpdateSyncEvent.class)
|
|
|
+ @Async
|
|
|
+ @SneakyThrows
|
|
|
+ public void syncOrgUserChangeInfo(OrgUserBindUpdateSyncEvent event) {
|
|
|
+ log.info("机构用户信息发生变化 syncOrgUserChangeInfo...");
|
|
|
+ DataOperationType dataOperationType = event.getDataOperationType();
|
|
|
+ List<String> organizationUserIds = event.getOrganizationUserIds();
|
|
|
+ if (dataOperationType != null) {
|
|
|
+ List<OrganizationUser> organizationUsers = organizationUserDao.findByIdIn(organizationUserIds);
|
|
|
+ if (ObjectUtils.isNotEmpty(organizationUsers)) {
|
|
|
+ List<DevicePermiss> list = devicePermissDao.findByOrganizationUserIn(organizationUsers);
|
|
|
+ if (dataOperationType == DataOperationType.Update) {
|
|
|
+ for (DevicePermiss permiss : list) {
|
|
|
+ buildOrgUserInfo(permiss, permiss.getOrganizationUser());
|
|
|
+ }
|
|
|
+ devicePermissDao.saveAll(list);
|
|
|
+ } else if (dataOperationType == DataOperationType.Delete) {
|
|
|
+ devicePermissDao.deleteAll(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页数据
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @param pageable
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<Page<DevicePermissModel>> page(DevicePermissSearch param, Pageable pageable) {
|
|
|
+ String projectOid = param.getProjectOid();
|
|
|
+ Organization organization = organizationDao.findTopByOid(projectOid);
|
|
|
+ if (ObjectUtils.isEmpty(organization)) {
|
|
|
+ return ResultContent.buildFail("projectOid不存在");
|
|
|
+ }
|
|
|
+ Page<DevicePermiss> page = devicePermissDao.page(pageable, param);
|
|
|
+ return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
|
|
|
+ }
|
|
|
|
|
|
+ public ResultContent<DevicePermissModel> getDetail(String id) {
|
|
|
+ DevicePermiss entity = devicePermissDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess(toModel(entity));
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultContent deleteInfo(String id) {
|
|
|
+ DevicePermiss entity = devicePermissDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ devicePermissDao.delete(entity);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultContent deleteByIds(IDsParam param) {
|
|
|
+ if (ObjectUtils.isNotEmpty(param.getIds())) {
|
|
|
+ devicePermissDao.deleteAllById(param.getIds());
|
|
|
+ }
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|