CNLiveSaveStandDB.swift 6.32 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]) {
        if objects.count > 0 {
            do{
                try  database.insert(objects, intoTable: CNLiveStandListTableName)
            }catch{
                print("增加一条数据失败")
            }
        }else{
            print("数据为空")
        }
    }
    
    /// 删除一条数据
    /// - 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("更新多条数据失败")
        }
    }
    
    
    /// 获取所有数据
    /// - Returns: 返回查找的所以数据
    func searchAllObject() -> Array<StandListModel>{
        do{
            let allData: [StandListModel] =  try database.getObjects(fromTable: CNLiveStandListTableName)
            return allData
        }catch{
            print("获取失败")
            return []
        }
    }
    
    /// 获取所有数据唯一值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()
        }
    }
    
}