| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.framework.web.domain.R; |
| | | import com.ruoyi.production.dto.ProductProcessRouteItemDto; |
| | | import com.ruoyi.production.mapper.*; |
| | | import com.ruoyi.production.pojo.*; |
| | | import com.ruoyi.production.service.ProductProcessRouteItemService; |
| | | import com.ruoyi.production.service.ProductProcessRouteService; |
| | | import com.ruoyi.production.service.ProductWorkOrderService; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | | import com.ruoyi.quality.pojo.QualityInspect; |
| | |
| | | @Api(tags = "生产工艺路线") |
| | | public class ProductProcessRouteItemController { |
| | | |
| | | private final ProductOrderMapper productOrderMapper; |
| | | |
| | | private ProductProcessRouteItemService productProcessRouteItemService; |
| | | |
| | | private ProductWorkOrderService productWorkOrderService; |
| | | |
| | | private ProductWorkOrderMapper productWorkOrderMapper; |
| | | |
| | | private SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | private ProductionProductMainMapper productionProductMainMapper; |
| | | |
| | | private ProductionProductInputMapper productionProductInputMapper; |
| | | |
| | | private ProductionProductOutputMapper productionProductOutputMapper; |
| | | |
| | | private QualityInspectMapper qualityInspectMapper; |
| | | |
| | | private SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper; |
| | | private ProductProcessRouteService productProcessRouteService; |
| | | |
| | | @GetMapping("list") |
| | | @ApiOperation("根据Id查询工艺项目") |
| | | @ApiOperation("根据Id查询工艺路线子表") |
| | | public R list(Long orderId) { |
| | | return R.ok(productProcessRouteItemService.listItem(orderId)); |
| | | } |
| | | |
| | | @PostMapping("/updateRouteItem") |
| | | @ApiOperation("批量新增修改") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R addOrUpdate(@RequestBody ProductProcessRouteItemDto processRouteItemDto) { |
| | | |
| | | ProductOrder productOrder = productOrderMapper.selectById(processRouteItemDto.getRouteId()); |
| | | if (productOrder == null) { |
| | | return R.fail("未找到ID为[" + processRouteItemDto.getRouteId() + "]的产品订单"); |
| | | } |
| | | SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectOne(new LambdaQueryWrapper<SalesLedgerProduct>() |
| | | .eq(SalesLedgerProduct::getSalesLedgerId, productOrder.getSalesLedgerId())); |
| | | |
| | | if (salesLedgerProduct == null) { |
| | | return R.fail("未找到销售台账ID为[" + productOrder.getSalesLedgerId() + "]的台账产品"); |
| | | } |
| | | |
| | | List<ProductProcessRouteItem> items = processRouteItemDto.getProcessRouteItem(); |
| | | if (CollectionUtils.isEmpty(items)) { |
| | | return R.ok(); |
| | | } |
| | | |
| | | Map<Boolean, List<ProductProcessRouteItem>> partitioned = items.stream() |
| | | .collect(Collectors.partitioningBy( |
| | | item -> item.getId() != null && item.getId() > 0 |
| | | )); |
| | | |
| | | List<ProductProcessRouteItem> toUpdate = partitioned.get(true); |
| | | List<ProductProcessRouteItem> toInsert = partitioned.get(false); |
| | | // 批量处理 |
| | | boolean result = true; |
| | | if (!toInsert.isEmpty()) { |
| | | result = productProcessRouteItemService.saveBatch(toInsert); |
| | | if (result) { |
| | | // 生成工单号 |
| | | String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
| | | // 查询今日最大工单号 |
| | | QueryWrapper<ProductWorkOrder> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.likeRight("work_order_no", datePrefix) |
| | | .select("MAX(work_order_no) as maxNo"); |
| | | |
| | | List<Map<String, Object>> maxNoList = productWorkOrderMapper.selectMaps(queryWrapper); |
| | | String maxWorkOrderNo = null; |
| | | if (!maxNoList.isEmpty() && maxNoList.get(0) != null && maxNoList.get(0).get("maxNo") != null) { |
| | | maxWorkOrderNo = maxNoList.get(0).get("maxNo").toString(); |
| | | } |
| | | int startSequence = 1; |
| | | if (maxWorkOrderNo != null && maxWorkOrderNo.startsWith(datePrefix)) { |
| | | try { |
| | | String seqStr = maxWorkOrderNo.substring(datePrefix.length()); |
| | | startSequence = Integer.parseInt(seqStr) + 1; |
| | | } catch (NumberFormatException e) { |
| | | startSequence = 1; |
| | | } |
| | | } |
| | | // 批量生成工单 |
| | | List<ProductWorkOrder> workOrders = new ArrayList<>(); |
| | | for (int i = 0; i < toInsert.size(); i++) { |
| | | ProductProcessRouteItem item = toInsert.get(i); |
| | | String workOrderNoStr = String.format("%s%03d", datePrefix, startSequence + i); |
| | | ProductWorkOrder workOrder = new ProductWorkOrder(); |
| | | workOrder.setProductProcessRouteItemId(item.getId()); |
| | | workOrder.setProductOrderId(item.getRouteId()); |
| | | workOrder.setWorkOrderNo(workOrderNoStr); |
| | | workOrder.setPlanQuantity(salesLedgerProduct.getQuantity()); |
| | | workOrder.setStatus(1); |
| | | workOrders.add(workOrder); |
| | | } |
| | | result = productWorkOrderService.saveBatch(workOrders); |
| | | } |
| | | } |
| | | if (!toUpdate.isEmpty()) { |
| | | result = productProcessRouteItemService.updateBatchById(toUpdate) && result; |
| | | } |
| | | return R.ok(result); |
| | | @GetMapping("listMain") |
| | | @ApiOperation("根据Id查询工艺路线主表") |
| | | public R listMain(Long orderId) { |
| | | return R.ok(productProcessRouteService.listMain(orderId)); |
| | | } |
| | | |
| | | @DeleteMapping("/deleteRouteItem") |
| | | @PostMapping("/addRouteItem") |
| | | @ApiOperation("新增生产订单的工艺路线详情") |
| | | public R addRouteItem(@RequestBody ProductProcessRouteItem productProcessRouteItem) { |
| | | return productProcessRouteItemService.addRouteItem(productProcessRouteItem); |
| | | } |
| | | |
| | | @PostMapping("/updateRouteItem") |
| | | @ApiOperation("修改生产订单的工艺路线详情") |
| | | public R updateRouteItem(@RequestBody ProductProcessRouteItem productProcessRouteItem) { |
| | | return R.ok(productProcessRouteItemService.updateById(productProcessRouteItem)); |
| | | } |
| | | |
| | | @DeleteMapping("/deleteRouteItem/{id}") |
| | | @ApiOperation("删除生产工艺路线") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R deleteRouteItem(@RequestBody ProductProcessRouteItemDto processRouteItemDto) { |
| | | public R deleteRouteItem(@PathVariable("id") Long id) { |
| | | return productProcessRouteItemService.deleteRouteItem(id); |
| | | } |
| | | |
| | | if (processRouteItemDto == null || processRouteItemDto.getId() == null) { |
| | | return R.fail("删除失败:工艺路线项ID不能为空"); |
| | | } |
| | | Long routeItemId = processRouteItemDto.getId(); |
| | | |
| | | try { |
| | | // 查询工单 |
| | | ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectOne( |
| | | new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .eq(ProductWorkOrder::getProductProcessRouteItemId, routeItemId) |
| | | .last("LIMIT 1") |
| | | ); |
| | | if (productWorkOrder == null) { |
| | | return R.fail("删除失败:未找到关联的生产工单"); |
| | | } |
| | | Long workOrderId = productWorkOrder.getId(); |
| | | Long productOrderId = productWorkOrder.getProductOrderId(); |
| | | |
| | | // 查询生产主表 |
| | | List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList( |
| | | new LambdaQueryWrapper<ProductionProductMain>() |
| | | .eq(ProductionProductMain::getWorkOrderId, workOrderId) |
| | | ); |
| | | if (!productionProductMains.isEmpty()) { |
| | | // 批量删除子表 |
| | | for (ProductionProductMain main : productionProductMains) { |
| | | Long mainId = main.getId(); |
| | | // 删除投入 |
| | | productionProductInputMapper.delete(new LambdaQueryWrapper<ProductionProductInput>() |
| | | .eq(ProductionProductInput::getProductMainId, mainId)); |
| | | // 删除产出 |
| | | productionProductOutputMapper.delete(new LambdaQueryWrapper<ProductionProductOutput>() |
| | | .eq(ProductionProductOutput::getProductMainId, mainId)); |
| | | // 删除质检 |
| | | qualityInspectMapper.delete(new LambdaQueryWrapper<QualityInspect>() |
| | | .eq(QualityInspect::getProductMainId, mainId)); |
| | | } |
| | | } |
| | | |
| | | // 删除报工(生产主表) |
| | | productionProductMainMapper.delete(new LambdaQueryWrapper<ProductionProductMain>() |
| | | .eq(ProductionProductMain::getWorkOrderId, workOrderId)); |
| | | |
| | | // 查询订单 + 删除核算 |
| | | ProductOrder productOrder = productOrderMapper.selectById(productOrderId); |
| | | if (productOrder != null && productOrder.getSalesLedgerId() != null) { |
| | | salesLedgerProductionAccountingMapper.delete(new LambdaQueryWrapper<SalesLedgerProductionAccounting>() |
| | | .eq(SalesLedgerProductionAccounting::getSalesLedgerId, productOrder.getSalesLedgerId())); |
| | | } |
| | | |
| | | // 删除关联工单 |
| | | productWorkOrderMapper.delete(new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .eq(ProductWorkOrder::getProductProcessRouteItemId, routeItemId)); |
| | | |
| | | // 删除主表数据 |
| | | boolean removeFlag = productProcessRouteItemService.removeById(routeItemId); |
| | | if (!removeFlag) { |
| | | return R.fail("删除失败:工艺路线项主表数据不存在"); |
| | | } |
| | | |
| | | return R.ok(); |
| | | } catch (Exception e) { |
| | | return R.fail("删除生产工艺路线失败:" + e.getMessage()); |
| | | } |
| | | @PostMapping ("/sortRouteItem") |
| | | @ApiOperation("排序") |
| | | public R sortRouteItem(@RequestBody ProductProcessRouteItem productProcessRouteItem) { |
| | | return R.ok(productProcessRouteItemService.sortRouteItem(productProcessRouteItem)); |
| | | } |
| | | } |