package org.jeecg.modules.dnc.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.shiro.SecurityUtils; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.modules.dnc.dto.DeviceGroupExt; import org.jeecg.modules.dnc.exception.ExceptionCast; import org.jeecg.modules.dnc.mapper.DeviceGroupMapper; import org.jeecg.modules.dnc.response.CommonCode; import org.jeecg.modules.dnc.response.DeviceGroupCode; import org.jeecg.modules.dnc.response.UcenterCode; import org.jeecg.modules.dnc.service.*; import org.jeecg.modules.dnc.ucenter.Department; import org.jeecg.modules.dnc.utils.ValidateUtil; import org.jeecg.modules.dnc.entity.*; import org.jeecg.modules.system.entity.SysUser; import org.jeecg.modules.dnc.utils.file.FileUtilS; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @Service public class DeviceGroupServiceImpl extends ServiceImpl implements IDeviceGroupService { @Autowired private IDeviceGroupPermissionService groupPermissionService; @Autowired private IDevicePermissionStreamService devicePermissionStreamService; @Autowired private IDeviceGroupDepartService deviceGroupDepartService; @Autowired private IDepartmentService departmentService; @Autowired @Lazy private IDeviceInfoService deviceInfoService; @Autowired private INcLogInfoService iNcLogInfoService; @Override @Transactional(rollbackFor = {Exception.class}) public boolean addDeviceGroup(DeviceGroup deviceGroup) { if(deviceGroup == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(!ValidateUtil.validateString(deviceGroup.getGroupName())) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NAME); LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); DeviceGroup en = findByGroupName(deviceGroup.getGroupName()); if(en != null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_EXIST); } List oldDepartPermList = null; if (ValidateUtil.validateString(deviceGroup.getParentId())) { en = super.getById(deviceGroup.getParentId()); if(en == null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PARENT_NOT_EXIST); } deviceGroup.setRankLevel(en.getRankLevel() + 1); oldDepartPermList = devicePermissionStreamService.getDepartPermByGroupId(en.getGroupId()); } else { deviceGroup.setParentId(null); deviceGroup.setRankLevel(1); } //添加日志 NcLogInfo ncLogInfo = new NcLogInfo(); //模块 ncLogInfo.setModuleInfo("设备结构树"); //类型 ncLogInfo.setOperateType(2); //日志内容 ncLogInfo.setLogContent("分组名称:"+deviceGroup.getGroupName()+",分组描述:"+deviceGroup.getDescription()); iNcLogInfoService.saveLogNcInfos(ncLogInfo); boolean b = super.save(deviceGroup); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_SAVE_ERROR); } if(oldDepartPermList != null && !oldDepartPermList.isEmpty()) { List groupDepartList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); oldDepartPermList.forEach(item -> { DeviceGroupDepart dp = new DeviceGroupDepart(); dp.setDepartId(item.getDepartId()); dp.setGroupId(deviceGroup.getGroupId()); groupDepartList.add(dp); DevicePermissionStream stream = new DevicePermissionStream(); stream.setDepartId(item.getDepartId()); stream.setGroupId(deviceGroup.getGroupId()); permissionStreamList.add(stream); }); if(!groupDepartList.isEmpty()) { b = deviceGroupDepartService.saveBatch(groupDepartList); if(!b) ExceptionCast.cast(CommonCode.FAIL); } if(!permissionStreamList.isEmpty()) { b = devicePermissionStreamService.saveBatch(permissionStreamList); if(!b) ExceptionCast.cast(CommonCode.FAIL); } } DeviceGroupPermission permission = new DeviceGroupPermission(); permission.setGroupId(deviceGroup.getGroupId()); permission.setUserId(userId); b = groupPermissionService.save(permission); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_SAVE_ERROR); } DevicePermissionStream stream = new DevicePermissionStream(); stream.setGroupId(deviceGroup.getGroupId()); stream.setUserId(userId); if (StringUtils.isNotBlank(deviceGroup.getParentId())) { String path = null; List strings = findListParentTree(deviceGroup.getParentId(),null); if (strings != null && !strings.isEmpty()) { path = StringUtils.join(strings.toArray(), "/"); } FileUtilS.saveFileFromPath(path + "/" + deviceGroup.getGroupName()); } else { FileUtilS.saveFileFromPath(deviceGroup.getGroupName()); } return devicePermissionStreamService.save(stream); } @Override public DeviceGroup findByGroupName(String groupName) { if(!ValidateUtil.validateString(groupName)) return null; List list = super.lambdaQuery().eq(DeviceGroup::getGroupName, groupName).list(); if(list == null || list.isEmpty()) return null; return list.get(0); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean editDeviceGroup(String id, DeviceGroup deviceGroup) { if(!ValidateUtil.validateString(id) || deviceGroup == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceGroup en = super.getById(id); if(en == null) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); deviceGroup.setGroupId(id); deviceGroup.setParentId(null); deviceGroup.setRankLevel(null); if (StringUtils.isNotBlank(en.getParentId())) { String path = null; List strings = findListParentTree(en.getParentId(),null); if (strings != null && !strings.isEmpty()) { path = StringUtils.join(strings.toArray(), "/"); } FileUtilS.updateFileFromPath( path + "/"+ deviceGroup.getGroupName(), path + "/"+ en.getGroupName()); } else { FileUtilS.updateFileFromPath(deviceGroup.getGroupName(),en.getGroupName()); } //添加日志 NcLogInfo ncLogInfo = new NcLogInfo(); //模块 ncLogInfo.setModuleInfo("设备结构树"); //类型 ncLogInfo.setOperateType(3); //日志内容 ncLogInfo.setLogContent("分组名称:"+deviceGroup.getGroupName()); //修改保存备注 ncLogInfo.setRemark(JSONObject.toJSONString(en)); iNcLogInfoService.saveLogNcInfos(ncLogInfo); return super.updateById(deviceGroup); } // 查询所以父节点 @Override public List findListParentTree(String parentId,List stringList){ if (StringUtils.isEmpty(parentId)) { return null; } if (stringList == null || stringList.isEmpty()) { stringList = new ArrayList<>(); } boolean p = true; if (p) { DeviceGroup en = super.getById(parentId); if (en != null) { stringList.add(0,en.getGroupName()); } if (StringUtils.isNotBlank(en.getParentId())) { parentId = en.getParentId(); findListParentTree(parentId,stringList); } else { p = false; return stringList; } } return stringList; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean deleteDeviceGroup(String id) { if(!ValidateUtil.validateString(id)) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceGroup en = super.getById(id); if(en == null) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); List childList = getChildrenByParentId(id); if(childList != null && !childList.isEmpty()) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_CHILD_EXIST); List deviceInfoList = deviceInfoService.getByGroupId(en.getGroupId()); if(deviceInfoList != null && !deviceInfoList.isEmpty()) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_DEVICE_EXIST); boolean b = groupPermissionService.deleteByGroupId(en.getGroupId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = deviceGroupDepartService.deleteByGroupId(en.getGroupId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = devicePermissionStreamService.deleteUserByGroupId(en.getGroupId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = devicePermissionStreamService.deleteDepartByGroupId(en.getGroupId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); //删除对应文件 //添加日志 NcLogInfo ncLogInfo = new NcLogInfo(); //模块 ncLogInfo.setModuleInfo("设备结构树"); //类型 ncLogInfo.setOperateType(4); //日志内容 ncLogInfo.setLogContent("分组名称:"+en.getGroupName()); iNcLogInfoService.saveLogNcInfos(ncLogInfo); return super.removeById(en.getGroupId()); } @Override public List findExtAll() { return super.getBaseMapper().findExtAll(); } @Override public List getByUserPerms(String userId) { if(!ValidateUtil.validateString(userId)) return Collections.emptyList(); return super.getBaseMapper().getByUserPerms(userId); } @Override public List getByUserPermsAs(String userId) { if(!ValidateUtil.validateString(userId)) return Collections.emptyList(); return super.getBaseMapper().getByUserPermsAs(userId); } @Override public List findListParentTreeAll(String groupId) { if ( StringUtils.isEmpty(groupId)) { return null; } List strings = new ArrayList<>(); DeviceGroup en = super.getById(groupId); if (en == null) { return null; } strings.add(en.getGroupName()); if (StringUtils.isEmpty(en.getParentId())) { return strings; } else { return findListParentTree(en.getParentId(),strings); } } @Override public List getChildrenByParentId(String groupId) { if(!ValidateUtil.validateString(groupId)) return null; List extList = super.getBaseMapper().findByParentId(groupId); if(extList == null || extList.isEmpty()) return null; List list = new ArrayList<>(); extList.forEach(item -> { item.getAllChildren(list); }); return list; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddUser(DeviceGroup deviceGroup, Collection userList) { if(deviceGroup == null || userList == null || userList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List permissionList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); userList.forEach(item -> { DeviceGroupPermission en = groupPermissionService.getByUserIdAndGroupId(item.getId(), deviceGroup.getGroupId()); if(en == null) { en = new DeviceGroupPermission(); en.setUserId(item.getId()); en.setGroupId(deviceGroup.getGroupId()); permissionList.add(en); } DevicePermissionStream stream = devicePermissionStreamService.getByGroupIdAndUserId(deviceGroup.getGroupId(), item.getId()); if(stream == null) { stream = new DevicePermissionStream(); stream.setUserId(item.getId()); stream.setGroupId(deviceGroup.getGroupId()); permissionStreamList.add(stream); } }); if(!permissionList.isEmpty()) { boolean b = groupPermissionService.saveBatch(permissionList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = devicePermissionStreamService.saveBatch(permissionStreamList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveUser(DeviceGroup deviceGroup, Collection userList) { if(deviceGroup == null || userList == null || userList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List permissionList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); userList.forEach(item -> { DeviceGroupPermission en = groupPermissionService.getByUserIdAndGroupId(item.getId(), deviceGroup.getGroupId()); if(en != null) { permissionList.add(en); } DevicePermissionStream stream = devicePermissionStreamService.getByGroupIdAndUserId(deviceGroup.getGroupId(), item.getId()); if(stream != null) { permissionStreamList.add(stream); } }); //校验清空设备分组权限操作 List existList = groupPermissionService.getByGroupId(deviceGroup.getGroupId()); if(existList.size() <= permissionList.size()) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_USER_NONE); if(!permissionList.isEmpty()) { boolean b = groupPermissionService.removeByCollection(permissionList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = devicePermissionStreamService.removeByCollection(permissionStreamList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } return true; } @Override public List getDepartPermsList(String groupId) { return deviceGroupDepartService.getDepartPermsByGroupId(groupId); } @Override public List getDepartNonPermsList(String groupId) { return deviceGroupDepartService.getDepartNonPermsByGroupId(groupId); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddDepartment(String groupId, Integer relativeFlag, String[] departmentIds) { if(!ValidateUtil.validateString(groupId) || !ValidateUtil.validateInteger(relativeFlag) || departmentIds == null || departmentIds.length < 1) ExceptionCast.cast(CommonCode.INVALID_PARAM); List ids = new ArrayList<>(departmentIds.length); Collections.addAll(ids, departmentIds); Collection departmentList = departmentService.listByIds(ids); if(departmentList == null || departmentList.isEmpty() || departmentList.size() != departmentIds.length) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceGroup deviceGroup = super.getById(groupId); if(deviceGroup == null) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); boolean b1 = deviceInfoService.checkDevicePerm(1, deviceGroup.getGroupId()); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } b1 = assignAddDepartment(deviceGroup, departmentList); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } if(relativeFlag == 1) { //获取分组下所有的子分组 List childrenList = getChildrenByParentId(deviceGroup.getGroupId()); if(childrenList != null && !childrenList.isEmpty()) { childrenList.forEach(item -> { boolean b = deviceInfoService.checkDevicePerm(1, item.getGroupId()); if(b) { b = assignAddDepartment(item, departmentList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } }); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveDepartment(String groupId, Integer relativeFlag, String[] departmentIds) { if(!ValidateUtil.validateString(groupId) || !ValidateUtil.validateInteger(relativeFlag) || departmentIds == null || departmentIds.length < 1) ExceptionCast.cast(CommonCode.INVALID_PARAM); List ids = new ArrayList<>(departmentIds.length); Collections.addAll(ids, departmentIds); Collection departmentList = departmentService.listByIds(ids); if(departmentList == null || departmentList.isEmpty() || departmentList.size() != departmentIds.length) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceGroup deviceGroup = super.getById(groupId); if(deviceGroup == null) ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); boolean b1 = deviceInfoService.checkDevicePerm(1, deviceGroup.getGroupId()); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } b1 = assignRemoveDepartment(deviceGroup, departmentList); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } if(relativeFlag == 1) { //获取分组下所有的子分组 List childrenList = getChildrenByParentId(deviceGroup.getGroupId()); if(childrenList != null && !childrenList.isEmpty()) { childrenList.forEach(item -> { boolean b = deviceInfoService.checkDevicePerm(1, item.getGroupId()); if(b) { b = assignRemoveDepartment(item, departmentList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } }); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddDepartment(DeviceGroup deviceGroup, Collection departmentList) { if(deviceGroup == null || departmentList == null || departmentList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List deviceGroupDepartList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); departmentList.forEach(item -> { DeviceGroupDepart en = deviceGroupDepartService.getDepartByGroupAndDepartId(deviceGroup.getGroupId(), item.getDepartId()); if(en == null) { en = new DeviceGroupDepart(); en.setDepartId(item.getDepartId()); en.setGroupId(deviceGroup.getGroupId()); deviceGroupDepartList.add(en); } DevicePermissionStream stream = devicePermissionStreamService.getByGroupIdAndDepartId(deviceGroup.getGroupId(), item.getDepartId()); if(stream == null) { stream = new DevicePermissionStream(); stream.setDepartId(item.getDepartId()); stream.setGroupId(deviceGroup.getGroupId()); permissionStreamList.add(stream); } }); if(!deviceGroupDepartList.isEmpty()) { boolean b = deviceGroupDepartService.saveBatch(deviceGroupDepartList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = devicePermissionStreamService.saveBatch(permissionStreamList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveDepartment(DeviceGroup deviceGroup, Collection departmentList) { if(deviceGroup == null || departmentList == null || departmentList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List departList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); departmentList.forEach(item -> { DeviceGroupDepart en = deviceGroupDepartService.getDepartByGroupAndDepartId(deviceGroup.getGroupId(), item.getDepartId()); if(en != null) { departList.add(en); } DevicePermissionStream stream = devicePermissionStreamService.getByGroupIdAndDepartId(deviceGroup.getGroupId(), item.getDepartId()); if(stream != null) { permissionStreamList.add(stream); } }); if(!departList.isEmpty()) { boolean b = deviceGroupDepartService.removeByCollection(departList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = devicePermissionStreamService.removeByCollection(permissionStreamList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } return true; } @Override public List findExtByDeparts(List departIds) { return super.getBaseMapper().findExtByDeparts(departIds); } }