package com.zy.asrs.task.handler; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.core.exception.CoolException; import com.zy.asrs.entity.DocType; import com.zy.asrs.entity.Order; import com.zy.asrs.entity.OrderDetl; import com.zy.asrs.service.ApiLogService; import com.zy.asrs.service.DocTypeService; import com.zy.asrs.service.OrderDetlService; import com.zy.asrs.service.OrderService; import com.zy.asrs.task.AbstractHandler; import com.zy.asrs.task.core.ReturnT; import com.zy.common.constant.MesConstant; import com.zy.common.utils.HttpHandler; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by vincent on 2020/7/7 */ @Slf4j @Service public class OrderSyncHandler extends AbstractHandler { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private OrderService orderService; @Autowired private OrderDetlService orderDetlService; @Autowired private ApiLogService apiLogService; @Autowired private DocTypeService docTypeService; @Transactional public ReturnT start(Order order) { DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { log.info("上报出错,不存在该单据类型: " + order); return SUCCESS; } List orderDetls = orderDetlService.selectByOrderId(order.getId()); // 获取请求头 Map headers = getHeaders(); // 构造请求体 String body = getBody(orderDetls, order); // 入库完成上报 String response = ""; boolean success = false; try { response = new HttpHandler.Builder() .setUri(MesConstant.URL) .setPath(MesConstant.PAKIN_URL) .setHeaders(headers) .setJson(body) .build() .doPost(); JSONObject jsonObject = JSON.parseObject(response); JSONObject std_data = jsonObject.getJSONObject("std_data"); JSONObject execution = std_data.getJSONObject("execution"); String code = execution.getString("code"); JSONObject std_data1 = std_data.getJSONObject("parameter"); JSONObject execution1 = std_data1.getJSONObject("response_result"); String status = execution1.getString("Status"); if ("0".equals(code) && "0".equals(status)) { success = true; // 修改订单状态 4.完成 ===>> 6.已上报 if (!orderService.updateSettle(order.getId(), 6L, null)) { throw new CoolException("服务器内部错误,请联系管理员"); } } else { orderService.updateSettle(order.getId(), 7L, null); log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.PAKIN_URL, body, response); throw new CoolException("上报mes系统失败"); } } catch (Exception e) { log.error("fail", e); // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return FAIL.setMsg(e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( docType.getPakin() == 1 ?"入库上报" : "出库上报", MesConstant.URL + MesConstant.PAKIN_URL, null, "127.0.0.1", JSON.toJSONString(body), response, success ); } catch (Exception e) { log.error("", e); } } return SUCCESS; } private String getBody(List orderDetls, Order order) { JSONObject orderJson = new JSONObject(); JSONObject orderJson1 = new JSONObject(); JSONObject orderJson2 = new JSONObject(); JSONObject orderJson3 = new JSONObject(); JSONArray jsonArray = new JSONArray(); for(OrderDetl orderDetl:orderDetls) { Map map = new HashMap<>(); map.put("matnr",orderDetl.getMatnr()); map.put("anfme",orderDetl.getAnfme()); jsonArray.add(map); } orderJson3.put("orderNo",order.getOrderNo()); orderJson3.put("TypeKey",order.getDocType$()); orderJson3.put("matList",jsonArray); orderJson2.put("importData",orderJson3); orderJson1.put("parameter",orderJson2); orderJson.put("std_data",orderJson1); return orderJson.toJSONString(); } Map getHeaders(){ // digi-type: sync // digi-protocol: raw // digi-host: {"prod":"XThirdParty","ver":"1.0","ip":"61.153.227.86","id":"XWMS","timezone":"+8","timestamp":"20240613062240538","acct":"dcms"} // digi-service: {"prod":"E10","name":"XCommon.ImportData","id":"XFX","ip":"139.196.196.39"} // digi-key: 504011D3B5AD32B465FE0C097C7FDAAC // digi-datakey: XCommon.ImportData // 获取当前时间 LocalDateTime now = LocalDateTime.now(); // 定义日期时间格式化器,根据你的需求定义格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); // 格式化日期时间为字符串 String formattedDateTime = now.format(formatter); JSONObject digiHost = new JSONObject(); digiHost.put("prod","XThirdParty"); digiHost.put("ver","1.0"); digiHost.put("ip","61.153.227.86"); digiHost.put("id","XWMS"); digiHost.put("timezone","+8"); digiHost.put("timestamp",formattedDateTime); digiHost.put("acct","dcms"); JSONObject digiService = new JSONObject(); digiService.put("prod","E10"); digiService.put("name","XCommon.ImportData"); digiService.put("ip","139.196.196.39"); digiService.put("id","XFX"); String key = digiHost.toJSONString() + digiService.toJSONString(); String keyMd5 = DigestUtils.md5Hex(key); Map headers = new HashMap<>(); headers.put("digi-type","sync "); headers.put("digi-protocol","raw"); headers.put("digi-host",digiHost.toJSONString()); headers.put("digi-service",digiService.toJSONString()); headers.put("digi-key",keyMd5); headers.put("digi-datakey"," XCommon.ImportData"); return headers; } public static void main(String[] args) { String msg = "{\n" + " \"std_data\": {\n" + " \"execution\": {\n" + " \"code\": \"0\",\n" + " \"sql_code\": \"\",\n" + " \"description\": \"执行成功\",\n" + " \"token_id\": \"e7b4b7d668ec41fa83f643eb1b322959\"\n" + " },\n" + " \"parameter\": {\n" + " \"response_result\": {\n" + " \"Status\": \"0\",\n" + " \"Message\": \"操作成功!\",\n" + " \"DOC_NO\": \"\"\n" + " }\n" + " }\n" + " }\n" + "}"; JSONObject jsonObject = JSON.parseObject(msg); JSONObject std_data = jsonObject.getJSONObject("std_data"); JSONObject execution = std_data.getJSONObject("execution"); System.out.println(jsonObject.get("std_data")); System.out.println(execution.getString("code")); // JSONObject orderJson = new JSONObject(); // JSONObject orderJson1 = new JSONObject(); // JSONObject orderJson2 = new JSONObject(); // JSONObject orderJson3 = new JSONObject(); // // JSONArray jsonArray = new JSONArray(); // Map map = new HashMap<>(); // map.put("matnr","301080001"); // map.put("anfme","23456"); // JSONObject jsonObject = new JSONObject(); // jsonObject.put("matnr","301080001"); // jsonObject.put("anfme","23456"); // jsonArray.add(map); // // orderJson3.put("orderNo","3600-201702010001"); // orderJson3.put("TypeKey","PURCHASE_ARRIVAL"); // orderJson3.put("matList",jsonArray); // orderJson2.put("importData",orderJson3); // orderJson1.put("parameter",orderJson2); // orderJson.put("std_data",orderJson1); // System.out.println(orderJson.toJSONString()); } }