Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ad6d53af
authored
May 07, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
现在离线订单将会采取分批上传(避免一次性上传太多订单导致后台处理异常)
parent
e88899ac
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
5 deletions
+54
-5
app/src/main/java/com/bgycc/smartcanteen/data/dao/PayDataDao.java
+8
-1
app/src/main/java/com/bgycc/smartcanteen/repository/PayDataRepository.java
+6
-0
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
+40
-4
No files found.
app/src/main/java/com/bgycc/smartcanteen/data/dao/PayDataDao.java
View file @
ad6d53af
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
();
/**
...
...
app/src/main/java/com/bgycc/smartcanteen/repository/PayDataRepository.java
View file @
ad6d53af
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
();
}
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
View file @
ad6d53af
...
...
@@ -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
,
pay
Offline
Data
);
payRequest
=
new
PayRequest
(
deviceSN
,
payData
);
String
requestStr
=
gson
.
toJson
(
payRequest
);
payOfflineState
.
postValue
(
new
PayOfflineState
(
PayOfflineState
.
SEND
,
requestStr
));
SCWebSocketClient
.
getInstance
().
send
(
requestStr
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment