|
@@ -2,10 +2,14 @@ package com.malk.lingmingguangzi.service;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
+
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.auth0.jwt.JWT;
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
|
@@ -20,16 +24,31 @@ import com.malk.server.dingtalk.DDR_New;
|
|
|
import com.malk.server.teambition.TBConf;
|
|
|
import com.malk.server.teambition.TBR;
|
|
|
import com.malk.service.aliwork.YDClient;
|
|
|
+import com.malk.service.dingtalk.DDClient;
|
|
|
import com.malk.utils.UtilHttp;
|
|
|
import com.malk.utils.UtilMap;
|
|
|
import com.malk.utils.UtilToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -44,6 +63,9 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
@Autowired
|
|
|
private TBConf tbConf;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DDClient ddClient;
|
|
|
+
|
|
|
@Value(value = "${erp.url}")
|
|
|
private String erpUrl;
|
|
|
|
|
@@ -53,6 +75,9 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
@Value(value = "${erp.password}")
|
|
|
private String password;
|
|
|
|
|
|
+ @Value(value = "${erp.filePath}")
|
|
|
+ private String filePath;
|
|
|
+
|
|
|
private final Object $lock = new Object[0];
|
|
|
|
|
|
private static final Long EXPIRES_IN = 7200000L;
|
|
@@ -415,8 +440,33 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
detail.put("die_model","");//
|
|
|
return detail;
|
|
|
}).collect(Collectors.toList());
|
|
|
+ //附件
|
|
|
+ List<Map> list2 = (List<Map>) formData.get("tableField_ly8fxusx");
|
|
|
+ List<Map> makSoFilesList = list2.stream().map(map1 -> {
|
|
|
+ Map detail = new HashMap();
|
|
|
+ String fileType = getString(map1.get("selectField_m0ls7yb8"));
|
|
|
+ if (fileType.equals("合同")){
|
|
|
+ detail.put("fileType","D1_CONTRACT");
|
|
|
+ }else {
|
|
|
+ detail.put("fileType","D2_TECHNICAL_DOC");
|
|
|
+ }
|
|
|
+ List<Map> fileList = (List<Map>) JSONArray.parse(String.valueOf(map1.get("attachmentField_ly8fxusz")));
|
|
|
+ String ydDownloadUrl = fileList.get(0).get("downloadUrl").toString();
|
|
|
+ String fileName = fileList.get(0).get("name").toString();
|
|
|
+ //将downloadurl下载到本地
|
|
|
+ try {
|
|
|
+ downloadFile(ydDownloadUrl, filePath + fileName);
|
|
|
+ System.out.println("文件下载成功!");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //上传附件
|
|
|
+ detail.put("files",postWithFile(erpUrl + "pws/sys/common/upload",filePath + fileName));
|
|
|
+ return detail;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
body.put("makSoDetailList",makSoDetailList);
|
|
|
- body.put("makSoFilesList",new ArrayList<>());
|
|
|
+ body.put("makSoFilesList",makSoFilesList);
|
|
|
|
|
|
Map res = post(erpUrl + "pws/mak/makSalesOrder/add", null, getHeader(), body);
|
|
|
String orderCode = getString(res.get("result"));
|
|
@@ -429,6 +479,39 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
return McR.success();
|
|
|
}
|
|
|
|
|
|
+ public void downloadFile(String fileURL, String savePath) throws IOException {
|
|
|
+ //若是宜搭附件url 则需获得附件临时免登地址
|
|
|
+ Map<String,Object> param = new HashMap<>();
|
|
|
+ param.put("systemToken",ydConf.getSystemToken());
|
|
|
+ param.put("userId","344749020127590108");//wzy
|
|
|
+ param.put("fileUrl",fileURL);
|
|
|
+
|
|
|
+ fileURL = ((DDR_New) UtilHttp.doGet("https://api.dingtalk.com/v1.0/yida/apps/temporaryUrls/" + ydConf.getAppType(), ddClient.initTokenHeader(), param, DDR_New.class)).getResult().toString();
|
|
|
+
|
|
|
+ URL url = new URL(fileURL);
|
|
|
+ HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
|
|
|
+ int responseCode = httpConn.getResponseCode();
|
|
|
+
|
|
|
+ // 检查HTTP响应代码是否为200
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ InputStream inputStream = httpConn.getInputStream();
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(savePath);
|
|
|
+
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int bytesRead = -1;
|
|
|
+
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+
|
|
|
+ outputStream.close();
|
|
|
+ inputStream.close();
|
|
|
+ } else {
|
|
|
+ System.out.println("无法下载文件。HTTP响应代码: " + responseCode);
|
|
|
+ }
|
|
|
+ httpConn.disconnect();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public McR updateSaleOrder(Map map) {
|
|
|
if (Objects.isNull(map.get("formInstId"))){
|
|
@@ -438,8 +521,9 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
Map formData = ydClient.queryData(YDParam.builder().formInstId(formInstId)
|
|
|
.build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
|
|
|
|
|
|
- //新增销售订单
|
|
|
+ //更新销售订单
|
|
|
Map body = new HashMap();
|
|
|
+ body.put("soCode",getString(formData.get("textField_m0alsjj1")));
|
|
|
body.put("customerCode",getString(formData.get("textField_ly5m36nw")));//客户编码
|
|
|
String priority = getString(formData.get("selectField_lwpsp162"));//紧急级别
|
|
|
switch (priority) {
|
|
@@ -480,10 +564,35 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
detail.put("die_model","");//
|
|
|
return detail;
|
|
|
}).collect(Collectors.toList());
|
|
|
+ //附件
|
|
|
+ List<Map> list2 = (List<Map>) formData.get("tableField_ly8fxusx");
|
|
|
+ List<Map> makSoFilesList = list2.stream().map(map1 -> {
|
|
|
+ Map detail = new HashMap();
|
|
|
+ String fileType = getString(map1.get("selectField_m0ls7yb8"));
|
|
|
+ if (fileType.equals("合同")){
|
|
|
+ detail.put("fileType","D1_CONTRACT");
|
|
|
+ }else {
|
|
|
+ detail.put("fileType","D2_TECHNICAL_DOC");
|
|
|
+ }
|
|
|
+ List<Map> fileList = (List<Map>) JSONArray.parse(String.valueOf(map1.get("attachmentField_ly8fxusz")));
|
|
|
+ String ydDownloadUrl = fileList.get(0).get("downloadUrl").toString();
|
|
|
+ String fileName = fileList.get(0).get("name").toString();
|
|
|
+ //将downloadurl下载到本地
|
|
|
+ try {
|
|
|
+ downloadFile(ydDownloadUrl, filePath + fileName);
|
|
|
+ System.out.println("文件下载成功!");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //上传附件
|
|
|
+ detail.put("files",postWithFile(erpUrl + "pws/sys/common/upload",filePath + fileName));
|
|
|
+ return detail;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
body.put("makSoDetailList",makSoDetailList);
|
|
|
- body.put("makSoFilesList",new ArrayList<>());
|
|
|
+ body.put("makSoFilesList",makSoFilesList);
|
|
|
|
|
|
- Map res = post(erpUrl + "pws/mak/makSalesOrder/add", null, getHeader(), body);
|
|
|
+ Map res = put(erpUrl + "pws/mak/makSalesOrder/change", null, getHeader(), body);
|
|
|
|
|
|
return McR.success();
|
|
|
}
|
|
@@ -597,6 +706,53 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ public String postWithFile(String url, String filePath) {
|
|
|
+ // 文件路径
|
|
|
+ File file = new File(filePath);
|
|
|
+
|
|
|
+ // 创建HttpClient实例
|
|
|
+ HttpClient client = HttpClients.createDefault();
|
|
|
+
|
|
|
+ // 创建HttpPost实例
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
+
|
|
|
+ // 添加请求头
|
|
|
+ post.setHeader("X-Access-Token", getHeader().get("X-Access-Token"));
|
|
|
+
|
|
|
+ // 使用MultipartEntityBuilder来构建multipart请求
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
+ builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName());
|
|
|
+
|
|
|
+ // 设置请求体
|
|
|
+ HttpEntity multipart = builder.build();
|
|
|
+ post.setEntity(multipart);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送请求并获取响应
|
|
|
+ org.apache.http.HttpResponse response = client.execute(post);
|
|
|
+
|
|
|
+ // 处理响应
|
|
|
+ // 例如,打印响应状态码
|
|
|
+ System.out.println(response.getStatusLine().getStatusCode());
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String fileName = "";
|
|
|
+ // 判断响应实体是否为null
|
|
|
+ if (entity != null) {
|
|
|
+ // 使用EntityUtils读取响应体内容(这里假设是文本类型)
|
|
|
+ String responseString = EntityUtils.toString(entity, "UTF-8"); // 指定字符集
|
|
|
+ System.out.println("Response Body: " + responseString);
|
|
|
+ Map parse = (Map) JSONObject.parse(responseString);
|
|
|
+ fileName = getString(parse.get("message"));
|
|
|
+ // 释放响应实体资源(虽然EntityUtils.toString()在某些HttpClient版本中会自动处理)
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ }
|
|
|
+ return fileName;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public McR getTbStageDetails(String stageName) {
|
|
|
Map<String, Object> result = new HashMap<>();
|