|
@@ -1,5 +1,6 @@
|
|
|
package com.doverfuelingsolutions.issp.view.fragment
|
|
|
|
|
|
+import android.content.Context
|
|
|
import android.os.Bundle
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
@@ -7,20 +8,20 @@ import android.view.ViewGroup
|
|
|
import androidx.databinding.DataBindingUtil
|
|
|
import androidx.fragment.app.Fragment
|
|
|
import androidx.fragment.app.viewModels
|
|
|
+import androidx.lifecycle.LifecycleObserver
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
import com.doverfuelingsolutions.issp.DFSApplication
|
|
|
import com.doverfuelingsolutions.issp.R
|
|
|
+import com.doverfuelingsolutions.issp.api.SystemApi
|
|
|
import com.doverfuelingsolutions.issp.data.GlobalData
|
|
|
import com.doverfuelingsolutions.issp.databinding.FragmentSearchTypeBinding
|
|
|
import com.doverfuelingsolutions.issp.fusion.FusionManager
|
|
|
import com.doverfuelingsolutions.issp.utils.log.DFSLog
|
|
|
import com.doverfuelingsolutions.issp.utils.sp.SPUtil
|
|
|
import com.doverfuelingsolutions.issp.view.MainActivity
|
|
|
-import kotlinx.coroutines.Dispatchers
|
|
|
-import kotlinx.coroutines.GlobalScope
|
|
|
-import kotlinx.coroutines.async
|
|
|
-import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.*
|
|
|
|
|
|
class FragmentSelect : Fragment(),
|
|
|
View.OnClickListener {
|
|
@@ -28,6 +29,8 @@ class FragmentSelect : Fragment(),
|
|
|
private lateinit var binding: FragmentSearchTypeBinding
|
|
|
private val viewModel: SearchTypeViewModel by viewModels()
|
|
|
|
|
|
+ private var job: Job? = null
|
|
|
+
|
|
|
override fun onCreateView(
|
|
|
inflater: LayoutInflater,
|
|
|
container: ViewGroup?,
|
|
@@ -49,16 +52,51 @@ class FragmentSelect : Fragment(),
|
|
|
handleUnsolvedOrder()
|
|
|
}
|
|
|
|
|
|
+ override fun onStart() {
|
|
|
+ super.onStart()
|
|
|
+
|
|
|
+ refreshLoginToken()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStop() {
|
|
|
+ super.onStop()
|
|
|
+
|
|
|
+ job?.let {
|
|
|
+ if (it.isActive) it.cancel()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onClick(v: View?) {
|
|
|
+ when (v) {
|
|
|
+ binding.chooseNozzle -> {
|
|
|
+ (activity as MainActivity).setFragment(
|
|
|
+ FragmentNozzle.build(FusionManager.pumpList),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ }
|
|
|
+ // TODO 油品选择
|
|
|
+ binding.chooseOil -> {
|
|
|
+ (activity as MainActivity).setFragment(
|
|
|
+ FragmentNozzle.build(FusionManager.pumpList),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 当本地未处理订单无法按预期处理时(如本地锁的单,但在服务器上被其他渠道解锁了,造成解锁失败;消单同理;)
|
|
|
/* 未处理的订单 */
|
|
|
private fun handleUnsolvedOrder() {
|
|
|
- DFSLog.i("「未解锁,未消」订单批处理")
|
|
|
+ DFSLog.i("try handle all locked, uncleared orders created by this device")
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
+ val lockList = SPUtil.getLockOrderList()
|
|
|
+ val unclearedList = SPUtil.getUnclearedOrderList()
|
|
|
+ DFSLog.d("size of locked orders: ${lockList.size}, uncleared orders: ${unclearedList.size}")
|
|
|
// 解锁订单
|
|
|
- SPUtil.getLockOrderList().let { lockList ->
|
|
|
- DFSLog.d("未解锁订单数:${lockList.size}")
|
|
|
- if (lockList.isEmpty()) return@let
|
|
|
+ lockList.let { list ->
|
|
|
+ if (list.isEmpty()) return@let
|
|
|
|
|
|
- val deferredList = lockList.map { item ->
|
|
|
+ val deferredList = list.map { item ->
|
|
|
val info = item.split("-")
|
|
|
val pumpId = info[0].toInt()
|
|
|
val transactionNo = info[1]
|
|
@@ -69,23 +107,22 @@ class FragmentSelect : Fragment(),
|
|
|
}
|
|
|
val resultList = deferredList.map { item -> item.await() }
|
|
|
val remainLockList = resultList.mapIndexed { index, result ->
|
|
|
- val tip = "自动解锁订单「${lockList[index]}」"
|
|
|
+ val tip = "unlock order「${list[index]}」"
|
|
|
if (result.success) {
|
|
|
- DFSLog.d("$tip 成功")
|
|
|
+ DFSLog.d("$tip succeeded")
|
|
|
null
|
|
|
} else {
|
|
|
- DFSLog.d("$tip 失败")
|
|
|
- lockList[index]
|
|
|
+ DFSLog.d("$tip failed")
|
|
|
+ list[index]
|
|
|
}
|
|
|
}.filterNotNull()
|
|
|
SPUtil.replaceLockOrderList(remainLockList)
|
|
|
}
|
|
|
// 消除订单
|
|
|
- SPUtil.getUnclearedOrderList().let { unclearedList ->
|
|
|
- DFSLog.d("未消订单数:${unclearedList.size}")
|
|
|
- if (unclearedList.isEmpty()) return@let
|
|
|
+ unclearedList.let { list ->
|
|
|
+ if (list.isEmpty()) return@let
|
|
|
|
|
|
- val deferredList = unclearedList.map { item ->
|
|
|
+ val deferredList = list.map { item ->
|
|
|
val info = item.split("-")
|
|
|
val pumpId = info[0].toInt()
|
|
|
val transactionNo = info[1]
|
|
@@ -96,13 +133,13 @@ class FragmentSelect : Fragment(),
|
|
|
}
|
|
|
val resultList = deferredList.map { item -> item.await() }
|
|
|
val remainLockList = resultList.mapIndexed { index, result ->
|
|
|
- val tip = "自动消单「${unclearedList[index]}」"
|
|
|
+ val tip = "clear order「${list[index]}」"
|
|
|
if (result.success) {
|
|
|
- DFSLog.d("$tip 成功")
|
|
|
+ DFSLog.d("$tip succeeded")
|
|
|
null
|
|
|
} else {
|
|
|
- DFSLog.d("$tip 失败")
|
|
|
- unclearedList[index]
|
|
|
+ DFSLog.d("$tip failed")
|
|
|
+ list[index]
|
|
|
}
|
|
|
}.filterNotNull()
|
|
|
SPUtil.replaceUnclearedOrderList(remainLockList)
|
|
@@ -110,20 +147,25 @@ class FragmentSelect : Fragment(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onClick(v: View?) {
|
|
|
- when (v) {
|
|
|
- binding.chooseNozzle -> {
|
|
|
- (activity as MainActivity).setFragment(
|
|
|
- FragmentNozzle.build(FusionManager.pumpList),
|
|
|
- true
|
|
|
- )
|
|
|
+ /* 刷新登录状态 */
|
|
|
+ private fun refreshLoginToken() {
|
|
|
+ // 登录有效时长两小时,为了避免在操作过程出现接口验证失败
|
|
|
+ job = lifecycleScope.launch {
|
|
|
+ // 8min 余留,避免边缘时刻进入流程
|
|
|
+ val expireMoment = GlobalData.accessTokenExpire.get()
|
|
|
+ val delayTime = expireMoment - 480000 - System.currentTimeMillis()
|
|
|
+ if (delayTime > 0) {
|
|
|
+ delay(delayTime)
|
|
|
}
|
|
|
- // TODO 油品选择
|
|
|
- binding.chooseOil -> {
|
|
|
- (activity as MainActivity).setFragment(
|
|
|
- FragmentNozzle.build(FusionManager.pumpList),
|
|
|
- true
|
|
|
- )
|
|
|
+ val result = SystemApi.login(GlobalData.accountName.get(), GlobalData.password.get())
|
|
|
+ if (result.success) {
|
|
|
+ DFSLog.d("loginToken refresh succeeded")
|
|
|
+ // 处理持续停留
|
|
|
+ refreshLoginToken()
|
|
|
+ } else {
|
|
|
+ DFSLog.d("loginToken refresh failed: ${result.message}")
|
|
|
+ // 刷新失败返回登录界面
|
|
|
+ (activity as MainActivity).backToLogin()
|
|
|
}
|
|
|
}
|
|
|
}
|