Просмотр исходного кода

feat(解锁):解锁时检查订单云端状态

Zhenghj 1 год назад
Родитель
Сommit
2fe131073a

+ 2 - 2
app/build.gradle

@@ -21,8 +21,8 @@ android {
         applicationId "com.doverfuelingsolutions.issp"
         minSdkVersion 22
         targetSdkVersion 26
-        versionCode 20
-        versionName "1.2.07"
+        versionCode 21
+        versionName "1.2.08"
         archivesBaseName = versionName + "." + getTime()
 
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

+ 3 - 1
app/src/main/java/com/doverfuelingsolutions/issp/api/WayneApiConfig.kt

@@ -82,7 +82,9 @@ class WayneApiConfig {
             return if (domain.isEmpty() || port.isEmpty()) HOST_ASSETS_DEFAULT else "$domain:$port"
         }
         val CHECK_ORDER_BASE:String get() {
-            return "http://tkhs.net.cn:8721"
+            return "http://tkhs.net.cn:8721"  //生产
+//            return "http://47.97.120.160:8721"//测试
+//            return "http://47.101.220.106:8721" //开发
         }
     }
 }

+ 5 - 0
app/src/main/java/com/doverfuelingsolutions/issp/fusion/FusionManager.kt

@@ -190,6 +190,11 @@ object FusionManager : LifecycleObserver, OnFdcClientStateChangedListener,
             val ffst = fdcMessage as FdcMessageFuelSaleTrx
             if (ffst.singleDeviceClass == null) return
             val dc = ffst.singleDeviceClass
+
+            //获取到的 dc 中 releaseTokenAttribute 为 null ,而 releaseTokenElement 不为 null ,将其值赋予 releaseTokenAttribute
+            //两者意义相同,这里赋予 releaseTokenAttribute 是让下面 SPUtil.removexxx 能正常找到订单,进而删除
+            if (dc.releaseTokenAttribute == null && dc.releaseTokenElement != null) dc.releaseTokenAttribute = dc.releaseTokenElement
+            if (dc.releaseTokenElement == null && dc.releaseTokenAttribute != null) dc.releaseTokenElement = dc.releaseTokenAttribute
             // 到这获取到更新的订单——或全新,或旧订单更新,或旧订单删除
 
             if (null != dc.state) {

+ 1 - 1
app/src/main/java/com/doverfuelingsolutions/issp/utils/sp/SPUtil.kt

@@ -272,7 +272,7 @@ object SPUtil {
         val symbol = getDeviceClassSymbol(dc)
         val list = getLockOrderList()
         //超过一定数量要删除最先的 ,可能自助机解锁不成功后后台帮忙解锁,那么这笔单在自助机就会一直解锁不成功(因为订单已经解锁了)
-        if (list.size > 100) list.removeAt(0)
+        if (list.size > 5) list.removeAt(0)
         DFSLog.v("ADD ORDER_LOCK START $symbol,the size of lock order list is ${list.size}")
         if (!list.contains(symbol)) {
             DFSLog.v("add order_lock $symbol")

+ 60 - 4
app/src/main/java/com/doverfuelingsolutions/issp/view/MainActivity.kt

@@ -53,6 +53,7 @@ import java.math.BigDecimal
 import java.text.DateFormat
 import java.text.SimpleDateFormat
 import java.util.*
+import kotlin.collections.HashMap
 
 class MainActivity : AppCompatActivity(),
     View.OnClickListener,
@@ -78,6 +79,8 @@ class MainActivity : AppCompatActivity(),
     private var homeAndMenuReceiver: HomeAndMenuReceiver? = null
     private var isInLockTaskMode: Boolean = false
 
+    private val orderClearCountMap:HashMap<String,Int> = hashMapOf() //用于记录消单次数
+
     private val mLoginTokenRefresher = LoginTokenRefresher()
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -122,7 +125,6 @@ class MainActivity : AppCompatActivity(),
         fragmentRouter.stopFragmentToolbarTimer()
         FusionManager.onFusionStatus = null
         mLoginTokenRefresher.stop()
-        this.stopLockTask()
     }
 
     @SuppressLint("MissingSuperCall")
@@ -153,7 +155,7 @@ class MainActivity : AppCompatActivity(),
                         )
 
                         if (isRelogin) {
-                            this@MainActivity.stopLockTask()
+                            stopLockTask()
 
                             GlobalData.isLogin = false
                             GlobalData.password.set("")
@@ -350,10 +352,48 @@ class MainActivity : AppCompatActivity(),
 
             val lockList = SPUtil.getLockOrderList()
             // 解锁订单
+            //先查询云端状态,如果是支付过了,走消单流程;未支付的,走解锁流程
             lockList.let { list ->
                 if (list.isEmpty()) return@let
 
-                val deferredList = list.map { item ->
+                //查云端状态
+                val checkList = list.map { item ->
+                    val info = item.split("-")
+                    val pumpId = info[0].toInt()
+                    val transactionNo = info[1]
+                    async {
+                        val deviceClass = DeviceClass()
+                        deviceClass.pumpNo = pumpId
+                        deviceClass.transactionSeqNo = transactionNo
+                        SystemApi.getOrderStateForClound(deviceClass)
+                    }
+                }
+                val checkResultList = checkList.map { item -> item.await() }
+                val unLockList = arrayListOf<String>()
+                checkResultList.forEachIndexed { index, dfsResult ->
+                    if (dfsResult.success) {
+                        val iListResult = dfsResult.data?.iListResult
+                        val find = iListResult?.find { it.resultCode == "0" }
+                        if (find != null) { //这笔单已支付了,删去待锁单记录,加入待消单记录,进行消单
+                            val info = list[index].split("-")
+                            val pumpId = info[0].toInt()
+                            val transactionNo = info[1]
+                            val token = info[2]
+                            val deviceClass = DeviceClass()
+                            deviceClass.pumpNo = pumpId
+                            deviceClass.transactionSeqNo = transactionNo
+                            deviceClass.releaseTokenAttribute = token
+
+                            SPUtil.removeLockOrder(deviceClass)
+                            SPUtil.addUnclearedOrder(deviceClass)
+                        } else {            //这笔单还未支付,加入解锁列表,做解锁
+                            unLockList.add(list[index])
+                        }
+                    }
+                }
+
+                //做解锁
+                val deferredList = unLockList.map { item ->
                     val info = item.split("-")
                     val pumpId = info[0].toInt()
                     val transactionNo = info[1]
@@ -397,10 +437,26 @@ class MainActivity : AppCompatActivity(),
                     if (result.success) {
                         DFSLog.d("$tip succeeded")
                         SPUtil.removeWayneOrder(list[index])
+                        orderClearCountMap.remove(list[index])
                         null
                     } else {
                         DFSLog.d("$tip failed")
-                        list[index]
+                        /**          消单失败,会记录次数,如果三次消单都失败,便不再对这笔单消单
+                         * 场景:这笔单在自助机执行 SPUtil.addUnclearedOrder(deviceClass) 前,被其他设备 clear 了,
+                              这个时候自助机再 clear 就肯定失败了,而且也无意义。
+                              虽然订单为 clear 状态时会在  FusionManager.onFdcMessageReceived 收到该订单为 clear,
+                         并调用 SPUtil.removeUnclearedOrder(dc) 删除,但如果是 FusionManager.onFdcMessageReceived
+                         执行完后再 SPUtil.addUnclearedOrder(deviceClass), 就会一直存在 SP 中尝试 clear。因此加了clear 次数
+                        */
+                        val unclearItem = list[index]
+                        val clearTime = orderClearCountMap[unclearItem] ?: 1
+                        if (clearTime >= 3) {
+                            orderClearCountMap.remove(unclearItem)
+                            null
+                        } else {
+                            orderClearCountMap[unclearItem] = clearTime + 1
+                            list[index]
+                        }
                     }
                 }.filterNotNull()
                 SPUtil.replaceUnclearedOrderList(remainLockList)

+ 22 - 1
app/src/main/java/com/doverfuelingsolutions/issp/view/fragment/FragmentOrderList.kt

@@ -128,6 +128,26 @@ class FragmentOrderList private constructor() : FragmentBasic(),RefreshOrder {
                 }
                 type == 1 && dc.isLock -> {
                     if (dc.lockingApplicationSender == GlobalData.workStationId.get() || BuildConfig.DEBUG) {
+                        val checkResult = SystemApi.getOrderStateForClound(dc)
+                        if (checkResult.success) {
+                            val results = checkResult.data?.iListResult
+                            val find = results?.find { it.resultCode == "0" }
+                            if (find != null) { //表示这边订单已经支付过,接下来直接消单即可
+                                DFSToastUtil.info("此订单已支付,请勿重复支付")
+                                SPUtil.removeLockOrder(dc)
+                                val resultClear = FusionManager.clearOrder(dc)
+                                if (resultClear.success) {
+                                    dc.state = "Cleared"
+                                    SPUtil.removeUnclearedOrder(dc)
+                                    SPUtil.removeWayneOrder(dc)
+                                } else {
+                                    // 失败则记下来,后续再尝试消单
+                                    SPUtil.addUnclearedOrder(dc)
+                                }
+                                return@launchWhenStarted
+                            }
+                        }
+
                         val result = FusionManager.lockOrder(dc, false)
                         if (result.success) {
                             dc.state = "Payable"
@@ -157,7 +177,7 @@ class FragmentOrderList private constructor() : FragmentBasic(),RefreshOrder {
                 val results = checkResult.data?.iListResult
                 val find = results?.find { it.resultCode == "0" }
                 if (find != null) { //表示这边订单已经支付过,接下来直接消单即可
-
+                    dialog.dismiss()
                     // 支付成功后,移除本地云订单,清单
                     GlobalScope.launch(Dispatchers.IO) {
                         SPUtil.removeLockOrder(dc)
@@ -244,6 +264,7 @@ class FragmentOrderList private constructor() : FragmentBasic(),RefreshOrder {
                     }
                 }
             } else {
+                dialog.dismiss()
                 DFSToastUtil.fail("查询云端订单状态失败 ${checkResult.message}")
             }