hyingbo
7 天以前 cc0e9036de6e922e8fe254fef01d8de9996024b7
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/DeviceGroupServiceImpl.java
@@ -1,27 +1,21 @@
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.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.response.UcenterCode;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.ucenter.Department;
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.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;
@@ -36,148 +30,8 @@
    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<DevicePermissionStream> 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<DeviceGroupDepart> groupDepartList = new ArrayList<>();
            List<DevicePermissionStream> 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<String> 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<DeviceGroup> 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<String> 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<String> findListParentTree(String parentId,List<String> stringList){
@@ -202,44 +56,6 @@
            }
        }
        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<DeviceGroup> childList = getChildrenByParentId(id);
        if(childList != null && !childList.isEmpty())
            ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_CHILD_EXIST);
        List<DeviceInfo> 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
@@ -352,164 +168,6 @@
            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<Department> getDepartPermsList(String groupId) {
        return deviceGroupDepartService.getDepartPermsByGroupId(groupId);
    }
    @Override
    public List<Department> 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<String> ids = new ArrayList<>(departmentIds.length);
        Collections.addAll(ids, departmentIds);
        Collection<Department> 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<DeviceGroup> 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<String> ids = new ArrayList<>(departmentIds.length);
        Collections.addAll(ids, departmentIds);
        Collection<Department> 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<DeviceGroup> 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<Department> departmentList) {
        if(deviceGroup == null || departmentList == null || departmentList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<DeviceGroupDepart> deviceGroupDepartList = new ArrayList<>();
        List<DevicePermissionStream> 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<Department> departmentList) {
        if(deviceGroup == null || departmentList == null || departmentList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<DeviceGroupDepart> departList = new ArrayList<>();
        List<DevicePermissionStream> 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);
            }