| | |
| | | import android.widget.Toast |
| | | import androidx.appcompat.app.AppCompatActivity |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.utils.MornyUtil |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.baselibrary.view.TitleBar |
| | | import com.dayu.general.BaseApplication |
| | |
| | | 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 |
| | | import com.dayu.general.tool.CardCommon.Companion.USER_CARD_TYPE_1 |
| | | import com.dayu.general.tool.CardOperationType |
| | | |
| | | class RechargeDetailActivity : AppCompatActivity() { |
| | |
| | | |
| | | // 支付方式相关属性 |
| | | private var paymentMethod: String = "现金" |
| | | private var paymentId: Long = 0 |
| | | private var paymentId: String = "" |
| | | private var paymentMethodList: List<PaymentMethod> = listOf() |
| | | |
| | | companion object { |
| | |
| | | private const val EXTRA_CARD_ADDRESS = "extra_card_address" |
| | | private const val EXTRA_USER_CARD = "extra_user_card" |
| | | |
| | | fun start(context: Context, cardInfo: CardInfoResult?, cardAddress: String?, userCard: UserCard?) { |
| | | fun start( |
| | | context: Context, |
| | | cardInfo: CardInfoResult?, |
| | | cardAddress: String?, |
| | | userCard: UserCard? |
| | | ) { |
| | | val intent = Intent(context, RechargeDetailActivity::class.java) |
| | | intent.putExtra(EXTRA_CARD_INFO, cardInfo) |
| | | intent.putExtra(EXTRA_CARD_ADDRESS, cardAddress) |
| | |
| | | // 处理充值逻辑 |
| | | handleRecharge() |
| | | } |
| | | |
| | | |
| | | // 设置金额输入限制 |
| | | setupAmountInputLimit(binding.rechargeMorny) |
| | | setupAmountInputLimit(binding.rechargeWater) |
| | |
| | | private fun setupAmountInputLimit(editText: EditText) { |
| | | editText.addTextChangedListener(object : TextWatcher { |
| | | override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} |
| | | |
| | | |
| | | override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} |
| | | |
| | | |
| | | override fun afterTextChanged(s: Editable?) { |
| | | val text = s.toString() |
| | | if (text.isEmpty()) return |
| | | |
| | | |
| | | // 检查是否包含小数点 |
| | | if (text.contains(".")) { |
| | | val parts = text.split(".") |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | // 防止输入多个小数点 |
| | | val dotCount = text.count { it == '.' } |
| | | if (dotCount > 1) { |
| | |
| | | editText.setSelection(newText.length) |
| | | editText.addTextChangedListener(this) |
| | | } |
| | | |
| | | |
| | | // 防止以小数点开头 |
| | | if (text.startsWith(".")) { |
| | | editText.removeTextChangedListener(this) |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取支付方式列表 |
| | | * 获取支付方式列表 - 使用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( |
| | |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | android.util.Log.e("RechargeDetail", "网络请求失败", e) |
| | | Toast.makeText( |
| | | this@RechargeDetailActivity, |
| | | "获取支付方式失败: ${e?.message ?: "网络异常"}", |
| | |
| | | binding.redInitCode.text = cardAddress ?: "" |
| | | binding.userName.text = info.userName ?: "" |
| | | binding.redCardNum.text = info.cardNum ?: "" |
| | | |
| | | |
| | | // 使用用户卡中的余额显示 |
| | | val balance = userCard?.let { |
| | | val balance = userCard?.let { |
| | | // 将分转换为元,保留两位小数 |
| | | String.format("%.2f", it.balance / 100.0) |
| | | } ?: run { |
| | |
| | | binding.redRemainderBlance.text = "$balance 元" |
| | | |
| | | // 设置卡状态和对应颜色 |
| | | val cardStatus = when (info.status) { |
| | | val cardStatus = when (info.state) { |
| | | 1 -> "正常" |
| | | 2 -> "挂失" |
| | | 3 -> "锁定" |
| | |
| | | binding.redStatu.text = cardStatus |
| | | |
| | | // 根据卡状态设置不同颜色 |
| | | val statusColor = when (info.status) { |
| | | val statusColor = when (info.state) { |
| | | 1 -> android.graphics.Color.parseColor("#4CAF50") // 绿色-正常 |
| | | 2 -> android.graphics.Color.parseColor("#FF9800") // 橙色-挂失 |
| | | 3 -> android.graphics.Color.parseColor("#F44336") // 红色-锁定 |
| | |
| | | 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,可以根据实际情况调整 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 重新获取支付方式并重试充值 |
| | | */ |
| | | 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(rechargeResult: RechargeResult, rechargeAmount: Double, bonusAmount: Double) { |
| | | private fun startWriteCardActivity( |
| | | rechargeResult: RechargeResult, |
| | | rechargeAmount: Double, |
| | | bonusAmount: Double |
| | | ) { |
| | | try { |
| | | // 创建UserCard对象用于写卡 |
| | | val userCard = UserCard().apply { |
| | | // 设置用户卡信息 |
| | | cardInfo?.let { info -> |
| | | userCode = info.cardNum ?: "" |
| | | balance = ((rechargeAmount + bonusAmount) * 100).toInt() // 转换为分 |
| | | balance = MornyUtil.changeY2F(rechargeResult.balance) |
| | | } |
| | | |
| | | |
| | | // 设置其他必要信息 |
| | | projectCode = rechargeResult.projectNo |
| | | waterPrice = rechargeResult.waterPrice.toFloat() |
| | | waterPrice = MornyUtil.changeY2F(rechargeResult.waterPrice.toString()) |
| | | rechargeDate = java.util.Calendar.getInstance() |
| | | } |
| | | |
| | | // 启动写卡Activity |
| | | val intent = Intent(this, NfcWreatActivity::class.java).apply { |
| | | putExtra("cardType", "USER_CARD") // 用户卡类型 |
| | | putExtra("cardType", USER_CARD_TYPE_1) // 用户卡类型 |
| | | putExtra("cardAddr", cardAddress) |
| | | putExtra("operationTypeCode", CardOperationType.Recharge.code) |
| | | putExtra("orderNumber", rechargeResult.orderNo) |
| | | putExtra("userCard", userCard) |
| | | putExtra("rechargeAmount", rechargeAmount) // 传递充值金额 |
| | | putExtra("bonusAmount", bonusAmount) // 传递赠送金额 |
| | | } |
| | | |
| | | startActivity(intent) |
| | | |
| | | // 显示成功信息 |
| | | val formattedRecharge = String.format("%.2f", rechargeAmount) |
| | | val formattedBonus = String.format("%.2f", bonusAmount) |
| | | val formattedTotal = String.format("%.2f", rechargeAmount + bonusAmount) |
| | | |
| | | ToastUtil.show("充值订单创建成功\n订单号: ${rechargeResult.orderNo}\n充值金额: ${formattedRecharge}元\n赠送金额: ${formattedBonus}元\n总金额: ${formattedTotal}元\n请贴卡进行写卡操作") |
| | | |
| | | finish() |
| | | } catch (e: Exception) { |
| | | ToastUtil.show("启动写卡界面失败: ${e.message}") |
| | | } |