记得上下班打卡 | git大法好,push需谨慎

Commit d81fbc12 authored by sunyuntian's avatar sunyuntian

划卡调任务优化

parent 16ab3758
...@@ -64,4 +64,6 @@ public class KylinRedisConst { ...@@ -64,4 +64,6 @@ public class KylinRedisConst {
public static final String ADMIN_BANNER_LIST = "kylin:bannerList"; public static final String ADMIN_BANNER_LIST = "kylin:bannerList";
public static final String RETURN_ADDRESS_CODE = "kylin:address:code";//退货地址 public static final String RETURN_ADDRESS_CODE = "kylin:address:code";//退货地址
public static final String EXPRESS_TYPE = "express:type";//退货地址
} }
package com.liquidnet.service.kylin.entity; package com.liquidnet.service.kylin.dao;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
@Data @Data
public class KylinFreightCharge { public class KylinFreightChargeDao implements Serializable {
@TableId(value = "mid", type = IdType.AUTO) @TableId(value = "mid", type = IdType.AUTO)
private Integer mid; private Integer mid;
private String name; //区县名 private String fieldsId;
private String province; //省
private String city;//市
private String adname; //区县
private String adcode; //区县code private String adcode; //区县code
private String price; //价格 private String price; //价格
private String businessType; //快运参数 private String businessType; //快运参数
private String businessTypeDesc; //快运类型 private String businessTypeDesc; //快运类型
private static final KylinFreightChargeDao obj = new KylinFreightChargeDao();
public static KylinFreightChargeDao getNew() {
try {
return (KylinFreightChargeDao) obj.clone();
} catch (CloneNotSupportedException e) {
return new KylinFreightChargeDao();
}
}
} }
\ No newline at end of file
package com.liquidnet.service.kylin.mapper; package com.liquidnet.service.kylin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liquidnet.service.kylin.entity.KylinFreightCharge; import com.liquidnet.service.kylin.dao.KylinFreightChargeDao;
import java.util.HashMap; import java.util.HashMap;
public interface KylinFreightChargeMapper { public interface KylinFreightChargeMapper extends BaseMapper<KylinFreightChargeDao>{
int setFreightCharge(KylinFreightCharge kylinFreightCharge); int setFreightCharge(KylinFreightChargeDao kylinFreightCharge);
} }
...@@ -2,22 +2,22 @@ ...@@ -2,22 +2,22 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liquidnet.service.kylin.mapper.KylinFreightChargeMapper"> <mapper namespace="com.liquidnet.service.kylin.mapper.KylinFreightChargeMapper">
<insert id="setFreightCharge">
<insert id="setFreightCharge" parameterType="com.liquidnet.service.kylin.dao.KylinFreightChargeDao" useGeneratedKeys="true" keyProperty="mid">
insert into kylin_freight_charge( insert into kylin_freight_charge(
<if test="name != null and name != ''">name,</if> mid,fields_id,province,city,adname,adcode,price,business_type,business_type_desc
<if test="adcode != null and adcode != ''">adcode,</if>
<if test="price != null and price != ''">price,</if>
<if test="businessType != null and businessType != ''">business_type,</if>
<if test="businessTypeDesc != null and businessTypeDesc != ''">business_type_desc,</if>
)values ( )values (
<if test="name != null and name != ''">#{name},</if> #{mid},
<if test="fieldsId != null and fieldsId != ''">#{fieldsId},</if>
<if test="province != null and province != ''">#{province},</if>
<if test="city != null and city != ''">#{city},</if>
<if test="adname != null and adname != ''">#{adname},</if>
<if test="adcode != null and adcode != ''">#{adcode},</if> <if test="adcode != null and adcode != ''">#{adcode},</if>
<if test="price != null and price != ''">#{price},</if> <if test="price != null and price != ''">#{price},</if>
<if test="businessType != null and businessType != ''">#{businessType},</if> <if test="businessType != null and businessType != ''">#{businessType},</if>
<if test="businessTypeDesc != null and businessTypeDesc != ''">#{businessTypeDesc},</if> <if test="businessTypeDesc != null and businessTypeDesc != ''">#{businessTypeDesc}</if>
) )
</insert> </insert>
</mapper> </mapper>
package com.liquidnet.service.kylin.controller;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.dao.KylinFreightChargeDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "前端-获取快递价格")
@RestController
@RequestMapping("getShunFengPrice")
@Slf4j
public class KylinGetShunFengPriceController {
@Autowired
private RedisDataSourceUtil redisDataSourceUtil;
@Autowired
private RedisUtil redisUtil;
@GetMapping("getPrice")
@ApiOperation("运费查询")
public String getFreightCharge(String adcode,String expressType){
Object obj =redisUtil.get(KylinRedisConst.RETURN_ADDRESS_CODE+adcode+KylinRedisConst.EXPRESS_TYPE+expressType);
if (obj != null){
KylinFreightChargeDao k= (KylinFreightChargeDao)obj;
return k.getPrice();
}else {
return null;
}
}
}
package com.liquidnet.service.kylin.controller; package com.liquidnet.service.platform.controller.kylin;
import com.liquidnet.service.platform.service.impl.kylin.KylinFreightChargeServiceImpl;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.kylin.service.impl.KylinFreightChargeServiceImpl;
import com.liquidnet.service.kylin.utils.ShunfengSignUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -12,9 +8,6 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -12,9 +8,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "运费") @Api(tags = "运费")
@RestController @RestController
@RequestMapping("freightCharge") @RequestMapping("freightCharge")
......
package com.liquidnet.service.kylin.service.impl; package com.liquidnet.service.platform.service.impl.kylin;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.common.cache.redis.util.RedisUtil; import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.JsonUtils; import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.kylin.constant.KylinRedisConst; import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.entity.KylinFreightCharge; import com.liquidnet.service.kylin.dao.KylinFreightChargeDao;
import com.liquidnet.service.kylin.mapper.KylinFreightChargeMapper; import com.liquidnet.service.kylin.mapper.KylinFreightChargeMapper;
import com.liquidnet.service.kylin.utils.ShunfengSignUtils; import com.liquidnet.service.platform.utils.ShunfengSignUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -20,15 +23,13 @@ import java.util.Map; ...@@ -20,15 +23,13 @@ import java.util.Map;
public class KylinFreightChargeServiceImpl { public class KylinFreightChargeServiceImpl {
@Autowired @Autowired
private ShunfengSignUtils shunfengSignUtils; private ShunfengSignUtils shunfengSignUtils;
@Autowired @Autowired
private RedisUtil redisUtil; private RedisDataSourceUtil redisDataSourceUtil;
@Autowired
private KylinFreightCharge kylinFreightCharge;
@Autowired @Autowired
private KylinFreightChargeMapper kylinFreightChargeMapper; private KylinFreightChargeMapper kylinFreightChargeMapper;
// @Autowired
// private KylinRedisConst kylinRedisConst;
public void getFreightCharge() { public void getFreightCharge() {
...@@ -44,7 +45,8 @@ public class KylinFreightChargeServiceImpl { ...@@ -44,7 +45,8 @@ public class KylinFreightChargeServiceImpl {
hBody.put("dProvince", "北京市"); hBody.put("dProvince", "北京市");
hBody.put("dCity", "北京"); hBody.put("dCity", "北京");
hBody.put("dAddress", "东城区"); hBody.put("dAddress", "东城区");
hBody.put("expressType", "2"); String expressType = "1";
hBody.put("expressType", "1");
try { try {
// 生成签名并请求 // 生成签名并请求
String result = shunfengSignUtils.generateSignatureAndRequestNew(hBody, "/public/order/v1/getFreight"); String result = shunfengSignUtils.generateSignatureAndRequestNew(hBody, "/public/order/v1/getFreight");
...@@ -52,15 +54,20 @@ public class KylinFreightChargeServiceImpl { ...@@ -52,15 +54,20 @@ public class KylinFreightChargeServiceImpl {
HashMap<String,String> map = (HashMap<String, String>) hashMap.get("result"); HashMap<String,String> map = (HashMap<String, String>) hashMap.get("result");
map.put("address",jProvince+":"+jCity+":"+jAddress); map.put("address",jProvince+":"+jCity+":"+jAddress);
KylinFreightChargeDao kylinFreightCharge = KylinFreightChargeDao.getNew();
kylinFreightCharge.setFieldsId(IDGenerator.nextTimeId2());
kylinFreightCharge.setProvince(jProvince);
kylinFreightCharge.setCity(jCity);
kylinFreightCharge.setAdname(jAddress);
kylinFreightCharge.setPrice(map.get("price")); kylinFreightCharge.setPrice(map.get("price"));
kylinFreightCharge.setAdcode(addressCode); kylinFreightCharge.setAdcode(addressCode);
kylinFreightCharge.setBusinessType(map.get("businessType")); kylinFreightCharge.setBusinessType(map.get("businessType"));
kylinFreightCharge.setBusinessTypeDesc(map.get("businessTypeDesc")); kylinFreightCharge.setBusinessTypeDesc(map.get("businessTypeDesc"));
kylinFreightCharge.setName(jProvince+":"+jCity+":"+jAddress);
//存数据库 //存数据库
int i= kylinFreightChargeMapper.setFreightCharge(kylinFreightCharge); int i= kylinFreightChargeMapper.setFreightCharge(kylinFreightCharge);
//存redis //存redis
//redisUtil.set(kylinRedisConst.RETURN_ADDRESS_CODE+addressCode,map); redisDataSourceUtil.getRedisKylinUtil().set(KylinRedisConst.RETURN_ADDRESS_CODE+addressCode+KylinRedisConst.EXPRESS_TYPE+expressType,kylinFreightCharge);
}catch (Exception e) { }catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
log.error("顺丰接口调用失败", e); log.error("顺丰接口调用失败", e);
......
package com.liquidnet.service.platform.utils;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpsUtils {
private static final String HTTP = "http";
private static final String HTTPS = "https";
private static SSLConnectionSocketFactory sslsf = null;
private static PoolingHttpClientConnectionManager cm = null;
private static SSLContextBuilder builder = null;
static {
try {
builder = new SSLContextBuilder();
// 全部信任 不做身份鉴定
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(HTTP, new PlainConnectionSocketFactory())
.register(HTTPS, sslsf)
.build();
cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(200);//max connection
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* httpClient post请求
*
* @param url 请求url
* @param header 头部信息
* @param param 请求参数 form提交适用
* @param entity 请求实体 json/xml提交适用
* @return 可能为空 需要处理
* @throws Exception
*/
public static String post(String url, Map<String, String> header, Map<String, String> param, HttpEntity entity) throws Exception {
String result = "";
CloseableHttpClient httpClient = null;
try {
httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(url);
// 设置头信息
if (!CollectionUtils.isEmpty(header)) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
}
// 设置请求参数
if (!CollectionUtils.isEmpty(param)) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : param.entrySet()) {
//给参数赋值
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
httpPost.setEntity(urlEncodedFormEntity);
}
// 设置实体 优先级高
if (entity != null) {
httpPost.setEntity(entity);
}
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
} else {
readHttpResponse(httpResponse);
}
} catch (Exception e) {
throw e;
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return result;
}
public static CloseableHttpClient getHttpClient() throws Exception {
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setConnectionManager(cm)
.setConnectionManagerShared(true)
.build();
return httpClient;
}
public static String readHttpResponse(HttpResponse httpResponse)
throws ParseException, IOException {
StringBuilder builder = new StringBuilder();
// 获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 响应状态
builder.append("status:" + httpResponse.getStatusLine());
builder.append("headers:");
HeaderIterator iterator = httpResponse.headerIterator();
while (iterator.hasNext()) {
builder.append("\t" + iterator.next());
}
// 判断响应实体是否为空
if (entity != null) {
String responseString = EntityUtils.toString(entity);
builder.append("response length:" + responseString.length());
builder.append("response content:" + responseString.replace("\r\n", ""));
}
return builder.toString();
}
}
\ No newline at end of file
package com.liquidnet.service.kylin.utils; package com.liquidnet.service.platform.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.*; import java.io.*;
@Slf4j @Slf4j
@Component @Component
public class JSONUtils { public class JSONUtils {
/** /**
* 读取json文件 * 读取json文件
* @param fileName json文件路径 * @param fileName json文件路径
...@@ -25,19 +25,15 @@ public class JSONUtils { ...@@ -25,19 +25,15 @@ public class JSONUtils {
int ch = 0; int ch = 0;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) { while ((ch = reader.read()) != -1) {
sb.append((char) ch); sb.append((char) ch); }fileReader.close();
}
fileReader.close();
reader.close(); reader.close();
jsonStr = sb.toString(); jsonStr = sb.toString();
log.info("————读取" + jsonFile.getPath() + "文件结束!————"); log.info("————读取" + jsonFile.getPath() + "文件结束!————");
//System.out.println("————读取" + jsonFile.getPath() + "文件结束!————"); //System.out.println("————读取" + jsonFile.getPath() + "文件结束!————");
return jsonStr; return jsonStr; } catch (Exception e) {
} catch (Exception e) {
log.info("————读取" + jsonFile.getPath() + "文件出现异常,读取失败!————"); log.info("————读取" + jsonFile.getPath() + "文件出现异常,读取失败!————");
// System.out.println("————读取" + jsonFile.getPath() + "文件出现异常,读取失败!————"); // System.out.println("————读取" + jsonFile.getPath() + "文件出现异常,读取失败!————");
e.printStackTrace(); e.printStackTrace();
return null; return null; } }}
}
}
}
...@@ -2,10 +2,13 @@ package com.liquidnet.service.platform.utils; ...@@ -2,10 +2,13 @@ package com.liquidnet.service.platform.utils;
import com.liquidnet.commons.lang.constant.LnsEnum; import com.liquidnet.commons.lang.constant.LnsEnum;
import com.liquidnet.commons.lang.util.CurrentUtil; import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
...@@ -13,6 +16,8 @@ import org.springframework.stereotype.Component; ...@@ -13,6 +16,8 @@ import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** /**
* <p> * <p>
...@@ -25,6 +30,13 @@ import java.util.Arrays; ...@@ -25,6 +30,13 @@ import java.util.Arrays;
@Component @Component
@Slf4j @Slf4j
public class ShunfengSignUtils { public class ShunfengSignUtils {
@Value("${liquidnet.express.shunfeng.appid}")
private String APP_ID;
/**
* url
*/
@Value("${liquidnet.express.shunfeng.url}")
private String URL;
/** /**
* sk * sk
*/ */
...@@ -38,6 +50,39 @@ public class ShunfengSignUtils { ...@@ -38,6 +50,39 @@ public class ShunfengSignUtils {
@Autowired @Autowired
Environment environment; Environment environment;
/**
* 生成签名并请求
* @param hbody 请求body
* @return
*/
public String generateSignatureAndRequestNew(Map<String, String> hbody, String url) {
long currentTimeMillis = System.currentTimeMillis(); // 时间戳
String timestamp = currentTimeMillis + "";
hbody.put("companyId", APP_ID);
String body = JsonUtils.toJson(hbody);
// 生成签名
String sign = genSign(timestamp,body);
Map<String, String> headers = new HashMap<>();
headers.put("sendAppId", APP_ID);
headers.put("timestamp", timestamp);
headers.put("sign", sign);
headers.put("Content-Type", "application/json;charset=utf-8");
headers.put("Accept", "application/json");
Map<String, String> querys = new HashMap<>();
try {
HttpEntity httpEntity = new StringEntity(body, "utf-8");
log.info("generateSignatureAndRequestNew body:[{}], headers:[{}]", body, headers);
String post = HttpsUtils.post(URL + url, headers, querys, httpEntity);
return post;
} catch (Exception e) {
System.out.println("发送请求失败");
e.printStackTrace();
return e.getMessage();
}
}
/** /**
* 生成签名 * 生成签名
* @param timestamp 时间戳 * @param timestamp 时间戳
......
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