Commit d4b74d34 by pye52

Merge branch 'fea-payoffline_split' into develop

# Conflicts:
#	app/src/main/java/com/bgycc/smartcanteen/data/dao/PayDataDao.java
parents bf4d5769 a80af9ec
......@@ -74,6 +74,14 @@ public class PayOfflineViewModel extends ViewModel {
SCTaskExecutor.getInstance().schedule(runnable, REQUEST_DELAY, TimeUnit.MILLISECONDS);
}
private void traversalPayOfflineData() {
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
timeoutFuture = SCTaskExecutor.getInstance().schedule(timeoutRunnable, TIMEOUT, TimeUnit.MILLISECONDS);
RequestRunnable runnable = new RequestRunnable();
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
}
private void cancelTimeout() {
if (timeoutFuture == null) return;
timeoutFuture.cancel(true);
......@@ -86,12 +94,7 @@ public class PayOfflineViewModel extends ViewModel {
@Override
public void onOpen(ServerHandshake data) {
// 只要Socket链接成功,则搜索数据库需要离线支付的订单,并发送给后台
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
timeoutFuture = SCTaskExecutor.getInstance().schedule(timeoutRunnable, TIMEOUT, TimeUnit.MILLISECONDS);
RequestRunnable runnable = new RequestRunnable();
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
traversalPayOfflineData();
}
@Override
......@@ -105,12 +108,13 @@ public class PayOfflineViewModel extends ViewModel {
// 1、没有action
// 2、message为"操作完成"
LogUtils.d(TAG, "离线支付结果响应: " + original);
cancelTimeout();
ResponseRunnable runnable = new ResponseRunnable(original);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
}
};
// 当支付时WebSocket未链接,则走离线支付流程
// 此时直接标记订单为"离线支付"状态
private class MarkRunnable implements Runnable {
private PayData payData;
......@@ -141,14 +145,14 @@ public class PayOfflineViewModel extends ViewModel {
private class RequestRunnable implements Runnable {
@Override
public void run() {
// 获取需要离线支付的订单
List<PayData> payOfflineData = payDataRepository.queryPayOfflineData();
if (payOfflineData == null || payOfflineData.isEmpty()) {
List<PayData> payData = payDataRepository.queryPayOfflineData();
if (payData == null || payData.isEmpty()) {
cancelTimeout();
payRequest = null;
LogUtils.d(TAG, "所有离线订单处理完毕");
return;
}
payRequest = new PayRequest(deviceSN, payOfflineData);
payRequest = new PayRequest(deviceSN, payData);
String requestStr = gson.toJson(payRequest);
payOfflineState.postValue(new PayOfflineState(PayOfflineState.SEND, requestStr));
SCWebSocketClient.getInstance().send(requestStr);
......@@ -165,12 +169,14 @@ public class PayOfflineViewModel extends ViewModel {
@Override
public void run() {
cancelTimeout();
if (payRequest == null || payRequest.getData().isEmpty()) {
LogUtils.w(TAG, "后台返回离线支付结果,但没有待处理任务");
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
return;
}
// 保存服务器返回的结果
PayResponse payResponse = gson.fromJson(response, PayResponse.class);
payOfflineState.postValue(new PayOfflineState(PayOfflineState.SUCCESS, payResponse.getMessage()));
long lastInsertId = payResponseRepository.insertPayResponse(payResponse);
......@@ -178,18 +184,18 @@ public class PayOfflineViewModel extends ViewModel {
LogUtils.w(TAG, "离线支付结果插入数据库失败: " + payResponse.toString());
}
List<PayData> dataList = payRequest.getData();
// 设置所有离线支付订单状态为支付成功
List<PayData> payData = payRequest.getData();
// 设置离线支付订单状态为支付成功
// 同时在日志中记录所有订单的payCode
StringBuilder ids = new StringBuilder();
for (PayData data : dataList) {
for (PayData data : payData) {
ids.append(data.getPayCode())
.append(",");
data.paySuccess();
}
payDataRepository.updatePayData(dataList);
payDataRepository.updatePayData(payData);
if (ids.length() > 0) {
LogUtils.d(TAG, "待上传的所有离线支付订单号: " + ids.substring(0, ids.length() - 1));
LogUtils.d(TAG, "已上传的离线支付订单号: " + ids.substring(0, ids.length() - 1));
}
payRequest = null;
......@@ -198,10 +204,13 @@ public class PayOfflineViewModel extends ViewModel {
} catch (Exception ignored) {
} finally {
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
// 继续处理下一批离线订单
traversalPayOfflineData();
}
}
}
// 离线订单发送到后台超时后,重置其标志位保存到数据库
private class TimeoutRunnable implements Runnable {
@Override
public void run() {
......
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