package com.fs.task; import com.fs.erp.domain.ErpOrder; import com.fs.erp.domain.FsErpFinishPush; import com.fs.erp.dto.ErpOrderResponse; import com.fs.erp.mapper.FsErpFinishPushMapper; import com.fs.erp.service.IErpOrderService; import com.fs.store.domain.FsStoreOrder; import com.fs.store.service.IFsStoreOrderService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; @Service("erpTask") @Slf4j public class ErpTask { @Autowired private FsErpFinishPushMapper fsErpFinishPushMapper; @Autowired private IErpOrderService erpOrderService; @Autowired private IFsStoreOrderService fsStoreOrderService; /** * 推送完成订单到ERP */ public void pushFinishOrderToErp(){ List fsErpFinishPushes = fsErpFinishPushMapper.queryPenddingOrder(); for (FsErpFinishPush fsErpFinishPush : fsErpFinishPushes) { FsStoreOrder fsStoreOrder = fsStoreOrderService.selectFsStoreOrderById(fsErpFinishPush.getOrderId()); try { ErpOrder erpOrder = fsStoreOrderService.getErpOrder(fsStoreOrder); ErpOrderResponse erpOrderResponse = erpOrderService.finishOrder(erpOrder); fsErpFinishPush.setParams(erpOrderResponse.getRequestRawData()); fsErpFinishPush.setResult(erpOrderResponse.getResponseRawData()); fsErpFinishPush.setUpdateTime(new Date()); if(erpOrderResponse.getSuccess()!= null && erpOrderResponse.getSuccess()){ fsErpFinishPush.setTaskStatus(1); log.error("推送完成订单到ERP成功! 订单号: {}",fsErpFinishPush.getOrderId()); } else { fsErpFinishPush.setTaskStatus(2); fsErpFinishPush.setRetryCount(fsErpFinishPush.getRetryCount()+1); log.error("推送完成订单到ERP失败! 订单号: {}",fsErpFinishPush.getOrderId()); } } catch (Throwable e) { fsErpFinishPush.setRetryCount(fsErpFinishPush.getRetryCount()+1); fsErpFinishPush.setErrorMessage(ExceptionUtils.getStackTrace(e)); fsErpFinishPush.setTaskStatus(2); log.error("订单推送失败!原因: {}", ExceptionUtils.getStackTrace(e),e); } fsErpFinishPushMapper.updateById(fsErpFinishPush); } } }