package com.lxzn.nc.service.impl; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.lxzn.auth.JwtUtil; import com.lxzn.framework.domain.nc.DeviceGroup; import com.lxzn.framework.domain.nc.DeviceInfo; import com.lxzn.framework.domain.nc.DeviceManagement; import com.lxzn.framework.domain.nc.DeviceType; import com.lxzn.framework.domain.nc.ext.DeviceGroupExt; import com.lxzn.framework.domain.nc.response.DeviceCode; import com.lxzn.framework.exception.ExceptionCast; import com.lxzn.framework.model.response.CommonCode; import com.lxzn.framework.model.response.QueryPageResponseResult; import com.lxzn.framework.utils.ValidateUtil; import com.lxzn.nc.dao.DeviceManagementMapper; import com.lxzn.nc.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Service public class DeviceManagementServiceImpl extends ServiceImpl implements IDeviceManagementService { @Autowired private IDeviceGroupService iDeviceGroupService; @Autowired private IDeviceGroupPermissionService iDeviceGroupPermissionService; @Autowired private IDeviceInfoService iDeviceInfoService; @Autowired private IDeviceTypeService deviceTypeService; /** * 新增设备类信息 * @param deviceManagement * @return */ @Override public boolean add(DeviceManagement deviceManagement){ if(deviceManagement == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(!ValidateUtil.validateString(deviceManagement.getEquipmentIds())) ExceptionCast.cast(DeviceCode.DEVICE_CLASS_ERROR); List deviceManagementList =this.getDeviceManagementList(deviceManagement.getProductionId(),deviceManagement.getDeviceManagementName(),deviceManagement.getDeviceManagementCode()); if (deviceManagementList != null && !deviceManagementList.isEmpty()) { ExceptionCast.cast(DeviceCode.DEVICE_CLASS_EXIST); } return this.save(deviceManagement); } /** * 编辑设备类信息 * @param deviceManagement * @return */ @Override public boolean edit(DeviceManagement deviceManagement){ // 检查传入的设备类对象是否为空 if (deviceManagement == null) { ExceptionCast.cast(DeviceCode.DEVICE_CLASS_NONE); } // 检查设备类名称是否有效 if (!ValidateUtil.validateString(deviceManagement.getDeviceManagementName())) { ExceptionCast.cast(DeviceCode.DEVICE_CLASS_NAME_ERROR); } // 根据设备类 ID 获取设备类信息 DeviceManagement existingCutter = super.getById(deviceManagement.getId()); if (existingCutter == null) { ExceptionCast.cast(DeviceCode.DEVICE_CLASS_NOT_EXIST); } // 过滤掉当前要编辑的设备类,检查是否有其他设备类存在相同编号 List otherCuttersWithSameNo =this.getDeviceManagementList(deviceManagement.getProductionId(),deviceManagement.getDeviceManagementName(),deviceManagement.getDeviceManagementCode()).stream() .filter(cut -> !cut.getId().equals(deviceManagement.getId())) .collect(Collectors.toList()); if (!otherCuttersWithSameNo.isEmpty()) { // 如果存在除当前设备类外的其他设备类编号重复 ExceptionCast.cast(DeviceCode.DEVICE_CLASS_CODE_EXIST); } // 尝试更新设备类信息 return this.updateById(deviceManagement); } /** * 根据id删除设备类信息 * @param id * @return */ @Override public boolean delete(String id){ if(!ValidateUtil.validateString(id)) ExceptionCast.cast(CommonCode.INVALID_PARAM); DeviceManagement en = super.getById(id); if(en == null) ExceptionCast.cast(DeviceCode.DEVICE_CLASS_ERROR); return super.removeById(id); } /** * 批量删除设备类 * @param ids * @return */ @Override public boolean deleteBatch(List ids){ if(ids == null || ids.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); return super.removeByIds(ids); } /** * 设备类信息分页查询 * @param deviceManagement * @param pageNo * @param pageSize * @return */ @Override public QueryPageResponseResult query(DeviceManagement deviceManagement, Integer pageNo, Integer pageSize){ // String userId = JwtUtil.getUserId(); // List productIdList = iDeviceGroupService.getByUserPerms(userId) // .stream().map(DeviceGroupExt::getGroupId).collect(Collectors.toList()); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StrUtil.isNotEmpty(deviceManagement.getProductionId()),"production_id",deviceManagement.getProductionId()); queryWrapper.like(StrUtil.isNotEmpty(deviceManagement.getDeviceManagementName()),"device_management_name",deviceManagement.getDeviceManagementName()); queryWrapper.like(StrUtil.isNotEmpty(deviceManagement.getDeviceManagementCode()),"device_management_code",deviceManagement.getDeviceManagementCode()); // queryWrapper.in("production_id",productIdList); queryWrapper.orderByDesc("create_time"); Page page = new Page<>(pageNo, pageSize); IPage pageList = super.page(page, queryWrapper); return new QueryPageResponseResult<>(CommonCode.SUCCESS, pageList); } /** * 通过车间id和设备类名称查询设备类信息 * @param productionId * @parame deviceManagementName * @return */ public List getDeviceManagementList(String productionId,String deviceManagementName,String deviceManagementCode){ return super.list(new QueryWrapper() .eq(StrUtil.isNotEmpty(productionId),"production_id", productionId) .eq(StrUtil.isNotEmpty(deviceManagementName),"device_management_name", deviceManagementName) .eq(StrUtil.isNotEmpty(deviceManagementCode),"device_management_code", deviceManagementCode)); } /** * 获取用户权限拥有的车间信息 * @param userId * @return */ @Override public List getProductionIdsByUserId(String userId){ //权限信息,改为结构树权限 return iDeviceGroupService.getByUserPerms(userId); } /** * 通过车间id查询设备列表信息 * @param productionId * @return */ @Override public IPage getEquipmentListByProductionId(String productionId, String equipmentId, String equipmentName, Integer pageNo, Integer pageSize){ //查询子节点 List productionIds = iDeviceGroupService.findListParentTreeAll(productionId); if (productionIds == null) { return null; } if (!productionIds.isEmpty()) { QueryWrapper queryWrapper = new QueryWrapper() .in("group_id", productionIds) .like(StrUtil.isNotEmpty(equipmentName),"device_name", equipmentName) .eq(StrUtil.isNotEmpty(equipmentId),"device_id", equipmentId); queryWrapper.orderByDesc("create_time"); Page page = new Page<>(pageNo, pageSize); return iDeviceInfoService.page(page, queryWrapper); } return null; } /** * 通过车间id查询设备类管理列表信息 * @param productionId * @return */ @Override public List getDeviceManagementListByProductionId(String productionId){ QueryWrapper queryWrapper = new QueryWrapper() .eq(StrUtil.isNotEmpty(productionId),"production_id", productionId); queryWrapper.orderByDesc("create_time"); return super.list(queryWrapper); } /** * 通过id查询设备列表信息 * @param id * @return */ @Override public List getEquipmentListById(String id){ DeviceManagement deviceManagement = super.getById(id); if (deviceManagement == null) { return null; }else { List equipmentIds = Arrays.asList(deviceManagement.getEquipmentIds().split(",")); return iDeviceInfoService.list(new QueryWrapper() .in("device_id", equipmentIds)); } } /** * 查询最新一条记录 * @param workshopNo * @param equipmentId * @return */ @Override public DeviceManagement findEquipmentIdsFromEqId(String workshopNo,String equipmentId){ if(StringUtils.isEmpty(workshopNo) || StringUtils.isEmpty(equipmentId)) { return null; } List list = super.lambdaQuery(). eq(DeviceManagement::getProductionId, workshopNo). like(DeviceManagement::getEquipmentIds,equipmentId).list(); if(list == null || list.isEmpty()) { return null; } return list.get(0); } }