package com.zy.asrs.task.handler;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.core.common.Cools;
|
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.utils.HttpHandler;
|
import lombok.extern.slf4j.Slf4j;
|
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.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
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> {
|
|
private static final String LK_TEST_URI = "192.168.0.41";
|
private static final String LK_PROD_URI = "192.168.0.42";
|
|
private static final String LK_ENT_CODE = "lfd";
|
private static final String LK_USERNAME = "TL002";
|
private static final String LK_PASSWORD_MD5 = "e10adc3949ba59abbe56e057f20f883e";
|
|
private volatile String lkToken;
|
|
@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) {
|
return SUCCESS;
|
}
|
List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
|
|
String docName = docType.getDocName();
|
if (!Cools.isEmpty(docName) && docName.contains("退")) {
|
ReturnT<String> returnT = reportProductReturn(order, orderDetls);
|
if (!returnT.isSuccess()) {
|
return returnT;
|
}
|
return SUCCESS;
|
}
|
|
if (docType.getPakin() == 1) {
|
ReturnT<String> returnT = reportPurchaseInStock(order, orderDetls);
|
if (!returnT.isSuccess()) {
|
return returnT;
|
}
|
} else if (docType.getPakout() == 1) {
|
ReturnT<String> returnT = reportIssueOrFeed(order, docType, orderDetls);
|
if (!returnT.isSuccess()) {
|
return returnT;
|
}
|
}
|
return SUCCESS;
|
}
|
|
private ReturnT<String> reportPurchaseInStock(Order order, List<OrderDetl> orderDetls) {
|
String path = "/purchaseInStock/lkPurchaseInStockAdd";
|
String memo = "立库采购入库";
|
JSONObject request = new JSONObject();
|
request.put("purchaseInStockDtls", buildBatchQtyList(orderDetls));
|
|
String response = "";
|
boolean success = false;
|
try {
|
JSONObject res = postLk(path, request);
|
response = res.toJSONString();
|
if (lkSuccess(res)) {
|
success = true;
|
if (!orderService.updateSettle(order.getId(), 6L, null)) {
|
throw new CoolException("服务器内部错误,请联系管理员");
|
}
|
} else {
|
throw new CoolException("上报接口失败");
|
}
|
} catch (Exception e) {
|
log.error("fail", e);
|
return FAIL.setMsg(e.getMessage());
|
} finally {
|
try {
|
apiLogService.save(
|
memo,
|
lkUri() + path,
|
null,
|
"127.0.0.1",
|
request.toJSONString(),
|
response,
|
success
|
);
|
} catch (Exception e) { log.error("", e); }
|
}
|
return SUCCESS;
|
}
|
|
private ReturnT<String> reportIssueOrFeed(Order order, DocType docType, List<OrderDetl> orderDetls) {
|
String docName = docType.getDocName();
|
String path;
|
String memo;
|
if (!Cools.isEmpty(docName) && docName.contains("补")) {
|
path = "/productFeed/lkProductFeedAdd";
|
memo = "立库生产补料";
|
} else {
|
path = "/productIssue/lkProductIssueAdd";
|
memo = "立库生产发料";
|
}
|
|
JSONObject dtl = new JSONObject();
|
dtl.put("planSpNo", order.getOrderNo());
|
List<JSONObject> boms = buildBatchQtyList(orderDetls);
|
String bomsKey;
|
if (path.contains("productFeed")) {
|
bomsKey = "productFeedBoms";
|
} else if (path.contains("productReturn")) {
|
bomsKey = "productReturnBoms";
|
} else {
|
bomsKey = "productIssueBoms";
|
}
|
dtl.put(bomsKey, boms);
|
|
String dtlsKey;
|
if (path.contains("productFeed")) {
|
dtlsKey = "productFeedDtls";
|
} else if (path.contains("productReturn")) {
|
dtlsKey = "productReturnDtls";
|
} else {
|
dtlsKey = "productIssueDtls";
|
}
|
JSONObject request = new JSONObject();
|
request.put(dtlsKey, Collections.singletonList(dtl));
|
|
String response = "";
|
boolean success = false;
|
try {
|
JSONObject res = postLk(path, request);
|
response = res.toJSONString();
|
if (lkSuccess(res)) {
|
success = true;
|
if (!orderService.updateSettle(order.getId(), 6L, null)) {
|
throw new CoolException("服务器内部错误,请联系管理员");
|
}
|
} else {
|
throw new CoolException("上报接口失败");
|
}
|
} catch (Exception e) {
|
log.error("fail", e);
|
return FAIL.setMsg(e.getMessage());
|
} finally {
|
try {
|
apiLogService.save(
|
memo,
|
lkUri() + path,
|
null,
|
"127.0.0.1",
|
request.toJSONString(),
|
response,
|
success
|
);
|
} catch (Exception e) { log.error("", e); }
|
}
|
return SUCCESS;
|
}
|
|
private ReturnT<String> reportProductReturn(Order order, List<OrderDetl> orderDetls) {
|
String path = "/productReturn/lkProductReturnAdd";
|
String memo = "立库生产退料";
|
|
JSONObject dtl = new JSONObject();
|
dtl.put("planSpNo", order.getOrderNo());
|
dtl.put("productReturnBoms", buildBatchQtyList(orderDetls));
|
|
JSONObject request = new JSONObject();
|
request.put("productReturnDtls", Collections.singletonList(dtl));
|
|
String response = "";
|
boolean success = false;
|
try {
|
JSONObject res = postLk(path, request);
|
response = res.toJSONString();
|
if (lkSuccess(res)) {
|
success = true;
|
if (!orderService.updateSettle(order.getId(), 6L, null)) {
|
throw new CoolException("服务器内部错误,请联系管理员");
|
}
|
} else {
|
throw new CoolException("上报接口失败");
|
}
|
} catch (Exception e) {
|
log.error("fail", e);
|
return FAIL.setMsg(e.getMessage());
|
} finally {
|
try {
|
apiLogService.save(
|
memo,
|
lkUri() + path,
|
null,
|
"127.0.0.1",
|
request.toJSONString(),
|
response,
|
success
|
);
|
} catch (Exception e) { log.error("", e); }
|
}
|
return SUCCESS;
|
}
|
|
private List<JSONObject> buildBatchQtyList(List<OrderDetl> orderDetls) {
|
List<JSONObject> list = new ArrayList<>();
|
for (OrderDetl detl : orderDetls) {
|
if (Cools.isEmpty(detl.getBatch())) {
|
throw new CoolException("批次号不能为空");
|
}
|
JSONObject one = new JSONObject();
|
one.put("batchNum", detl.getBatch());
|
one.put("quantity", detl.getAnfme());
|
list.add(one);
|
}
|
return list;
|
}
|
|
private JSONObject postLk(String path, JSONObject request) throws IOException {
|
try {
|
return doPostLk(path, request, ensureLkToken(false));
|
} catch (Exception e) {
|
return doPostLk(path, request, ensureLkToken(true));
|
}
|
}
|
|
private JSONObject doPostLk(String path, JSONObject request, String token) throws IOException {
|
Map<String, Object> headers = new HashMap<>();
|
if (!Cools.isEmpty(token)) {
|
headers.put("Authorization", token);
|
}
|
String response = new HttpHandler.Builder()
|
.setUri(lkUri())
|
.setPath(path)
|
.setHeaders(headers)
|
.setJson(request.toJSONString())
|
.build()
|
.doPost();
|
return JSON.parseObject(response);
|
}
|
|
private boolean lkSuccess(JSONObject response) {
|
if (response == null) {
|
return false;
|
}
|
Integer code = response.getInteger("code");
|
String error = response.getString("error");
|
return code != null && code == 200 && "SUCCESS".equalsIgnoreCase(error);
|
}
|
|
private String ensureLkToken(boolean force) throws IOException {
|
if (!force && !Cools.isEmpty(lkToken)) {
|
return lkToken;
|
}
|
synchronized (this) {
|
if (!force && !Cools.isEmpty(lkToken)) {
|
return lkToken;
|
}
|
JSONObject req = new JSONObject();
|
req.put("entCode", LK_ENT_CODE);
|
req.put("username", LK_USERNAME);
|
req.put("password", LK_PASSWORD_MD5);
|
String resp = new HttpHandler.Builder()
|
.setUri(lkUri())
|
.setPath("/acco/login")
|
.setJson(req.toJSONString())
|
.build()
|
.doPost();
|
JSONObject json = JSON.parseObject(resp);
|
Integer code = json.getInteger("code");
|
if (code == null || code != 200) {
|
throw new CoolException("登录失败");
|
}
|
String token = json.getString("data");
|
if (Cools.isEmpty(token)) {
|
throw new CoolException("登录失败");
|
}
|
lkToken = token;
|
return lkToken;
|
}
|
}
|
|
private String lkUri() {
|
String override = System.getProperty("lk.uri");
|
if (!Cools.isEmpty(override)) {
|
return override;
|
}
|
String env = System.getProperty("lk.env");
|
if (!Cools.isEmpty(env) && "prod".equalsIgnoreCase(env)) {
|
return LK_PROD_URI;
|
}
|
return LK_TEST_URI;
|
}
|
|
}
|