Commit 64c6cd3a by wubh

包活干V2.3.0-民生易租代码初始化

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@30360 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent 70298be4
package com.glsx.ordercenter.service;
import java.util.List;
import com.glsx.ordercenter.dto.resp.DSCResultObject;
/**
* <p>Description:
* <p>Project: car-service
*
* @author wbh
* @date 2021-05-18
*/
public interface MsDeviceService {
DSCResultObject checkDevice(List<String> deviceNoList);
}
package com.glsx.ordercenter.service.impl;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtils;
import com.glsx.ordercenter.constants.BizConstants;
import com.glsx.ordercenter.constants.ResultCodeEnum;
import com.glsx.ordercenter.dto.req.DaSouCheRequest;
import com.glsx.ordercenter.dto.resp.DSCResultObject;
import com.glsx.ordercenter.service.MsDeviceService;
import com.glsx.ordercenter.utils.MsSignUtil;
import com.souche.api.SoucheApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* <p>Description:
* <p>Project: car-service
*
* @author wbh
* @date 2021-05-18
*/
@Service
public class MsDeviceServiceImpl implements MsDeviceService {
public final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${msyizu.valiate.url}")
private String dscUrl;
@Value("${msyizu.appKey}")
private String appKey;
@Value("${msyizu.appSecret}")
private String appSecret;
@Value("${msyizu.device.check.api}")
private String deviceCheckApi;
@Override
public DSCResultObject checkDevice(List<String> deviceNoList) {
DSCResultObject msResult;
try {
JSONObject resultData = new JSONObject();
for (String deviceNo : deviceNoList) {
DaSouCheRequest msReq = new DaSouCheRequest();
this.wrapBizParam(msReq, deviceNo);
String respJson = this.postForResult(msReq);
// 返回的是个JSON格式的String:"{\"code\": 111, \"message\": \"xxxx\"}"
// 所以要先转换成正常的字符串
DSCResultObject checkResult = this.proceedResult(JSON.parseObject(
respJson, String.class));
if (!"200".equals(checkResult.getCode())) {
resultData.put(deviceNo, checkResult.getMessage());
}
}
msResult = resultData.isEmpty()
? new DSCResultObject(ResultCodeEnum.DSC_SUCCESS.getCode() + "",
ResultCodeEnum.DSC_SUCCESS.getMsg())
: new DSCResultObject(ResultCodeEnum.FAIL.getCode() + "",
ResultCodeEnum.FAIL.getMsg(), resultData);
} catch (Exception e) {
this.logger.error("==== MsDeviceService ERROR: ", e);
msResult = new DSCResultObject(ResultCodeEnum.ERROR.getCode() + "",
ResultCodeEnum.ERROR.getMsg());
}
return msResult;
}
/**
* 封装业务参数
*
* @param msReq 参数请求体
*/
private void wrapBizParam(DaSouCheRequest msReq, String deviceNo) throws SoucheApiException {
// 系统参数
long timestamp = System.currentTimeMillis() / 1000;
msReq.setAppKey(this.appKey);
msReq.setTimestamp(timestamp);
msReq.setApi(this.deviceCheckApi);
// 业务参数
JSONObject reqData = new JSONObject();
reqData.put("imei", deviceNo);
msReq.setData(JSON.toJSONString(reqData));
// 签名
MsSignUtil.fillSign(msReq, this.appSecret);
}
/**
* 请求接口
*
* @param req 请求参数
* @return 接口返回结果
* @throws Exception 异常
*/
private String postForResult(DaSouCheRequest req) throws Exception {
this.logger.info("==== MsDeviceService url: {}, param: {}",
this.dscUrl, JSON.toJSONString(req));
HttpUtils httpUtils = new HttpUtils();
String resultJson = httpUtils.postJson(this.dscUrl, JSON.toJSONString(req));
this.logger.info("==== MsDeviceService response: {}", resultJson);
return resultJson;
}
/**
* 处理请求结果
*
* @param respJson 请求结果
* @return 结果
*/
private DSCResultObject proceedResult(String respJson) {
DSCResultObject dscResult = new DSCResultObject();
JSONObject resp = JSON.parseObject(respJson);
String respCode = resp.getString("code");
String respStatus = resp.getString("status");
String respMsg = resp.getString("message");
JSONObject respData = JSON.parseObject(resp.getString("data"));
if ("0".equals(respCode) && "200".equals(respStatus)) {
// 定位正常,判断是否在时效内
boolean inTime = this.isDeviceLocationInTime(respData);
dscResult.setCode(inTime ? respStatus : "-1");
dscResult.setMessage(inTime ? respMsg : ResultCodeEnum.DEVICE_GPS_VERIFY_FAIL.getMsg());
} else {
// 定位不正常
dscResult.setCode("-1");
dscResult.setMessage(ResultCodeEnum.DEVICE_GPS_VERIFY_FAIL.getMsg());
}
dscResult.setData(respData);
return dscResult;
}
/**
* 判断返回的定位信息是否在时效内,通过gpsTime判断
*
* @param respData 返回的数据中的data
* @return true-在时效内;false-超时
*/
private boolean isDeviceLocationInTime(JSONObject respData) {
boolean inTime;
try {
JSONObject innerData = respData.getJSONObject("data");
Long gpsTime = innerData.getLong("gpsTime");
Integer type = innerData.getInteger("type");
long currentMs = new Date().getTime();
long timeoutMs = (BizConstants.DSC_DEVICE_CHECK_WIRELESS == type)
? BizConstants.DSC_WIRELESS_DEVICE_TIMEOUT
: BizConstants.DSC_WIRED_DEVICE_TIMEOUT;
long timeDiff = currentMs - gpsTime;
inTime = (timeDiff < timeoutMs);
} catch (NullPointerException e) {
// 发生空指针异常,可能为获取数据为空,即表示定位失败
this.logger.error("==== MsDeviceService Error when getting data: ", e);
inTime = false;
}
return inTime;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!