Commit 522b63d4 by pye52

不再通过数据监听的方式实现离线订单遍历

parent 8bfdc54a
package com.bgycc.smartcanteen.data.dao;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
......@@ -14,15 +13,9 @@ import java.util.List;
@Dao
public interface PayDataDao {
/**
* 获取所有需要离线支付的订单
* 获取需要离线支付的订单
*/
@Query("select * from paydata where payState == -1 limit 10")
LiveData<List<PayData>> queryPayOfflineDataLiveData();
/**
* 获取所有需要离线支付的订单
*/
@Query("select * from paydata where payState == -1 limit 10")
@Query("select * from paydata where payState == -1 limit 2")
List<PayData> queryPayOfflineData();
/**
......
package com.bgycc.smartcanteen.repository;
import androidx.lifecycle.LiveData;
import com.bgycc.smartcanteen.data.dao.PayDataDao;
import com.bgycc.smartcanteen.entity.PayData;
......@@ -14,10 +12,6 @@ public class PayDataRepository {
this.dao = dao;
}
public LiveData<List<PayData>> queryPayOfflineDataLiveData() {
return dao.queryPayOfflineDataLiveData();
}
public List<PayData> queryPayOfflineData() {
return dao.queryPayOfflineData();
}
......
......@@ -4,7 +4,6 @@ import android.text.TextUtils;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import com.bgycc.smartcanteen.entity.PayData;
......@@ -48,7 +47,6 @@ public class PayOfflineViewModel extends ViewModel {
private PayResponseRepository payResponseRepository;
private MutableLiveData<PayOfflineState> payOfflineState = new MutableLiveData<>();
private LiveData<List<PayData>> payOfflineData;
public LiveData<PayOfflineState> getPayOfflineStateEvent() {
return payOfflineState;
......@@ -65,7 +63,6 @@ public class PayOfflineViewModel extends ViewModel {
this.payResponseRepository = payResponseRepository;
this.gson = gson;
this.deviceSN = deviceSN;
this.payOfflineData = payDataRepository.queryPayOfflineDataLiveData();
}
public void initialize() {
......@@ -77,22 +74,12 @@ public class PayOfflineViewModel extends ViewModel {
SCTaskExecutor.getInstance().schedule(runnable, REQUEST_DELAY, TimeUnit.MILLISECONDS);
}
private void addDataObserver() {
if (SCTaskExecutor.getInstance().isMainThread()) {
payOfflineData.observeForever(dataObserver);
} else {
SCTaskExecutor.getMainThreadExecutor()
.execute(() -> payOfflineData.observeForever(dataObserver));
}
}
private void removeDataObserver() {
if (SCTaskExecutor.getInstance().isMainThread()) {
payOfflineData.removeObserver(dataObserver);
} else {
SCTaskExecutor.getMainThreadExecutor()
.execute(() -> payOfflineData.removeObserver(dataObserver));
}
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() {
......@@ -101,27 +88,13 @@ public class PayOfflineViewModel extends ViewModel {
timeoutFuture = null;
}
private Observer<List<PayData>> dataObserver = payData -> {
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
timeoutFuture = SCTaskExecutor.getInstance().schedule(timeoutRunnable, TIMEOUT, TimeUnit.MILLISECONDS);
RequestRunnable runnable = new RequestRunnable(payData);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
};
private SCWebSocketListener listener = new SCWebSocketListenerAdapter() {
private static final String RESPONSE_MESSAGE = "message";
private static final String RESPONSE_OFFLINE_RESULT = "操作完成";
@Override
public void onOpen(ServerHandshake data) {
addDataObserver();
// 只要Socket链接成功,则搜索数据库需要离线支付的订单,并发送给后台
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
timeoutFuture = SCTaskExecutor.getInstance().schedule(timeoutRunnable, TIMEOUT, TimeUnit.MILLISECONDS);
RequestRunnable runnable = new RequestRunnable();
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
traversalPayOfflineData();
}
@Override
......@@ -139,19 +112,9 @@ public class PayOfflineViewModel extends ViewModel {
ResponseRunnable runnable = new ResponseRunnable(original);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
}
@Override
public void onClose(int code, String reason, boolean remote) {
removeDataObserver();
}
@Override
public void onError(Exception ex) {
removeDataObserver();
}
};
// 当支付时websocket未链接,则走离线支付流程
// 当支付时WebSocket未链接,则走离线支付流程
// 此时直接标记订单为"离线支付"状态
private class MarkRunnable implements Runnable {
private PayData payData;
......@@ -181,26 +144,12 @@ public class PayOfflineViewModel extends ViewModel {
}
private class RequestRunnable implements Runnable {
private List<PayData> payData;
RequestRunnable() {
}
RequestRunnable(List<PayData> payData) {
this.payData = payData;
}
@Override
public void run() {
if (payData == null) {
payData = payDataRepository.queryPayOfflineData();
}
List<PayData> payData = payDataRepository.queryPayOfflineData();
if (payData == null || payData.isEmpty()) {
cancelTimeout();
payRequest = null;
// 所有离线订单处理完毕后,断开数据监听
removeDataObserver();
LogUtils.d(TAG, "所有离线订单处理完毕");
return;
}
......@@ -235,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;
......@@ -255,6 +204,8 @@ public class PayOfflineViewModel extends ViewModel {
} catch (Exception ignored) {
} finally {
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
// 继续处理下一批离线订单
traversalPayOfflineData();
}
}
}
......
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