Commit dee7658c by pye52

现在离线订单将会采取分批上传(避免一次性上传太多订单导致后台处理异常)

parent 74ca81fd
package com.bgycc.smartcanteen.data.dao;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
......@@ -15,7 +16,13 @@ public interface PayDataDao {
/**
* 获取所有需要离线支付的订单
*/
@Query("select * from paydata where payState == -1")
@Query("select * from paydata where payState == -1 limit 10")
LiveData<List<PayData>> queryPayOfflineDataLiveData();
/**
* 获取所有需要离线支付的订单
*/
@Query("select * from paydata where payState == -1 limit 10")
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;
......@@ -12,6 +14,10 @@ public class PayDataRepository {
this.dao = dao;
}
public LiveData<List<PayData>> queryPayOfflineDataLiveData() {
return dao.queryPayOfflineDataLiveData();
}
public List<PayData> queryPayOfflineData() {
return dao.queryPayOfflineData();
}
......
......@@ -4,6 +4,7 @@ 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;
......@@ -47,6 +48,7 @@ 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;
......@@ -63,6 +65,7 @@ public class PayOfflineViewModel extends ViewModel {
this.payResponseRepository = payResponseRepository;
this.gson = gson;
this.deviceSN = deviceSN;
this.payOfflineData = payDataRepository.queryPayOfflineDataLiveData();
}
public void initialize() {
......@@ -80,12 +83,21 @@ 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) {
payOfflineData.observeForever(dataObserver);
// 只要Socket链接成功,则搜索数据库需要离线支付的订单,并发送给后台
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
......@@ -109,6 +121,16 @@ public class PayOfflineViewModel extends ViewModel {
ResponseRunnable runnable = new ResponseRunnable(original);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
}
@Override
public void onClose(int code, String reason, boolean remote) {
payOfflineData.removeObserver(dataObserver);
}
@Override
public void onError(Exception ex) {
payOfflineData.removeObserver(dataObserver);
}
};
private class MarkRunnable implements Runnable {
......@@ -139,16 +161,30 @@ 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() {
// 获取需要离线支付的订单
List<PayData> payOfflineData = payDataRepository.queryPayOfflineData();
if (payOfflineData == null || payOfflineData.isEmpty()) {
if (payData == null) {
payData = payDataRepository.queryPayOfflineData();
}
if (payData == null || payData.isEmpty()) {
cancelTimeout();
payRequest = null;
// 所有离线订单处理完毕后,断开数据监听
payOfflineData.removeObserver(dataObserver);
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);
......
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