Browse Source

【华商龙-华勤】

DINGYL 1 week ago
parent
commit
35d01074a9

+ 56 - 0
src/main/java/com/uas/eis/config/HQConfig.java

@@ -0,0 +1,56 @@
+package com.uas.eis.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+
+@Configuration
+@PropertySource("classpath:hq.properties")
+public class HQConfig {
+    private String ev;
+    @Value("${prod.url}")
+    private String apiUrl;
+    @Value("${prod.userName}")
+    private String userName;
+    @Value("${prod.password}")
+    private String pwd;
+
+    @Value("${test.url}")
+    private String apiUrlTest;
+    @Value("${test.userName}")
+    private String userNameTest;
+    @Value("${test.password}")
+    private String pwdTest;
+
+
+    public String getEv() {
+        return ev;
+    }
+
+    public void setEv(String ev) {
+        this.ev = ev;
+    }
+    public String getApiUrl() {
+        if("test".equals(getEv())){
+            return apiUrlTest;
+        }else {
+            return apiUrl;
+        }
+    }
+
+    public String getUserName() {
+        if("test".equals(getEv())){
+            return userNameTest;
+        }else {
+            return userName;
+        }
+    }
+
+    public String getPwd() {
+        if("test".equals(getEv())){
+            return pwdTest;
+        }else {
+            return pwd;
+        }
+    }
+}

+ 19 - 0
src/main/java/com/uas/eis/controller/HQController.java

@@ -0,0 +1,19 @@
+package com.uas.eis.controller;
+
+
+import com.uas.eis.sdk.entity.ApiResult;
+import com.uas.eis.service.HQService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class HQController {
+    @Autowired
+    private HQService hqService;
+    @RequestMapping("/erp/syncHQPOPlan")
+    public ApiResult<String> syncHQPOPlan(String master, Integer id, String emCode, String caller){
+        return hqService.syncHQPOPlan(master, id,emCode,caller);
+    }
+
+}

+ 24 - 0
src/main/java/com/uas/eis/entity/HQ/HQPODadaList.java

@@ -0,0 +1,24 @@
+package com.uas.eis.entity.HQ;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class HQPODadaList {
+    @JSONField(name = "OU_NAME")
+    private String ou_name ;//华勤通讯香港有限公司",
+    @JSONField(name = "PO_NUMBER")
+    private String po_number;//": "110306037",
+    @JSONField(name = "PO_LINE")
+    private String po_line;//": "1.1",
+    @JSONField(name = "HQ_PN")
+    private String hq_pn;//": "HQ11430351000",
+    @JSONField(name = "ORDERED_QUANTITY")
+    private Double ordered_quantity;//": 12000,
+    @JSONField(name = "REQUIRE_DATE")
+    private String require_date;//": "2025/08/01",
+    @JSONField(name = "ORIGIN_MANUFACTURER_PO_INFO")
+    private List<HQPOInfo> origin_manufacturer_po_info;//
+}

+ 22 - 0
src/main/java/com/uas/eis/entity/HQ/HQPOInfo.java

@@ -0,0 +1,22 @@
+package com.uas.eis.entity.HQ;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import lombok.Data;
+
+@Data
+public class HQPOInfo {
+    @JSONField(name = "ORIGIN_PO_NO")
+    private String origin_po_no;//": "STOCK",
+    @JSONField(name = "ORIGIN_PO_LINE")
+    private String origin_po_line;//": "1",
+    @JSONField(name = "MANUFACTURER_PN")
+    private String manufacturer_pn;//": "BF1109-A2R4NCCT/LF",
+    @JSONField(name = "HQ_PN")
+    private String hq_pn;//": "HQ11430351000",
+    @JSONField(name = "SUPPLIER_ORDER_DATE")
+    private String supplier_order_date;//": "2999/12/31",
+    @JSONField(name = "QUANTITY")
+    private Double quantity;//": 12000,
+    @JSONField(name = "ESTIMATE_SHIP_DATE")
+    private String estimate_ship_date;//": "2025/08/01"
+}

+ 16 - 0
src/main/java/com/uas/eis/entity/HQ/HQPOReq.java

@@ -0,0 +1,16 @@
+package com.uas.eis.entity.HQ;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class HQPOReq {
+    @JSONField(name = "VENDOR_NAME")
+    private String vendor_name;
+    @JSONField(name = "VENDOR_CODE")
+    private String vendor_code;
+    @JSONField(name = "PODATALIST")
+    private List<HQPODadaList> podatalist;
+}

+ 35 - 0
src/main/java/com/uas/eis/entity/HQ/HQPOResp.java

@@ -0,0 +1,35 @@
+package com.uas.eis.entity.HQ;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import lombok.Data;
+
+@Data
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class HQPOResp {
+    private String msg;
+    private HQPORespHeader header;
+    @Data
+    @JsonIgnoreProperties(ignoreUnknown = true)
+    public static class HQPORespHeader {
+        private String code;//-- 非200为请求失败
+        private String desc;//"success" -- code非200时存放错误信息
+    }
+
+    public String getCode() {
+        if(header!=null){
+            return header.getCode();
+        }
+        return "500";
+    }
+
+    public String getErrTip() {
+        if(msg!=null){
+            return msg;
+        }else {
+            if(header!=null&&header.getDesc()!=null){
+                return header.getDesc();
+            }
+        }
+        return "";
+    }
+}

+ 7 - 0
src/main/java/com/uas/eis/service/HQService.java

@@ -0,0 +1,7 @@
+package com.uas.eis.service;
+
+import com.uas.eis.sdk.entity.ApiResult;
+
+public interface HQService {
+    ApiResult<String> syncHQPOPlan(String master, Integer id, String emCode, String caller);
+}

+ 126 - 0
src/main/java/com/uas/eis/service/Impl/HQServiceImpl.java

@@ -0,0 +1,126 @@
+package com.uas.eis.service.Impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.uas.eis.config.HQConfig;
+import com.uas.eis.core.support.TokenProperties;
+import com.uas.eis.dao.BaseDao;
+import com.uas.eis.dao.SqlRowList;
+import com.uas.eis.entity.HQ.HQPODadaList;
+import com.uas.eis.entity.HQ.HQPOInfo;
+import com.uas.eis.entity.HQ.HQPOReq;
+import com.uas.eis.entity.HQ.HQPOResp;
+import com.uas.eis.sdk.entity.ApiResult;
+import com.uas.eis.sdk.resp.ApiResponse;
+import com.uas.eis.service.HQService;
+import com.uas.eis.utils.BaseUtil;
+import com.uas.eis.utils.DateUtil;
+import com.uas.eis.utils.HttpUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class HQServiceImpl implements HQService {
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    @Autowired
+    private BaseDao baseDao;
+
+    @Autowired
+    private HQConfig hqConfig;
+
+    @Override
+    public ApiResult<String> syncHQPOPlan(String master, Integer id, String emCode, String caller) {
+        logger.info("syncHQPOPlan-Begin:master {} id {}",master,id);
+        hqConfig.setEv(getMasterEv(master));
+        // 执行查询
+        SqlRowList docMsg = baseDao.queryForRowSet(" select vp_statuscode from "+master+".vendPurExecplan where vp_id  = "+id);
+        if(!docMsg.next()){
+            baseDao.execute("update "+master+".VENDPUREXECPLAN set vp_uploadstatus='上传失败',vp_uploaderr='上传单据不存在。' where vp_id = "+id);
+            logger.info("syncHQPOPlan-End:master {} id {} err {}",master,id,"上传单据不存在");
+            return ApiResponse.failRsp("101","上传单据不存在");
+        }
+        if(!docMsg.getGeneralString("vp_statuscode").equals("AUDITED")){
+            baseDao.execute("update "+master+".VENDPUREXECPLAN set vp_uploadstatus='上传失败',vp_uploaderr='请先审核再上传。' where vp_id = "+id);
+            logger.info("syncHQPOPlan-End:master {} id {} err {}",master,id,"请先审核再上传");
+            return ApiResponse.failRsp("101","请先审核再上传");
+        }
+        SqlRowList docList = baseDao.queryForRowSet(" select * from "+master+".VENDPUREXECPLANDETAIL where VD_VPID  = "+id);
+        if(docList.next()){
+            HQPOReq hqVendPurExecplanReq = new HQPOReq();
+            hqVendPurExecplanReq.setVendor_code(docList.getGeneralString("vd_vendor_code"));
+            hqVendPurExecplanReq.setVendor_name(docList.getGeneralString("vd_vendor_name"));
+            List<HQPODadaList> podatalist = new ArrayList<>();
+            List<Map<String, Object>> dataList = docList.getResultList();
+            Map<String, List<Map<String, Object>>> POGroup = BaseUtil.groupsMap(dataList, new Object[] { "vd_po_number", "vd_po_line"});
+            List<Map<String, Object>> items;
+            for (Object m : POGroup.keySet()) {
+                items = POGroup.get(m);
+                Map<String, Object> poMap = items.get(0);
+                String hq_pn = poMap.get("vd_hq_pn").toString();
+                HQPODadaList hqpoDadaList = new HQPODadaList();
+                hqpoDadaList.setOu_name(poMap.get("vd_ou_name").toString());
+                hqpoDadaList.setPo_number(poMap.get("vd_po_number").toString());
+                hqpoDadaList.setPo_line(poMap.get("vd_po_line").toString());
+                hqpoDadaList.setHq_pn(hq_pn);
+                hqpoDadaList.setOrdered_quantity(Double.valueOf(poMap.get("vd_ordered_quantity").toString()));
+                hqpoDadaList.setRequire_date(DateUtil.format((Date)poMap.get("vd_require_date"),"yyyy/MM/dd"));
+                List<HQPOInfo> poInfos = new ArrayList<>();
+                for (Map<String, Object> poInfo : items){
+                    HQPOInfo hqpoInfo = new HQPOInfo();
+                    hqpoInfo.setOrigin_po_no(poInfo.get("vd_origin_po_no").toString());
+                    hqpoInfo.setOrigin_po_line(poInfo.get("vd_origin_po_line").toString());
+                    hqpoInfo.setManufacturer_pn(poInfo.get("vd_manufacturer_pn").toString());
+                    hqpoInfo.setHq_pn(hq_pn);
+                    hqpoInfo.setSupplier_order_date(DateUtil.format((Date)poInfo.get("vd_supplier_order_date"),"yyyy/MM/dd"));
+                    hqpoInfo.setQuantity(Double.valueOf(poInfo.get("vd_quantity").toString()));
+                    hqpoInfo.setEstimate_ship_date(DateUtil.format((Date)poInfo.get("vd_estimate_ship_date"),"yyyy/MM/dd"));
+                    poInfos.add(hqpoInfo);
+                }
+                hqpoDadaList.setOrigin_manufacturer_po_info(poInfos);
+                podatalist.add(hqpoDadaList);
+            }
+            hqVendPurExecplanReq.setPodatalist(podatalist);
+            String jsonBody = JSON.toJSONString(hqVendPurExecplanReq,
+                    SerializerFeature.WriteMapNullValue).replace("null","\"\"");
+            System.out.println("jsonBody: "+jsonBody);
+            try {
+                String url = hqConfig.getApiUrl();
+                String userName = hqConfig.getUserName();
+                String pwd = hqConfig.getPwd();
+                HttpUtil.Response response = HttpUtil.sendPostWithBasicAuthRequest(url,userName,pwd, jsonBody);
+                logger.info("syncHQPOPlan-End:master {} id {} tip {}",master,id,"("+response.getStatusCode()+")"+response.getResponseText());
+                HQPOResp hqpoResp = new ObjectMapper().readValue(response.getResponseText(), HQPOResp.class);
+                if(hqpoResp.getCode().equals("200")){
+                    baseDao.execute("update "+master+".VENDPUREXECPLAN set vp_uploadtime=sysdate,vp_uploadstatus='上传成功',vp_uploaderr='' where vp_id = "+id);
+                    return ApiResponse.successRsp("200","上传成功");
+                }else {
+                    baseDao.execute("update "+master+".VENDPUREXECPLAN set vp_uploadstatus='上传失败',vp_uploaderr='"+hqpoResp.getCode()+": "+hqpoResp.getErrTip()+"' where vp_id = "+id);
+                    return ApiResponse.failRsp(hqpoResp.getCode(),hqpoResp.getErrTip());
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }else {
+            baseDao.execute("update "+master+".VENDPUREXECPLAN set vp_uploadstatus='上传失败',vp_uploaderr='请先审核再上传。' where vp_id = "+id);
+            logger.info("syncHQPOPlan-End:master {} id {} err {}",master,id,"请先审核再上传");
+            return ApiResponse.failRsp("101","请先审核再上传");
+        }
+
+        return null;
+    }
+
+
+    private String getMasterEv(String master){
+        return baseDao.queryForObject("select max(DISPLAY) from "+master+".configs LEFT JOIN "+master+".CONFIGPROPS ON CONFIGPROPS.CONFIG_ID = configs.ID AND configs.DATA =  CONFIGPROPS.VALUE " +
+                "where configs.CODE='DocEv'",String.class);
+    }
+}

+ 37 - 0
src/main/java/com/uas/eis/task/HQPODataUploadTask.java

@@ -0,0 +1,37 @@
+package com.uas.eis.task;
+
+import com.uas.eis.dao.BaseDao;
+import com.uas.eis.sdk.entity.ApiResult;
+import com.uas.eis.service.ERPService;
+import com.uas.eis.service.HQService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Component
+@EnableAsync
+@EnableScheduling
+public class HQPODataUploadTask {
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+    @Autowired
+    private BaseDao baseDao;
+    @Autowired
+    private HQService hqService;
+    @Scheduled(cron = "0 0 0 * * ?")
+    public void uploadHQPOPlan() {
+        logger.info("uploadHQPOPlan-begin");
+        String master = "N_HUASL_SZ";
+        Integer vpId = baseDao.queryForObject("select max(vp_id) vp_id from "+master+".vendPurExecplan where vp_statuscode='AUDITED' and nvl(vp_remark,' ') not like '%测试%'",Integer.class);
+        if(vpId!=null && vpId > 0){
+            String emCode="TASK";
+            String caller = "";
+            ApiResult<String> apiResult = hqService.syncHQPOPlan(master,vpId,emCode,caller);
+            System.out.println(apiResult.toString());
+        }
+        logger.info("uploadHQPOPlan-End:master {} id {}",master,vpId);
+    }
+}

+ 27 - 1
src/main/java/com/uas/eis/utils/BaseUtil.java

@@ -177,5 +177,31 @@ public class BaseUtil {
 	public static String parseGridStore2Str(List<Map<String, Object>> list) {
 		return JacksonUtil.toJsonArray(list);
 	}
-	
+	public static Map<String, List<Map<String, Object>>> groupsMap(List<Map<String, Object>> maps, Object[] objects) {
+		Map<String, List<Map<String, Object>>> set = new HashMap<String, List<Map<String, Object>>>();
+		List<Map<String, Object>> list = null;
+		String keyValue = null;
+		Object value = null;
+		for (Map<String, Object> map : maps) {
+			keyValue = null;
+			for (Object field : objects) {
+				if (keyValue != null) {
+					keyValue += "#";
+				} else {
+					keyValue = "";
+				}
+				value = map.get(field);
+				keyValue += value == null ? "" : value;
+			}
+			// Object key = map.get(groupField);
+			if (keyValue != null && !keyValue.equals("") && set.containsKey(keyValue)) {
+				list = set.get(keyValue);
+			} else {
+				list = new ArrayList<Map<String, Object>>();
+			}
+			list.add(map);
+			set.put(keyValue, list);
+		}
+		return set;
+	}
 }

+ 39 - 6
src/main/java/com/uas/eis/utils/HttpUtil.java

@@ -12,16 +12,12 @@ import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.security.KeyManagementException;
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.CertificateException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.Map.Entry;
-import java.util.Set;
 
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.TrustManager;
@@ -1017,4 +1013,41 @@ public class HttpUtil {
 			return null;
 		}
 	}
+	public static Response sendPostWithBasicAuthRequest(String postUrl,String userName,String pwd, String reqBody) throws Exception {
+		// 创建URL对象
+		URL url = new URL(postUrl);
+		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
+		try {
+			// 设置请求方法
+			urlConn.setRequestMethod("POST");
+			// 设置Basic Auth认证头
+			String auth = userName+":"+pwd;
+			String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
+			String authHeader = "Basic " + encodedAuth;
+			urlConn.setRequestProperty("Authorization", authHeader);
+			// 设置请求头
+			urlConn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+			urlConn.setRequestProperty("Accept", "application/json");
+			// 启用输出流,用于发送请求体
+			urlConn.setDoOutput(true);
+			urlConn.setDoInput(true);
+
+			urlConn.setUseCaches(false);
+			urlConn.setInstanceFollowRedirects(true);
+			if (null != reqBody) {
+				OutputStreamWriter osw = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
+				osw.write(reqBody);
+				osw.flush();
+				osw.close();
+			}
+			return new Response(urlConn.getResponseCode() == 200, streamToString(urlConn.getInputStream()));
+		} catch (Exception e) {
+			return new Response(false, e.getMessage());
+		} finally {
+			if (urlConn != null) {
+				urlConn.disconnect();
+			}
+		}
+	}
+
 }

+ 6 - 0
src/main/resources/hq.properties

@@ -0,0 +1,6 @@
+test.url=https://editst.huaqin.com:10443/httpsync/agent-vendor-api/poData
+test.userName=14058
+test.password=kgNv9FBdfQ
+prod.url=https://edi.huaqin.com:10443/httpsync/agent-vendor-api/poData
+prod.userName=14058
+prod.password=kgNv9FBdfQ

+ 5 - 2
src/test/java/com/uas/eis/UasEisApplicationTests.java

@@ -1,7 +1,9 @@
 package com.uas.eis;
 
+import com.uas.eis.config.HQConfig;
 import com.uas.eis.dao.*;
 import com.uas.eis.service.ERPService;
+import com.uas.eis.service.HQService;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,10 +18,11 @@ public class UasEisApplicationTests {
 	@Autowired
 	private BaseDao baseDao;
 	@Autowired
-	private ERPService erpService;
+	private HQService hqService;
 
 	@Test
 	public void Test() throws Exception {
-		System.out.println("test");
+		System.out.println(123);
+		hqService.syncHQPOPlan("N_HUASL_SZ",3000,"ADMIN","");
 	}
 }