package com.cr.common

import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.OPEN_READWRITE
import com.cr.data.CrUtil
import java.io.File

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/31 09:09
 * 描述：数据管理类
 */
open class DatabaseBaseManager {
    // define: 2023/3/31 配置库
    var sqLiteDatabaseConfig: SQLiteDatabase? = null

    /**
     * 在此代码块中加入静态方法
     */
    companion object {
        /**
         * 获取静态实例 单例
         * @return DatabaseManager
         */
        fun getInstance() = InstanceHelper.self
    }

    object InstanceHelper {
        var self = DatabaseBaseManager()
    }

    /**
     * 打开配置库
     * @return Boolean
     */
    open fun openConfigDatabase(): Boolean {
        var filePath: String = CrUtil.DATABASE_PATH
        var file: File = File(filePath)
        if (!file.exists()) return false
        if (sqLiteDatabaseConfig == null) {
            sqLiteDatabaseConfig = SQLiteDatabase.openDatabase(filePath, null, OPEN_READWRITE)
        }
        return sqLiteDatabaseConfig!!.isOpen
    }

    /**
     * 通用查询方法
     * @param sql String SQL
     * @return List<HashMap<String,String>>
     */
    fun query(sql: String): List<HashMap<String, String>> {
        var res = mutableListOf<HashMap<String, String>>()
        var cursor: Cursor? = sqLiteDatabaseConfig?.rawQuery(sql, null)
        // todo: 2023/4/3 获取列
        cursor?.let {
            var columns = it.columnNames
            while (it.moveToNext()) {
                var map = hashMapOf<String, String>()
                for (column in columns) {
                    var columnName = column.toLowerCase()
                    try{
                        map[columnName] = it.getString(it.getColumnIndex(column))
                    }catch (e:java.lang.Exception){
                        map[columnName] = ""
                    }

                }
                res.add(map)
            }
        }
        return res
    }

    /**
     * 更新数据
     * @param tabName String 待更新表名
     * @param values HashMap<String, String> 更新值集合
     * @param where String 条件
     * @return Boolean 是否更新成功
     */
    fun update(tabName: String, values: HashMap<String, String>, where: String): Boolean {
        var uValues = ContentValues()
        for (value in values) {
            uValues.put(value.key, value.value)
        }
        var resInt = sqLiteDatabaseConfig?.update(tabName, uValues, where, null)
        return resInt!! > 0
    }

    /**
     * 删除数据
     * @param tabName String 表名
     * @param where String 条件
     * @return Boolean
     */
    fun delete(tabName:String,where:String):Boolean{
        var resInt = sqLiteDatabaseConfig?.delete(tabName,where,null)
        return resInt!! > 0
    }

    /**
     * 插入数据
     * @param tabName String 表名
     * @param values HashMap<String, String> 插入集合
     * @return Boolean
     */
    fun insert(tabName: String, values: HashMap<String, String>):Boolean{
        var uValues = ContentValues()
        for (value in values) {
            uValues.put(value.key, value.value)
        }
        var resInt = sqLiteDatabaseConfig?.insert(tabName, null,uValues)
        return resInt!! > 0
    }
}