Browse Source

定时同步发货单

wzy 10 months ago
parent
commit
3482ae5ea2

+ 8 - 0
mjava-kabeiyi/src/main/java/com/malk/kabeiyi/controller/KabeiyiController.java

@@ -8,6 +8,7 @@ import com.malk.utils.UtilDateTime;
 import com.malk.utils.UtilMap;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.web.bind.annotation.*;
 
 import java.time.LocalDateTime;
@@ -90,4 +91,11 @@ public class KabeiyiController {
     public McR ocr(String downloadUrl,String name,String type){
         return kabeiyiService.ocr(downloadUrl,name,type);
     }
+
+    //每天0点定时同步发货单信息
+    @Scheduled(cron = "0 0 0 * * ?")
+    @GetMapping("/getDeliveryInfo")
+    public McR getDeliveryInfo(){
+        return kabeiyiService.getDeliveryInfo();
+    }
 }

+ 2 - 0
mjava-kabeiyi/src/main/java/com/malk/kabeiyi/service/KabeiyiService.java

@@ -38,4 +38,6 @@ public interface KabeiyiService {
     McR ocr(String downloadUrl,String name,String type);
 
     McR addPlanApproval(Map map);
+
+    McR getDeliveryInfo();
 }

+ 163 - 8
mjava-kabeiyi/src/main/java/com/malk/kabeiyi/service/impl/KabeiyiServiceImpl.java

@@ -1,8 +1,15 @@
 package com.malk.kabeiyi.service.impl;
 
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.malk.core.McProject;
 import com.malk.kabeiyi.service.KabeiyiService;
 import com.malk.kabeiyi.util.RecognizeAllText;
@@ -17,17 +24,14 @@ import com.malk.service.aliwork.YDClient;
 import com.malk.service.aliwork.YDService;
 import com.malk.service.dingtalk.DDClient;
 import com.malk.service.dingtalk.DDClient_Workflow;
-import com.malk.utils.PublicUtil;
 import com.malk.utils.UtilHttp;
 import com.malk.utils.UtilToken;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateUtils;
-import org.apache.logging.log4j.util.Strings;
 import org.slf4j.MDC;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import java.io.FileOutputStream;
@@ -35,10 +39,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import java.net.URLEncoder;
 import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -76,6 +78,21 @@ public class KabeiyiServiceImpl implements KabeiyiService {
     @Value("${ocr.filePath}")
     private String filePath;
 
+    @Value("${u8.fromAccount}")
+    private String fromAccount;
+
+    @Value("${u8.toAccount}")
+    private String toAccount;
+
+    @Value("${u8.appKey}")
+    private String u8AppKey;
+
+    @Value("${u8.appSecret}")
+    private String u8AppSecret;
+
+    //u8数据源库序号
+    private static final int[] DS_SEQUENCES = {2,3,5,8,9,10,12,14};
+
     //审批实例url前缀(卡倍亿)
     private static final String PROC_INST_PREFIX_URL = "https://aflow.dingtalk.com/dingtalk/web/query/pchomepage.htm?from=oflow&op=true&corpid=dingc3a744cb591a7346f2c783f7214b6d69#/plainapproval?procInstId=";
     //宜搭-关联数据(卡倍亿)
@@ -530,6 +547,145 @@ public class KabeiyiServiceImpl implements KabeiyiService {
         return McR.success();
     }
 
+    @Override
+    public McR getDeliveryInfo() {
+        MDC.put("MDC_KEY_PID","1009");
+
+        for (int dsSequence : DS_SEQUENCES) {
+            //获取昨天所有发货单
+            DateTime dateTime = DateUtil.offsetDay(new Date(), -1);
+            String yesterday = dateTime.toString("yyyy-MM-dd");
+//            String yesterday = dateTime.toString("2024-07-01");
+
+            Map<String,Object> param = new HashMap<>();
+            param.put("from_account",fromAccount);
+            param.put("to_account",toAccount);
+            param.put("app_key", u8AppKey);
+            param.put("token", getU8Token());
+            param.put("page_index", 1);
+            param.put("rows_per_page", 50);
+            param.put("ds_sequence", dsSequence);
+            param.put("date_begin", yesterday);
+            param.put("date_end", yesterday);
+
+            Map<String, Object> map = get("https://api.yonyouup.com/api/consignmentlist/batch_get", param, null);
+            if (!map.get("page_count").equals("1")){
+                Integer rowCount = Integer.valueOf(map.get("row_count").toString());
+                param.put("rows_per_page",rowCount);
+                map = get("https://api.yonyouup.com/api/consignmentlist/batch_get", param, null);
+            }
+            List<Map> consignmentList = (List<Map>) map.get("consignmentlist");
+            for (Map consignment : consignmentList) {
+                //查询单个发货单详情
+                String id = getString(consignment.get("code"));
+
+                Map<String, Object> param2 = new HashMap<>();
+                param2.put("from_account", fromAccount);
+                param2.put("to_account", fromAccount);
+                param2.put("app_key", u8AppKey);
+                param2.put("token", getU8Token());
+                param2.put("id", id);
+                param2.put("ds_sequence", dsSequence);
+
+                Map<String, Object> map2 = get("https://api.yonyouup.com/api/consignment/get", param2, null);
+
+                Map consignment2 = (Map) map2.get("consignment");
+
+                Map formData = new HashMap();
+                formData.put("textField_m18qfuiz",getString(consignment2.get("operation_type")));//操作类型
+                formData.put("textField_m18qfuj0",getString(consignment2.get("saletypename")));//销售类型
+                formData.put("textField_m18nvp87",getString(consignment2.get("code")));//发货单code
+                formData.put("dateField_m18nvp7x",DateUtil.parseDate(yesterday).getTime());//发货日期
+                formData.put("textField_m18nvp7y",getString(consignment2.get("cusname")));//客户名称
+                formData.put("textField_m18nvp7z",getString(consignment2.get("deptname")));//部门
+                formData.put("textField_m18nvp81",getString(consignment2.get("maker")));//生产员
+                formData.put("textField_m18nvp80",getString(consignment2.get("verifier")));//生产员
+                formData.put("numberField_m18wkih7",dsSequence);//生产员
+
+                List<Map> entry = (List<Map>) consignment2.get("entry");
+                List<Map> collect = entry.stream().map(item -> {
+                    Map detail = new HashMap();
+                    detail.put("textField_m18nvp8c", getString(item.get("socode")));//销售订单号
+                    detail.put("textField_m18nvp8d", getString(item.get("warehouse_name")));//仓库名称
+                    detail.put("textField_m18nvp8e", getString(item.get("inventory_name")));//库存名称
+                    detail.put("textField_m18nvp8f", getString(item.get("cinvm_unit")));//单位
+                    detail.put("numberField_m18nvp8h", getString(item.get("quantity")));//发货长度
+                    detail.put("numberField_m18nvp8j", getString(item.get("price")));//未税单价
+                    detail.put("numberField_m18nvp8l", getString(item.get("money")));//未税总价
+                    detail.put("numberField_m18nvp8k", getString(item.get("taxprice")));//含税单价
+                    detail.put("numberField_m18nvp8m", getString(item.get("sum")));//含税总价
+                    detail.put("numberField_m18nvp8n", getString(item.get("taxrate")));//税率
+                    detail.put("numberField_m18nvp8o", getString(item.get("tax")));//总税价
+                    detail.put("dateField_m18yzlob", DateUtil.parseDate(yesterday).getTime());//发货日期
+                    detail.put("textField_m18z3yn8", getString(consignment2.get("cusname")));//客户名称
+
+                    return detail;
+                }).collect(Collectors.toList());
+
+                formData.put("tableField_m18nvp82",collect);//发货明细
+
+                ydClient.operateData(YDParam.builder()
+                        .formUuid("FORM-BE542784AAC04206BD46AF3D9CD1F12DYSKF")
+                        .formDataJson(JSON.toJSONString(formData))
+                        .build(), YDConf.FORM_OPERATION.create);
+            }
+        }
+
+        return McR.success();
+    }
+
+    /**
+     * 封装GET请求
+     *
+     * @param url    请求的URL
+     * @param params 请求参数
+     * @param headers 请求头
+     * @return 响应内容
+     */
+    public static Map<String, Object> get(String url, Map<String, Object> params, Map<String, String> headers) {
+        log.info("GET 请求url:{}, 请求参数:{}, 请求头:{}", url, params, headers);
+        HttpRequest get = HttpUtil.createGet(url);
+        if (params!= null){
+            get.form(params);
+        }
+
+        if (headers!= null){
+            get.addHeaders(headers);
+        }
+        HttpResponse response = get.execute();
+
+        Map<String, Object> result = null;
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            result = objectMapper.readValue(response.body(), new TypeReference<Map<String, Object>>() {});
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+
+        log.info("响应结果: {}", result);
+        return result;
+    }
+
+    private String getU8Token(){
+        String accessToken = UtilToken.get("u8-token");
+        if (StringUtils.isNotBlank(accessToken)) {
+            return accessToken;
+        } else {
+            Map<String,Object> param = new HashMap<>();
+            param.put("from_account", fromAccount);
+            param.put("app_key", u8AppKey);
+            param.put("app_secret", u8AppSecret);
+
+            Map<String, Object> result = get("https://api.yonyouup.com/system/token", param, null);
+            Map token = (Map) result.get("token");
+            String id = token.get("id").toString();
+            log.info("u响应token, {}", id);
+            UtilToken.put("u8-token", id, (int) token.get("expiresIn") * 1000L);
+
+            return id;
+        }
+    }
+
 
     private String getString(Object obj){
         return obj == null ? "" : obj.toString();
@@ -549,6 +705,5 @@ public class KabeiyiServiceImpl implements KabeiyiService {
             UtilToken.put("tc-invalid-token-dingtalk", accessToken, (long)r.getExpiresIn() * 1000L);
             return accessToken;
         }
-
     }
 }

+ 7 - 0
mjava-kabeiyi/src/main/resources/application-dev.yml

@@ -63,3 +63,10 @@ aliwork:
 
 ocr:
   filePath: d:\
+
+u8:
+  fromAccount: kbeapi
+  toAccount: kbeapi
+  appKey : opa52c74dd3e0563458
+  appSecret : 5b96fad7470a489bbae369ec79406fb1
+

+ 7 - 1
mjava-kabeiyi/src/main/resources/application-prod.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8115
+  port: 8113
   servlet:
     context-path: /kabeiyi
 
@@ -63,3 +63,9 @@ aliwork:
 
 ocr:
   filePath: /home/server/kabeiyi/file/
+
+u8:
+  fromAccount: kbeapi
+  toAccount: kbeapi
+  appKey : opa52c74dd3e0563458
+  appSecret : 5b96fad7470a489bbae369ec79406fb1

+ 1 - 1
mjava-kabeiyi/src/main/resources/application.yml

@@ -1,6 +1,6 @@
 spring:
   profiles:
-    active: prod
+    active: dev
   servlet:
     multipart:
       max-file-size: 100MB