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 @Lazy private IDeviceInfoService deviceInfoService; // 查询所以父节点 @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 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 findExtByDeparts(List departIds) { return super.getBaseMapper().findExtByDeparts(departIds); } }