当前位置: 首页 > news >正文

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

旧代码

    @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);

    }

相关文章:

  • LeetCode 513. 找树左下角的值 java题解
  • 《Spring日志整合与注入技术:从入门到精通》
  • 物理服务器抵御网络攻击的方法都有哪些?
  • SCSS详解
  • 创建模式-工厂方法模式(Factory Method Pattern)
  • UE5以插件的形式加载第三方库
  • AI+视频监控电力巡检:EasyCVR视频中台方案如何赋能电力行业智能化转型
  • 爬虫的精准识别:基于 User-Agent 的正则实现
  • 【RTSP】客户端(一):RTSP协议实现
  • 【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】
  • [Web]ServletContext域(Application)
  • 【Agent】Windows 和 CentOS 安装 Conda
  • wireguard搭配udp2raw部署内网
  • 坐落于杭州的电商代运营公司品融电商
  • 智能验证码破解:突破reCAPTCHA、Cloudflare和hCaptcha的全方位解决方案
  • selenium的鼠标操作
  • 每天一道算法题【蓝桥杯】【x的平方根】
  • 通义万相2.1 图生视频:为AI绘梦插上翅膀,开启ALGC算力领域新纪元
  • Qt5.10版本以下 qml ui语言动态切换
  • MySQL EXPLAIN 详解
  • 网警打谣:传播涉刘国梁不实信息,2人被处罚
  • 法律顾问被控配合他人诈骗酒店资产一审判8年,二审辩称无罪
  • 本周看啥|《歌手》今晚全开麦直播,谁能斩获第一名?
  • 定制基因编辑疗法治愈罕见遗传病患儿
  • 标普500指数连涨四日,大型科技股多数下跌
  • 中国青年报:为见义勇为者安排补考,体现了教育的本质目标