| | |
| | | package com.zy.asrs.task.handler; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.core.common.DateUtils; |
| | | 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.model.MesPakinParam; |
| | | 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 org.springframework.transaction.interceptor.TransactionAspectSupport; |
| | | |
| | | 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 |
| | |
| | | private OrderService orderService; |
| | | @Autowired |
| | | private OrderDetlService orderDetlService; |
| | | @Autowired |
| | | private ApiLogService apiLogService; |
| | | @Autowired |
| | | private DocTypeService docTypeService; |
| | | |
| | | |
| | | |
| | | @Transactional |
| | | public ReturnT<String> start(Order order) { |
| | | DocType docType = docTypeService.selectById(order.getDocType()); |
| | | if (null == docType) { |
| | | log.info("上报出错,不存在该单据类型: " + order); |
| | | return SUCCESS; |
| | | } |
| | | List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId()); |
| | | |
| | | MesPakinParam pakinParam = new MesPakinParam(); |
| | | pakinParam.setPakinTime(DateUtils.convert(order.getUpdateTime())); |
| | | pakinParam.setLgortFrom("5008"); |
| | | pakinParam.setLgortTo("5006"); |
| | | for (OrderDetl orderDetl : orderDetls) { |
| | | pakinParam.getList().add(new MesPakinParam.Detl(orderDetl.getMatnr(), orderDetl.getAnfme())); |
| | | } |
| | | // 获取请求头 |
| | | Map<String, Object> headers = getHeaders(); |
| | | |
| | | // 构造请求体 |
| | | String body = getBody(orderDetls, order); |
| | | |
| | | // 入库完成上报 |
| | | String response = ""; |
| | | boolean success = false; |
| | | try { |
| | | String response = new HttpHandler.Builder() |
| | | .setUri(MesConstant.URL) |
| | | .setPath(MesConstant.PAKIN_URL) |
| | | .setJson(JSON.toJSONString(pakinParam)) |
| | | .build() |
| | | .doPost(); |
| | | response = new HttpHandler.Builder() |
| | | .setUri(MesConstant.URL) |
| | | .setPath(MesConstant.PAKIN_URL) |
| | | .setHeaders(headers) |
| | | .setJson(body) |
| | | .build() |
| | | .doPost(); |
| | | JSONObject jsonObject = JSON.parseObject(response); |
| | | if (jsonObject.getInteger("code").equals(200)) { |
| | | 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 { |
| | | log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PAKIN_URL, JSON.toJSONString(pakinParam), response); |
| | | orderService.updateSettle(order.getId(), 6L, null); |
| | | log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.PAKIN_URL, body, response); |
| | | throw new CoolException("上报mes系统失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("fail", e); |
| | | e.printStackTrace(); |
| | | TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); |
| | | // 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<OrderDetl> 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<String,Object> 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<String, Object> 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<String,Object> 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<String,Object> 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()); |
| | | } |
| | | } |