Commit c8a3b955 by pye52

1、现在服务器在返回"写入成功"的通知后,会判断其code是否成功

2、优化离线订单上传的逻辑,保证上传的离线订单和设备号一致
parent 9fca2e6b
......@@ -159,6 +159,9 @@ public class SCWebSocketClient extends WebSocketClient {
@Override
public void onMessage(String response) {
// 由于项目设计阶段没有规范返回的结果
// 导致做统一处理比较麻烦(需要考虑到很多情况)
// 因此目前只采取通过JsonObject来逐个字段判断的低效方案
JsonObject jsonObject;
String action = "";
try {
......
......@@ -22,6 +22,8 @@ import com.google.gson.JsonObject;
import org.java_websocket.handshake.ServerHandshake;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
......@@ -100,6 +102,7 @@ public class PayOfflineViewModel extends ViewModel {
private static final String RESPONSE_MESSAGE = "message";
private static final String RESPONSE_OFFLINE_RESULT = "操作完成";
private static final String RESPONSE_PAY_OFFLINE_RESULT = "PAY_OFFLINE_RESULT";
private static final String RESPONSE_SUCCESS = "1";
// 每心跳多少次后进行一次离线订单检测
private static final int PAY_OFFLINE_CHECK = 30;
// 当链接频繁断开时,给予离线订单检测一定缓冲时间
......@@ -126,12 +129,6 @@ public class PayOfflineViewModel extends ViewModel {
heartbeatCount++;
if (heartbeatCount >= PAY_OFFLINE_CHECK) {
heartbeatCount = 0;
long currentTime = System.currentTimeMillis();
if ((currentTime - lastPayTime) < PAY_OFFLINE_INTERVAL) {
LogUtils.w(TAG, "心跳引发离线检测冲突,跳过该次检测");
return;
}
lastPayTime = currentTime;
traversalPayOfflineData();
}
}
......@@ -139,10 +136,17 @@ public class PayOfflineViewModel extends ViewModel {
@Override
public void onMessage(String action, JsonObject obj, String original) {
if (action.equals(RESPONSE_PAY_OFFLINE_RESULT)) {
// 离线订单已写入到服务器数据库,可以标记为支付成功
LogUtils.d(TAG, "离线支付结果响应: " + original);
ResponseRunnable runnable = new ResponseRunnable(original);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
String code = null;
// 这里为了兼容新的消息体需要单独对code作判断
if (obj.has("code")) {
code = obj.get("code").getAsString();
}
if (code != null && code.equals(RESPONSE_SUCCESS)) {
// 离线订单已写入到服务器数据库,可以标记为支付成功
LogUtils.d(TAG, "离线支付结果响应: " + original);
ResponseRunnable runnable = new ResponseRunnable(original);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
}
return;
}
......@@ -201,12 +205,58 @@ public class PayOfflineViewModel extends ViewModel {
LogUtils.d(TAG, "所有离线订单处理完毕");
return;
}
payRequest = new PayRequest(deviceSN, payOfflineData);
// 由于离线支付订单有可能和设备码不一致,需要兼容该情况
// 保证离线订单的equipmentId与equipmentNo一致
// 去除不一致的订单(留待下次上传)
String mainEquipmentNo = acquireMaxEquipmentNo(payOfflineData);
if (TextUtils.isEmpty(mainEquipmentNo)) {
mainEquipmentNo = deviceSN;
}
ArrayList<PayData> theSameEquipmentNoData = new ArrayList<>();
for (PayData t : payOfflineData) {
if (t.getEquipmentNo().equals(mainEquipmentNo)) {
theSameEquipmentNoData.add(t);
}
}
payRequest = new PayRequest(mainEquipmentNo, theSameEquipmentNoData);
String requestStr = gson.toJson(payRequest);
payOfflineState.postValue(new PayOfflineState(PayOfflineState.SEND, requestStr));
SCWebSocketClient.getInstance().send(requestStr);
LogUtils.d(TAG, "离线支付: " + payRequest.toString());
}
private String acquireMaxEquipmentNo(List<PayData> list) {
HashMap<String, Integer> tMap = new HashMap<>();
String mainEquipmentNo = "";
int size = list.size();
int max = 0;
for (PayData t : list) {
String key = t.getEquipmentNo();
int value;
if (tMap.containsKey(key)) {
Integer vWrapper = tMap.get(key);
if (vWrapper == null) {
value = 1;
} else {
value = vWrapper + 1;
}
if (value > (size / 2)) {
return key;
}
} else {
value = 1;
}
if (value >= max) {
max = value;
mainEquipmentNo = key;
}
tMap.put(key, value);
}
tMap.clear();
return mainEquipmentNo;
}
}
private class UpdateUploadTimeRunnable implements Runnable {
......
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 sign in to comment