左晓为主开发手持机充值管理机
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", "解析支付方式数据失败", e)
                            android.util.Log.e("RechargeDetail", "原始数据类型: ${response.content?.javaClass?.name}")
                            android.util.Log.e("RechargeDetail", "原始数据内容: $response.content")
                            Toast.makeText(
                                this@RechargeDetailActivity,
                                "解析支付方式数据失败: ${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 ?: "网络异常"}",
@@ -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
        }
        // 调用充值接口,传递当前余额
        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("支付方式未加载,正在重新获取...")
            // 重新获取支付方式,成功后自动重试充值
            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 // 默认操作员ID,可以根据实际情况调整
@@ -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", "解析支付方式数据失败", e)
                            android.util.Log.e("RechargeDetail", "原始数据类型: ${response.content?.javaClass?.name}")
                            android.util.Log.e("RechargeDetail", "原始数据内容: ${response.content}")
                            ToastUtil.show("解析支付方式数据失败: ${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)
                }
                // 设置其他必要信息