|  |  | 
 |  |  |  | 
 |  |  | import android.content.Intent | 
 |  |  | import android.os.Bundle | 
 |  |  | import androidx.lifecycle.lifecycleScope | 
 |  |  | import com.dayu.baselibrary.net.subscribers.SubscriberListener | 
 |  |  | import com.dayu.baselibrary.tools.nfc.NFCCallBack | 
 |  |  | import com.dayu.baselibrary.utils.ToastUtil | 
 |  |  | import com.dayu.general.bean.card.ClearCard | 
 |  |  | import com.dayu.general.bean.card.UserCard | 
 |  |  | 
 |  |  | import com.dayu.general.net.BaseResponse | 
 |  |  | import com.dayu.general.tool.NfcReadHelper | 
 |  |  | import com.dayu.general.tool.NfcWreatHelper | 
 |  |  | import com.dayu.general.dao.BaseDaoSingleton | 
 |  |  | import com.tencent.bugly.crashreport.CrashReport | 
 |  |  | import kotlinx.coroutines.launch | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author: zuo | 
 |  |  | 
 |  |  |                     nfcWreatHelper.writeData(clearCard.getZeroBytes(), 7, 0) { success, message -> | 
 |  |  |                         if (success) { | 
 |  |  |                             postCardData(cardType, cardAddr, "") | 
 |  |  |                             // 写卡成功后更新数据库中的isCardWritten状态 | 
 |  |  |                             updateCardWrittenStatus(cardAddr) | 
 |  |  |                             ToastUtil.show("写卡成功!") | 
 |  |  |                             // 处理写卡成功的情况 | 
 |  |  |                         } else { | 
 |  |  | 
 |  |  |  | 
 |  |  |                 CardCommon.USER_CARD_TYPE_1 -> { | 
 |  |  |                     binding?.cardData?.text = "写用户卡" | 
 |  |  |                     nfcWreatHelper.writeUserData(userCard) | 
 |  |  |                     nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { | 
 |  |  |                         override fun isSusses(flag: Boolean, msg: String?) { | 
 |  |  |                             if (flag) { | 
 |  |  |                                 // 写卡成功后更新数据库中的isCardWritten状态 | 
 |  |  |                                 updateCardWrittenStatus(cardAddr) | 
 |  |  |                                 ToastUtil.show("写卡成功!") | 
 |  |  |                             } else { | 
 |  |  |                                 ToastUtil.show("写卡失败: ${msg ?: "未知错误"}") | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     }) | 
 |  |  |  | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 更新CardRegistrationBean中的isCardWritten状态为true | 
 |  |  |      */ | 
 |  |  |     private fun updateCardWrittenStatus(cardNumber: String) { | 
 |  |  |         lifecycleScope.launch { | 
 |  |  |             try { | 
 |  |  |                 val cardRegistrationDao = BaseDaoSingleton.getInstance(this@NfcWreatActivity) | 
 |  |  |                     .cardRegistrationDao() | 
 |  |  |                  | 
 |  |  |                 // 根据卡号查找CardRegistrationBean记录 | 
 |  |  |                 val cardRegistration = cardRegistrationDao.getByCardNumber(cardNumber) | 
 |  |  |                 if (cardRegistration != null) { | 
 |  |  |                     // 创建更新后的CardRegistrationBean对象,将isCardWritten设置为true | 
 |  |  |                     val updatedCardRegistration = cardRegistration.copy(isCardWritten = true) | 
 |  |  |                     // 更新数据库记录 | 
 |  |  |                     cardRegistrationDao.update(updatedCardRegistration) | 
 |  |  |                 } | 
 |  |  |             } catch (e: Exception) { | 
 |  |  |                 CrashReport.postCatchedException(e) | 
 |  |  |                 e.printStackTrace() | 
 |  |  |                 ToastUtil.show("更新写卡状态失败: ${e.message}") | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     fun postCardData(cardType: String, cardAddr: String, remark: String) { | 
 |  |  |         when (cardType) { | 
 |  |  |             CardCommon.CHECK_CARD -> { |