From 57f9a55581343a805cd589bbdc00e4d28a28aa97 Mon Sep 17 00:00:00 2001 From: zwl <1051256694@qq.com> Date: 星期一, 28 七月 2025 13:26:42 +0800 Subject: [PATCH] erp对接 --- src/main/java/com/zy/asrs/service/impl/CheckDetlServiceImpl.java | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 119 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/zy/asrs/service/impl/CheckDetlServiceImpl.java b/src/main/java/com/zy/asrs/service/impl/CheckDetlServiceImpl.java index b57ac9f..503bf9a 100644 --- a/src/main/java/com/zy/asrs/service/impl/CheckDetlServiceImpl.java +++ b/src/main/java/com/zy/asrs/service/impl/CheckDetlServiceImpl.java @@ -1,12 +1,131 @@ package com.zy.asrs.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.zy.asrs.entity.CheckDetl; +import com.zy.asrs.entity.OrderDetl; import com.zy.asrs.mapper.CheckDetlMapper; +import com.zy.asrs.service.ApiLogService; import com.zy.asrs.service.CheckDetlService; +import com.zy.common.utils.HttpHandler; +import javafx.print.Printer; +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.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Set; @Service +@Slf4j public class CheckDetlServiceImpl extends ServiceImpl<CheckDetlMapper, CheckDetl> implements CheckDetlService { + @Value("${erp.address.URL}") + //绔彛 + private String URL; + @Value("${erp.address.outaddressSave}") + private String uploadAddress; + @Autowired + private CheckDetlService checkDetlService; + @Autowired + private ApiLogService apiLogService; + + @Override + @Transactional + public void upload(List<CheckDetl> list) { + Date now = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + + // 鏋勫缓涓诲崟 JSON 鏁版嵁 + JSONObject requestBody = new JSONObject(); + if (!list.isEmpty()) { + CheckDetl first = list.get(0); + requestBody.put("orderNo", first.getOrderNo()); + } + // 鏋勫缓鐗╂枡鍒楄〃 + JSONArray matList = new JSONArray(); + + for(CheckDetl checkDetl : list) { + JSONObject mat = new JSONObject(); + mat.put("detailId", checkDetl.getDetailId()); + mat.put("matnr", checkDetl.getMatnr()); + mat.put("anfme", checkDetl.getAnfme()); + mat.put("number", checkDetl.getNumber()); + matList.add(mat); + } + requestBody.put("data", matList); + // 鍙戦�� POST 璇锋眰 + String response = ""; + boolean success = false; + try { + response = new HttpHandler.Builder() + .setUri(URL) + .setPath(uploadAddress) // 璁剧疆浣犵殑鎺ュ彛璺緞 + .setJson(requestBody.toJSONString()) + .build() + .doPost(); + + JSONObject data = JSON.parseObject(response); + Object isSuccess = findValueByKey(data, "IsSuccess"); + String bool = isSuccess != null ? isSuccess.toString() : "false"; + + if ("true".equals(bool)) { + success = true; + } + } catch (Exception e) { + log.error("涓婃姤ERP澶辫触", e); + } finally { + try { + apiLogService.save( + "鐩樼偣涓婃姤", + URL + uploadAddress, + null, + "127.0.0.1", + requestBody.toJSONString(), + response, + success + ); + } catch (Exception e) { + log.error("鏃ュ織淇濆瓨澶辫触", e); + } + } + if(success) { + for (CheckDetl entity : list){ + checkDetlService.delete(new EntityWrapper<>(entity)); + } + } + } + + 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; + } } \ No newline at end of file -- Gitblit v1.9.1