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.DocType; import com.zy.asrs.entity.OrderDetl; import com.zy.asrs.entity.OrderDetlPakin; import com.zy.asrs.entity.OrderPakin; import com.zy.asrs.service.ApiLogService; import com.zy.asrs.service.DocTypeService; import com.zy.asrs.service.OrderDetlPakinService; import com.zy.asrs.service.OrderPakinService; import com.zy.asrs.task.AbstractHandler; import com.zy.asrs.task.core.ReturnT; import com.zy.common.constant.MesConstant; import com.zy.common.model.MesPakinParam; 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.List; /** * Created by vincent on 2020/7/7 */ @Slf4j @Service public class OrderPakinSyncHandler extends AbstractHandler { @Autowired private OrderPakinService orderPakinService; @Autowired private OrderDetlPakinService orderDetlPakinService; @Autowired private ApiLogService apiLogService; @Autowired private DocTypeService docTypeService; @Transactional public ReturnT startOrderReport(OrderPakin order) { DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { log.error("未找到对应的DocType,订单号:{},单据类型:{}", order.getOrderNo(), order.getDocType()); return FAIL.setMsg("未找到对应的单据类型:" + order.getOrderNo()); } MesPakinParam pakinParam = new MesPakinParam(); List orderDetls = orderDetlPakinService.selectByOrderId(order.getId()); for (OrderDetlPakin orderDetl : orderDetls) { String serial = Cools.isEmpty(orderDetl.getBatch()) ? "" : orderDetl.getBatch(); pakinParam.getList().add(new MesPakinParam.Detl(orderDetl.getMatnr() + (Cools.isEmpty(serial) ? "" : "-" + serial), orderDetl.getAnfme())); } String response = ""; boolean success = false; ReturnT result = SUCCESS; try { response = new HttpHandler.Builder() .setUri(MesConstant.URI) .setPath(MesConstant.PAKIN_PATH) .setJson(JSON.toJSONString(pakinParam)) .build() .doPost(); JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code").equals(200)) { // 修改订单状态 4.完成 ===>> 6.已上报 orderPakinService.updateSettle(order.getId(), 6L, null); success = true; } else { log.error("入库完成上报erp失败!url:{};request:{};response:{}", MesConstant.URI + MesConstant.PAKIN_PATH, JSON.toJSONString(pakinParam), response); throw new CoolException("入库完成上报erp失败"); } } catch (Exception e) { log.error("入库完成上报erp异常", e); result = FAIL.setMsg("入库完成上报异常:" + e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( "入库完成上报", MesConstant.URI + MesConstant.PAKIN_PATH, null, "127.0.0.1", JSON.toJSONString(pakinParam), JSON.toJSONString(response), success ); } catch (Exception e) { log.error("入库保存接口日志异常", e); } } return result; } }