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

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

    }


http://www.kler.cn/a/586007.html

相关文章:

  • SpringBoot注解驱动CRUD工具:spring-avue-plus
  • BOE(京东方)携手微博举办“微博影像年”年度影像大展 创新科技赋能专业影像惊艳呈现
  • 芯谷D8563TS:低功耗CMOS实时时钟/日历电路的优选方案
  • CentOS-7安装Docker(更新时间:2025-03-12)
  • markdown 转 word 工具 ‌Pandoc‌
  • 谷歌手机LEA流程
  • Vue 中的 transition 组件如何处理动画效果?
  • 世界坐标到UV纹理坐标的映射
  • PinnDE:基于物理信息神经网络的微分方程求解库
  • RabbitMQ入门:从安装到高级消息模式
  • axios配置全局接口超时时间
  • 某乎x-zse-96加密算法分析与还原
  • Leetcode3340:检查平衡字符串
  • 【漫话机器学习系列】132.概率质量函数(Probability Mass Function, PMF)
  • 软件性能测试与功能测试联系和区别
  • 开源:LMDB 操作工具:lmcmd
  • 笔试刷题专题(一)
  • 《MySQL数据库从零搭建到高效管理|表的增删改查(基础)》
  • STM32与HAL库开发实战:深入探索ESP8266的多种工作模式
  • 46. HarmonyOS NEXT 登录模块开发教程(一):模态窗口登录概述