zhangherong
2025-06-25 23855599412c4d61b38d78f0f3abd3430a48b5b1
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/DeviceGroupServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,188 @@
package org.jeecg.modules.dnc.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.dnc.dto.DeviceGroupExt;
import org.jeecg.modules.dnc.entity.DeviceGroup;
import org.jeecg.modules.dnc.entity.DeviceGroupPermission;
import org.jeecg.modules.dnc.entity.DevicePermissionStream;
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.service.IDeviceGroupPermissionService;
import org.jeecg.modules.dnc.service.IDeviceGroupService;
import org.jeecg.modules.dnc.service.IDevicePermissionStreamService;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
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<DeviceGroupMapper, DeviceGroup> implements IDeviceGroupService {
    @Autowired
    private IDeviceGroupPermissionService groupPermissionService;
    @Autowired
    private IDevicePermissionStreamService devicePermissionStreamService;
    //  æŸ¥è¯¢æ‰€ä»¥çˆ¶èŠ‚ç‚¹
    @Override
    public List<String> findListParentTree(String parentId,List<String> 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<DeviceGroupExt> findExtAll() {
        return super.getBaseMapper().findExtAll();
    }
    @Override
    public List<DeviceGroupExt> getByUserPerms(String userId) {
        if(!ValidateUtil.validateString(userId))
            return Collections.emptyList();
        return super.getBaseMapper().getByUserPerms(userId);
    }
    @Override
    public List<DeviceGroupExt> getByUserPermsAs(String userId) {
        if(!ValidateUtil.validateString(userId))
            return Collections.emptyList();
        return super.getBaseMapper().getByUserPermsAs(userId);
    }
    @Override
    public List<String> findListParentTreeAll(String groupId) {
        if ( StringUtils.isEmpty(groupId)) {
            return null;
        }
        List<String> 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<DeviceGroup> getChildrenByParentId(String groupId) {
        if(!ValidateUtil.validateString(groupId))
            return null;
        List<DeviceGroupExt> extList = super.getBaseMapper().findByParentId(groupId);
        if(extList == null || extList.isEmpty())
            return null;
        List<DeviceGroup> list = new ArrayList<>();
        extList.forEach(item -> {
            item.getAllChildren(list);
        });
        return list;
    }
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignAddUser(DeviceGroup deviceGroup, Collection<SysUser> userList) {
        if(deviceGroup == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<DeviceGroupPermission> permissionList = new ArrayList<>();
        List<DevicePermissionStream> 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<SysUser> userList) {
        if(deviceGroup == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<DeviceGroupPermission> permissionList = new ArrayList<>();
        List<DevicePermissionStream> 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<DeviceGroupPermission> 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<DeviceGroupExt> findExtByDeparts(List<String> departIds) {
        return super.getBaseMapper().findExtByDeparts(departIds);
    }
}