| | |
| | | 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.mapper.CheckDetlMapper; |
| | | import com.zy.asrs.service.ApiLogService; |
| | | import com.zy.asrs.service.CheckDetlService; |
| | | import com.zy.asrs.task.kingdee.handler.LoginAuthenticationHandler; |
| | | 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.outaddressSubmit}") |
| | | //上报出入库地址 |
| | | private String outaddressSubmit; |
| | | @Autowired |
| | | private CheckDetlService checkDetlService; |
| | | @Autowired |
| | | private ApiLogService apiLogService; |
| | | |
| | | @Autowired |
| | | private LoginAuthenticationHandler loginAuthenticationHandler; |
| | | |
| | | |
| | | @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(); |
| | | requestBody.put("createTime", sdf.format(now)); |
| | | // 构建物料列表 |
| | | JSONArray matList = new JSONArray(); |
| | | |
| | | for(CheckDetl checkDetl : list) { |
| | | JSONObject mat = new JSONObject(); |
| | | mat.put("detailId", checkDetl.getDetailId()); |
| | | mat.put("orderNo", checkDetl.getOrderNo()); |
| | | mat.put("matnr", checkDetl.getMatnr()); |
| | | mat.put("anfme", checkDetl.getAnfme()); |
| | | matList.add(mat); |
| | | } |
| | | requestBody.put("data", matList); |
| | | // 发送 POST 请求 |
| | | String response = ""; |
| | | boolean success = false; |
| | | try { |
| | | HashMap<String, Object> headers = new HashMap<>(); |
| | | // cookie |
| | | headers.put("Cookie", loginAuthenticationHandler.start().getContent()); |
| | | |
| | | response = new HttpHandler.Builder() |
| | | .setHeaders(headers) |
| | | .setUri(URL) |
| | | .setPath(outaddressSubmit) // 设置你的接口路径 |
| | | .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 + outaddressSubmit, |
| | | 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; |
| | | } |
| | | } |