package com.zy.asrs.task.handler; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.exception.CoolException; import com.zy.asrs.entity.*; import com.zy.asrs.service.ApiLogService; import com.zy.asrs.service.DocTypeService; import com.zy.asrs.service.OrderDetlPakoutService; import com.zy.asrs.service.OrderPakoutService; import com.zy.asrs.task.AbstractHandler; import com.zy.asrs.task.core.ReturnT; import com.zy.common.constant.ApiInterfaceConstant; import com.zy.common.constant.MesConstant; import com.zy.common.model.MesPakoutParam; import com.zy.common.utils.HttpHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; /** * Created by vincent on 2020/7/7 */ @Slf4j @Service public class OrderPakoutSyncHandler extends AbstractHandler { @Autowired private OrderPakoutService orderPakoutService; @Autowired private OrderDetlPakoutService orderDetlPakoutService; @Autowired private ApiLogService apiLogService; @Autowired private DocTypeService docTypeService; @Transactional public ReturnT startOrderReport(OrderPakout order) { DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { log.error("未找到对应的DocType,订单号:{},单据类型:{}", order.getOrderNo(), order.getDocType()); return FAIL.setMsg("未找到对应的单据类型:" + order.getOrderNo()); } ErpReportDto param = new ErpReportDto(); param.setOrderDetails(new ArrayList<>()); List orderDetls = orderDetlPakoutService.selectByOrderId(order.getId()); for (OrderDetlPakout orderDetl : orderDetls) { param.getOrderDetails().add(new ErpReportDto.DetlDto(orderDetl.getMatnr(),orderDetl.getQty())); } param.setOrderNo(order.getOrderNo()); String response = ""; boolean success = false; ReturnT result = SUCCESS; try { response = new HttpHandler.Builder() .setUri(ApiInterfaceConstant.ERP_IP) .setPath(ApiInterfaceConstant.PAKOUT_PATH) .setJson(JSON.toJSONString(param)) .build() .doPost(); JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code").equals(200)) { // 修改订单状态 4.完成 ===>> 6.已上报 orderPakoutService.updateSettle(order.getId(), 6L, null); success = true; } else { log.error("出库完成上报erp失败!url:{};request:{};response:{}", ApiInterfaceConstant.ERP_IP + ApiInterfaceConstant.PAKOUT_PATH, JSON.toJSONString(param), response); throw new CoolException("出库完成上报erp失败"); } } catch (Exception e) { log.error("出库完成上报erp异常", e); result = FAIL.setMsg("出库完成上报异常:" + e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( "出库完成上报", ApiInterfaceConstant.ERP_IP + ApiInterfaceConstant.PAKOUT_PATH, null, "127.0.0.1", JSON.toJSONString(param), response, success ); } catch (Exception e) { log.error("出库保存接口日志异常", e); } } return result; } }