cloudroam
17 小时以前 1a281d8354622a3606360f9f9a7fe4bb7d6a2c3a
app/src/main/java/com/example/firstapp/utils/CodeUtils.kt
@@ -7,37 +7,41 @@
object CodeUtils {
    private const val TAG = "CodeUtils"
    private val lock = Any()
    /**
     * 保存code到数据库,如果已存在则跳过
     * @param code 要保存的code对象
     * @return Boolean 是否成功保存
     */
    fun saveCode(code: Code): Boolean {
        // 检查必要字段是否为空
        if (code.oneLevel.isEmpty() || code.code.isEmpty()) {
            Log.d(TAG, "保存失败:必要字段为空")
            return false
        }
        synchronized(lock) {
            // 检查必要字段是否为空
            if (code.oneLevel.isEmpty() || code.code.isEmpty()) {
                Log.d(TAG, "保存失败:必要字段为空")
                return false
            }
        // 检查是否已存在相同记录
        val existingCode = Core.code.queryByTypeAndCodeAndDate(
            code.category,
            code.code,
            code.createTime
        )
            // 检查是否已存在相同记录
            val existingCode = Core.code.queryByTypeAndCodeAndDate(
                code.category,
                code.code,
                code.createTime
            )
        return if (existingCode == null) {
            try {
                Core.code.insert(code)
                Log.d(TAG, "成功保存${code.category}记录: ${code.code}")
                true
            } catch (e: Exception) {
                Log.e(TAG, "保存${code.category}记录失败: ${e.message}")
            return if (existingCode == null) {
                try {
                    Core.code.insert(code)
                    Log.d(TAG, "成功保存${code.category}记录: ${code.code}")
                    true
                } catch (e: Exception) {
                    Log.e(TAG, "保存${code.category}记录失败: ${e.message}")
                    false
                }
            } else {
                Log.d(TAG, "发现重复${code.category}记录,跳过保存: ${code.code}")
                false
            }
        } else {
            Log.d(TAG, "发现重复${code.category}记录,跳过保存: ${code.code}")
            false
        }
    }
@@ -199,4 +203,4 @@
            remarks = trips ?: ""
        )
    }
}
}