| | |
| | | package com.ruoyi.production.controller; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.ruoyi.framework.web.domain.R; |
| | | import com.ruoyi.production.dto.ProcessRouteItemDto; |
| | | import com.ruoyi.production.dto.ProductProcessRouteItemDto; |
| | | import com.ruoyi.production.pojo.ProcessRouteItem; |
| | | import com.ruoyi.production.pojo.ProductProcessRouteItem; |
| | | import com.ruoyi.production.mapper.*; |
| | | import com.ruoyi.production.pojo.*; |
| | | import com.ruoyi.production.service.ProductProcessRouteItemService; |
| | | import com.ruoyi.production.service.ProductWorkOrderService; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | | import com.ruoyi.quality.pojo.QualityInspect; |
| | | import com.ruoyi.sales.mapper.SalesLedgerMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | @RequestMapping("productProcessRoute") |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @RequestMapping("/productProcessRoute") |
| | | @RestController |
| | | @AllArgsConstructor |
| | | @Api(tags = "生产工艺路线") |
| | | public class ProductProcessRouteItemController { |
| | | @Autowired |
| | | |
| | | 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; |
| | | |
| | | @GetMapping("list") |
| | | @ApiOperation("根据Id查询工艺项目") |
| | |
| | | return R.ok(productProcessRouteItemService.listItem(orderId)); |
| | | } |
| | | |
| | | @PostMapping () |
| | | @ApiOperation("新增修改") |
| | | @PostMapping("/updateRouteItem") |
| | | @ApiOperation("批量新增修改") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R addOrUpdate(@RequestBody ProductProcessRouteItemDto processRouteItemDto) { |
| | | productProcessRouteItemService.remove(new QueryWrapper<ProductProcessRouteItem>().lambda().eq(ProductProcessRouteItem::getRouteId, processRouteItemDto.getRouteId())); |
| | | return R.ok(productProcessRouteItemService.saveBatch(processRouteItemDto.getProcessRouteItem())); |
| | | |
| | | 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); |
| | | } |
| | | |
| | | @DeleteMapping("/deleteRouteItem") |
| | | @ApiOperation("删除生产工艺路线") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R deleteRouteItem(@RequestBody ProductProcessRouteItemDto processRouteItemDto) { |
| | | |
| | | 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()); |
| | | } |
| | | } |
| | | } |