sap关账+策略模式(避免大量if elseif)

发布于:2025-03-16 ⋅ 阅读:(34) ⋅ 点赞:(0)

旧代码

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult purchaseOrderReceiptOutSourceAfterSapCloseAccountingPeriod(Long id) {
        SysPurorderPostingLog sysPurorderPostingLog = sysPurorderPostingLogMapper.selectSysPurorderPostingLogById(id);
        if (Objects.isNull(sysPurorderPostingLog)) {
            throw new ServiceException("未找到过账日志信息");
        }
        if (SysConstants.SYS_SUCCESS.equals(sysPurorderPostingLog.getStatus())) {
            throw new ServiceException("该日志已成功过账, 禁止重复过账");
        }
        //根据不同类型补充过账
        if (InventoryOperation.PURCHASE_RECEIPT.getCode().equals(sysPurorderPostingLog.getInterfaceType())) {
            //采购订单
            return getPurchaseReceipt(sysPurorderPostingLog);

        } else if (InventoryOperation.TRANSFER_IN_SKIP.getCode().equals(sysPurorderPostingLog.getInterfaceType())) {
            //301预留
            AjaxResult ajaxResult = wmsFactoryTransferService.sapFactoryTransferOutSourcePosting(Long.valueOf(sysPurorderPostingLog.getOrderId()));
            if (ajaxResult.isSuccess()) {
                String requestJson = (String) ajaxResult.get("requestJson");
                String responseJson = (String) ajaxResult.get("responseJson");
                sysPurorderPostingLog.setOperParam(requestJson);
                sysPurorderPostingLog.setJsonResult(responseJson);
                sysPurorderPostingLog.setStatus(SysConstants.SYS_SUCCESS);
                sysPurorderPostingLog.setUpdateTime(new Date());
                sysPurorderPostingLogMapper.updateSysPurorderPostingLog(sysPurorderPostingLog);
                return AjaxResult.success("过账成功");
            }
        } else {
            throw new ServiceException("未知的过账类型");
        }

        return AjaxResult.error("过账失败");
    }

步骤
1.定义策略接口:创建一个接口,定义所有具体策略类必须实现的方法。

public interface PostingStrategy {
    AjaxResult executePosting(SysPurorderPostingLog sysPurorderPostingLog);
}


2.实现具体策略类:为每种过账类型实现具体的策略类。

2.1采购订单过账策略

public class PurchaseReceiptPostingStrategy implements PostingStrategy {
    @Override
    public AjaxResult executePosting(SysPurorderPostingLog sysPurorderPostingLog) {
        // 具体的采购订单过账逻辑
        return getPurchaseReceipt(sysPurorderPostingLog); // 假设这个方法已经存在
    }
}

2.2 301预留过账策略

package com.kpl.sys.domain;

import com.kpl.common.constant.SysConstants;
import com.kpl.common.core.domain.AjaxResult;
import com.kpl.sys.mapper.SysPurorderPostingLogMapper;
import com.kpl.sys.service.PostingStrategy;
import com.kpl.wms.service.impl.WmsFactoryTransferServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 实现具体策略类:为每种过账类型实现具体的策略类 +  301预留
 */
@Component
public class FactoryTransferPostingStrategy implements PostingStrategy {


    @Autowired
    private WmsFactoryTransferServiceImpl wmsFactoryTransferService;

    @Autowired
    private SysPurorderPostingLogMapper sysPurorderPostingLogMapper;


    @Override
    public AjaxResult executePosting(SysPurorderPostingLog sysPurorderPostingLog) {
        AjaxResult ajaxResult = wmsFactoryTransferService.sapFactoryTransferOutSourcePosting(Long.valueOf(sysPurorderPostingLog.getOrderId()));
        if (ajaxResult.isSuccess()) {
            String requestJson = (String) ajaxResult.get("requestJson");
            String responseJson = (String) ajaxResult.get("responseJson");
            sysPurorderPostingLog.setOperParam(requestJson);
            sysPurorderPostingLog.setJsonResult(responseJson);
            sysPurorderPostingLog.setStatus(SysConstants.SYS_SUCCESS);
            sysPurorderPostingLog.setUpdateTime(new Date());
            sysPurorderPostingLogMapper.updateSysPurorderPostingLog(sysPurorderPostingLog);
            return AjaxResult.success("过账成功");
        }
        return AjaxResult.error("过账失败");
    }
}


3.上下文类:创建一个上下文类来管理策略对象,并提供设置和获取策略的方法。

package com.kpl.sys.domain;


import com.kpl.common.core.domain.AjaxResult;
import com.kpl.common.exception.ServiceException;
import com.kpl.sys.service.PostingStrategy;
import lombok.Data;
import lombok.Setter;
import org.springframework.stereotype.Component;

/**
 * 上下文类:创建一个上下文类来管理策略对象,并提供设置和获取策略的方法
 */
@Setter
@Component
public class PostingContext {

    private PostingStrategy postingStrategy;

    public AjaxResult executePosting(SysPurorderPostingLog sysPurorderPostingLog) {
        if (postingStrategy == null) {
            throw new ServiceException("策略模式不存在!");
        }
        return postingStrategy.executePosting(sysPurorderPostingLog);
    }
}


4.重构原方法:使用上下文类来调用适当的策略。

    /**
     * 关账过账
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult purchaseOrderReceiptOutSourceAfterSapCloseAccountingPeriod(Long id) {
        SysPurorderPostingLog sysPurorderPostingLog = sysPurorderPostingLogMapper.selectSysPurorderPostingLogById(id);
        if (Objects.isNull(sysPurorderPostingLog)) {
            throw new ServiceException("未找到过账日志信息");
        }
        if (SysConstants.SYS_SUCCESS.equals(sysPurorderPostingLog.getStatus())) {
            throw new ServiceException("该日志已成功过账, 禁止重复过账");
        }

        //使用策略模式
        PostingContext context = new PostingContext();
        PostingStrategy strategy = null;
        switch (sysPurorderPostingLog.getInterfaceType()) {
            case "101P":
                strategy = applicationContext.getBean(PurchaseReceiptPostingStrategy.class);
                break;
            case "301i":
                strategy = applicationContext.getBean(FactoryTransferPostingStrategy.class);
                break;
            default:
                throw new ServiceException("未知的过账类型");
        }

        context.setPostingStrategy(strategy);
        return context.executePosting(sysPurorderPostingLog);

    }


网站公告

今日签到

点亮在社区的每一天
去签到