feat(database): 增加管理卡相关功能并优化数据库结构- 新增 ManagerCardBean 数据类用于管理卡信息
- 在 AppDataBase 中添加 ManagerCardDao 接口
- 实现管理卡的数据库迁移策略
- 优化支付方式 ID 类型,从 Long改为 String
- 重构更新写卡和上报状态的逻辑,支持管理卡和用户卡
| | |
| | | |
| | | // 支付方式相关属性 |
| | | private var paymentMethod: String = "现金" |
| | | private var paymentId: Long = 0 |
| | | private var paymentId: String = "" |
| | | private var paymentMethodList: List<PaymentMethod> = listOf() |
| | | |
| | | companion object { |
| | |
| | | override fun onNext(t: BaseResponse<String>) { |
| | | if (t.success) { |
| | | // 上报成功,更新本地数据库isReported为true |
| | | updateCardReportedStatus(cardAddr, orderNumber) |
| | | updateCardReportedStatus(cardAddr, orderNumber, operationTypeCode) |
| | | } else { |
| | | // 上报失败,记录错误但不影响用户操作 |
| | | CrashReport.postCatchedException(Exception("上报写卡结果失败: ${t.msg}")) |
| | |
| | | |
| | | /** |
| | | * 更新本地数据库中的上报状态 |
| | | * 将CardRegistrationBean中的isReported状态设置为true |
| | | * 根据操作类型判断是更新ManagerCardBean还是CardRegistrationBean的isReported状态为true |
| | | * |
| | | * @param cardNumber 卡号 |
| | | * @param orderNumber 订单号 |
| | | * @param operationTypeCode 操作类型代码 |
| | | */ |
| | | private fun updateCardReportedStatus(cardNumber: String, orderNumber: String = "") { |
| | | private fun updateCardReportedStatus(cardNumber: String, orderNumber: String = "", operationTypeCode: Int = -1) { |
| | | lifecycleScope.launch { |
| | | try { |
| | | val cardRegistrationDao = BaseDaoSingleton.getInstance(this@MainActivity) |
| | | .cardRegistrationDao() |
| | | |
| | | // 根据订单号查找CardRegistrationBean记录 |
| | | val cardRegistration = if (orderNumber.isNotEmpty()) { |
| | | cardRegistrationDao.getByOrderId(orderNumber) |
| | | } else { |
| | | // 如果没有订单号,则通过卡号查找 |
| | | cardRegistrationDao.getByCardNumber(cardNumber) |
| | | } |
| | | val baseDaoSingleton = BaseDaoSingleton.getInstance(this@MainActivity) |
| | | |
| | | if (cardRegistration != null) { |
| | | // 创建更新后的CardRegistrationBean对象,将isReported设置为true |
| | | val updatedCardRegistration = cardRegistration.copy(isReported = true) |
| | | // 更新数据库记录 |
| | | cardRegistrationDao.update(updatedCardRegistration) |
| | | // 根据操作类型判断是管理卡还是用户卡操作 |
| | | val isManagerCardOperation = operationTypeCode in 100..108 |
| | | |
| | | if (isManagerCardOperation) { |
| | | // 管理卡制作操作类型,查询和更新ManagerCardBean |
| | | val managerCardDao = baseDaoSingleton.managerCardDao() |
| | | val managerCard = if (orderNumber.isNotEmpty()) { |
| | | managerCardDao.getByOrderId(orderNumber) |
| | | } else { |
| | | managerCardDao.getByCardAddress(cardNumber) |
| | | } |
| | | |
| | | if (managerCard != null) { |
| | | val updatedManagerCard = managerCard.copy(isReported = true) |
| | | managerCardDao.update(updatedManagerCard) |
| | | } |
| | | } else { |
| | | // 用户卡操作类型,查询和更新CardRegistrationBean |
| | | val cardRegistrationDao = baseDaoSingleton.cardRegistrationDao() |
| | | val cardRegistration = if (orderNumber.isNotEmpty()) { |
| | | cardRegistrationDao.getByOrderId(orderNumber) |
| | | } else { |
| | | // 如果没有订单号,则通过卡号查找 |
| | | cardRegistrationDao.getByCardNumber(cardNumber) |
| | | } |
| | | |
| | | if (cardRegistration != null) { |
| | | // 创建更新后的CardRegistrationBean对象,将isReported设置为true |
| | | val updatedCardRegistration = cardRegistration.copy(isReported = true) |
| | | // 更新数据库记录 |
| | | cardRegistrationDao.update(updatedCardRegistration) |
| | | } |
| | | } |
| | | } catch (e: Exception) { |
| | | // 记录异常信息,但不影响用户操作 |
| | |
| | | private var paymentMethod: String = "现金" |
| | | |
| | | // 支付方式ID |
| | | private var paymentId: Long = 0 |
| | | private var paymentId: String = "" |
| | | |
| | | // 支付方式列表 |
| | | private var paymentMethodList: List<PaymentMethod> = listOf() |
| | |
| | | ApiManager.getInstance().requestGetLoading( |
| | | this, |
| | | "terminal/paymentmethod/get", |
| | | PaymentMethodResponse::class.java, |
| | | Array<PaymentMethod>::class.java, |
| | | null, |
| | | object : SubscriberListener<BaseResponse<PaymentMethodResponse>>() { |
| | | override fun onNext(response: BaseResponse<PaymentMethodResponse>) { |
| | | object : SubscriberListener<BaseResponse<Array<PaymentMethod>>>() { |
| | | override fun onNext(response: BaseResponse<Array<PaymentMethod>>) { |
| | | if (response.success) { |
| | | // 获取支付方式列表 |
| | | val paymentMethods = response.content?.obj ?: listOf() |
| | | // 获取支付方式列表,现在content直接是PaymentMethod数组 |
| | | val paymentMethods = response.content?.toList() ?: listOf() |
| | | if (paymentMethods.isNotEmpty()) { |
| | | paymentMethodList = paymentMethods |
| | | // 更新支付方式显示 |
| | |
| | | clientId = clientId, |
| | | cardFee = cardFee, |
| | | remark = binding.newCardRemark.text.toString(), |
| | | paymentMethod = paymentId.toInt(), |
| | | paymentMethod = paymentId.toLongOrNull()?.toInt() ?: 0, |
| | | isReported = true, |
| | | isCardWritten = false, // 初始设置为false,写卡成功后再更新为true |
| | | operatorId = orderId, |
| | |
| | | |
| | | /** |
| | | * 更新本地数据库中的写卡状态 |
| | | * 将CardRegistrationBean中的isCardWritten状态设置为true |
| | | * 根据操作类型判断是更新ManagerCardBean还是CardRegistrationBean的isCardWritten状态为true |
| | | * 然后跳转到写卡成功界面,并通知MainActivity调用postCardData |
| | | * |
| | | * @param cardNumber 卡号 |
| | |
| | | private fun updateCardWrittenStatus(cardNumber: String) { |
| | | lifecycleScope.launch { |
| | | try { |
| | | val cardRegistrationDao = BaseDaoSingleton.getInstance(this@NfcWreatActivity) |
| | | .cardRegistrationDao() |
| | | |
| | | // 根据订单号查找CardRegistrationBean记录 |
| | | val cardRegistration = cardRegistrationDao.getByOrderId(orderNumber) |
| | | |
| | | if (cardRegistration != null) { |
| | | // 创建更新后的CardRegistrationBean对象,将isCardWritten设置为true |
| | | val updatedCardRegistration = cardRegistration.copy(isCardWritten = true) |
| | | // 更新数据库记录 |
| | | cardRegistrationDao.update(updatedCardRegistration) |
| | | |
| | | // 在主线程中关闭Activity并跳转到成功页面 |
| | | runOnUiThread { |
| | | setResult(RESULT_OK) |
| | | finish() |
| | | |
| | | // 跳转到写卡成功界面 |
| | | Intent(this@NfcWreatActivity, CardWriteSuccessActivity::class.java).apply { |
| | | putExtra("cardNumber", cardNumber) |
| | | if (::userCard.isInitialized) { |
| | | putExtra("userCard", userCard) |
| | | } |
| | | putExtra("operationTypeCode", operationTypeCode) |
| | | startActivity(this) |
| | | } |
| | | |
| | | // 通知MainActivity调用postCardData |
| | | notifyMainActivityToPostCardData(cardNumber) |
| | | val baseDaoSingleton = BaseDaoSingleton.getInstance(this@NfcWreatActivity) |
| | | |
| | | // 根据操作类型判断是管理卡还是用户卡操作 |
| | | val isManagerCardOperation = operationTypeCode in 100..108 |
| | | |
| | | var updateSuccess = false |
| | | |
| | | if (isManagerCardOperation) { |
| | | // 管理卡制作操作类型,查询和更新ManagerCardBean |
| | | val managerCardDao = baseDaoSingleton.managerCardDao() |
| | | val managerCard = managerCardDao.getByOrderId(orderNumber) |
| | | |
| | | if (managerCard != null) { |
| | | val updatedManagerCard = managerCard.copy(isCardWritten = true) |
| | | managerCardDao.update(updatedManagerCard) |
| | | updateSuccess = true |
| | | } |
| | | } else { |
| | | // 用户卡操作类型,查询和更新CardRegistrationBean |
| | | val cardRegistrationDao = baseDaoSingleton.cardRegistrationDao() |
| | | val cardRegistration = cardRegistrationDao.getByOrderId(orderNumber) |
| | | |
| | | if (cardRegistration != null) { |
| | | val updatedCardRegistration = cardRegistration.copy(isCardWritten = true) |
| | | cardRegistrationDao.update(updatedCardRegistration) |
| | | updateSuccess = true |
| | | } |
| | | } |
| | | |
| | | // 无论是否找到记录,都跳转到成功界面 |
| | | runOnUiThread { |
| | | setResult(RESULT_OK) |
| | | finish() |
| | | |
| | | // 跳转到写卡成功界面 |
| | | Intent(this@NfcWreatActivity, CardWriteSuccessActivity::class.java).apply { |
| | | putExtra("cardNumber", cardNumber) |
| | | if (::userCard.isInitialized) { |
| | | putExtra("userCard", userCard) |
| | | } |
| | | putExtra("operationTypeCode", operationTypeCode) |
| | | startActivity(this) |
| | | } |
| | | |
| | | // 通知MainActivity调用postCardData |
| | | notifyMainActivityToPostCardData(cardNumber) |
| | | } |
| | | |
| | | } catch (e: Exception) { |
| | | // 记录异常信息 |
| | | CrashReport.postCatchedException(e) |
| | |
| | | |
| | | // 支付方式相关属性 |
| | | private var paymentMethod: String = "现金" |
| | | private var paymentId: Long = 0 |
| | | private var paymentId: String = "" |
| | | private var paymentMethodList: List<PaymentMethod> = listOf() |
| | | |
| | | companion object { |
| | |
| | | ApiManager.getInstance().requestGetLoading( |
| | | this, |
| | | "terminal/paymentmethod/get", |
| | | PaymentMethodResponse::class.java, |
| | | Array<PaymentMethod>::class.java, |
| | | null, |
| | | object : SubscriberListener<BaseResponse<PaymentMethodResponse>>() { |
| | | override fun onNext(response: BaseResponse<PaymentMethodResponse>) { |
| | | object : SubscriberListener<BaseResponse<Array<PaymentMethod>>>() { |
| | | override fun onNext(response: BaseResponse<Array<PaymentMethod>>) { |
| | | if (response.success) { |
| | | // 获取支付方式列表 |
| | | val paymentMethods = response.content?.obj ?: listOf() |
| | | // 获取支付方式列表,现在content直接是PaymentMethod数组 |
| | | val paymentMethods = response.content?.toList() ?: listOf() |
| | | if (paymentMethods.isNotEmpty()) { |
| | | paymentMethodList = paymentMethods |
| | | // 更新支付方式显示 |
| | |
| | | money = String.format("%.0f", rechargeAmount), |
| | | amount = String.format("%.0f", bonusAmount), |
| | | gift = String.format("%.0f", bonusAmount), |
| | | paymentId = paymentId.toString(), |
| | | paymentId = paymentId, |
| | | price = String.format("%.2f", currentWaterPrice), // 使用统一获取的水价 |
| | | remarks = "充值", |
| | | operator = BaseApplication.userId // 默认操作员ID,可以根据实际情况调整 |
New file |
| | |
| | | package com.dayu.general.bean.db |
| | | |
| | | import androidx.room.Entity |
| | | import androidx.room.PrimaryKey |
| | | |
| | | @Entity(tableName = "manager_card") |
| | | data class ManagerCardBean( |
| | | @PrimaryKey(autoGenerate = true) |
| | | val id: Long = 0, |
| | | val cardAddress: String, // 卡地址 |
| | | val orderId: String, // 订单id |
| | | val isReported: Boolean = false, // 是否上报成功 |
| | | val isCardWritten: Boolean = false, // 是否写卡成功 |
| | | val operatorId: String = "", // 操作人id |
| | | val createTime: Long = System.currentTimeMillis() // 创建时间 |
| | | ) |
| | |
| | | |
| | | // 支付方式数据类 |
| | | data class PaymentMethod( |
| | | val id: Long, |
| | | val id: String, |
| | | val name: String |
| | | ) |
| | | |
| | |
| | | import androidx.room.RoomDatabase |
| | | import com.dayu.general.bean.db.CardData |
| | | import com.dayu.general.bean.db.CardRegistrationBean |
| | | import com.dayu.general.bean.db.ManagerCardBean |
| | | import com.dayu.general.bean.db.PassWordCardBean |
| | | import com.dayu.general.bean.db.ProjectDataBean |
| | | import com.dayu.general.bean.db.RechargeRecordBean |
| | | |
| | | @Database(entities = [PassWordCardBean::class, CardData::class, ProjectDataBean::class, CardRegistrationBean::class, RechargeRecordBean::class], version = 4, exportSchema = false) |
| | | @Database(entities = [PassWordCardBean::class, CardData::class, ProjectDataBean::class, CardRegistrationBean::class, RechargeRecordBean::class, ManagerCardBean::class], version = 5, exportSchema = false) |
| | | abstract class AppDataBase : RoomDatabase() { |
| | | abstract fun cardDataDao(): CardDataDao |
| | | abstract fun projectDataDao(): ProjectDataDao |
| | | abstract fun cardRegistrationDao(): CardRegistrationDao |
| | | abstract fun rechargeRecordDao(): RechargeRecordDao |
| | | abstract fun managerCardDao(): ManagerCardDao |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | // 数据库迁移策略:从版本4迁移到版本5 |
| | | private val MIGRATION_4_5 = object : Migration(4, 5) { |
| | | override fun migrate(database: SupportSQLiteDatabase) { |
| | | // 创建管理卡表 |
| | | database.execSQL(""" |
| | | CREATE TABLE IF NOT EXISTS `manager_card` ( |
| | | `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, |
| | | `cardAddress` TEXT NOT NULL, |
| | | `orderId` TEXT NOT NULL, |
| | | `isReported` INTEGER NOT NULL DEFAULT 0, |
| | | `isCardWritten` INTEGER NOT NULL DEFAULT 0, |
| | | `operatorId` TEXT NOT NULL DEFAULT '', |
| | | `createTime` INTEGER NOT NULL |
| | | ) |
| | | """.trimIndent()) |
| | | } |
| | | } |
| | | |
| | | fun getInstance(context: Context): AppDataBase { |
| | | if (baseDao == null) { |
| | | baseDao = Room.databaseBuilder<AppDataBase>( |
| | | context, |
| | | AppDataBase::class.java, |
| | | SqlitePath + "ConfigurationData_generalV1" |
| | | ).allowMainThreadQueries().build() |
| | | ).allowMainThreadQueries() |
| | | .addMigrations(MIGRATION_3_4, MIGRATION_4_5) // 添加迁移策略 |
| | | .build() |
| | | } |
| | | return baseDao as AppDataBase |
| | | } |
| | |
| | | AppDataBase::class.java, |
| | | "GeneralLibrary.db" |
| | | ) |
| | | .addMigrations(MIGRATION_3_4) // 添加迁移策略 |
| | | .addMigrations(MIGRATION_3_4, MIGRATION_4_5) // 添加迁移策略 |
| | | .build() |
| | | } |
| | | } |
New file |
| | |
| | | package com.dayu.general.dao |
| | | |
| | | import androidx.room.* |
| | | import com.dayu.general.bean.db.ManagerCardBean |
| | | |
| | | @Dao |
| | | interface ManagerCardDao { |
| | | @Insert |
| | | suspend fun insert(managerCard: ManagerCardBean): Long |
| | | |
| | | @Update |
| | | suspend fun update(managerCard: ManagerCardBean) |
| | | |
| | | @Delete |
| | | suspend fun delete(managerCard: ManagerCardBean) |
| | | |
| | | @Query("SELECT * FROM manager_card WHERE cardAddress = :cardAddress") |
| | | suspend fun getByCardAddress(cardAddress: String): ManagerCardBean? |
| | | |
| | | @Query("SELECT * FROM manager_card WHERE orderId = :orderId") |
| | | suspend fun getByOrderId(orderId: String): ManagerCardBean? |
| | | |
| | | @Query("SELECT * FROM manager_card WHERE isReported = 0") |
| | | suspend fun getUnreportedCards(): List<ManagerCardBean> |
| | | |
| | | @Query("SELECT * FROM manager_card WHERE isCardWritten = 0") |
| | | suspend fun getUnwrittenCards(): List<ManagerCardBean> |
| | | |
| | | @Query("SELECT * FROM manager_card ORDER BY createTime DESC") |
| | | suspend fun getAllCards(): List<ManagerCardBean> |
| | | } |