您的位置:首页 > 健康 > 养生 > 保定自助建站_房产交易网上预约平台_百度地图导航_seo营销培训

保定自助建站_房产交易网上预约平台_百度地图导航_seo营销培训

2025/3/16 4:23:35 来源:https://blog.csdn.net/weixin_44568816/article/details/146191456  浏览:    关键词:保定自助建站_房产交易网上预约平台_百度地图导航_seo营销培训
保定自助建站_房产交易网上预约平台_百度地图导航_seo营销培训

旧代码

    @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 {@Overridepublic 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 {@Autowiredprivate WmsFactoryTransferServiceImpl wmsFactoryTransferService;@Autowiredprivate SysPurorderPostingLogMapper sysPurorderPostingLogMapper;@Overridepublic 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);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com