liding
3 天以前 3b71cac369fb246ceafa59ffa3b775c2afc8add4
src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
@@ -6,36 +6,39 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.production.controller.ProductWorkOrderController;
import com.ruoyi.production.dto.ProcessRouteItemDto;
import com.ruoyi.production.dto.ProductOrderDto;
import com.ruoyi.production.dto.ProductionProductMainDto;
import com.ruoyi.production.mapper.*;
import com.ruoyi.production.pojo.*;
import com.ruoyi.production.pojo.ProductProcessRouteItem;
import com.ruoyi.production.pojo.ProductionProductMain;
import com.ruoyi.production.pojo.ProductionProductOutput;
import com.ruoyi.production.service.ProductionProductMainService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.production.mapper.ProductionProductMainMapper;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
@Service
@AllArgsConstructor
public class ProductionProductMainServiceImpl extends ServiceImpl<ProductionProductMainMapper, ProductionProductMain> implements ProductionProductMainService {
    @Autowired
    private ProductionProductMainMapper productionProductMainMapper;
    @Autowired
    private ProductWorkOrderController productWorkOrderController;
    @Autowired
    private ProductWorkOrderMapper productWorkOrderMapper;
    @Autowired
    private ProductProcessRouteItemMapper productProcessRouteItemMapper;
    @Autowired
    private ProductionProductOutputMapper productionProductOutputMapper;
    @Autowired
    private ProcessRouteItemMapper processRouteItemMapper;
    @Autowired
    private ProductModelMapper productModelMapper;
@@ -45,43 +48,86 @@
    }
    @Override
    public Boolean addProductMain(ProductionProductMainDto productionProductMainDto) {
    @Transactional(rollbackFor = Exception.class)
    public Boolean addProductMain(ProductionProductMainDto dto) {
        if (dto == null) {
            throw new RuntimeException("参数不能为空");
        }
        // 判断是新增还是更新
        if (dto.getId() != null) {
            // 更新逻辑 - 只更新数量
            QueryWrapper<ProductionProductOutput> outputWrapper = new QueryWrapper<>();
            outputWrapper.eq("product_main_id", dto.getId());
            ProductionProductOutput output = productionProductOutputMapper.selectOne(outputWrapper);
            if (output == null) {
                throw new RuntimeException("产出记录不存在");
            }
            // 只更新数量
            if (dto.getQuantity() != null) {
                output.setQuantity(dto.getQuantity());
                productionProductOutputMapper.updateById(output);
            }
            return true;
        }
        // 新增逻辑
        ProductionProductMain productionProductMain = new ProductionProductMain();
        ProductProcessRouteItem productProcessRouteItem = productProcessRouteItemMapper.selectById(productionProductMainDto.getProductProcessRouteItemId());
        ProductProcessRouteItem productProcessRouteItem = productProcessRouteItemMapper.selectById(dto.getProductProcessRouteItemId());
        if (productProcessRouteItem == null) {
            throw new RuntimeException("工艺路线项不存在");
        }
        String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 查询今日已存在的最大工单号
        QueryWrapper<ProductionProductMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("work_order_no", datePrefix)
                .orderByDesc("work_order_no")
                .last("LIMIT 1");
        queryWrapper.select("MAX(product_no) as maxNo")
                .likeRight("product_no", datePrefix);
        ProductionProductMain lastWorkOrder = productionProductMainMapper.selectOne(queryWrapper);
        // 修正:安全处理可能为空的查询结果
        List<Map<String, Object>> resultList = productionProductMainMapper.selectMaps(queryWrapper);
        int sequenceNumber = 1; // 默认序号
        if (lastWorkOrder != null && lastWorkOrder.getProductNo() != null) {
            String lastNo = lastWorkOrder.getProductNo().toString();
            if (lastNo.startsWith(datePrefix)) {
                String seqStr = lastNo.substring(datePrefix.length());
                try {
                    sequenceNumber = Integer.parseInt(seqStr) + 1;
                } catch (NumberFormatException e) {
                    sequenceNumber = 1;
        int sequenceNumber = 1;
        if (resultList != null && !resultList.isEmpty()) {
            Map<String, Object> result = resultList.get(0);
            if (result != null) {
                Object maxNoObj = result.get("maxNo");
                if (maxNoObj != null) {
                    String lastNo = maxNoObj.toString();
                    System.out.println("lastNo: " + lastNo);
                    if (lastNo.startsWith(datePrefix)) {
                        try {
                            String seqStr = lastNo.substring(datePrefix.length());
                            sequenceNumber = Integer.parseInt(seqStr) + 1;
                        } catch (NumberFormatException e) {
                            sequenceNumber = 1;
                        }
                    }
                }
            }
        }
        String workOrderNoStr = String.format("%s%03d", datePrefix, sequenceNumber);
        productionProductMain.setProductNo(workOrderNoStr);
        productionProductMain.setUserId(productionProductMainDto.getUserId());
        productionProductMain.setProductProcessRouteItemId(productionProductMainDto.getProductProcessRouteItemId());
        String productNo = String.format("%s%03d", datePrefix, sequenceNumber);
        productionProductMain.setProductNo(productNo);
        productionProductMain.setUserId(dto.getUserId());
        productionProductMain.setProductProcessRouteItemId(dto.getProductProcessRouteItemId());
        productionProductMain.setWorkOrderId(dto.getWorkOrderId());
        productionProductMain.setStatus(0);
        //添加报工主表
        // 添加报工主表
        productionProductMainMapper.insert(productionProductMain);
        // 添加产出
        ProductionProductOutput productionProductOutput = new ProductionProductOutput();
        productionProductOutput.setProductMainId(productionProductMain.getId());
        productionProductOutput.setProductModelId(productProcessRouteItem.getProductModelId());
        productionProductOutput.setQuantity(productionProductMainDto.getQuantity());
        //添加产出
        productionProductOutput.setQuantity(dto.getQuantity() != null ? dto.getQuantity() : BigDecimal.ZERO);
        productionProductOutputMapper.insert(productionProductOutput);
        return true;
    }
}