CNLiveSaveAuthDB.swift 8.2 KB
//
//  CNLiveSaveAuthDataSourceDB.swift
//  metaCode
//
//  Created by open on 2023/9/13.
//

import Foundation
//import WCDBSwift

///身份认证表名
let CNLiveAuthListTableName = "CNLiveAuthListTable"

class CNLiveSaveAuthDB : NSObject{
    
    ///数据库
//    var database: Database
    
    static let shared = CNLiveSaveAuthDB()
    
    //重新init方法为私有,不让外部调用
//    private override init() {
//        //数据库地址
//        let filePath = NSHomeDirectory() + "/Documents/DB"+UserInfoManager.shared.userInfo.uid+"/CNLiveAuthListTable.db"
//        database = Database(at: filePath)
//
//        super.init()
//        do {
//            try database.create(table: CNLiveAuthListTableName, of: MineAuthListModel.self)
//        } catch {
//            print("创建失败")
//        }
//    }
    
    /// 新增一条数据
    /// - Parameter object: 数据
    func insterObject(object: MineAuthListModel) {
//        let array: [MineAuthListModel] = [object]
//        do{
//            try  database.insert(array, intoTable: CNLiveAuthListTableName)
//        }catch{
//            print("增加一条数据失败")
//        }
    }
    
    /// 批量增加数据
    /// - Parameter objects: 数据组
    func insertObjects(objects: [MineAuthListModel]) {
        
        do {
            try FileManager.default.removeItem(atPath: userFileDir+"storage.plist")
        } catch _ {}
        
        
        var temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if (temp_dic == nil) {
            temp_dic = NSMutableDictionary.init()
        }
        for item in objects {
            temp_dic?.setObject((item.toJSON() ?? Dictionary()) as Dictionary, forKey: CNLiveAuthListTableName+"\(item.identityId)" as NSCopying)
        }
        temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
        
//        if objects.count > 0 {
//            do{
//                for model in objects {
//                    let m = model
//                    print("插入时的数据结构===>\(m.identityId)")
//                }
//                try  database.insert(objects, intoTable: CNLiveAuthListTableName)
//            }catch{
//                print("增加一条数据失败")
//            }
//        }else{
//            print("数据为空")
//        }
    }
    
    /// 获取所有数据
    /// - Returns: 返回查找的所以数据
    func searchAllObject() -> Array<MineAuthListModel>{
        
        let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if (temp_dic == nil) {
            return Array()
        }
        let tempArray = NSMutableArray.init()
        temp_dic?.allValues.forEach({ value in
            let listModel = dictionaryToModel(value as! [String : Any], MineAuthListModel.self,"")
            tempArray.add(listModel)
        })
        return tempArray as! Array<MineAuthListModel>
        
//        do{
//            let allData: [MineAuthListModel] =  try database.getObjects(fromTable: CNLiveAuthListTableName)
//            return allData
//        }catch{
//            print("获取失败")
//            return []
//        }
    }
    
    /// 删除一条数据
    /// - Parameter delete: 被删除的数据
    func deleteObject(delete: MineAuthListModel){
        
//        do{
//            try database.delete(fromTable: CNLiveAuthListTableName, where: MineAuthListModel.Properties.identityId == delete.identityId)
//        }catch{
//            print("删除一条数据失败")
//        }
    }
    
    /// 删除本地所有数据
    func deleteAllObjects() {
//        do {
//            try database.delete(fromTable: CNLiveAuthListTableName)
//        } catch {
//            print("数据删除失败")
//        }
    }
    
    
    /// 更新一条数据
    /// - Parameter newObject: 更新的数据源
    func updateObject(newObject: MineAuthListModel) {
//        do{
//            //需要更新的字段
//            let updateProperties = [MineAuthListModel.Properties.name,
//                                    MineAuthListModel.Properties.icon,
//                                    MineAuthListModel.Properties.cardImg,
//                                    MineAuthListModel.Properties.authTime,
//                                    MineAuthListModel.Properties.code,
//                                    MineAuthListModel.Properties.userStatus,]
//
//            try database.update(table: CNLiveAuthListTableName, on:updateProperties, with: newObject,where: MineAuthListModel.Properties.identityId == newObject.identityId)
//        }catch{
//            print("更新一条数据失败")
//        }
    }
    
    /// 更新多条数据
    /// - Parameter newObjects: 新数据数组
    func updateObjects(newObjects: [MineAuthListModel]) {
//        do {
//            let tempArray = NSMutableArray()
//            for _ in newObjects {
//                //需要更新的字段
//                let updateProperties = [MineAuthListModel.Properties.name,
//                                        MineAuthListModel.Properties.icon,
//                                        MineAuthListModel.Properties.cardImg,
//                                        MineAuthListModel.Properties.authTime,
//                                        MineAuthListModel.Properties.code,
//                                        MineAuthListModel.Properties.userStatus,]
//                tempArray.add(updateProperties)
//            }
//            try database.update(table: CNLiveAuthListTableName, on: tempArray as! [PropertyConvertible], with: newObjects)
//        } catch {
//            print("更新多条数据失败")
//        }
    }
    
    
    
    
    /// 获取所有数据唯一值id
    /// - Returns: 返回查找的所以数据
//    func searchAllObjectId() -> Array<String>{
//        do{
//            let allData: [MineAuthListModel] =  try database.getObjects(fromTable: CNLiveAuthListTableName)
//            let tempArray = NSMutableArray.init()
//            for item in allData {
//                print("数据库查找之后数据==>\(item.identityId)")
//                tempArray.add(item.identityId)
//            }
//            return tempArray as! Array<String>
//        }catch{
//            print("获取失败")
//            return []
//        }
//    }
    
    /// 通过id获取特定数据
    /// - Parameter identityId: id
    /// - Returns: 返回查找的数据
//    func searchOneObject(identityId: String) -> MineAuthListModel{
//        do {
//            let searchPropertie = [MineAuthListModel.Properties.identityId]
//            let array: [MineAuthListModel] =  try database.getObjects(on: searchPropertie, fromTable: CNLiveAuthListTableName, where: MineAuthListModel.Properties.identityId == identityId)
//            return array.count > 0 ? array.first! : MineAuthListModel.init()
//        } catch {
//            print("获取失败")
//            return MineAuthListModel.init()
//        }
//    }
    
    
    private lazy var userFileDir: String = {
        let userFileDir = NSString()
        var path: String = ""
        path = "\(fileDir)/userId_\(UserInfoManager.shared.userInfo.uid)/"
        let fileManager = FileManager.default
        var isDir: ObjCBool = false
        let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
        if !(isDir.boolValue && exist) {
            do {
                try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
            } catch {
                debugPrint("文件目录创建失败")
                return ""
            }
        }
        return path as String
    }()
    
    private lazy var fileDir: String = {
        let path = "\(NSHomeDirectory())/Documents/metaCode/saveAuthDB"
        let fileManager = FileManager.default
        var isDir: ObjCBool = false
        let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
        if !(isDir.boolValue && exist) {
            do {
                try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
            } catch {
                debugPrint("文件目录创建失败")
                return ""
            }
        }
        return path as String
    }()
}