| package com.zy.asrs.task.kingdee.handler; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.alibaba.fastjson.JSONArray; | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
| import com.zy.asrs.entity.DocType; | 
| import com.zy.asrs.entity.Order; | 
| import com.zy.asrs.service.ApiLogService; | 
| import com.zy.asrs.service.DocTypeService; | 
| import com.zy.asrs.service.OrderService; | 
| import com.zy.asrs.task.AbstractHandler; | 
| import com.zy.asrs.task.core.ReturnT; | 
| import com.zy.asrs.utils.OrderInAndOutUtil; | 
| import com.zy.common.utils.HttpHandler; | 
| import com.zy.erp.kingdee.enums.KingDeeUtilType; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.util.HashMap; | 
| import java.util.Set; | 
|   | 
| /** | 
|  * Created by Monkey D. Luffy on 2023.10.21 | 
|  */ | 
| @Slf4j | 
| @Service | 
| public class SaveOrderSyncHandler extends AbstractHandler<String> { | 
|     @Value("${erp.address.URL}") | 
|     //端口 | 
|     private String URL; | 
|   | 
|     @Value("${erp.address.outaddressSave}") | 
|     //审核地址 | 
|     private String outaddressSave; | 
|   | 
|     @Autowired | 
|     private OrderService orderService; | 
|     @Autowired | 
|     private ApiLogService apiLogService; | 
|     @Autowired | 
|     private DocTypeService docTypeService; | 
|     @Autowired | 
|     private LoginAuthenticationHandler loginAuthenticationHandler; | 
|   | 
|     @Transactional | 
|     public ReturnT<String> start(Order order) { | 
|         //登录金蝶r | 
|         ReturnT<String> start = loginAuthenticationHandler.start(); | 
|         DocType docType = docTypeService.selectById(order.getDocType()); | 
|         if (null == docType) { | 
|             return SUCCESS; | 
|         } | 
|         KingDeeUtilType kingDeeUtilType = KingDeeUtilType.get(docType.getDocName()); | 
|   | 
|         //条件拼接 | 
|         JSONObject jsonObject = new JSONObject(); | 
|         jsonObject.put("Numbers",order.getOrderNo()); | 
|         JSONObject jsonObject1 = new JSONObject(); | 
|         jsonObject1.put("data", jsonObject); | 
|         jsonObject1.put("FormId",kingDeeUtilType.correspondingFormId); | 
|         String add = jsonObject1.toJSONString(); | 
|         //上报 | 
|         String response = ""; | 
|         boolean success = false; | 
|         try { | 
|             //获取Cookie值 | 
|             HashMap<String, Object> headers = new HashMap<>(); | 
|             headers.put("Cookie", start.getContent()); | 
|             response = new HttpHandler.Builder() | 
|                     .setHeaders(headers) | 
|                     .setUri(URL) | 
|                     .setPath(outaddressSave) | 
|                     .setJson(add) | 
|                     .build() | 
|                     .doPost(); | 
|             JSONObject data = JSON.parseObject(response); | 
|             Object IsSuccess = findValueByKey(JSON.parseObject(response), "IsSuccess"); | 
|             String bool=IsSuccess.toString(); | 
|             //保存完成原订单8转6 | 
|             if(bool.equals("true")){ | 
|                 success = true; | 
|                 order.setSettle(6L); | 
| //                orderService.update(order,new EntityWrapper<Order>().eq("order_no",order.getOrderNo())); | 
|                 OrderInAndOutUtil.updateOrder(order.getPakinPakoutStatus$(),order.getId(), 6L, null); | 
|   | 
|             } | 
|         } catch (Exception e) { | 
|             log.error("fail", e); | 
| //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); | 
|             return FAIL.setMsg(e.getMessage()); | 
|         } finally { | 
|             try { | 
|                 // 保存接口日志 | 
|                 apiLogService.save( | 
|                         "保存", | 
|                         URL+ outaddressSave, | 
|                         null, | 
|                         "127.0.0.1", | 
|                         add, | 
|                         response, | 
|                         success | 
|                 ); | 
|             } catch (Exception e) { | 
|                 log.error("", e); | 
|             } | 
|         } | 
|         return SUCCESS; | 
|     } | 
|     public static Object findValueByKey(JSONObject json, String key) { | 
|         Set<String> keySet = json.keySet(); | 
|         for (String k : keySet) { | 
|             Object v = json.get(k); | 
|             if (k.equals(key)) { | 
|                 return v; | 
|             } else if (v instanceof JSONArray) { | 
|                 int size = ((JSONArray) v).size(); | 
|                 for (int i = 0; i <= size - 1; i++) { | 
|                     Object result = findValueByKey((JSONObject) ((JSONArray) v).get(i), key); | 
|                     if (result != null){ | 
|                         return result; | 
|                     } | 
|                 } | 
|             } else if (v instanceof JSONObject){ | 
|                 Object result = findValueByKey((JSONObject) v, key); | 
|                 if (result != null){ | 
|                     return result; | 
|                 } | 
|             } | 
|         } | 
|         return null; | 
|     } | 
| } |