|
|
@@ -0,0 +1,232 @@
|
|
|
+package com.uas.eis.serviceImpl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.eis.core.TokenManager;
|
|
|
+import com.uas.eis.core.config.SeresConfig;
|
|
|
+import com.uas.eis.core.config.SpObserver;
|
|
|
+import com.uas.eis.dao.BaseDao;
|
|
|
+import com.uas.eis.dao.SqlRowList;
|
|
|
+import com.uas.eis.entity.SeresStock;
|
|
|
+import com.uas.eis.entity.SeresStockDet;
|
|
|
+import com.uas.eis.service.SERESService;
|
|
|
+import com.uas.eis.utils.*;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class SERESServiceImpl implements SERESService {
|
|
|
+ @Autowired
|
|
|
+ private SeresConfig seresConfig;
|
|
|
+ @Autowired
|
|
|
+ private BaseDao baseDao;
|
|
|
+
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+
|
|
|
+ // Token最大有效期 60分钟 毫秒
|
|
|
+ private static final long MAX_TOKEN_VALID_MS = 60L * 60 * 1000;
|
|
|
+ // 提前1分钟刷新Token(毫秒)
|
|
|
+ private static final long REFRESH_ADVANCE_MS = 60L * 1000;
|
|
|
+
|
|
|
+ private static final String seresTokenKey = "SERES_TOKEN" ;
|
|
|
+
|
|
|
+ static final String errLogSql="insert into Seres_Stock_Alert_errlog(ID_,CODE_,DATE_,MESSAGE_,DATA_)values(?,?,sysdate,?,?)";
|
|
|
+ /**
|
|
|
+ * 获取有效Token,自动判断是否刷新
|
|
|
+ */
|
|
|
+ public synchronized String getValidToken() throws IOException {
|
|
|
+ String accessToken = TokenManager.getToken(seresTokenKey,REFRESH_ADVANCE_MS);
|
|
|
+ long nowMs = System.currentTimeMillis();
|
|
|
+ // 无token 或 距离过期不足1分钟,重新拉取
|
|
|
+ if (accessToken == null ) {
|
|
|
+ logger.info("token 为空重新获取");
|
|
|
+ accessToken=refreshToken();
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String refreshToken() throws IOException {
|
|
|
+ String accessToken ="";
|
|
|
+ String tokenUrl = seresConfig.getUrl() + "/aito/DIS/COMM_GET_JWT_TOKEN/1.0.0";
|
|
|
+ String clientId=seresConfig.getClientId();
|
|
|
+ String clientSecret=seresConfig.getClientSecret();
|
|
|
+
|
|
|
+ long nowMs = System.currentTimeMillis();
|
|
|
+ long expTs = nowMs + MAX_TOKEN_VALID_MS;
|
|
|
+ // 请求体
|
|
|
+ JSONObject reqBody = new JSONObject();
|
|
|
+ reqBody.put("grant_type", "client_credentials");
|
|
|
+ reqBody.put("client_id", clientId);
|
|
|
+ reqBody.put("client_secret", clientSecret);
|
|
|
+ reqBody.put("exp", expTs);
|
|
|
+ System.out.println(expTs);
|
|
|
+ // 发送POST
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(tokenUrl);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ StringEntity entity = new StringEntity(reqBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String respJson = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ JSONObject respObj = JSON.parseObject(respJson);
|
|
|
+ if (!respObj.containsKey("access_token")) {
|
|
|
+ throw new RuntimeException("获取Token失败,响应:" + respJson);
|
|
|
+ }
|
|
|
+ accessToken = respObj.getString("access_token");
|
|
|
+ TokenManager.setToken(seresTokenKey,respObj.getString("access_token"),expTs);
|
|
|
+ logger.info("token {}",accessToken);
|
|
|
+ System.out.println("Token刷新成功,有效期:" + respObj.get("expires_in") + "秒");
|
|
|
+ } finally {
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getStock() throws IOException {
|
|
|
+ Map<String, Object> modelMap = new HashMap<String, Object>();
|
|
|
+ String token = getValidToken();
|
|
|
+ String apiPath="/aito/DIS/INV_STOCK_STS/1.0.0";
|
|
|
+ String fullUrl = seresConfig.getUrl() + apiPath;
|
|
|
+
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(fullUrl);
|
|
|
+ // 鉴权头
|
|
|
+ httpPost.setHeader("Authorization", "Bearer " + token);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ JSONObject reqBody = new JSONObject();
|
|
|
+ //供应商编码
|
|
|
+ reqBody.put("SupplierCode", seresConfig.getClientCode());
|
|
|
+ //工厂编码
|
|
|
+ //reqBody.put("FactoryCode", seresConfig.getClient_code());
|
|
|
+ //物料编码
|
|
|
+ //reqBody.put("MaterialCode", seresConfig.getClient_code());
|
|
|
+ //页码
|
|
|
+ //reqBody.put("Skip", seresConfig.getClient_code());
|
|
|
+ //分页大小
|
|
|
+ //reqBody.put("Take", seresConfig.getClient_code());
|
|
|
+ if (reqBody != null) {
|
|
|
+ StringEntity entity = new StringEntity(JSON.toJSONString(reqBody), "UTF-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ }
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String respStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ return JSON.parseObject(respStr);
|
|
|
+ } finally {
|
|
|
+ httpClient.close();
|
|
|
+ return modelMap;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getERPStock(String param){
|
|
|
+ Map<String, Object> modelMap = new HashMap<String, Object>();
|
|
|
+ SpObserver.putSp("AW_ZHJK");
|
|
|
+ List<SeresStock> data=new ArrayList();
|
|
|
+ String con="";
|
|
|
+ String MaterialCode_Q="";
|
|
|
+ if(StringUtil.hasText(param)){
|
|
|
+ Map<Object, Object> map = BaseUtil.parseFormStoreToMap(param);
|
|
|
+ if(StringUtil.hasText(map.get("MaterialCode"))){
|
|
|
+ MaterialCode_Q=map.get("MaterialCode").toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(StringUtil.hasText(MaterialCode_Q)){
|
|
|
+ con="MaterialCode='"+MaterialCode_Q+"'";
|
|
|
+ }else{
|
|
|
+ con="1=1";
|
|
|
+ }
|
|
|
+ SqlRowList rs = baseDao.queryForRowSet("select MaterialCode,MaterialName from ZHJK_SERES_STOCK_VIEW where "+con+" group by MaterialCode,MaterialName order by MaterialCode");
|
|
|
+ int n = 1;
|
|
|
+ if(rs.size()>0){
|
|
|
+ while (rs.next()){
|
|
|
+ SeresStock seresStock=new SeresStock();
|
|
|
+ seresStock.setSerialNumber(n);
|
|
|
+ n++;
|
|
|
+ seresStock.setSupplierCode(seresConfig.getClientCode());
|
|
|
+ seresStock.setSupplierName(seresConfig.getClientName());
|
|
|
+ seresStock.setMaterialCode(rs.getString("MaterialCode"));
|
|
|
+ seresStock.setMaterialName(rs.getString("MaterialName"));
|
|
|
+ String prcon="MaterialCode='"+rs.getString("MaterialCode")+"'";
|
|
|
+ List<SeresStockDet> det=new ArrayList();
|
|
|
+ SqlRowList rs_det = baseDao.queryForRowSet("select StockCode,StockName,StockAddress,FGQualifiedStock,FGNonQualifiedStock from ZHJK_SERES_STOCK_VIEW where "+prcon+" order by StockCode");
|
|
|
+ while (rs_det.next()){
|
|
|
+ SeresStockDet detInfo=new SeresStockDet();
|
|
|
+ detInfo.setStockCode(rs.getString("StockCode"));
|
|
|
+ detInfo.setStockName(rs.getString("StockName"));
|
|
|
+ detInfo.setStockAddress(rs.getString("StockAddress"));
|
|
|
+ detInfo.setFGQualifiedStock(rs.getGeneralDouble("FGQualifiedStock"));
|
|
|
+ detInfo.setFGNonQualifiedStock(rs.getGeneralDouble("FGNonQualifiedStock"));
|
|
|
+ det.add(detInfo);
|
|
|
+ }
|
|
|
+ data.add(seresStock);
|
|
|
+ }
|
|
|
+ System.out.println(JSONArray.toJSONString(data));
|
|
|
+ modelMap.put("code",0);
|
|
|
+ modelMap.put("message","成功");
|
|
|
+ modelMap.put("data",JSONArray.toJSONString(data));
|
|
|
+ } else if (StringUtil.hasText(MaterialCode_Q)) {
|
|
|
+ //物料号不存在
|
|
|
+ modelMap.put("code",1);
|
|
|
+ modelMap.put("message","物料号不存在");
|
|
|
+ System.out.println("物料号不存在");
|
|
|
+ }else{
|
|
|
+ //未配置对照关系
|
|
|
+ modelMap.put("code",1);
|
|
|
+ modelMap.put("message","未配置对照关系");
|
|
|
+ System.out.println("未配置对照关系");
|
|
|
+ }
|
|
|
+ return modelMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getToken() throws IOException {
|
|
|
+ String token=getValidToken();
|
|
|
+ logger.info("当前token {}",token);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> saveAlert(String data) {
|
|
|
+ Map<String, Object> modelMap = new HashMap<String, Object>();
|
|
|
+ List<Map<Object, Object>> dets = BaseUtil.parseGridStoreToMaps(data);
|
|
|
+ Object id = baseDao.getSeqId("SERES_STOCK_ALERT_SEQ");
|
|
|
+ String code = baseDao.sGetMaxNumber("Seres_Stock_Alert", 2);
|
|
|
+ baseDao.execute("insert into SERES_STOCK_ALERT(Sta_id,sta_code,sta_recorddate) values("+id+",'"+code+"',sysdate)");
|
|
|
+ for (Map<Object, Object> det : dets) {
|
|
|
+ det.put("stad_id",baseDao.getSeqId("SERES_STOCK_ALERT_DET_SEQ"));
|
|
|
+ det.put("stad_staid",id);
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ List<String> gridSql = SqlUtil.getInsertSqlbyGridStore(dets, "SERES_STOCK_ALERT_DET");
|
|
|
+ baseDao.execute(gridSql);
|
|
|
+ baseDao.execute("update Seres_Stock_Alert set sta_dockStatus='成功' where Sta_id="+id);
|
|
|
+ modelMap.put("code",0);
|
|
|
+ modelMap.put("message","接收成功");
|
|
|
+ modelMap.put("data","");
|
|
|
+ }catch (Exception e){
|
|
|
+ Object errid = baseDao.getSeqId("SERES_ERRLOG_SEQ");
|
|
|
+ baseDao.execute("update Seres_Stock_Alert set sta_dockStatus='失败' where Sta_id="+id);
|
|
|
+ baseDao.execute(errLogSql,
|
|
|
+ new Object[]{errid,code,e.getMessage(),data});
|
|
|
+ modelMap.put("code",1);
|
|
|
+ modelMap.put("message","接收失败");
|
|
|
+ modelMap.put("data","");
|
|
|
+ }
|
|
|
+
|
|
|
+ return modelMap;
|
|
|
+ }
|
|
|
+}
|