Commit 2e50d72f by java-温文海

优化代码

parent 89e9dbc4
package com.foc.service; package com.foc.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.foc.dao.StatisticalDao; import com.foc.dao.StatisticalDao;
import com.foc.entity.DspReleaseInfo; import com.foc.entity.DspReleaseInfo;
...@@ -12,6 +11,7 @@ import com.foc.thirdpart.esb.HttpClient; ...@@ -12,6 +11,7 @@ import com.foc.thirdpart.esb.HttpClient;
import com.foc.thirdpart.esb.PageParam; import com.foc.thirdpart.esb.PageParam;
import com.foc.thirdpart.esb.nativehttp.HttpClientImpl; import com.foc.thirdpart.esb.nativehttp.HttpClientImpl;
import com.foc.util.DateUtils; import com.foc.util.DateUtils;
import com.foc.util.Empty4JUtil;
import com.foc.util.PropertiesUtils; import com.foc.util.PropertiesUtils;
import com.foc.util.StringUtils; import com.foc.util.StringUtils;
import com.google.gson.Gson; import com.google.gson.Gson;
...@@ -51,62 +51,41 @@ public class DspReleaseInfoService { ...@@ -51,62 +51,41 @@ public class DspReleaseInfoService {
} }
} }
private static List<String> nativeHttpCall() {
public static void addInfo() throws Exception {
Properties properties = PropertiesUtils.getProperties(); Properties properties = PropertiesUtils.getProperties();
String gatewayUatUrl = properties.getProperty("esb.security.gateway.uat.url"); String gatewayUatUrl = properties.getProperty("esb.security.gateway.uat.url");
String key = properties.getProperty("esb.security.key"); String key = properties.getProperty("esb.security.key");
String userAccount = properties.getProperty("esb.security.account");
String password = properties.getProperty("esb.security.password");
if (StringUtils.isEmpty(gatewayUatUrl) || StringUtils.isEmpty(key)) { if (StringUtils.isEmpty(gatewayUatUrl) || StringUtils.isEmpty(key)) {
log.error("获取配置文件中机型的文件"); log.error("获取配置文件中机型的文件");
return null;
}
log.info("**********************************************");
log.info("----nativeHttpCall-----");
List<String> resultList = new ArrayList<>();
try {
HttpClient httpClient = new HttpClientImpl();
List<Map<String, String>> params = buildGatewayRequestString();
for (Map<String, String> s:params
) {
String result = httpClient.doPost(gatewayUatUrl, s,
HttpClientImpl.DEFAULT_CHARSET, 60000, 60000);
resultList.add(result);
log.info("nativeHttpCall result=" + result);
log.info("nativeHttpCall decrypt result=" + EncodeUtil.decrypt(result, key));
} }
StatisticalDao dao = session.getMapper(StatisticalDao.class);
List<String> flightNoList = dao.getFlightNoList();
} catch (Exception e) { for (String s : flightNoList
e.printStackTrace(); ) {
//生成requestCode
Map<String, String> map = buildGatewayRequestString(s, key, userAccount, password);
//掉接口
String resultString = nativeHttpCall(map, gatewayUatUrl, key);
//解密
decrypt(resultString, key, dao);
} }
log.info("**********************************************"); session.commit();
return resultList;
} }
public static List<Map<String,String>> buildGatewayRequestString() throws Exception {
Properties properties = PropertiesUtils.getProperties();
String gatewayUatUrl = properties.getProperty("esb.security.gateway.uat.url");
String key = properties.getProperty("esb.security.key");
String userAccount = properties.getProperty("esb.security.account");
String password = properties.getProperty("esb.security.password");
if (StringUtils.isEmpty(gatewayUatUrl) || StringUtils.isEmpty(key)) {
log.error("获取配置文件中机型的文件");
return null;
}
public static Map<String, String> buildGatewayRequestString(String flightNo, String key, String userAccount, String password) throws UnsupportedEncodingException, GeneralSecurityException {
Gson gson = new Gson(); Gson gson = new Gson();
String currentTimeMillisStr = String.valueOf(System.currentTimeMillis()); String currentTimeMillisStr = String.valueOf(System.currentTimeMillis());
/** step1: 参考接口文档 2.4输入参数说明 章节,并组装成json string **/ /** step1: 参考接口文档 2.4输入参数说明 章节,并组装成json string **/
List<Map<String,String>> maps = new ArrayList<>();
StatisticalDao dao = session.getMapper(StatisticalDao.class);
List<String> flightNoList = dao.getFlightNoList();
for (String s:flightNoList
) {
ApiRequest apiRequest = new ApiRequest(); ApiRequest apiRequest = new ApiRequest();
apiRequest.getOptions().put("flightNos", s); apiRequest.getOptions().put("flightNos", flightNo);
apiRequest.getOptions().put("stdTimeStart", "2020-12-09 00:00:00"); apiRequest.getOptions().put("stdTimeStart", DateUtils.getTodayStartTime());
apiRequest.getOptions().put("stdTimeEnd", "2020-12-09 23:59:59"); apiRequest.getOptions().put("stdTimeEnd", DateUtils.getTodayEndTime());
//分页参数 //分页参数
PageParam pageParam = new PageParam(); PageParam pageParam = new PageParam();
//从第一页开始 //从第一页开始
...@@ -145,38 +124,45 @@ public class DspReleaseInfoService { ...@@ -145,38 +124,45 @@ public class DspReleaseInfoService {
Map<String, String> requstMap = new HashMap<>(); Map<String, String> requstMap = new HashMap<>();
requstMap.put("userName", userAccount); requstMap.put("userName", userAccount);
requstMap.put("requestString", requestString); requstMap.put("requestString", requestString);
maps.add(requstMap); return requstMap;
} }
return maps;
}
private static String nativeHttpCall(Map<String, String> params, String gatewayUatUrl, String key) {
log.info("**********************************************");
log.info("----nativeHttpCall-----");
String result = "";
try {
HttpClient httpClient = new HttpClientImpl();
public static void addInfo() throws GeneralSecurityException, UnsupportedEncodingException { result = httpClient.doPost(gatewayUatUrl, params,
Properties properties = PropertiesUtils.getProperties(); HttpClientImpl.DEFAULT_CHARSET, 60000, 60000);
String key = properties.getProperty("esb.security.key"); log.info("nativeHttpCall result=" + result);
StatisticalDao dao = session.getMapper(StatisticalDao.class); log.info("nativeHttpCall decrypt result=" + EncodeUtil.decrypt(result, key));
} catch (Exception e) {
e.printStackTrace();
}
log.info("**********************************************");
return result;
}
List<String> resultList = nativeHttpCall();
for (String result:resultList public static void decrypt(String resultString, String key, StatisticalDao dao) throws GeneralSecurityException, UnsupportedEncodingException {
) { String response = EncodeUtil.decrypt(resultString, key);
String response = EncodeUtil.decrypt(result, key);
System.out.println(response); System.out.println(response);
//转成jsonObject //转成jsonObject
JSONObject jsonObject = JSON.parseObject(response); JSONObject jsonObject = JSON.parseObject(response);
//取出jsonObject中的data //取出jsonObject中的data
String resultJson = jsonObject.getString("result"); String resultJson = jsonObject.getString("result");
System.out.println(resultJson);
JSONObject resultJsonObject = JSON.parseObject(resultJson); JSONObject resultJsonObject = JSON.parseObject(resultJson);
ResultMsg resultMsg = JSONObject.toJavaObject(resultJsonObject,ResultMsg.class); ResultMsg resultMsg = JSONObject.toJavaObject(resultJsonObject, ResultMsg.class);
if (Objects.nonNull(resultMsg)){ if (Objects.nonNull(resultMsg)) {
if ("0".equals(resultMsg.getResultCode())){ if ("0".equals(resultMsg.getResultCode())) {
//取出jsonObject中的data //取出jsonObject中的data
String data = jsonObject.getString("data"); String data = jsonObject.getString("data");
System.out.println(data); List<DspReleaseInfo> dspReleaseInfoList = JSON.parseArray(data, DspReleaseInfo.class);
List<DspReleaseInfo> dspReleaseInfoList = JSON.parseArray(data,DspReleaseInfo.class); if (Empty4JUtil.listIsNotEmpty(dspReleaseInfoList)) {
if (Objects.nonNull(dspReleaseInfoList)){ for (DspReleaseInfo ss : dspReleaseInfoList
for (DspReleaseInfo ss:dspReleaseInfoList
) { ) {
ss.setDelFlag("0"); ss.setDelFlag("0");
ss.setCreateDate(DateUtils.getTime("yyyy-MM-dd hh:mm:ss")); ss.setCreateDate(DateUtils.getTime("yyyy-MM-dd hh:mm:ss"));
...@@ -184,10 +170,9 @@ public class DspReleaseInfoService { ...@@ -184,10 +170,9 @@ public class DspReleaseInfoService {
} }
//调添加接口 //调添加接口
dao.insertDspReleaseInfo(dspReleaseInfoList); dao.insertDspReleaseInfo(dspReleaseInfoList);
session.commit();
}
} }
} }
} }
} }
} }
...@@ -46,4 +46,33 @@ public class DateUtils { ...@@ -46,4 +46,33 @@ public class DateUtils {
} }
return date1.after(date2); return date1.after(date2);
} }
/**
* @return 前天的零点时间
*/
public static String getTodayStartTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(calendar.getTime().getTime()));
return date;
}
/**
* @return 当天的零点时间
*/
public static String getTodayEndTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(calendar.getTime().getTime()));
return date;
}
} }
package com.foc.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author kaiya
* @Desc 数据工具类
* @date 2020/4/16 17:30
*/
public class Empty4JUtil {
/**
* 判断字符串为空
*
* @param str 字符串
* @return 若字符串不存在 或者 字符串去空格后为空(length为0),返回true
*/
public static boolean stringIsEmpty(String str) {
/**
* isEmpty()从JDK1.6开始
*/
return str == null || str.trim().isEmpty() || "null".equals(str);
}
/**
* 判断包装类为空
*
* @param str 包装类
* @return 若包装类不存在返回true
*/
public static boolean numberIsEmpty(Number str) {
return null == str;
}
/**
* 判断包装类不为空
*
* @param str 字符串
* @return 若字符串存在 并且 包装类去空格后不为空(length>0),返回true
*/
public static boolean numberIsNotEmpty(Number str) {
return !numberIsEmpty(str);
}
/**
* 判断数字包装类型是否为空或者是否为0
*
* @param str 包装类
* @return 若包装类不存在或值为0返回true
*/
public static boolean numberIsEmptyOrIsZero(Number str) {
return null == str || str.intValue()==0;
}
/**
* 判断字符串不为空
*
* @param str 字符串
* @return 若字符串存在 并且 字符串去空格后不为空(length>0),返回true
*/
public static boolean stringIsNotEmpty(String str) {
return !stringIsEmpty(str);
}
/**
* 判断list列表为空
* @param <T> 泛型
*
* @param list List列表
* @return 若list列表不存在 或者 list列表不包含元素,返回true
*/
public static <T> boolean listIsEmpty(List<T> list) {
return list == null || list.isEmpty();
}
/**
* 判断set集合不为空
* @param <T> 泛型
*
* @param set set结合
* @return set存在且包含元素返回true
*/
public static <T> boolean setIsNotEmpty(Set<T> set) {
return !setIsEmpty(set);
}
/**
* 判断set集合为空
* @param <T> 泛型
*
* @param set 集合
* @return set不存在存在或不包含元素返回true
*/
public static <T> boolean setIsEmpty(Set<T> set) {
return set == null || set.isEmpty();
}
/**
* 判断list列表不为空
* @param <T> 泛型
*
* @param list List列表
* @return 若list列表存在 并且 list列表包含元素,返回true
*/
public static <T> boolean listIsNotEmpty(List<T> list) {
return !listIsEmpty(list);
}
/**
* 判断map映射为空
* @param <K> 泛型
* @param <V> 泛型
*
* @param map Map映射
* @return 若map映射不存在 或者map映射未包含键-值映射关系,返回true
*/
public static <K, V> boolean mapIsEmpty(Map<K, V> map) {
return map == null || map.isEmpty();
}
/**
* 判断map映射不为空
* @param <K> 泛型
* @param <V> 泛型
*
* @param map Map映射
* @return 若map映射存在 并且map映射包含键-值映射关系,返回true
*/
public static <K, V> boolean mapIsNotEmpty(Map<K, V> map) {
return !mapIsEmpty(map);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment