Commit 522b63d4 by pye52

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

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