From dd2562d8dc2b01bcdfca3152f82f09efbbd09259 Mon Sep 17 00:00:00 2001
From: zuoxiao <lf_zuo@163.com>
Date: 星期三, 25 六月 2025 19:49:48 +0800
Subject: [PATCH] fix(generallibrary): 优化卡片处理和支付方式获取逻辑- 修复地区卡处理逻辑,增加对"00"类型卡的特殊处理 - 优化用户卡数据解析和显示逻辑,提高容错性 - 改进支付方式获取方法,增加错误处理和日志记录 -调整充值接口调用参数,确保正确传递当前余额等信息- 修复 AreaCard 中 areaNumber 类型,改为字符串处理

---
 generallibrary/src/main/java/com/dayu/general/activity/RechargeDetailActivity.kt |  184 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 152 insertions(+), 32 deletions(-)

diff --git a/generallibrary/src/main/java/com/dayu/general/activity/RechargeDetailActivity.kt b/generallibrary/src/main/java/com/dayu/general/activity/RechargeDetailActivity.kt
index 8ea5625..7658e5b 100644
--- a/generallibrary/src/main/java/com/dayu/general/activity/RechargeDetailActivity.kt
+++ b/generallibrary/src/main/java/com/dayu/general/activity/RechargeDetailActivity.kt
@@ -23,6 +23,7 @@
 import com.dayu.general.bean.net.RechargeRequest
 import com.dayu.general.bean.net.RechargeResult
 import com.dayu.general.bean.card.UserCard
+import com.dayu.general.bean.net.PaymentMethodListResponse
 import com.dayu.general.databinding.ActivityRechargeDetailBinding
 import com.dayu.general.net.ApiManager
 import com.dayu.general.net.BaseResponse
@@ -38,7 +39,7 @@
 
     // 鏀粯鏂瑰紡鐩稿叧灞炴��
     private var paymentMethod: String = "鐜伴噾"
-    private var paymentId: Long = 0
+    private var paymentId: String = ""
     private var paymentMethodList: List<PaymentMethod> = listOf()
 
     companion object {
@@ -149,23 +150,60 @@
     }
 
     /**
-     * 鑾峰彇鏀粯鏂瑰紡鍒楄〃
+     * 鑾峰彇鏀粯鏂瑰紡鍒楄〃 - 浣跨敤Object绫诲瀷瀹夊叏澶勭悊JSON鏁扮粍
      */
     private fun getPaymentMethods() {
         ApiManager.getInstance().requestGetLoading(
             this,
-            "sell/paymentmethod/get",
-            PaymentMethodResponse::class.java,
+            "terminal/paymentmethod/get",
+            Any::class.java,
             null,
-            object : SubscriberListener<BaseResponse<PaymentMethodResponse>>() {
-                override fun onNext(response: BaseResponse<PaymentMethodResponse>) {
+            object : SubscriberListener<BaseResponse<Any>>() {
+                override fun onNext(response: BaseResponse<Any>) {
                     if (response.success) {
-                        // 鑾峰彇鏀粯鏂瑰紡鍒楄〃
-                        val paymentMethods = response.content?.obj ?: listOf()
-                        if (paymentMethods.isNotEmpty()) {
-                            paymentMethodList = paymentMethods
-                            // 鏇存柊鏀粯鏂瑰紡鏄剧ず
-                            updatePaymentMethodRadioGroup()
+                        try {
+                            // 瀹夊叏鍦板鐞嗚繑鍥炵殑content锛屾湇鍔″櫒杩斿洖鐨勬槸ArrayList<LinkedHashMap>
+                            val paymentMethods = mutableListOf<PaymentMethod>()
+                            val content = response.content
+                            
+                            if (content is List<*>) {
+                                content.forEach { item ->
+                                    if (item is Map<*, *>) {
+                                        val id = item["id"]?.toString() ?: ""
+                                        val name = item["name"]?.toString() ?: ""
+                                        if (id.isNotEmpty() && name.isNotEmpty()) {
+                                            paymentMethods.add(PaymentMethod(id, name))
+                                        }
+                                    }
+                                }
+                            }
+                            
+                            if (paymentMethods.isNotEmpty()) {
+                                paymentMethodList = paymentMethods
+                                // 鏇存柊鏀粯鏂瑰紡鏄剧ず
+                                updatePaymentMethodRadioGroup()
+                                
+                                // 璋冭瘯鏃ュ織
+                                android.util.Log.d("RechargeDetail", "鎴愬姛鑾峰彇${paymentMethods.size}涓敮浠樻柟寮�:")
+                                paymentMethods.forEach { method ->
+                                    android.util.Log.d("RechargeDetail", "- ID: ${method.id}, Name: ${method.name}")
+                                }
+                            } else {
+                                Toast.makeText(
+                                    this@RechargeDetailActivity,
+                                    "鑾峰彇鏀粯鏂瑰紡澶辫触锛氳繑鍥炴暟鎹负绌�",
+                                    Toast.LENGTH_SHORT
+                                ).show()
+                            }
+                        } catch (e: Exception) {
+                            android.util.Log.e("RechargeDetail", "瑙f瀽鏀粯鏂瑰紡鏁版嵁澶辫触", e)
+                            android.util.Log.e("RechargeDetail", "鍘熷鏁版嵁绫诲瀷: ${response.content?.javaClass?.name}")
+                            android.util.Log.e("RechargeDetail", "鍘熷鏁版嵁鍐呭: $response.content")
+                            Toast.makeText(
+                                this@RechargeDetailActivity,
+                                "瑙f瀽鏀粯鏂瑰紡鏁版嵁澶辫触: ${e.message}",
+                                Toast.LENGTH_SHORT
+                            ).show()
                         }
                     } else {
                         Toast.makeText(
@@ -178,6 +216,7 @@
 
                 override fun onError(e: Throwable?) {
                     super.onError(e)
+                    android.util.Log.e("RechargeDetail", "缃戠粶璇锋眰澶辫触", e)
                     Toast.makeText(
                         this@RechargeDetailActivity,
                         "鑾峰彇鏀粯鏂瑰紡澶辫触: ${e?.message ?: "缃戠粶寮傚父"}",
@@ -267,7 +306,7 @@
             binding.redRemainderBlance.text = "$balance 鍏�"
 
             // 璁剧疆鍗$姸鎬佸拰瀵瑰簲棰滆壊
-            val cardStatus = when (info.status) {
+            val cardStatus = when (info.state) {
                 1 -> "姝e父"
                 2 -> "鎸傚け"
                 3 -> "閿佸畾"
@@ -277,7 +316,7 @@
             binding.redStatu.text = cardStatus
 
             // 鏍规嵁鍗$姸鎬佽缃笉鍚岄鑹�
-            val statusColor = when (info.status) {
+            val statusColor = when (info.state) {
                 1 -> android.graphics.Color.parseColor("#4CAF50") // 缁胯壊-姝e父
                 2 -> android.graphics.Color.parseColor("#FF9800") // 姗欒壊-鎸傚け
                 3 -> android.graphics.Color.parseColor("#F44336") // 绾㈣壊-閿佸畾
@@ -322,31 +361,62 @@
             0.0
         }
 
-        // 璋冪敤鍏呭�兼帴鍙�
-        callRechargeApi(rechargeAmount, bonusAmount)
+        // 鑾峰彇褰撳墠浣欓锛堣浆鎹负鍏冿級
+        val currentBalance = userCard?.let {
+            // 灏嗗垎杞崲涓哄厓
+            it.balance / 100.0
+        } ?: run {
+            // 濡傛灉鐢ㄦ埛鍗′负绌猴紝鍒欎娇鐢ㄦ湇鍔″櫒杩斿洖鐨勪綑棰�
+            cardInfo?.balance ?: 0.0
+        }
+
+        // 璁$畻鍏呭�煎悗鐨勬�讳綑棰�
+        val totalAmountAfterRecharge = currentBalance + rechargeAmount + bonusAmount
+
+        // 妫�鏌ユ槸鍚﹁秴杩囨渶澶т綑棰濋檺鍒�9999.99鍏�
+        if (totalAmountAfterRecharge > 9999.99) {
+            val maxRechargeAmount = 9999.99 - currentBalance
+            ToastUtil.show("鍏呭�煎け璐ワ細鍏呭�煎悗浣欓涓嶈兘瓒呰繃9999.99鍏僜n褰撳墠浣欓锛�${String.format("%.2f", currentBalance)}鍏僜n鏈�澶氬彲鍏呭�硷細${String.format("%.2f", maxRechargeAmount)}鍏�")
+            return
+        }
+
+        // 璋冪敤鍏呭�兼帴鍙o紝浼犻�掑綋鍓嶄綑棰�
+        callRechargeApi(currentBalance, rechargeAmount, bonusAmount)
     }
 
     /**
      * 璋冪敤鍏呭�兼帴鍙�
      */
-    private fun callRechargeApi(rechargeAmount: Double, bonusAmount: Double) {
+    private fun callRechargeApi(currentBalance: Double, rechargeAmount: Double, bonusAmount: Double) {
         val cardNum = cardInfo?.cardNum ?: cardAddress ?: ""
         if (cardNum.isEmpty()) {
             ToastUtil.show("鍗″彿淇℃伅缂哄け")
             return
         }
 
+        // 楠岃瘉鏀粯鏂瑰紡鏄惁宸查�夋嫨锛屽鏋滀负绌哄垯閲嶆柊鑾峰彇鏀粯鏂瑰紡
+        if (paymentId.isEmpty()) {
+            ToastUtil.show("鏀粯鏂瑰紡鏈姞杞斤紝姝e湪閲嶆柊鑾峰彇...")
+            // 閲嶆柊鑾峰彇鏀粯鏂瑰紡锛屾垚鍔熷悗鑷姩閲嶈瘯鍏呭��
+            getPaymentMethodsAndRetryRecharge(currentBalance, rechargeAmount, bonusAmount)
+            return
+        }
+
         // 鑾峰彇姘翠环锛堝鏋滀负绌轰細鑷姩瑙﹀彂MainActivity鑾峰彇锛�
         val currentWaterPrice = BaseApplication.requestWaterPrice()
 
-        // 鏋勫缓鍏呭�艰姹傚弬鏁�
+        // 鎵撳嵃璋冭瘯淇℃伅
+        android.util.Log.d("RechargeDetail", "鍏呭�煎弬鏁� - paymentMethod: $paymentMethod, paymentId: $paymentId")
+        android.util.Log.d("RechargeDetail", "瀛楁鍚箟 - money(褰撳墠浣欓): ${String.format("%.2f", currentBalance)}鍏�, amount(鍏呭�奸噾棰�): ${String.format("%.2f", rechargeAmount)}鍏�, gift(璧犻�侀噾棰�): ${String.format("%.2f", bonusAmount)}鍏�")
+
+        // 鏋勫缓鍏呭�艰姹傚弬鏁� - 淇瀛楁鍚箟
         val rechargeRequest = RechargeRequest(
             rechargeType = 2,
             cardNum = cardNum,
-            money = String.format("%.0f", rechargeAmount),
-            amount = String.format("%.0f", bonusAmount),
-            gift = String.format("%.0f", bonusAmount),
-            paymentId = paymentId.toString(),
+            money = String.format("%.2f", currentBalance), // money涓哄綋鍓嶅崱浣欓
+            amount = String.format("%.2f", rechargeAmount), // amount涓哄厖鍊奸噾棰�
+            gift = String.format("%.2f", bonusAmount), // gift涓鸿禒閫侀噾棰�
+            paymentId = paymentId,
             price = String.format("%.2f", currentWaterPrice), // 浣跨敤缁熶竴鑾峰彇鐨勬按浠�
             remarks = "鍏呭��",
             operator = BaseApplication.userId // 榛樿鎿嶄綔鍛業D锛屽彲浠ユ牴鎹疄闄呮儏鍐佃皟鏁�
@@ -393,6 +463,64 @@
     }
 
     /**
+     * 閲嶆柊鑾峰彇鏀粯鏂瑰紡骞堕噸璇曞厖鍊�
+     */
+    private fun getPaymentMethodsAndRetryRecharge(currentBalance: Double, rechargeAmount: Double, bonusAmount: Double) {
+        ApiManager.getInstance().requestGetLoading(
+            this,
+            "terminal/paymentmethod/get",
+            Any::class.java,
+            null,
+            object : SubscriberListener<BaseResponse<Any>>() {
+                override fun onNext(response: BaseResponse<Any>) {
+                    if (response.success) {
+                        try {
+                            // 瀹夊叏鍦板鐞嗚繑鍥炵殑content锛屾湇鍔″櫒杩斿洖鐨勬槸ArrayList<LinkedHashMap>
+                            val paymentMethods = mutableListOf<PaymentMethod>()
+                            val content = response.content
+                            
+                            if (content is List<*>) {
+                                content.forEach { item ->
+                                    if (item is Map<*, *>) {
+                                        val id = item["id"]?.toString() ?: ""
+                                        val name = item["name"]?.toString() ?: ""
+                                        if (id.isNotEmpty() && name.isNotEmpty()) {
+                                            paymentMethods.add(PaymentMethod(id, name))
+                                        }
+                                    }
+                                }
+                            }
+                            
+                            if (paymentMethods.isNotEmpty()) {
+                                paymentMethodList = paymentMethods
+                                // 鏇存柊鏀粯鏂瑰紡鏄剧ず
+                                updatePaymentMethodRadioGroup()
+                                // 鏀粯鏂瑰紡鍔犺浇鎴愬姛鍚庯紝鑷姩閲嶈瘯鍏呭��
+                                ToastUtil.show("鏀粯鏂瑰紡鍔犺浇鎴愬姛锛屾鍦ㄩ噸璇曞厖鍊�...")
+                                callRechargeApi(currentBalance, rechargeAmount, bonusAmount)
+                            } else {
+                                ToastUtil.show("鑾峰彇鏀粯鏂瑰紡澶辫触锛氳繑鍥炴暟鎹负绌�")
+                            }
+                        } catch (e: Exception) {
+                            android.util.Log.e("RechargeDetail", "瑙f瀽鏀粯鏂瑰紡鏁版嵁澶辫触", e)
+                            android.util.Log.e("RechargeDetail", "鍘熷鏁版嵁绫诲瀷: ${response.content?.javaClass?.name}")
+                            android.util.Log.e("RechargeDetail", "鍘熷鏁版嵁鍐呭: ${response.content}")
+                            ToastUtil.show("瑙f瀽鏀粯鏂瑰紡鏁版嵁澶辫触: ${e.message}")
+                        }
+                    } else {
+                        ToastUtil.show("鑾峰彇鏀粯鏂瑰紡澶辫触: ${response.msg}")
+                    }
+                }
+
+                override fun onError(e: Throwable?) {
+                    super.onError(e)
+                    ToastUtil.show("鑾峰彇鏀粯鏂瑰紡澶辫触: ${e?.message ?: "缃戠粶寮傚父"}")
+                }
+            }
+        )
+    }
+
+    /**
      * 鍚姩鍐欏崱鐣岄潰
      */
     private fun startWriteCardActivity(
@@ -405,15 +533,7 @@
             val userCard = UserCard().apply {
                 // 璁剧疆鐢ㄦ埛鍗′俊鎭�
                 cardInfo?.let { info ->
-                    userCode = info.cardNum ?: ""
-
-                    // 璁$畻鏂颁綑棰濓細鍘熸湁浣欓 + 鍏呭�奸噾棰� + 璧犻�侀噾棰�
-                    val originalBalance =
-                        this@RechargeDetailActivity.userCard?.balance ?: 0 // 鍘熸湁浣欓锛堝垎锛�
-                    val rechargeAmountInCents = (rechargeAmount * 100).toInt() // 鍏呭�奸噾棰濊浆鍒�
-                    val bonusAmountInCents = (bonusAmount * 100).toInt() // 璧犻�侀噾棰濊浆鍒�
-
-                    balance = originalBalance + rechargeAmountInCents + bonusAmountInCents
+                    balance = MornyUtil.changeY2F(rechargeResult.balance)
                 }
 
                 // 璁剧疆鍏朵粬蹇呰淇℃伅
@@ -433,7 +553,7 @@
                 putExtra("bonusAmount", bonusAmount) // 浼犻�掕禒閫侀噾棰�
             }
             startActivity(intent)
-
+            finish()
         } catch (e: Exception) {
             ToastUtil.show("鍚姩鍐欏崱鐣岄潰澶辫触: ${e.message}")
         }

--
Gitblit v1.8.0