Lius
2025-03-21 87874019eefbe03dd429b5b7eed49d963ef03c88
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/DeviceInfoServiceImpl.java
@@ -1,6 +1,6 @@
package org.jeecg.modules.dnc.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
@@ -11,10 +11,17 @@
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.service.support.DeviceTreeWrapper;
import org.jeecg.modules.dnc.ucenter.UserDepartExt;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.IMdcProductionEquipmentService;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.entity.MdcProductionEquipment;
import org.jeecg.modules.system.entity.MdcUserProduction;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.IMdcUserProductionService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.dnc.response.DeviceCode;
import org.jeecg.modules.dnc.response.DeviceGroupCode;
@@ -54,8 +61,13 @@
    @Lazy
    private IProductInfoService productInfoService;
    @Autowired
    private INcLogInfoService iNcLogInfoService;
    private IMdcProductionService mdcProductionService;
    @Autowired
    private IMdcEquipmentService mdcEquipmentService;
    @Autowired
    private IMdcProductionEquipmentService mdcProductionEquipmentService;
    @Autowired
    private IMdcUserProductionService mdcUserProductionService;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addDeviceInfo(DeviceInfo deviceInfo) {
@@ -84,15 +96,6 @@
        if(group == null) {
            ExceptionCast.cast(DeviceCode.DEVICE_GROUP_NONE);
        }
        //添加日志
        NcLogInfo ncLogInfo = new NcLogInfo();
        //模块
        ncLogInfo.setModuleInfo("设备结构树");
        //类型
        ncLogInfo.setOperateType(2);
        //日志内容
        ncLogInfo.setLogContent("设备名称:"+deviceInfo.getDeviceName()+",设备编号:"+deviceInfo.getDeviceNo());
        iNcLogInfoService.saveLogNcInfos(ncLogInfo);
        boolean b = super.save(deviceInfo);
        if(!b) {
            ExceptionCast.cast(DeviceCode.DEVICE_SAVE_ERROR);
@@ -135,17 +138,6 @@
            ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST);
        deviceInfo.setDeviceId(id);
        deviceInfo.setGroupId(null);
        //添加日志
        NcLogInfo ncLogInfo = new NcLogInfo();
        //模块
        ncLogInfo.setModuleInfo("设备结构树");
        //类型
        ncLogInfo.setOperateType(3);
        //日志内容
        ncLogInfo.setLogContent("设备名称:"+deviceInfo.getDeviceName());
        //修改保存备注
        ncLogInfo.setRemark(JSONObject.toJSONString(en));
        iNcLogInfoService.saveLogNcInfos(ncLogInfo);
        return super.updateById(deviceInfo);
    }
@@ -169,15 +161,6 @@
        b = devicePermissionStreamService.deleteDepartByDeviceId(en.getGroupId(), en.getDeviceId());
        if(!b)
            ExceptionCast.cast(CommonCode.FAIL);
        //添加日志
        NcLogInfo ncLogInfo = new NcLogInfo();
        //模块
        ncLogInfo.setModuleInfo("设备结构树");
        //类型
        ncLogInfo.setOperateType(4);
        //日志内容
        ncLogInfo.setLogContent("设备名称:"+en.getDeviceName());
        iNcLogInfoService.saveLogNcInfos(ncLogInfo);
        return super.removeById(en.getDeviceId());
    }
@@ -208,19 +191,19 @@
        if(!ValidateUtil.validateString(userId))
            ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST);
        if(nodeType == 1) {
            DeviceGroup en = deviceGroupService.getById(paramId);
            if(en == null) {
            MdcProduction mdcProduction=mdcProductionService.getById(paramId);
            if(mdcProduction == null) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST);
            }
            DeviceGroupPermission permission = deviceGroupPermissionService.getByUserIdAndGroupId(userId, en.getGroupId());
            if(permission == null)
            MdcUserProduction mdcUserProduction=mdcUserProductionService.getByUserIdAndGroupId(userId,mdcProduction.getId());
            if(mdcUserProduction == null)
                return false;
            return true;
        }else if(nodeType == 2) {
            DeviceInfo deviceInfo = super.getById(paramId);
            if(deviceInfo == null)
            MdcEquipment mdcEquipment = mdcEquipmentService.getById(paramId);
            if(mdcEquipment == null)
                ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST);
            DevicePermission permission = devicePermissionService.getByUserIdAndDeviceId(userId, deviceInfo.getDeviceId());
            DevicePermission permission = devicePermissionService.getByUserIdAndDeviceId(userId, mdcEquipment.getId());
            if(permission == null)
                return false;
            return true;
@@ -230,13 +213,13 @@
    }
    @Override
    public List<UserDepartExt> getUserPermsList(Integer nodeType, String paramId) {
    public List<SysUser> getUserPermsList(Integer nodeType, String paramId) {
        if(!ValidateUtil.validateInteger(nodeType) || !ValidateUtil.validateString(paramId))
            return null;
        if(nodeType == 1) {
            return deviceGroupPermissionService.getUserPermsByGroupId(paramId);
            return mdcUserProductionService.getUserPermsByGroupId(paramId);
        }else if(nodeType == 2) {
            return devicePermissionService.getUserPermsByDeviceId(paramId);
            return mdcUserProductionService.getUserPermsByDeviceId(paramId);
        }else {
            return null;
        }
@@ -247,9 +230,9 @@
        if(!ValidateUtil.validateInteger(nodeType) || !ValidateUtil.validateString(paramId))
            return null;
        if(nodeType == 1) {
            return deviceGroupPermissionService.getUserNonPermsByGroupId(paramId);
            return mdcUserProductionService.getUserNonPermsByGroupId(paramId);
        }else if(nodeType == 2) {
            return devicePermissionService.getUserNonPermsByDeviceId(paramId);
            return mdcUserProductionService.getUserNonPermsByDeviceId(paramId);
        }else {
            return null;
        }
@@ -270,43 +253,43 @@
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(relativeFlag == 1 && nodeType == 1) {
            //分配分组权限才可向下传递用户
            DeviceGroup deviceGroup = deviceGroupService.getById(paramId);
            if(deviceGroup == null) {
            MdcProduction mdcProduction= mdcProductionService.getById(paramId);
            if(mdcProduction == null) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST);
            }
            boolean b1 = checkDevicePerm(1, deviceGroup.getGroupId());
            boolean b1 = checkDevicePerm(1, mdcProduction.getId());
            if(!b1) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
            b1 = deviceGroupService.assignAddUser(deviceGroup, userList);
            b1 = deviceGroupPermissionService.assignAddUser(mdcProduction, userList);
            if(!b1) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
            //获取分组下所有的子分组
            List<DeviceGroup> childrenList = deviceGroupService.getChildrenByParentId(deviceGroup.getGroupId());
            List<MdcProduction> childrenList = mdcProductionService.recursionChildrenByPid(mdcProduction.getId());
            List<String> childrenIds = new ArrayList<>();
            if(childrenList != null && !childrenList.isEmpty()) {
                childrenList.forEach(item -> {
                    childrenIds.add(item.getGroupId());
                    boolean b = checkDevicePerm(1, item.getGroupId());
                    childrenIds.add(item.getId());
                    boolean b = checkDevicePerm(1, item.getId());
                    if(b) {
                        b = deviceGroupService.assignAddUser(item, userList);
                        b = deviceGroupPermissionService.assignAddUser(item, userList);
                        if(!b) {
                            ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
                        }
                    }
                });
            }
            List<DeviceInfo> deviceInfoList = null;
            List<MdcEquipment> mdcEquipmentList = null;
            if(childrenIds.isEmpty()) {
                deviceInfoList = getByGroupId(deviceGroup.getGroupId());
                mdcEquipmentList =mdcProductionEquipmentService.queryEquipmentsOfProduction(mdcProduction.getId());
            }else {
                childrenIds.add(deviceGroup.getGroupId());
                deviceInfoList = getByGroupIdList(childrenIds);
                childrenIds.add(mdcProduction.getId());
                mdcEquipmentList = mdcProductionEquipmentService.queryEquipmentsOfProductions(childrenIds);
            }
            if(deviceInfoList != null && !deviceInfoList.isEmpty()) {
                deviceInfoList.forEach(item -> {
                    boolean b = checkDevicePerm(2, item.getDeviceId());
            if(mdcEquipmentList != null && !mdcEquipmentList.isEmpty()) {
                mdcEquipmentList.forEach(item -> {
                    boolean b = checkDevicePerm(2, item.getId());
                    if(b) {
                        b = assignAddUser(item, userList);
                        if(!b)
@@ -318,29 +301,29 @@
        }else if(relativeFlag == 2) {
            if(nodeType == 1) {
                //分批分组权限
                DeviceGroup deviceGroup = deviceGroupService.getById(paramId);
                if(deviceGroup == null) {
                MdcProduction mdcProduction = mdcProductionService.getById(paramId);
                if(mdcProduction == null) {
                    ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST);
                }
                boolean b = checkDevicePerm(1, deviceGroup.getGroupId());
                boolean b = checkDevicePerm(1, mdcProduction.getId());
                if(!b) {
                    ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
                }
                b = deviceGroupService.assignAddUser(deviceGroup, userList);
                b = deviceGroupPermissionService.assignAddUser(mdcProduction, userList);
                if(!b) {
                    ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
                }
                return true;
            }else if(nodeType == 2) {
                //分配设备权限
                DeviceInfo deviceInfo = super.getById(paramId);
                if(deviceInfo == null)
                MdcEquipment mdcEquipment=mdcEquipmentService.getById(paramId);
                if(mdcEquipment == null)
                    ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST);
                boolean b = checkDevicePerm(2, deviceInfo.getDeviceId());
                boolean b = checkDevicePerm(2, mdcEquipment.getId());
                if(!b) {
                    ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR);
                }
                b = assignAddUser(deviceInfo, userList);
                b = assignAddUser(mdcEquipment, userList);
                if(!b) {
                    ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR);
                }
@@ -352,25 +335,29 @@
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignAddUser(DeviceInfo deviceInfo, Collection<SysUser> userList) {
        if(deviceInfo == null || userList == null || userList.isEmpty())
    public boolean assignAddUser(MdcEquipment mdcEquipment, Collection<SysUser> userList) {
        if(mdcEquipment == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        MdcProductionEquipment mdcProductionEquipment=mdcProductionEquipmentService
                .list(new QueryWrapper<MdcProductionEquipment>().eq("equipment_id",mdcEquipment.getId())).get(0);
        List<DevicePermission> permissionList = new ArrayList<>();
        List<DevicePermissionStream> permissionStreamList = new ArrayList<>();
        userList.forEach(item -> {
            DevicePermission en = devicePermissionService.getByUserIdAndDeviceId(item.getId(), deviceInfo.getDeviceId());
            DevicePermission en = devicePermissionService.getByUserIdAndDeviceId(item.getId(), mdcEquipment.getId());
            if(en == null) {
                en = new DevicePermission();
                en.setUserId(item.getId());
                en.setDeviceId(deviceInfo.getDeviceId());
                en.setDeviceId(mdcEquipment.getId());
                permissionList.add(en);
            }
            DevicePermissionStream stream = devicePermissionStreamService.getByDeviceIdAndUserId(deviceInfo.getGroupId(), deviceInfo.getDeviceId(), item.getId());
            DevicePermissionStream stream = devicePermissionStreamService.getByDeviceIdAndUserId(mdcProductionEquipment.getId(), mdcEquipment.getId(), item.getId());
            if(stream == null) {
                stream = new DevicePermissionStream();
                stream.setUserId(item.getId());
                stream.setGroupId(deviceInfo.getGroupId());
                stream.setDeviceId(deviceInfo.getDeviceId());
                stream.setGroupId(mdcProductionEquipment.getProductionId());
                stream.setDeviceId(mdcEquipment.getId());
                permissionStreamList.add(stream);
            }
        });
@@ -391,23 +378,25 @@
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignRemoveUser(DeviceInfo deviceInfo, Collection<SysUser> userList) {
        if(deviceInfo == null || userList == null || userList.isEmpty())
    public boolean assignRemoveUser(MdcEquipment mdcEquipment, Collection<SysUser> userList) {
        if(mdcEquipment == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        MdcProductionEquipment mdcProductionEquipment=mdcProductionEquipmentService
                .list(new QueryWrapper<MdcProductionEquipment>().eq("equipment_id",mdcEquipment.getId())).get(0);
        List<DevicePermission> permissionList = new ArrayList<>();
        List<DevicePermissionStream> permissionStreamList = new ArrayList<>();
        userList.forEach(item -> {
            DevicePermission en = devicePermissionService.getByUserIdAndDeviceId(item.getId(), deviceInfo.getDeviceId());
            DevicePermission en = devicePermissionService.getByUserIdAndDeviceId(item.getId(), mdcEquipment.getId());
            if(en != null) {
                permissionList.add(en);
            }
            DevicePermissionStream stream = devicePermissionStreamService.getByDeviceIdAndUserId(deviceInfo.getGroupId(), deviceInfo.getDeviceId(), item.getId());
            DevicePermissionStream stream = devicePermissionStreamService.getByDeviceIdAndUserId(mdcProductionEquipment.getId(), mdcEquipment.getId(), item.getId());
            if(stream != null) {
                permissionStreamList.add(stream);
            }
        });
        //校验清空用户权限
        List<DevicePermission> existList = devicePermissionService.getByDeviceId(deviceInfo.getDeviceId());
        List<DevicePermission> existList = devicePermissionService.getByDeviceId(mdcEquipment.getId());
        if(existList.size() <= permissionList.size())
            ExceptionCast.cast(DeviceCode.DEVICE_USER_NONE);
        if(!permissionList.isEmpty()) {
@@ -456,43 +445,43 @@
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(relativeFlag == 1 && nodeType == 1) {
            //分配分组权限才可向下传递用户
            DeviceGroup deviceGroup = deviceGroupService.getById(paramId);
            if(deviceGroup == null) {
            MdcProduction mdcProduction=mdcProductionService.getById(paramId);
            if(mdcProduction == null) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST);
            }
            boolean b1 = checkDevicePerm(1, deviceGroup.getGroupId());
            boolean b1 = checkDevicePerm(1, mdcProduction.getId());
            if(!b1) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
            b1 = deviceGroupService.assignRemoveUser(deviceGroup, userList);
            b1 = deviceGroupPermissionService.assignRemoveUser(mdcProduction, userList);
            if(!b1) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
            //获取分组下所有的子分组
            List<DeviceGroup> childrenList = deviceGroupService.getChildrenByParentId(deviceGroup.getGroupId());
            List<MdcProduction> childrenList = mdcProductionService.recursionChildrenByPid(mdcProduction.getId());
            List<String> childrenIds = new ArrayList<>();
            if(childrenList != null && !childrenList.isEmpty()) {
                childrenList.forEach(item -> {
                    childrenIds.add(item.getGroupId());
                    boolean b = checkDevicePerm(1, item.getGroupId());
                    childrenIds.add(item.getId());
                    boolean b = checkDevicePerm(1, item.getId());
                    if(b) {
                        b = deviceGroupService.assignRemoveUser(item, userList);
                        b = deviceGroupPermissionService.assignRemoveUser(item, userList);
                        if(!b) {
                            ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
                        }
                    }
                });
            }
            List<DeviceInfo> deviceInfoList = null;
            List<MdcEquipment> mdcEquipmentList = null;
            if(childrenIds.isEmpty()) {
                deviceInfoList = getByGroupId(deviceGroup.getGroupId());
                mdcEquipmentList =mdcProductionEquipmentService.queryEquipmentsOfProduction(mdcProduction.getId());
            }else {
                childrenIds.add(deviceGroup.getGroupId());
                deviceInfoList = getByGroupIdList(childrenIds);
                childrenIds.add(mdcProduction.getId());
                mdcEquipmentList = mdcProductionEquipmentService.queryEquipmentsOfProductions(childrenIds);
            }
            if(deviceInfoList != null && !deviceInfoList.isEmpty()) {
                deviceInfoList.forEach(item -> {
                    boolean b = checkDevicePerm(2, item.getDeviceId());
            if(mdcEquipmentList != null && !mdcEquipmentList.isEmpty()) {
                mdcEquipmentList.forEach(item -> {
                    boolean b = checkDevicePerm(2, item.getId());
                    if(b) {
                        b = assignRemoveUser(item, userList);
                        if(!b)
@@ -504,29 +493,29 @@
        }else if(relativeFlag == 2) {
            if(nodeType == 1) {
                //分批分组权限
                DeviceGroup deviceGroup = deviceGroupService.getById(paramId);
                if(deviceGroup == null) {
                MdcProduction mdcProduction = mdcProductionService.getById(paramId);
                if(mdcProduction == null) {
                    ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST);
                }
                boolean b = checkDevicePerm(1, deviceGroup.getGroupId());
                boolean b = checkDevicePerm(1, mdcProduction.getId());
                if(!b) {
                    ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
                }
                b = deviceGroupService.assignRemoveUser(deviceGroup, userList);
                b = deviceGroupPermissionService.assignRemoveUser(mdcProduction, userList);
                if(!b) {
                    ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
                }
                return true;
            }else if(nodeType == 2) {
                //分配设备权限
                DeviceInfo deviceInfo = super.getById(paramId);
                if(deviceInfo == null)
                MdcEquipment mdcEquipment=mdcEquipmentService.getById(paramId);
                if(mdcEquipment == null)
                    ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST);
                boolean b = checkDevicePerm(2, deviceInfo.getDeviceId());
                boolean b = checkDevicePerm(2, mdcEquipment.getId());
                if(!b) {
                    ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR);
                }
                b = assignRemoveUser(deviceInfo, userList);
                b = assignRemoveUser(mdcEquipment, userList);
                if(!b) {
                    ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR);
                }