| | |
| | | import android.widget.Toast |
| | | import androidx.core.content.ContextCompat |
| | | import androidx.fragment.app.Fragment |
| | | import androidx.lifecycle.lifecycleScope |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.general.BaseApplication |
| | |
| | | import com.dayu.general.adapter.TabAdapter |
| | | import com.dayu.general.bean.net.UserInfoResult |
| | | import com.dayu.general.bean.net.WaterPriceResult |
| | | import com.dayu.general.dao.BaseDaoSingleton |
| | | import com.dayu.general.databinding.ActivityMainBinding |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.general.tool.CardOperationType |
| | | import com.tencent.bugly.crashreport.CrashReport |
| | | import kotlinx.coroutines.launch |
| | | |
| | | class MainActivity : BaseNfcActivity() { |
| | | |
| | |
| | | } |
| | | return super.onKeyDown(keyCode, event) |
| | | } |
| | | |
| | | /** |
| | | * 向服务器上报写卡操作结果 |
| | | * 写卡成功后调用此方法通知服务器操作完成,成功后更新本地数据库isReported为true |
| | | * |
| | | * @param cardAddr 卡地址/卡号 |
| | | * @param operationTypeCode 操作类型代码 |
| | | * @param orderNumber 订单号 |
| | | * @param regionNumber 区域号 |
| | | * @param projectNumber 项目号 |
| | | */ |
| | | fun postCardData( |
| | | cardAddr: String, |
| | | operationTypeCode: Int, |
| | | orderNumber: String = "", |
| | | regionNumber: String = "", |
| | | projectNumber: String = "" |
| | | ) { |
| | | val map = mutableMapOf<String, Any>() |
| | | |
| | | // 添加卡地址参数 |
| | | if (cardAddr.isNotEmpty()) { |
| | | map["cardAddr"] = cardAddr |
| | | } |
| | | |
| | | // 添加操作类型参数 |
| | | // 判断是否为管理卡制作操作类型(100-108),如果是则传递MANAGEMENT_CARD_WRITE的值(7) |
| | | val operateTypeToSend = if (operationTypeCode in 100..108) { |
| | | CardOperationType.MANAGEMENT_CARD_WRITE.code |
| | | } else { |
| | | operationTypeCode |
| | | } |
| | | map["operateType"] = operateTypeToSend |
| | | |
| | | // 添加订单号参数(如果存在) |
| | | if (orderNumber.isNotEmpty()) { |
| | | map["orderNumber"] = orderNumber |
| | | } |
| | | |
| | | // 添加区域号和项目号参数 |
| | | if (regionNumber.isNotEmpty()) { |
| | | map["regionNumber"] = regionNumber |
| | | } |
| | | if (projectNumber.isNotEmpty()) { |
| | | map["projectNumber"] = projectNumber |
| | | } |
| | | |
| | | // 调用服务器接口上报操作结果 |
| | | ApiManager.getInstance().requestPostHideLoading( |
| | | this, |
| | | "terminal/card/termCallBack", // 终端写卡回调接口 |
| | | String::class.java, |
| | | map, |
| | | object : SubscriberListener<BaseResponse<String>>() { |
| | | override fun onNext(t: BaseResponse<String>) { |
| | | if (t.success) { |
| | | // 上报成功,更新本地数据库isReported为true |
| | | updateCardReportedStatus(cardAddr, orderNumber, operationTypeCode) |
| | | } else { |
| | | // 上报失败,记录错误但不影响用户操作 |
| | | CrashReport.postCatchedException(Exception("上报写卡结果失败: ${t.msg}")) |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | // 网络错误,记录错误但不影响用户操作 |
| | | CrashReport.postCatchedException(e ?: Exception("上报写卡结果网络错误")) |
| | | } |
| | | } |
| | | ) |
| | | } |
| | | |
| | | /** |
| | | * 更新本地数据库中的上报状态 |
| | | * 根据操作类型判断是更新ManagerCardBean还是CardRegistrationBean的isReported状态为true |
| | | * |
| | | * @param cardNumber 卡号 |
| | | * @param orderNumber 订单号 |
| | | * @param operationTypeCode 操作类型代码 |
| | | */ |
| | | private fun updateCardReportedStatus(cardNumber: String, orderNumber: String = "", operationTypeCode: Int = -1) { |
| | | lifecycleScope.launch { |
| | | try { |
| | | val baseDaoSingleton = BaseDaoSingleton.getInstance(this@MainActivity) |
| | | |
| | | // 根据操作类型判断是管理卡还是用户卡操作 |
| | | 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) { |
| | | // 记录异常信息,但不影响用户操作 |
| | | CrashReport.postCatchedException(e) |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | } |
| | | } |