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.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<String> {
|
|
@Autowired
|
private JdbcTemplate jdbcTemplate;
|
@Autowired
|
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());
|
|
// 获取请求头
|
Map<String, Object> 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 = jsonObject.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, 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<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(){
|
JSONObject digiHost = new JSONObject();
|
digiHost.put("prod","XThirdParty");
|
digiHost.put("ver","1.0");
|
digiHost.put("ip","192.168.10.160");
|
digiHost.put("id","XWMS");
|
digiHost.put("timezone","+8");
|
digiHost.put("timestamp",new Date().getTime());
|
digiHost.put("acct","dcms");
|
|
JSONObject digiService = new JSONObject();
|
digiService.put("prod","E10");
|
digiService.put("name","XCommon.ImportData");
|
digiService.put("ip","192.168.10.160");
|
digiService.put("id","SW_TEST_External");
|
|
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());
|
}
|
}
|