CNLiveSaveStandDB.swift 8.81 KB
//
//  CNLiveSaveStanDB.swift
//  metaCode
//
//  Created by open on 2023/9/15.
//

import Foundation
//import WCDBSwift

///身份列表表名  identityApi/allList 接口数据存储
let CNLiveStandListTableName = "CNLiveStandListTableName"
class CNLiveSaveStandDB: NSObject {
    
    ///数据库
//    var database: Database
    
    static let shared = CNLiveSaveStandDB()
    
//    //重新init方法为私有,不让外部调用
//    private override init() {
//        //数据库地址
//        let filePath = NSHomeDirectory() + "/Documents/DB"+UserInfoManager.shared.userInfo.uid+"/CNLiveStandListTableName.db"
//        database = Database(at: filePath)
//
//        super.init()
//        do {
//            try database.create(table: CNLiveStandListTableName, of: StandListModel.self)
//        } catch {
//            print("创建失败")
//        }
//    }
    
    /// 新增一条数据
    /// - Parameter object: 数据
//    func insterObject(object: StandListModel){
//        let array: [StandListModel] = [object]
//        do{
//            try  database.insert(array, intoTable: CNLiveStandListTableName)
//        }catch{
//            print("增加一条数据失败")
//        }
//    }
    
    /// 批量增加数据
    /// - Parameter objects: 数据组
    func insertObjects(objects: [StandListModel]) {
        
        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: CNLiveStandListTableName+"\(item.id)" as NSCopying)
        }
        temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
        
        
//        if objects.count > 0 {
//            do{
//                try  database.insert(objects, intoTable: CNLiveStandListTableName)
//            }catch{
//                print("增加一条数据失败")
//            }
//        }else{
//            print("数据为空")
//        }
    }
    
    /// 获取所有数据
    /// - Returns: 返回查找的所以数据
    func searchAllObject() -> Array<StandListModel>{
        
        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], StandListModel.self,"")
            tempArray.add(listModel)
        })
        return tempArray as! Array<StandListModel>
        
//        do{
//            let allData: [StandListModel] =  try database.getObjects(fromTable: CNLiveStandListTableName)
//            return allData
//        }catch{
//            print("获取失败")
//            return []
//        }
    }
    
    /// 删除一条数据
    /// - Parameter delete: 被删除的数据
    func deleteObject(delete: StandListModel){
        
//        do{
//            try database.delete(fromTable: CNLiveStandListTableName, where: StandListModel.Properties.id == delete.id)
//        }catch{
//            print("删除一条数据失败")
//        }
    }
    
    /// 删除本地所有数据
    func deleteAllObjects() {
//        do {
//            try database.delete(fromTable: CNLiveStandListTableName)
//        } catch {
//            print("数据删除失败")
//        }
    }
    
    
    /// 更新一条数据
    /// - Parameter newObject: 更新的数据源
    func updateObject(newObject: StandListModel){
//        do{
//            //需要更新的字段
//            let updateProperties = [StandListModel.Properties.name,
//                                    StandListModel.Properties.enable,
//                                    StandListModel.Properties.createTime,
//                                    StandListModel.Properties.createTimeString,
//                                    StandListModel.Properties.timeString,
//                                    StandListModel.Properties.auth,
//                                    StandListModel.Properties.backgroundImg,
//                                    StandListModel.Properties.backgroundColor,
//                                    StandListModel.Properties.backgroundColour,
//                                    StandListModel.Properties.description,
//                                    StandListModel.Properties.cardImg,
//                                    StandListModel.Properties.downPath,]
//
//            try database.update(table: CNLiveStandListTableName, on:updateProperties, with: newObject,where: StandListModel.Properties.id == newObject.id)
//        }catch{
//            print("更新一条数据失败")
//        }
    }
    
    /// 更新多条数据
    /// - Parameter newObjects: 新数据数组
    func updateObjects(newObjects: [StandListModel]) {
//        do {
//            let tempArray = NSMutableArray()
//            for _ in newObjects {
//                //需要更新的字段
//                let updateProperties = [StandListModel.Properties.name,
//                                        StandListModel.Properties.enable,
//                                        StandListModel.Properties.createTime,
//                                        StandListModel.Properties.createTimeString,
//                                        StandListModel.Properties.timeString,
//                                        StandListModel.Properties.auth,
//                                        StandListModel.Properties.backgroundImg,
//                                        StandListModel.Properties.backgroundColor,
//                                        StandListModel.Properties.backgroundColour,
//                                        StandListModel.Properties.description,
//                                        StandListModel.Properties.cardImg,
//                                        StandListModel.Properties.downPath,]
//                tempArray.add(updateProperties)
//            }
//            try database.update(table: CNLiveStandListTableName, on: tempArray as! [PropertyConvertible], with: newObjects)
//        } catch {
//            print("更新多条数据失败")
//        }
    }
    
    /// 获取所有数据唯一值id
    /// - Returns: 返回查找的所以数据
//    func searchAllObjectId() -> Array<String>{
//        do{
//            let allData: [StandListModel] =  try database.getObjects(fromTable: CNLiveStandListTableName)
//            let tempArray = NSMutableArray.init()
//            for item in allData {
//                tempArray.add("\(item.id)")
//            }
//            return tempArray as! Array<String>
//        }catch{
//            print("获取失败")
//            return []
//        }
//    }
    
    /// 通过id获取特定数据
    /// - Parameter identityId: id
    /// - Returns: 返回查找的数据
//    func searchOneObject(identityId: String) -> StandListModel{
//        do {
//            let searchPropertie = [StandListModel.Properties.id]
//            let array: [StandListModel] =  try database.getObjects(on: searchPropertie, fromTable: CNLiveStandListTableName, where: StandListModel.Properties.id == identityId)
//            return array.count > 0 ? array.first! : StandListModel.init()
//        } catch {
//            print("获取失败")
//            return StandListModel.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/saveStannd"
        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
    }()
    
}