export default class DB {
    //打开数据库
    private dbName: string //数据库名称
    private db: any //数据库对象
    constructor(dbName: string) {
        this.dbName = dbName
    }
    public openStore(storeName: string, keyPath: string, indexs?: Array<string>) {
        const request = window.indexedDB.open(this.dbName, 2)
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log("数据库打开成功")
                this.db = event.target.result
                resolve(true)
            }
            request.onerror = (event) => {
                console.log("数据库打开失败")
                reject(event)
            }
            request.onupgradeneeded = (event) => {
                console.log("数据库升级成功")
                const { result }: any = event.target
                const store = result.createObjectStore(storeName, { autoIncrement: true, keyPath })
                if (indexs && indexs.length > 0) {
                    indexs?.map((v: string) => {
                        store.createIndex(v, v, { uqique: true })
                    })
                }
                store.transaction.oncomplate = (event: any) => {
                    console.log("创建对象仓库成功")
                }
                console.log(event)
            }
        })
    }
    //新增、修改数据
    updateItem(storeName: string, data: any) {
        console.log(storeName, data)
        const store = this.db.transaction([storeName], 'readwrite').objectStore(storeName)
        const request = store.put({ ...data, updateItem: new Date().getTime() })
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log("数据写入成功")
                resolve(event)
            }
            request.onerror = (event: any) => {
                console.log("数据写入失败")
                reject(event)
            }
        })
    }
    //删除数据
    deleteItem(storeName: string, key: number | string) {
        const store = this.db.transaction([storeName], 'readwrite').objectStore(storeName)
        const request = store.delete(key)
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log("删除成功")
                resolve(event)
            }
            request.onerror = (event: any) => {
                console.log("删除失败")
                reject(event)
            }
        })
    }
    //查询所有数据
    getList(storeName: string) {
        const store = this.db.transaction([storeName]).objectStore(storeName)
        const request = store.getAll()
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log("查询所有成功")
                resolve(event)
            }
            request.onerror = (event: any) => {
                console.log("查询所有失败")
                reject(event)
            }
        })
    }
    //查询某一条数据
    getItem(storeName: string, key: string | number) {
        const store = this.db.transaction([storeName]).objectStore(storeName)
        const request = store.get(key)
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log("查询某一条成功")
                resolve(event.target.result)
            }
            request.onerror = (event: any) => {
                console.log("查询某一条失败")
                reject(event)
            }
        })
    }
}