| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | 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.ruoyi.common.enums.StockQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockUnQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.procurementrecord.utils.StockUtils; |
| | | import com.ruoyi.production.dto.ProductBomDto; |
| | | import com.ruoyi.production.dto.ProductOrderDto; |
| | | import com.ruoyi.production.dto.ProductStructureDto; |
| | | import com.ruoyi.production.mapper.*; |
| | | import com.ruoyi.production.pojo.*; |
| | | import com.ruoyi.production.service.ProcessRouteService; |
| | | import com.ruoyi.production.service.ProductOrderService; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | | import com.ruoyi.quality.pojo.QualityInspect; |
| | | import com.ruoyi.stock.mapper.StockInventoryMapper; |
| | | import com.ruoyi.stock.pojo.StockInventory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | |
| | | @Autowired |
| | | private StockUtils stockUtils; |
| | | |
| | | @Autowired |
| | | private StockInventoryMapper stockInventoryMapper; |
| | | |
| | | @Override |
| | | public IPage<ProductOrderDto> pageProductOrder(Page page, ProductOrderDto productOrder) { |
| | |
| | | String workOrderNoStr = "GD" + String.format("%s%03d", datePrefix, sequenceNumber); |
| | | ProductWorkOrder productWorkOrder = new ProductWorkOrder(); |
| | | productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId()); |
| | | productWorkOrder.setPriority(productOrder.getPriority()); |
| | | productWorkOrder.setBatchNo(productOrder.getBatchNo()); |
| | | productWorkOrder.setProductOrderId(productOrder.getId()); |
| | | ProductOrder order = productOrderMapper.selectById(productOrder.getId()); |
| | | productWorkOrder.setPlanQuantity(order.getQuantity()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Boolean addProductOrder(ProductOrder productOrder) { |
| | | public Boolean addProductOrder(ProductOrderDto productOrderDto) { |
| | | String string = generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))); |
| | | ProductOrder productOrder = new ProductOrder(); |
| | | BeanUtils.copyProperties(productOrderDto, productOrder); |
| | | productOrder.setNpsNo(string); |
| | | productOrder.setCompleteQuantity(BigDecimal.ZERO); |
| | | List<StockInventory> stockInventoryList = stockInventoryMapper.selectList(null); |
| | | if (productOrder.getBatchNo() == null || productOrder.getBatchNo().isEmpty()) { |
| | | String batchNo; |
| | | // 获取当前月份(两位) |
| | | LocalDate now = LocalDate.now(); |
| | | String monthFlag = now.format(DateTimeFormatter.ofPattern("MM")); |
| | | |
| | | // 获取当前月份的最大流水号 |
| | | int maxSeq = getCurrentMonthMaxSeq(productOrderDto.getMaterialCode(), |
| | | productOrderDto.getProductModelName(), monthFlag, stockInventoryList); |
| | | |
| | | // 新流水号 = 最大流水号 + 1 |
| | | int newSeq = maxSeq + 1; |
| | | String seqStr = String.format("%03d", newSeq); |
| | | |
| | | // 组装batchNo |
| | | batchNo = productOrderDto.getMaterialCode() + productOrderDto.getProductModelName() + "P" + monthFlag + seqStr; |
| | | productOrder.setBatchNo(batchNo); |
| | | } else { |
| | | productOrder.setBatchNo(productOrder.getBatchNo()); |
| | | } |
| | | this.save(productOrder); |
| | | if (ObjectUtils.isNotEmpty(productOrder.getRouteId())) { |
| | | this.bindingRoute(productOrder); |
| | |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean delete(Long[] ids) { |
| | | |
| | | //批量查询productOrder |
| | | List<ProductOrder> productOrders = productOrderMapper.selectList( |
| | | new LambdaQueryWrapper<ProductOrder>() |
| | | .in(ProductOrder::getId, ids) |
| | | ); |
| | | if (!org.springframework.util.CollectionUtils.isEmpty(productOrders)) { |
| | | |
| | | |
| | | // 批量查询processRouteItems |
| | | List<ProductProcessRouteItem> allRouteItems = productProcessRouteItemMapper.selectList( |
| | | new LambdaQueryWrapper<ProductProcessRouteItem>() |
| | | .in(ProductProcessRouteItem::getProductOrderId, ids) |
| | | ); |
| | | |
| | | if (!com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(allRouteItems)) { |
| | | // 获取要删除的工序项ID |
| | | List<Long> routeItemIds = allRouteItems.stream() |
| | | .map(ProductProcessRouteItem::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询关联的工单ID |
| | | List<ProductWorkOrder> workOrders = productWorkOrderMapper.selectList( |
| | | new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds) |
| | | ); |
| | | if (!com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(workOrders)) { |
| | | List<Long> workOrderIds = workOrders.stream() |
| | | .map(ProductWorkOrder::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询关联的生产主表ID |
| | | List<ProductionProductMain> productMains = productionProductMainMapper.selectList( |
| | | new LambdaQueryWrapper<ProductionProductMain>() |
| | | .in(ProductionProductMain::getWorkOrderId, workOrderIds) |
| | | ); |
| | | List<Long> productMainIds = productMains.stream() |
| | | .map(ProductionProductMain::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 删除产出表、投入表数据 |
| | | if (!com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(productMainIds)) { |
| | | productionProductOutputMapper.deleteByProductMainIds(productMainIds); |
| | | productionProductInputMapper.deleteByProductMainIds(productMainIds); |
| | | List<QualityInspect> qualityInspects = qualityInspectMapper.selectList( |
| | | new LambdaQueryWrapper<QualityInspect>() |
| | | .in(QualityInspect::getProductMainId, productMainIds) |
| | | ); |
| | | //删除出库记录 |
| | | for (Long productMainId : productMainIds) { |
| | | //删除生产出库记录 |
| | | stockUtils.deleteStockOutRecord(productMainId, StockQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode()); |
| | | //删除报废的入库记录 |
| | | stockUtils.deleteStockInRecord(productMainId, StockUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode()); |
| | | } |
| | | qualityInspects.forEach(qualityInspect -> { |
| | | //inspectState=1 已提交 不能删除 |
| | | if (qualityInspect.getInspectState() == 1) { |
| | | throw new RuntimeException("已提交的检验单不能删除"); |
| | | } |
| | | }); |
| | | qualityInspectMapper.deleteByProductMainIds(productMainIds); |
| | | salesLedgerProductionAccountingMapper.delete(new LambdaQueryWrapper<SalesLedgerProductionAccounting>() |
| | | .in(SalesLedgerProductionAccounting::getProductMainId, productMainIds)); |
| | | } |
| | | |
| | | // 删除生产主表数据 |
| | | productionProductMainMapper.deleteByWorkOrderIds(workOrderIds); |
| | | |
| | | // 删除工单数据 |
| | | productWorkOrderMapper.delete(new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds)); |
| | | /** |
| | | * 查询当前月份已存在的最大流水号 |
| | | */ |
| | | private static int getCurrentMonthMaxSeq(String materialCode, String model, String monthFlag, List<StockInventory> existingList) { |
| | | int maxSeq = 0; |
| | | String prefix = materialCode + model + "P" + monthFlag; |
| | | // 正则匹配:前缀 + 3位数字 |
| | | Pattern pattern = Pattern.compile(Pattern.quote(prefix) + "(\\d{3})"); |
| | | for (StockInventory item : existingList) { |
| | | String batchNo = item.getBatchNo(); |
| | | if (batchNo == null) continue; |
| | | Matcher matcher = pattern.matcher(batchNo); |
| | | if (matcher.find()) { |
| | | int seq = Integer.parseInt(matcher.group(1)); |
| | | if (seq > maxSeq) { |
| | | maxSeq = seq; |
| | | } |
| | | } |
| | | // 批量删除processRouteItem |
| | | productProcessRouteItemMapper.delete(new LambdaQueryWrapper<ProductProcessRouteItem>() |
| | | .in(ProductProcessRouteItem::getProductOrderId, ids)); |
| | | |
| | | // 批量删除productProcessRoute |
| | | productProcessRouteMapper.delete(new LambdaQueryWrapper<ProductProcessRoute>() |
| | | .in(ProductProcessRoute::getProductOrderId, ids)); |
| | | |
| | | // 批量删除productOrder |
| | | productOrderMapper.delete(new LambdaQueryWrapper<ProductOrder>() |
| | | .in(ProductOrder::getId, ids)); |
| | | } |
| | | return maxSeq; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean delete(Long[] ids) { |
| | | //如果已经开始生产,不能删除 |
| | | //查询生产订单下的工单 |
| | | List<ProductWorkOrder> productWorkOrders = productWorkOrderMapper.selectList(Wrappers.<ProductWorkOrder>lambdaQuery().in(ProductWorkOrder::getProductOrderId, ids)); |
| | | if (productWorkOrders.size() > 0) { |
| | | //判断是否有报工数据 |
| | | List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList(Wrappers.<ProductionProductMain>lambdaQuery() |
| | | .in(ProductionProductMain::getWorkOrderId, productWorkOrders.stream().map(ProductWorkOrder::getId).collect(Collectors.toList()))); |
| | | if (productionProductMains.size() > 0) { |
| | | throw new RuntimeException("生产订单已经开始生产,不能删除"); |
| | | } |
| | | //删除工单 |
| | | productWorkOrderMapper.delete(Wrappers.<ProductWorkOrder>lambdaQuery().in(ProductWorkOrder::getProductOrderId, ids)); |
| | | } |
| | | //删除工艺路线 |
| | | productProcessRouteItemMapper.delete(new LambdaQueryWrapper<ProductProcessRouteItem>() |
| | | .in(ProductProcessRouteItem::getProductOrderId, ids)); |
| | | productProcessRouteMapper.delete(new LambdaQueryWrapper<ProductProcessRoute>() |
| | | .in(ProductProcessRoute::getProductOrderId, ids)); |
| | | //删除生产订单 |
| | | productOrderMapper.delete(new LambdaQueryWrapper<ProductOrder>() |
| | | .in(ProductOrder::getId, ids)); |
| | | return true; |
| | | } |
| | | |