liu
2 天以前 b490500184a57b740c016bb281765fe29731eab5
yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/sale/ErpSaleOrderServiceImpl.java
@@ -23,10 +23,14 @@
import cn.iocoder.yudao.module.mes.api.mps.dto.MesProMpsCreateReqDTO;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
@@ -73,6 +77,22 @@
    private MdmItemApi mdmItemApi;
    @Resource
    private MesProMpsApi mpsApi;
    @Resource
    private PlatformTransactionManager transactionManager;
    /**
     * MPS 创建使用独立事务(REQUIRES_NEW):
     * 审核时自动创建 MPS 属于尽力而为的联动,失败仅记录日志并回滚自身,
     * 不能污染销售订单审核事务(否则内层异常会把外层事务标记为 rollback-only,导致整个审核被回滚)
     */
    private TransactionTemplate mpsTransactionTemplate;
    @PostConstruct
    public void initMpsTransactionTemplate() {
        mpsTransactionTemplate = new TransactionTemplate(transactionManager);
        mpsTransactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    }
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
@@ -206,13 +226,19 @@
                continue;
            }
            try {
                Long mpsId = createMpsForSaleOrderItem(saleOrder, item);
                if (mpsId != null) {
                    mpsCount++;
                // 独立事务创建 MPS:失败只回滚 MPS 自身,不影响审核事务
                Long mpsId = mpsTransactionTemplate.execute(txStatus -> {
                    Long newMpsId = createMpsForSaleOrderItem(saleOrder, item);
                    if (newMpsId != null) {
                    // 更新销售订单明细的 MPS 关联
                    saleOrderItemMapper.updateById(new ErpSaleOrderItemDO()
                            .setId(item.getId())
                            .setMpsId(mpsId));
                                .setMpsId(newMpsId));
                    }
                    return newMpsId;
                });
                if (mpsId != null) {
                    mpsCount++;
                }
            } catch (Exception e) {
                log.error("[createMpsForSaleOrder] 创建 MPS 失败,saleOrderItemId={}", item.getId(), e);