|
@@ -1,5 +1,10 @@
|
|
|
package com.kmall.admin.service.impl.compared;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.internal.LinkedTreeMap;
|
|
|
import com.kmall.admin.dao.compared.OrderComparedErrorDao;
|
|
|
import com.kmall.admin.entity.OrderEntity;
|
|
|
import com.kmall.admin.entity.compared.OrderComparedErrorEntity;
|
|
@@ -7,9 +12,22 @@ import com.kmall.admin.entity.compared.PayOrderInfoEntity;
|
|
|
import com.kmall.admin.service.OrderService;
|
|
|
import com.kmall.admin.service.compared.OrderComparedErrorService;
|
|
|
import com.kmall.admin.service.compared.PayOrderInfoService;
|
|
|
+import com.kmall.admin.utils.data.response.ResponseMessage;
|
|
|
+import com.kmall.admin.utils.data.response.ResponseMessageData;
|
|
|
+import com.kmall.admin.utils.oms.OkHttpUtils;
|
|
|
+import com.kmall.admin.utils.oms.OmsSign;
|
|
|
+import com.kmall.common.utils.R;
|
|
|
+import com.kmall.manager.manager.merch.OmsMerchProperties;
|
|
|
+import okhttp3.Request;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -24,6 +42,9 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service("orderComparedErrorService")
|
|
|
public class OrderComparedErrorServiceImpl implements OrderComparedErrorService {
|
|
|
+
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(OrderComparedErrorServiceImpl.class);
|
|
|
+
|
|
|
@Autowired
|
|
|
private OrderComparedErrorDao orderComparedErrorDao;
|
|
|
|
|
@@ -32,6 +53,10 @@ public class OrderComparedErrorServiceImpl implements OrderComparedErrorService
|
|
|
@Autowired
|
|
|
private OrderService orderService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private OmsMerchProperties omsMerchProperties;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public OrderComparedErrorEntity queryObject(Integer moceId) {
|
|
|
return orderComparedErrorDao.queryObject(moceId);
|
|
@@ -71,6 +96,7 @@ public class OrderComparedErrorServiceImpl implements OrderComparedErrorService
|
|
|
* 对比订单
|
|
|
*/
|
|
|
@Override
|
|
|
+ @Transactional
|
|
|
public String comparedOrder() {
|
|
|
|
|
|
try {
|
|
@@ -80,75 +106,179 @@ public class OrderComparedErrorServiceImpl implements OrderComparedErrorService
|
|
|
return "暂无需要对比的订单";
|
|
|
}
|
|
|
|
|
|
- // 查询前一天的订单
|
|
|
- List<OrderEntity> orderEntities = orderService.queryLastDayOrder();
|
|
|
- // 异常订单集合
|
|
|
- Set<String> errorOrderSn = new HashSet<>();
|
|
|
- // 删除订单号集合
|
|
|
- Set<String> removeOrderSn = new HashSet<>();
|
|
|
-
|
|
|
- // 判断两边数量是否相等
|
|
|
- if(payOrderInfoEntities.size() != orderEntities.size()){
|
|
|
- Set<String> payOrderSnSet = payOrderInfoEntities.stream().map(PayOrderInfoEntity::getMerchOrderSn).collect(Collectors.toSet());
|
|
|
- Set<String> orderSnSet = orderEntities.stream().map(OrderEntity::getOrderSn).collect(Collectors.toSet());
|
|
|
- orderSnSet.removeAll(payOrderSnSet);
|
|
|
- errorOrderSn.addAll(orderSnSet);
|
|
|
- }
|
|
|
+ // 对比中网的订单数据
|
|
|
+ comparedKmallOrder(payOrderInfoEntities);
|
|
|
+ // 对比oms的订单数据
|
|
|
+ comparedOmsOrder(payOrderInfoEntities);
|
|
|
+
|
|
|
+ // 批量修改支付单对比状态跟对比时间
|
|
|
+ payOrderInfoService.updateBatch(payOrderInfoEntities);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "对比失败,原因:"+e.getMessage();
|
|
|
+ }
|
|
|
|
|
|
+ return "对比成功";
|
|
|
+ }
|
|
|
|
|
|
- for(PayOrderInfoEntity parOrderInfo : payOrderInfoEntities){
|
|
|
+ private void comparedOmsOrder(List<PayOrderInfoEntity> payOrderInfoEntities) throws IOException {
|
|
|
+ // 查询oms系统前一天的订单
|
|
|
+ List<OrderEntity> orderEntities = new ArrayList<>();
|
|
|
+ List<String> merchOrderSns = payOrderInfoEntities.stream().map(PayOrderInfoEntity::getMerchOrderSn).collect(Collectors.toList());
|
|
|
|
|
|
- String merchOrderSn = parOrderInfo.getMerchOrderSn();
|
|
|
- // 先将该订单装入集合中
|
|
|
- boolean add = errorOrderSn.add(merchOrderSn);
|
|
|
- for(OrderEntity order : orderEntities){
|
|
|
- String orderSn = order.getOrderSn();
|
|
|
- if(orderSn.equals(merchOrderSn)){
|
|
|
+ Map<String, String> sParaTemp = new TreeMap<String, String>();
|
|
|
+ sParaTemp.put("data",omsMerchProperties.getThirdSn());
|
|
|
+ sParaTemp.put("merchId",omsMerchProperties.getMerchSn());
|
|
|
+ String timestamp = String.valueOf(System.currentTimeMillis()/1000);
|
|
|
+ sParaTemp.put("timestamp", timestamp);
|
|
|
+ //生成要请求给oms秘钥
|
|
|
+// String sign = OmsSign.sign(sParaTemp,cus.getWaybill().get("secret-key"));
|
|
|
+ LOGGER.info("md5混淆码参数:" + omsMerchProperties.getMd5Salt());
|
|
|
+ String sign = OmsSign.sign(sParaTemp,omsMerchProperties.getMd5Salt());
|
|
|
+ sParaTemp.put("sign", sign);
|
|
|
+ //构建Request
|
|
|
+ String url = omsMerchProperties.getQueryOrderUrl();
|
|
|
+ url = "http://127.0.0.1:8680/al/shop/order/query";
|
|
|
+ Request request = OkHttpUtils.buildRequest(url, JSON.toJSONString(sParaTemp));
|
|
|
+ LOGGER.info("oms的请求报文:" + request);
|
|
|
+ // 同步访问,返回结果字符串
|
|
|
+ String responseString = null;
|
|
|
+ try {
|
|
|
|
|
|
- // 判断订单状态
|
|
|
- if(!order.getOrderStatus().equals(Integer.parseInt(parOrderInfo.getOrderStatus()))){
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 判断支付状态
|
|
|
- if(!order.getPayStatus().equals(Integer.parseInt(parOrderInfo.getPayStatus()))){
|
|
|
- continue;
|
|
|
- }
|
|
|
+ responseString = OkHttpUtils.post(request);
|
|
|
|
|
|
- // 如果都一致,在集合中移除改订单
|
|
|
- errorOrderSn.remove(merchOrderSn);
|
|
|
- removeOrderSn.add(merchOrderSn);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+
|
|
|
+ LOGGER.info("oms的响应报文"+responseString);
|
|
|
+ //解析响应数据
|
|
|
+ Gson gson = new Gson();
|
|
|
+ ResponseMessage result = gson.fromJson(responseString, ResponseMessage.class);
|
|
|
+ if(result == null){
|
|
|
+ String info = "解析响应数据Result失败";
|
|
|
+ LOGGER.error("--- {}", info);
|
|
|
+ throw new RuntimeException(info);
|
|
|
+ }
|
|
|
+ List rows = (List) result.getData().getRows().get(0);
|
|
|
+ // 要插入的异常订单集合
|
|
|
+ List<OrderComparedErrorEntity> insertList = new ArrayList<>();
|
|
|
+
|
|
|
+ // --------------------------- 开始对比支付单跟kmall中的订单 --------------------------
|
|
|
+
|
|
|
+ Map<String,OrderComparedErrorEntity> inconsistentMap = new HashMap<>();
|
|
|
+
|
|
|
+
|
|
|
+ for(PayOrderInfoEntity parOrderInfo : payOrderInfoEntities){
|
|
|
+
|
|
|
+ String merchOrderSn = parOrderInfo.getMerchOrderSn();
|
|
|
+ // 将订单号插入到表中
|
|
|
+ OrderComparedErrorEntity orderComparedErrorEntity = new OrderComparedErrorEntity();
|
|
|
+ orderComparedErrorEntity.setMerchOrderSn(merchOrderSn);
|
|
|
+ orderComparedErrorEntity.setOrderSource("OMS");
|
|
|
+ orderComparedErrorEntity.setReason("oms无订单");
|
|
|
+ inconsistentMap.put(merchOrderSn,orderComparedErrorEntity);
|
|
|
+
|
|
|
+ for(int i = 0 ; i < rows.size() ; i ++){
|
|
|
+ LinkedTreeMap<String,Object> treeMap = (LinkedTreeMap<String, Object>) rows.get(i);
|
|
|
+ String orderSn = (String) treeMap.get("merchOrderSn");
|
|
|
+ if(orderSn.equals(merchOrderSn)){
|
|
|
+ String orderStatus = (String) treeMap.get("orderStatus");
|
|
|
+ if("52".equals(orderStatus)){
|
|
|
+ orderStatus = "300";
|
|
|
}
|
|
|
- }
|
|
|
+ // 判断订单状态 不等于支付单状态
|
|
|
+ if(!orderStatus.equals(Integer.parseInt(parOrderInfo.getOrderStatus()))){
|
|
|
|
|
|
- parOrderInfo.setCompared("1");
|
|
|
- parOrderInfo.setComparedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ orderComparedErrorEntity.setOrderSource("OMS");
|
|
|
+ orderComparedErrorEntity.setReason("订单状态不一致");
|
|
|
+// inconsistentMap.put(orderSn,orderComparedErrorEntity);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 如果状态一直,然后map中有数据,则证明是当天退款的订单( 支付单:收费 退费 2条 订单 : 退 1条)
|
|
|
+ if(orderComparedErrorEntity != null){
|
|
|
+ inconsistentMap.remove(orderSn);
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
+ insertList = inconsistentMap.values().stream().collect(Collectors.toList());
|
|
|
+
|
|
|
+
|
|
|
+ parOrderInfo.setCompared("1");
|
|
|
+ parOrderInfo.setComparedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(insertList != null && insertList.size() > 0){
|
|
|
+ // 插入异常订单记录
|
|
|
+ orderComparedErrorDao.saveBatch(insertList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void comparedKmallOrder(List<PayOrderInfoEntity> payOrderInfoEntities) {
|
|
|
+ // 查询前一天的订单
|
|
|
+ List<OrderEntity> orderEntities = orderService.queryLastDayOrder();
|
|
|
+ // 要插入的异常订单集合
|
|
|
+ List<OrderComparedErrorEntity> insertList = new ArrayList<>();
|
|
|
|
|
|
- // 防止出现下了单当天退,支付宝会有两条数据,所以需要在删除一次
|
|
|
- errorOrderSn.removeAll(removeOrderSn);
|
|
|
+ // --------------------------- 开始对比支付单跟kmall中的订单 --------------------------
|
|
|
|
|
|
- List<OrderComparedErrorEntity> insertList = new ArrayList<>();
|
|
|
+ Map<String,OrderComparedErrorEntity> inconsistentMap = new HashMap<>();
|
|
|
|
|
|
- Iterator<String> iterator = errorOrderSn.iterator();
|
|
|
- while(iterator.hasNext()){
|
|
|
- String orderSn = iterator.next();
|
|
|
- // 将订单号插入到表中
|
|
|
- OrderComparedErrorEntity orderComparedErrorEntity = new OrderComparedErrorEntity();
|
|
|
- orderComparedErrorEntity.setMerchOrderSn(orderSn);
|
|
|
|
|
|
- insertList.add(orderComparedErrorEntity);
|
|
|
+ for(PayOrderInfoEntity parOrderInfo : payOrderInfoEntities){
|
|
|
+
|
|
|
+ String merchOrderSn = parOrderInfo.getMerchOrderSn();
|
|
|
+ // 将订单号插入到表中
|
|
|
+ OrderComparedErrorEntity orderComparedErrorEntity = new OrderComparedErrorEntity();
|
|
|
+ orderComparedErrorEntity.setMerchOrderSn(merchOrderSn);
|
|
|
+ orderComparedErrorEntity.setOrderSource("Kmall");
|
|
|
+ orderComparedErrorEntity.setReason("kmall无订单");
|
|
|
+ inconsistentMap.put(merchOrderSn,orderComparedErrorEntity);
|
|
|
+
|
|
|
+ for(OrderEntity order : orderEntities){
|
|
|
+ String orderSn = order.getOrderSn();
|
|
|
+ if(orderSn.equals(merchOrderSn)){
|
|
|
+
|
|
|
+ // 判断订单状态 不等于支付单状态
|
|
|
+ if(!order.getOrderStatus().equals(Integer.parseInt(parOrderInfo.getOrderStatus()))){
|
|
|
+
|
|
|
+ orderComparedErrorEntity.setOrderSource("Kmall");
|
|
|
+ orderComparedErrorEntity.setReason("订单状态不一致");
|
|
|
+// inconsistentMap.put(orderSn,orderComparedErrorEntity);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 如果状态一直,然后map中有数据,则证明是当天退款的订单( 支付单:收费 退费 2条 订单 : 退 1条)
|
|
|
+ if(orderComparedErrorEntity != null){
|
|
|
+ inconsistentMap.remove(orderSn);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
+ insertList = inconsistentMap.values().stream().collect(Collectors.toList());
|
|
|
|
|
|
+
|
|
|
+ parOrderInfo.setCompared("1");
|
|
|
+ parOrderInfo.setComparedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(insertList != null && insertList.size() > 0){
|
|
|
// 插入异常订单记录
|
|
|
orderComparedErrorDao.saveBatch(insertList);
|
|
|
- // 批量修改支付单对比状态跟对比时间
|
|
|
- payOrderInfoService.updateBatch(payOrderInfoEntities);
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return "对比失败,原因:"+e.getMessage();
|
|
|
}
|
|
|
|
|
|
- return "对比成功";
|
|
|
}
|
|
|
+
|
|
|
+ @RequestMapping("/orderReduce/{orderSn}")
|
|
|
+ public R orderReduce(@PathVariable("orderSn") String orderSn){
|
|
|
+
|
|
|
+
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
}
|