package com.abbidot.tracker.database.dao

import android.util.Log
import androidx.room.*
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery

/**
 *Created  by .yzq on 2022/4/1/001.
 * @link
 * @description:数据库Dao基类
 */
interface BaseDao<T> {
    /**
     * 获取表名
     */
//    val tableName: String
//        get() {
//            val clazz =
//                (javaClass.superclass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<*>
//            val tableName = clazz.simpleName
//            LogUtil.d("ez", "getTableName: -->$tableName")
//            return tableName
//        }

    /**
     * 添加单个对象
     * OnConflictStrategy.REPLACE新数据替换旧数据
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
     fun insert(obj: T): Long

    /**
     * 添加数组对象数据
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
     fun insert(vararg objs: T): LongArray?

    /**
     * 添加对象集合
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
     fun insert(personList: List<T>): List<Long>

    /**
     * 根据对象中的主键删除（主键是自动增长的，无需手动赋值）
     */
    @Delete
     fun delete(obj: T)

    /**
     * 根据对象中的主键更新（主键是自动增长的，无需手动赋值）
     */
    @Update
     fun update(vararg obj: T): Int

     fun deleteAll(tableName:String): Int {
        val query = SimpleSQLiteQuery("delete from $tableName")
        return doDeleteAll(query)
    }

    fun findAll(tableName:String): List<T>? {
        val query = SimpleSQLiteQuery("select * from $tableName")
        return doFindAll(query)
    }

    fun find(id: Long,tableName:String): T? {
        val query = SimpleSQLiteQuery("select * from $tableName where id = ?", arrayOf<Any>(id))
        return doFind(query)
    }

    /**
     * [params] 列名
     * [value] 列的值
     */
    fun deleteByParams(params: String, value: String,tableName:String): Int {
        val query = SimpleSQLiteQuery("delete from $tableName where $params='${value}'")
        Log.d("ez", "deleteByParams: ${"delete from $tableName where $params='${value}'"}")
        return doDeleteByParams(query)
    }

    /**
     * 分页查询，支持传入多个字段，但必须要按照顺序传入
     * key = value，key = value 的形式，一一对应（可以使用 stringbuilder 去构造一下，这里就不演示了）
     */
    fun doQueryByLimit(tableName:String,vararg string: String, limit: Int = 10, offset: Int = 0): List<T>? {
        val query = SimpleSQLiteQuery(
                "SELECT * FROM $tableName WHERE ${string[0]} = '${string[1]}' limit $limit offset $offset")
        return doQueryByLimit(query)
    }

    /**
     * 降序分页查询
     */
    fun doQueryByOrder(tableName:String,vararg string: String, limit: Int = 10, offset: Int = 10): List<T>? {
        val query = SimpleSQLiteQuery(
                "SELECT * FROM $tableName ORDER BY ${string[0]} desc limit '${limit}' offset '${offset}'")
        return doQueryByOrder(query)
    }


    @RawQuery
    fun doFindAll(query: SupportSQLiteQuery): List<T>?

    @RawQuery
    fun doFind(query: SupportSQLiteQuery): T

    @RawQuery
    fun doDeleteAll(query: SupportSQLiteQuery): Int

    @RawQuery
    fun doDeleteByParams(query: SupportSQLiteQuery): Int

    @RawQuery
    fun doQueryByLimit(query: SupportSQLiteQuery): List<T>?

    @RawQuery
    fun doQueryByOrder(query: SupportSQLiteQuery): List<T>?
}