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