/* * Copyright (c) 2018-2025, ztt All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the pig4cloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: ztt */ package com.chinaztt.mes.basic.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.basic.dto.WorkstationDTO; import com.chinaztt.mes.basic.entity.*; import com.chinaztt.mes.basic.excel.WorkstationData; import com.chinaztt.mes.basic.mapper.*; import com.chinaztt.mes.basic.service.WorkstationService; import com.chinaztt.mes.basic.util.DictUtils; import com.chinaztt.mes.equipment.mapper.EquipmentMapper; import com.chinaztt.ztt.admin.api.entity.SysDictItem; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; /** * 工作站 * * @author sunxiaoling * @date 2020-09-11 09:55:36 */ @Service @AllArgsConstructor @Transactional(rollbackFor = Exception.class) public class WorkstationServiceImpl extends ServiceImpl implements WorkstationService { private WorkstationEquipmentMapper workstationEquipmentMapper; private WorkstationLocationMapper workstationLocationMapper; private EquipmentMapper equipmentMapper; private LocationMapper locationMapper; private JoinWorkstationTemplateMapper joinWorkstationTemplateMapper; private DictUtils dictUtils; private FactoryMapper factoryMapper; /** * 分页查询用户信息(含有角色信息) * * @return */ @Override public void deleteAllWorkstation(Long id) { // 1.删除选中的工作站 baseMapper.deleteById(id); // 2.删除对应的工作站设备关联表 workstationEquipmentMapper.delete(Wrappers.query().lambda().eq(WorkstationEquipment::getWorkstationId, id)); // 3.删除对应的库位设备关联表 workstationLocationMapper.delete(Wrappers.query().lambda().eq(WorkstationLocation::getWorkstationId, id)); } /** * @return */ @Override public IPage> getNoWorkstation(Page page, QueryWrapper workstation) { return baseMapper.getNoWorkstation(page, workstation); } /** * Description: 删除 * * @param fid * @param tid * @author: sxl * @date: 2020/8/26 14:06 */ @Override public void delWorkstationEquipment(Long fid, Long tid) { workstationEquipmentMapper.delWorkstationEquipment(fid, tid); } @Override @Transactional(rollbackFor = Exception.class) public boolean fullSave(WorkstationDTO workstation) { try { // 1.将工作站的数据插入到主表中 baseMapper.insert(workstation); // 2.判断关联表的数据是否为空 if (CollectionUtil.isNotEmpty(workstation.getWorkstationEquipment())) { workstation.getWorkstationEquipment().forEach(a -> { a.setWorkstationId(workstation.getId()); workstationEquipmentMapper.insert(a); }); } // 3.判断库位关联表的数据是否为空 if (CollectionUtil.isNotEmpty(workstation.getWorkstationLocation())) { workstation.getWorkstationLocation().forEach(a -> { a.setWorkstationId(workstation.getId()); workstationLocationMapper.insert(a); }); } // 4.判断人工模板关联表的数据是否为空 if (CollectionUtil.isNotEmpty(workstation.getWorkstationTemplate())) { // 要判断人工模板里面的人工类型绑定的零件族是否存在重复零件 List handymanTemplateIdList = workstation.getWorkstationTemplate().stream().filter(a -> a.getHandymanTemplateId() != null).map(JoinWorkstationTemplate::getHandymanTemplateId).collect(Collectors.toList()); List partFamilyNameList = joinWorkstationTemplateMapper.checkOnePartFamily(handymanTemplateIdList); if (CollectionUtil.isNotEmpty(partFamilyNameList)) { throw new RuntimeException("人工类型的零件族重复:" + partFamilyNameList.stream().collect(Collectors.joining(","))); } workstation.getWorkstationTemplate().forEach(a -> { a.setWorkstationId(workstation.getId()); joinWorkstationTemplateMapper.insert(a); }); } } catch (RuntimeException e) { throw new RuntimeException(e.getMessage()); } catch (Exception e) { log.error("创建工作站失败", e); throw new RuntimeException("创建工作站失败"); } return true; } @Override public boolean fullUpdate(WorkstationDTO workstation) { try { //1.更新主表 baseMapper.updateById(workstation); // 2.1 删除所有关系表 workstationEquipmentMapper.delete(Wrappers.lambdaQuery().eq(WorkstationEquipment::getWorkstationId, workstation.getId())); // 2.2 新增关系表 if (CollectionUtil.isNotEmpty(workstation.getWorkstationEquipment())) { workstation.getWorkstationEquipment().forEach(a -> { workstationEquipmentMapper.insert(a); }); } // 3. 删增-库位关系 workstationLocationMapper.delete(Wrappers.lambdaQuery().eq(WorkstationLocation::getWorkstationId, workstation.getId())); if (CollectionUtil.isNotEmpty(workstation.getWorkstationLocation())) { workstation.getWorkstationLocation().forEach(a -> { workstationLocationMapper.insert(a); }); } joinWorkstationTemplateMapper.delete(Wrappers.lambdaQuery().eq(JoinWorkstationTemplate::getWorkstationId, workstation.getId())); if (CollectionUtil.isNotEmpty(workstation.getWorkstationTemplate())) { // 要判断人工模板里面的人工类型绑定的零件族是否存在重复零件 List handymanTemplateIdList = workstation.getWorkstationTemplate().stream().filter(a -> a.getHandymanTemplateId() != null).map(JoinWorkstationTemplate::getHandymanTemplateId).collect(Collectors.toList()); List partFamilyNameList = joinWorkstationTemplateMapper.checkOnePartFamily(handymanTemplateIdList); if (CollectionUtil.isNotEmpty(partFamilyNameList)) { throw new RuntimeException("人工类型的零件族重复:" + partFamilyNameList.stream().collect(Collectors.joining(","))); } workstation.getWorkstationTemplate().forEach(a -> { joinWorkstationTemplateMapper.insert(a); }); } } catch (RuntimeException e) { throw new RuntimeException(e.getMessage()); } catch (Exception e) { log.error("更新工作站失败id:" + workstation.getId(), e); throw new RuntimeException("更新工作站失败"); } return true; } @Override public WorkstationDTO getWorkstationById(Long id) { WorkstationDTO workstationDTO = new WorkstationDTO(); //当不为空时获取关联设备,为空时只获取未关联设备 if (id != 0L) { // 1.获取工作站 workstationDTO = baseMapper.selectDtoById(id); // 2.1获取关联的设备 List workstationEquipments = workstationEquipmentMapper.selectList(Wrappers.lambdaQuery().eq(WorkstationEquipment::getWorkstationId, workstationDTO.getId())); if (CollectionUtil.isNotEmpty(workstationEquipments)) { workstationDTO.setEquipmentIds(workstationEquipments.stream().map(e -> e.getEquipmentId()).collect(Collectors.toList())); } //线边仓和机台已投料库位不能相同 // 2.2获取关联的库位 库位类型(1:投料库位 2.产出待检库位 3.产出不合格库位 4.产出合格库位 5.机台已投料库位 6.检测待处理库位) List workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 1)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setFeedLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 2)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setInspectionLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 4)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setQualifiedLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 3)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setDisqualifiedLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 5)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setFedLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 6)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setInspToDealLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } workstationLocations = workstationLocationMapper.selectList(Wrappers.lambdaQuery() .eq(WorkstationLocation::getWorkstationId, workstationDTO.getId()) .eq(WorkstationLocation::getLocationType, 9)); if (CollectionUtil.isNotEmpty(workstationLocations)) { workstationDTO.setProductInspectionLocationIds(workstationLocations.stream().map(e -> e.getLocationId()).collect(Collectors.toList())); } //2.3获取关联的人工类型模板 List joinWorkstationTemplates = joinWorkstationTemplateMapper.selectList(Wrappers.lambdaQuery().eq(JoinWorkstationTemplate::getWorkstationId, workstationDTO.getId())); if (CollectionUtil.isNotEmpty(joinWorkstationTemplates)) { workstationDTO.setHandymanTypeIds(joinWorkstationTemplates.stream().map(e -> e.getHandymanTemplateId()).collect(Collectors.toList())); } } // 3.1获取未关联的设备 workstationDTO.setEquipments(equipmentMapper.getNoWorkstationEquipment(id)); // 3.2获取未关联的库位 workstationDTO.setLocations(locationMapper.getNoWorkstationLocation(id)); return workstationDTO; } @Override public WorkstationDTO getByFactoryId(Workstation workstation) { WorkstationDTO workstationDTO = new WorkstationDTO(); workstationDTO.setEquipments(equipmentMapper.getEquipmentByFactoryId(workstation.getFactoryId())); return workstationDTO; } @Override public IPage> pdaPage(Page page, QueryWrapper gen) { return baseMapper.pdaPage(page, gen); } @Override public void importWorkstationExcel(List list) { if (CollectionUtil.isEmpty(list)) { return; } //获取字典的数组值 List dict = dictUtils.getDict("workstation_type"); List factoryList = factoryMapper.selectList(null); for (WorkstationData data : list) { Workstation workstation = new Workstation(); dict.forEach(a -> { if (a.getLabel().equals(data.getType())) { workstation.setType(a.getValue()); } }); factoryList.forEach(b -> { if (b.getFactoryName().equals(data.getFactoryName())) { workstation.setFactoryId(b.getId()); } }); if (workstation.getType() == null) { log.error("工作站类型:" + data.getType() + "不存在"); continue; } if (workstation.getFactoryId() == null) { log.error("工厂:" + data.getFactoryName() + "不存在"); continue; } workstation.setWorkstationNo(data.getWorkstationNo()); workstation.setName(data.getName()); baseMapper.insert(workstation); } } }