| | |
| | | package com.ruoyi.production.service.impl; |
| | | |
| | | 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.framework.web.domain.R; |
| | | import com.ruoyi.production.dto.ProductProcessRouteItemDto; |
| | | import com.ruoyi.production.mapper.ProductProcessRouteItemMapper; |
| | | 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.quality.mapper.QualityInspectMapper; |
| | | import com.ruoyi.quality.pojo.QualityInspect; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.poi.ss.formula.functions.T; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @AllArgsConstructor |
| | | public class ProductProcessRouteItemServiceImpl extends ServiceImpl<ProductProcessRouteItemMapper, ProductProcessRouteItem> implements ProductProcessRouteItemService { |
| | | |
| | | @Autowired |
| | | |
| | | private ProductProcessRouteItemMapper productProcessRouteItemMapper; |
| | | |
| | | private ProductionProductMainMapper productionProductMainMapper; |
| | | |
| | | private ProductionProductInputMapper productionProductInputMapper; |
| | | |
| | | private ProductionProductOutputMapper productionProductOutputMapper; |
| | | |
| | | private QualityInspectMapper qualityInspectMapper; |
| | | |
| | | private SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper; |
| | | |
| | | private ProductWorkOrderMapper productWorkOrderMapper; |
| | | |
| | | private ProductOrderMapper productOrderMapper; |
| | | |
| | | private ProductProcessRouteMapper productProcessRouteMapper; |
| | | |
| | | private SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | |
| | | @Override |
| | | public List<ProductProcessRouteItemDto> listItem(Long orderId) { |
| | | return productProcessRouteItemMapper.listItem(orderId); |
| | | } |
| | | |
| | | @Override |
| | | public R deleteRouteItem(Long id) { |
| | | Long routeItemId = id; |
| | | try { |
| | | // 查询工单 |
| | | ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectOne( |
| | | new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .eq(ProductWorkOrder::getProductProcessRouteItemId, routeItemId) |
| | | .last("LIMIT 1") |
| | | ); |
| | | if (productWorkOrder == null) { |
| | | throw new RuntimeException("删除失败:未找到关联的生产工单"); |
| | | } |
| | | 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)); |
| | | |
| | | // 删除主表数据 |
| | | ProductProcessRouteItem deleteProductProcessRouteItem = productProcessRouteItemMapper.selectById(routeItemId); |
| | | Long productRouteId = deleteProductProcessRouteItem.getProductRouteId(); |
| | | // 删除指定数据 |
| | | productProcessRouteItemMapper.deleteById(id); |
| | | // 查询该工艺路线的所有工序并按照顺序排序 |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery() |
| | | .eq(ProductProcessRouteItem::getProductRouteId, productRouteId) |
| | | .orderByAsc(ProductProcessRouteItem::getDragSort)); |
| | | // 重新设置排序值,使序号连续 |
| | | for (int i = 0; i < productProcessRouteItems.size(); i++) { |
| | | ProductProcessRouteItem item = productProcessRouteItems.get(i); |
| | | if (!item.getDragSort().equals(i + 1)) { |
| | | item.setDragSort(i + 1); |
| | | productProcessRouteItemMapper.updateById(item); |
| | | } |
| | | } |
| | | return R.ok(); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("删除生产工艺路线失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public R addRouteItem(ProductProcessRouteItem productProcessRouteItem) { |
| | | ProductOrder productOrder = productOrderMapper.selectById(productProcessRouteItem.getProductOrderId()); |
| | | int insert = productProcessRouteItemMapper.insert(productProcessRouteItem); |
| | | // 生成当前日期的前缀:年月日 |
| | | String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
| | | if (insert > 0) { |
| | | // 查询今日已存在的最大工单号 |
| | | QueryWrapper<ProductWorkOrder> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.likeRight("work_order_no", datePrefix) |
| | | .orderByDesc("work_order_no") |
| | | .last("LIMIT 1"); |
| | | ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectOne(queryWrapper); |
| | | int sequenceNumber = 1; // 默认序号 |
| | | if (lastWorkOrder != null && lastWorkOrder.getWorkOrderNo() != null) { |
| | | String lastNo = lastWorkOrder.getWorkOrderNo().toString(); |
| | | if (lastNo.startsWith(datePrefix)) { |
| | | String seqStr = lastNo.substring(datePrefix.length()); |
| | | try { |
| | | sequenceNumber = Integer.parseInt(seqStr) + 1; |
| | | } catch (NumberFormatException e) { |
| | | sequenceNumber = 1; |
| | | } |
| | | } |
| | | } |
| | | // 生成完整的工单号 |
| | | String workOrderNoStr = String.format("%s%03d", datePrefix, sequenceNumber); |
| | | ProductWorkOrder productWorkOrder = new ProductWorkOrder(); |
| | | productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId()); |
| | | productWorkOrder.setProductOrderId(productProcessRouteItem.getProductOrderId()); |
| | | productWorkOrder.setPlanQuantity(productOrder.getQuantity()); |
| | | productWorkOrder.setWorkOrderNo(workOrderNoStr); |
| | | productWorkOrder.setStatus(1); |
| | | productWorkOrderMapper.insert(productWorkOrder); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public int sortRouteItem(ProductProcessRouteItem productProcessRouteItem) { |
| | | //查询被改动的这条数据 |
| | | ProductProcessRouteItem oldProductProcessRouteItem = productProcessRouteItemMapper.selectById(productProcessRouteItem.getId()); |
| | | //查询该工艺路线的所有工序并按照顺序排序 |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery() |
| | | .eq(ProductProcessRouteItem::getProductRouteId, oldProductProcessRouteItem.getProductRouteId()) |
| | | .orderByAsc(ProductProcessRouteItem::getDragSort)); |
| | | // 获取目标位置(移动到第几个之后) |
| | | Integer targetPosition = productProcessRouteItem.getDragSort(); |
| | | if (targetPosition != null && targetPosition >= 0) { |
| | | // 移动元素到新的位置 |
| | | productProcessRouteItems.remove(oldProductProcessRouteItem); |
| | | productProcessRouteItems.add(targetPosition-1, oldProductProcessRouteItem); |
| | | // 更新所有受影响的排序字段 |
| | | for (int i = 0; i < productProcessRouteItems.size(); i++) { |
| | | ProductProcessRouteItem item = productProcessRouteItems.get(i); |
| | | if (!item.getId().equals(oldProductProcessRouteItem.getId())) { |
| | | // 检查是否需要更新排序值 |
| | | if (item.getDragSort() != i+1) { |
| | | item.setDragSort(i+1); |
| | | productProcessRouteItemMapper.updateById(item); |
| | | } |
| | | } else { |
| | | // 更新原记录的新排序位置 |
| | | oldProductProcessRouteItem.setDragSort(targetPosition); |
| | | productProcessRouteItemMapper.updateById(oldProductProcessRouteItem); |
| | | } |
| | | } |
| | | return 1; |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | } |