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; import org.jeecg.modules.dnc.dto.DeviceGroupExt; import org.jeecg.modules.dnc.exception.ExceptionCast; import org.jeecg.modules.dnc.mapper.DeviceInfoMapper; 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.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; import org.jeecg.modules.dnc.response.UcenterCode; import org.jeecg.modules.dnc.response.CommonCode; import org.jeecg.modules.dnc.response.CommonGenericTree; 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 DeviceInfoServiceImpl extends ServiceImpl implements IDeviceInfoService { @Autowired private IDevicePermissionService devicePermissionService; @Autowired @Lazy private IDeviceGroupService deviceGroupService; @Autowired private IDeviceGroupPermissionService deviceGroupPermissionService; @Autowired private IDevicePermissionStreamService devicePermissionStreamService; @Autowired private ISysUserService userService; @Autowired @Lazy private IDocInfoService docInfoService; @Autowired @Lazy private IProductInfoService productInfoService; @Autowired private INcLogInfoService iNcLogInfoService; @Autowired 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) { if(!checkDeviceControlPoint()){ ExceptionCast.cast(CommonCode.LICENSE_DEVICE_POINT_LARGE); } if(deviceInfo == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(!ValidateUtil.validateString(deviceInfo.getDeviceName())) ExceptionCast.cast(DeviceCode.DEVICE_NAME_NONE); if(!ValidateUtil.validateString(deviceInfo.getDepartId())) ExceptionCast.cast(DeviceCode.DEVICE_DEPART_NONE); if(!ValidateUtil.validateString(deviceInfo.getGroupId())) ExceptionCast.cast(DeviceCode.DEVICE_GROUP_NONE); if(!ValidateUtil.validateString(deviceInfo.getDeviceNo())) ExceptionCast.cast(DeviceCode.DEVICE_NO_NONE); DeviceInfo en = getByDeviceNo(deviceInfo.getDeviceNo()); if(en != null) ExceptionCast.cast(DeviceCode.DEVICE_IS_EXIST); LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) { ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); } DeviceGroup group = deviceGroupService.getById(deviceInfo.getGroupId()); 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); } DevicePermission permission = new DevicePermission(); permission.setDeviceId(deviceInfo.getDeviceId()); permission.setUserId(userId); b = devicePermissionService.save(permission); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_SAVE_ERROR); } DevicePermissionStream departStream = new DevicePermissionStream(); departStream.setGroupId(deviceInfo.getGroupId()); departStream.setDeviceId(deviceInfo.getDeviceId()); departStream.setDepartId(deviceInfo.getDepartId()); b = devicePermissionStreamService.save(departStream); if(!b) { ExceptionCast.cast(CommonCode.FAIL); } DevicePermissionStream stream = new DevicePermissionStream(); stream.setGroupId(deviceInfo.getGroupId()); stream.setDeviceId(deviceInfo.getDeviceId()); stream.setUserId(userId); String path = null; List strings = deviceGroupService.findListParentTreeAll(deviceInfo.getGroupId()); if (strings != null && !strings.isEmpty()) { path = StringUtils.join(strings.toArray(), "/"); FileUtilS.saveDeviceFromPath(path + "/" + deviceInfo.getDeviceNo()); } return devicePermissionStreamService.save(stream); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean editDeviceInfo(String id, DeviceInfo deviceInfo) { if(!ValidateUtil.validateString(id) || deviceInfo == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceInfo en = super.getById(id); if(en == null) 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); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean deleteDeviceInfo(String id) { if(!ValidateUtil.validateString(id)) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceInfo en = super.getById(id); if(en == null) ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST); boolean b = docInfoService.deleteByDeviceId(en.getDeviceId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = devicePermissionService.deleteByDeviceId(en.getDeviceId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = devicePermissionStreamService.deleteUserByDeviceId(en.getGroupId(), en.getDeviceId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); 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()); } @Override public List loadTree() { LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) return null; List extAll = deviceGroupService.getByUserPermsAs(userId); if(extAll == null || extAll.isEmpty()) return null; List deviceInfoList = getDeviceByUserPermed(userId); return DeviceTreeWrapper.loadTree(extAll, deviceInfoList); } @Override public List getDeviceByUserPermed(String userId) { return super.getBaseMapper().getDeviceByUserPermed(userId); } @Override public boolean checkDevicePerm(Integer nodeType, String paramId) { if(!ValidateUtil.validateString(paramId) || !ValidateUtil.validateInteger(nodeType)) ExceptionCast.cast(CommonCode.INVALID_PARAM); LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); if(nodeType == 1) { MdcProduction mdcProduction=mdcProductionService.getById(paramId); if(mdcProduction == null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); } DeviceGroupPermission permission = deviceGroupPermissionService.getByUserIdAndGroupId(userId, mdcProduction.getId()); if(permission == null) return false; return true; }else if(nodeType == 2) { MdcEquipment mdcEquipment = mdcEquipmentService.getById(paramId); if(mdcEquipment == null) ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST); DevicePermission permission = devicePermissionService.getByUserIdAndDeviceId(userId, mdcEquipment.getId()); if(permission == null) return false; return true; }else { return false; } } @Override public List getUserPermsList(Integer nodeType, String paramId) { if(!ValidateUtil.validateInteger(nodeType) || !ValidateUtil.validateString(paramId)) return null; if(nodeType == 1) { return mdcUserProductionService.getUserPermsByGroupId(paramId); }else if(nodeType == 2) { return mdcUserProductionService.getUserPermsByDeviceId(paramId); }else { return null; } } @Override public List getUserNonPermsList(Integer nodeType, String paramId) { if(!ValidateUtil.validateInteger(nodeType) || !ValidateUtil.validateString(paramId)) return null; if(nodeType == 1) { return mdcUserProductionService.getUserNonPermsByGroupId(paramId); }else if(nodeType == 2) { return mdcUserProductionService.getUserNonPermsByDeviceId(paramId); }else { return null; } } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddUser(Integer nodeType, String paramId, Integer relativeFlag, String[] userIds) { if(!ValidateUtil.validateInteger(nodeType) || !ValidateUtil.validateString(paramId) || !ValidateUtil.validateInteger(relativeFlag)) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(userIds == null || userIds.length < 1) ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_NONE); List ids = new ArrayList<>(userIds.length); Collections.addAll(ids, userIds); Collection userList = userService.listByIds(ids); if(userList == null || userList.isEmpty() || userList.size() != userIds.length) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(relativeFlag == 1 && nodeType == 1) { //分配分组权限才可向下传递用户 MdcProduction mdcProduction= mdcProductionService.getById(paramId); if(mdcProduction == null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); } boolean b1 = checkDevicePerm(1, mdcProduction.getId()); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } b1 = deviceGroupPermissionService.assignAddUser(mdcProduction, userList); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } //获取分组下所有的子分组 List childrenList = mdcProductionService.recursionChildrenByPid(mdcProduction.getId()); List childrenIds = new ArrayList<>(); if(childrenList != null && !childrenList.isEmpty()) { childrenList.forEach(item -> { childrenIds.add(item.getId()); boolean b = checkDevicePerm(1, item.getId()); if(b) { b = deviceGroupPermissionService.assignAddUser(item, userList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } }); } List mdcEquipmentList = null; if(childrenIds.isEmpty()) { mdcEquipmentList =mdcProductionEquipmentService.queryEquipmentsOfProduction(mdcProduction.getId()); }else { childrenIds.add(mdcProduction.getId()); mdcEquipmentList = mdcProductionEquipmentService.queryEquipmentsOfProductions(childrenIds); } if(mdcEquipmentList != null && !mdcEquipmentList.isEmpty()) { mdcEquipmentList.forEach(item -> { boolean b = checkDevicePerm(2, item.getId()); if(b) { b = assignAddUser(item, userList); if(!b) ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } }); } return true; }else if(relativeFlag == 2) { if(nodeType == 1) { //分批分组权限 MdcProduction mdcProduction = mdcProductionService.getById(paramId); if(mdcProduction == null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); } boolean b = checkDevicePerm(1, mdcProduction.getId()); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } b = deviceGroupPermissionService.assignAddUser(mdcProduction, userList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } return true; }else if(nodeType == 2) { //分配设备权限 MdcEquipment mdcEquipment=mdcEquipmentService.getById(paramId); if(mdcEquipment == null) ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST); boolean b = checkDevicePerm(2, mdcEquipment.getId()); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } b = assignAddUser(mdcEquipment, userList); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } return true; } } return false; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddUser(MdcEquipment mdcEquipment, Collection userList) { if(mdcEquipment == null || userList == null || userList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); MdcProductionEquipment mdcProductionEquipment=mdcProductionEquipmentService .list(new QueryWrapper().eq("equipment_id",mdcEquipment.getId())).get(0); List permissionList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); userList.forEach(item -> { DevicePermission en = devicePermissionService.getByUserIdAndDeviceId(item.getId(), mdcEquipment.getId()); if(en == null) { en = new DevicePermission(); en.setUserId(item.getId()); en.setDeviceId(mdcEquipment.getId()); permissionList.add(en); } DevicePermissionStream stream = devicePermissionStreamService.getByDeviceIdAndUserId(mdcProductionEquipment.getId(), mdcEquipment.getId(), item.getId()); if(stream == null) { stream = new DevicePermissionStream(); stream.setUserId(item.getId()); stream.setGroupId(mdcProductionEquipment.getId()); stream.setDeviceId(mdcEquipment.getId()); permissionStreamList.add(stream); } }); if(!permissionList.isEmpty()) { boolean b = devicePermissionService.saveBatch(permissionList); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = devicePermissionStreamService.saveBatch(permissionStreamList); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveUser(MdcEquipment mdcEquipment, Collection userList) { if(mdcEquipment == null || userList == null || userList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); MdcProductionEquipment mdcProductionEquipment=mdcProductionEquipmentService .list(new QueryWrapper().eq("equipment_id",mdcEquipment.getId())).get(0); List permissionList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); userList.forEach(item -> { DevicePermission en = devicePermissionService.getByUserIdAndDeviceId(item.getId(), mdcEquipment.getId()); if(en != null) { permissionList.add(en); } DevicePermissionStream stream = devicePermissionStreamService.getByDeviceIdAndUserId(mdcProductionEquipment.getId(), mdcEquipment.getId(), item.getId()); if(stream != null) { permissionStreamList.add(stream); } }); //校验清空用户权限 List existList = devicePermissionService.getByDeviceId(mdcEquipment.getId()); if(existList.size() <= permissionList.size()) ExceptionCast.cast(DeviceCode.DEVICE_USER_NONE); if(!permissionList.isEmpty()) { boolean b = devicePermissionService.removeByCollection(permissionList); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = devicePermissionStreamService.removeByCollection(permissionStreamList); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } } return true; } @Override public List getByGroupIdList(List groupIdList) { if(groupIdList == null || groupIdList.isEmpty()) return null; if(groupIdList.size() == 1) return getByGroupId(groupIdList.get(0)); return super.lambdaQuery().in(DeviceInfo::getGroupId, groupIdList).list(); } @Override public List getByGroupId(String groupId) { if(!ValidateUtil.validateString(groupId)) return null; return super.lambdaQuery().eq(DeviceInfo::getGroupId, groupId).list(); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveUser(Integer nodeType, String paramId, Integer relativeFlag, String[] userIds) { if(!ValidateUtil.validateInteger(nodeType) || !ValidateUtil.validateString(paramId) || !ValidateUtil.validateInteger(relativeFlag)) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(userIds == null || userIds.length < 1) ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_NONE); List ids = new ArrayList<>(userIds.length); Collections.addAll(ids, userIds); Collection userList = userService.listByIds(ids); if(userList == null || userList.isEmpty() || userList.size() != userIds.length) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(relativeFlag == 1 && nodeType == 1) { //分配分组权限才可向下传递用户 MdcProduction mdcProduction=mdcProductionService.getById(paramId); if(mdcProduction == null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); } boolean b1 = checkDevicePerm(1, mdcProduction.getId()); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } b1 = deviceGroupPermissionService.assignRemoveUser(mdcProduction, userList); if(!b1) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } //获取分组下所有的子分组 List childrenList = mdcProductionService.recursionChildrenByPid(mdcProduction.getId()); List childrenIds = new ArrayList<>(); if(childrenList != null && !childrenList.isEmpty()) { childrenList.forEach(item -> { childrenIds.add(item.getId()); boolean b = checkDevicePerm(1, item.getId()); if(b) { b = deviceGroupPermissionService.assignRemoveUser(item, userList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } } }); } List mdcEquipmentList = null; if(childrenIds.isEmpty()) { mdcEquipmentList =mdcProductionEquipmentService.queryEquipmentsOfProduction(mdcProduction.getId()); }else { childrenIds.add(mdcProduction.getId()); mdcEquipmentList = mdcProductionEquipmentService.queryEquipmentsOfProductions(childrenIds); } if(mdcEquipmentList != null && !mdcEquipmentList.isEmpty()) { mdcEquipmentList.forEach(item -> { boolean b = checkDevicePerm(2, item.getId()); if(b) { b = assignRemoveUser(item, userList); if(!b) ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } }); } return true; }else if(relativeFlag == 2) { if(nodeType == 1) { //分批分组权限 MdcProduction mdcProduction = mdcProductionService.getById(paramId); if(mdcProduction == null) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_NOT_EXIST); } boolean b = checkDevicePerm(1, mdcProduction.getId()); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } b = deviceGroupPermissionService.assignRemoveUser(mdcProduction, userList); if(!b) { ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR); } return true; }else if(nodeType == 2) { //分配设备权限 MdcEquipment mdcEquipment=mdcEquipmentService.getById(paramId); if(mdcEquipment == null) ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST); boolean b = checkDevicePerm(2, mdcEquipment.getId()); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } b = assignRemoveUser(mdcEquipment, userList); if(!b) { ExceptionCast.cast(DeviceCode.DEVICE_USER_PERM_ERROR); } return true; } } return false; } @Override public List loadDepartTree(Integer nodeType, String paramId) { if(!ValidateUtil.validateString(paramId) || !ValidateUtil.validateInteger(nodeType)) return null; List departIds = productInfoService.getDepartIdsByParams(nodeType, paramId); if(departIds == null || departIds.isEmpty()) return null; List deviceGroupExts = deviceGroupService.findExtByDeparts(departIds); if(deviceGroupExts == null || deviceGroupExts.isEmpty()) return null; List deviceInfoList = findByDepartIds(departIds); if(deviceInfoList == null || deviceInfoList.isEmpty()) return null; return DeviceTreeWrapper.loadDepartTree(deviceGroupExts, deviceInfoList); } @Override public List findByDepartIds(List departIds) { return super.lambdaQuery().in(DeviceInfo::getDepartId, departIds).list(); } @Override public DeviceInfo getByDeviceNo(String deviceNo) { if(ValidateUtil.validateString(deviceNo)) { List list = super.lambdaQuery().eq(DeviceInfo::getDeviceNo, deviceNo).list(); if(list == null || list.isEmpty()) return null; return list.get(0); } return null; } @Override public boolean checkDeviceControlPoint() { // int num = Integer.valueOf(LicenseModel.getDncDeviceNumber()); // int count = super.count(); // if(count <= num){ return true; // } // return false; } }