CNLiveSaveAuthDB.swift
8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// 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]) {
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("数据为空")
// }
}
/// 删除一条数据
/// - 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("更新多条数据失败")
// }
}
/// 获取所有数据
/// - 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 []
// }
}
/// 获取所有数据唯一值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/saveAuth"
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
}()
}