OOAppsInfoDB.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // OOAppsInfoDB.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/9.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import FMDB
  10. final class OOAppsInfoDB: NSObject {
  11. static let shareInstance = OOAppsInfoDB()
  12. var queue:FMDatabaseQueue?
  13. private func _createDB() {
  14. var path = "\(NSHomeDirectory())/Documents/O2"
  15. if createPath(path) {
  16. path = path + "/OOAppsInfo.db"
  17. O2Logger.debug("OOAppsInfoDBWithPath:\(path)")
  18. queue = FMDatabaseQueue(path: path)
  19. let sql = "create table IF NOT EXISTS OOAppsInfo(`id` integer primary key autoincrement,`title` text not null,`appid` text not null,`storyboard` text,`vcname` text,`segueidentifier` text,`normalicon` text,`selectedicon` text,`order` integer,`mainorder` integer,`categorytype` integer)"
  20. queue?.inDatabase({ (db) in
  21. db?.executeUpdate(sql, withArgumentsIn: nil)
  22. })
  23. }
  24. }
  25. func dropTable() {
  26. if queue == nil {
  27. _createDB()
  28. dropTable()
  29. }
  30. let sql = "drop table if exists OOAppsInfo"
  31. queue?.inDatabase({ (db) in
  32. db?.executeUpdate(sql, withArgumentsIn: nil)
  33. })
  34. }
  35. func updateData(_ info:O2App,_ categoryType:Int){
  36. if queue == nil {
  37. _createDB()
  38. updateData(info,categoryType)
  39. }
  40. let sql = "UPDATE OOAppsInfo SET title=?,storyboard=?,vcname=?,segueidentifier=?,normalicon=?,selectedicon=?,`order`=?,mainorder=?,categorytype=? where appid=?"
  41. let argumentsArray = [(info.title ?? ""), (info.storyBoard ?? ""), (info.vcName ?? ""),(info.segueIdentifier ?? ""), (info.normalIcon ?? ""), (info.selectedIcon ?? ""), info.order, info.mainOrder, categoryType, info.appId] as [Any]
  42. queue?.inDatabase({ (db) in
  43. db?.executeUpdate(sql,withArgumentsIn:argumentsArray)
  44. })
  45. }
  46. func insertData(_ info:O2App){
  47. if queue == nil {
  48. _createDB()
  49. insertData(info)
  50. }
  51. let sSql = "select count(*) as appnumber,`categorytype` from OOAppsInfo where appid = '\(String(describing: info.appId!))'"
  52. let insertSql = "INSERT INTO OOAppsInfo(appid,title,storyboard,vcname,segueidentifier,normalicon,selectedicon,`order`,mainOrder,categorytype) values (?,?,?,?,?,?,?,?,?,?)"
  53. let argumentsArray1 = [info.appId , (info.title ?? ""), (info.storyBoard ?? ""),(info.vcName ?? ""), (info.segueIdentifier ?? ""), (info.normalIcon ?? ""), (info.selectedIcon ?? ""), info.order, info.mainOrder, 0] as [Any]
  54. var isUpdate = false
  55. var categoryType = 0
  56. queue?.inDatabase({ (db) in
  57. let resultSet = db?.executeQuery(sSql, withArgumentsIn: nil)
  58. if let _ = (resultSet?.next()) {
  59. if Int(resultSet?.int(forColumn:"appnumber") ?? 0 ) > 0 {
  60. isUpdate = true
  61. categoryType = Int(resultSet?.int(forColumn: "categorytype") ?? 0)
  62. }else{
  63. isUpdate = false
  64. db?.executeUpdate(insertSql, withArgumentsIn: argumentsArray1)
  65. }
  66. }else{
  67. db?.executeUpdate(insertSql, withArgumentsIn: argumentsArray1)
  68. }
  69. })
  70. if isUpdate == true {
  71. updateData(info, categoryType)
  72. }
  73. }
  74. func removeAll() {
  75. if queue == nil {
  76. _createDB()
  77. removeAll()
  78. }
  79. let delSql = "DELETE FROM OOAppsInfo WHERE 1=1"
  80. queue?.inDatabase({ (db) in
  81. db?.executeUpdate(delSql, withArgumentsIn: nil)
  82. })
  83. }
  84. func deleteNotExistApp(_ exitApps: [String]) {
  85. if queue == nil {
  86. _createDB()
  87. deleteNotExistApp(exitApps)
  88. }
  89. var infos:[O2App] = []
  90. let sql = "select * from OOAppsInfo ORDER BY `order` ASC"
  91. queue?.inDatabase({ (db) in
  92. let resultSet = db?.executeQuery(sql, withArgumentsIn: nil)
  93. while (resultSet?.next())!{
  94. let app = O2App()
  95. app.appId = resultSet?.string(forColumn: "appid")
  96. app.title = resultSet?.string(forColumn: "title")
  97. app.storyBoard = resultSet?.string(forColumn: "storyboard")
  98. app.vcName = resultSet?.string(forColumn: "vcname")
  99. app.segueIdentifier = resultSet?.string(forColumn: "segueidentifier")
  100. app.normalIcon = resultSet?.string(forColumn: "normalicon")
  101. app.selectedIcon = resultSet?.string(forColumn: "selectedicon")
  102. app.order = Int((resultSet?.int(forColumn: "order"))!)
  103. app.mainOrder = Int((resultSet?.int(forColumn: "mainorder"))!)
  104. infos.append(app)
  105. }
  106. infos.forEachEnumerated({ (index, app) in
  107. if let appId = app.appId {
  108. if !exitApps.contains(appId) {// 不存在就删除
  109. let delSql = "delete from OOAppsInfo where appid = '\(appId)'"
  110. db?.executeUpdate(delSql, withArgumentsIn: nil)
  111. }
  112. }
  113. })
  114. })
  115. }
  116. func delete(_ info:O2App){
  117. if queue == nil {
  118. _createDB()
  119. delete(info)
  120. }
  121. let delSql = "delete from OOAppsInfo where appid = '\(String(describing: info.appId!))'"
  122. queue?.inDatabase({ (db) in
  123. db?.executeUpdate(delSql, withArgumentsIn: nil)
  124. })
  125. }
  126. func queryData(_ appId:String) -> O2App? {
  127. if queue == nil {
  128. _createDB()
  129. let _ = queryData(appId)
  130. }
  131. var homeApp:O2App?
  132. let sql = "SELECT * FROM OOAppsInfo WHERE appid = '\(appId)'"
  133. queue?.inDatabase({ (db) in
  134. let resultSet = db?.executeQuery(sql, withArgumentsIn: nil)
  135. while(resultSet?.next())!{
  136. homeApp = O2App()
  137. homeApp?.appId = resultSet?.string(forColumn: "appid")
  138. homeApp?.title = resultSet?.string(forColumn: "title")
  139. homeApp?.storyBoard = resultSet?.string(forColumn: "storyboard")
  140. homeApp?.vcName = resultSet?.string(forColumn: "vcname")
  141. homeApp?.segueIdentifier = resultSet?.string(forColumn: "segueidentifier")
  142. homeApp?.normalIcon = resultSet?.string(forColumn: "normalicon")
  143. homeApp?.selectedIcon = resultSet?.string(forColumn: "selectedicon")
  144. homeApp?.order = Int((resultSet?.int(forColumn: "order"))!)
  145. homeApp?.mainOrder = Int((resultSet?.int(forColumn: "mainorder"))!)
  146. }
  147. })
  148. return homeApp
  149. }
  150. func queryData() -> [O2App] {
  151. if queue == nil {
  152. _createDB()
  153. let _ = queryData()
  154. }
  155. var infos:[O2App] = []
  156. let sql = "select * from OOAppsInfo ORDER BY `order` ASC"
  157. queue?.inDatabase({ (db) in
  158. let resultSet = db?.executeQuery(sql, withArgumentsIn: nil)
  159. while (resultSet?.next())!{
  160. let app = O2App()
  161. app.appId = resultSet?.string(forColumn: "appid")
  162. app.title = resultSet?.string(forColumn: "title")
  163. app.storyBoard = resultSet?.string(forColumn: "storyboard")
  164. app.vcName = resultSet?.string(forColumn: "vcname")
  165. app.segueIdentifier = resultSet?.string(forColumn: "segueidentifier")
  166. app.normalIcon = resultSet?.string(forColumn: "normalicon")
  167. app.selectedIcon = resultSet?.string(forColumn: "selectedicon")
  168. app.order = Int((resultSet?.int(forColumn: "order"))!)
  169. app.mainOrder = Int((resultSet?.int(forColumn: "mainorder"))!)
  170. infos.append(app)
  171. }
  172. })
  173. return infos
  174. }
  175. func queryNoMainData() -> [O2App] {
  176. if queue == nil {
  177. _createDB()
  178. let _ = queryNoMainData()
  179. }
  180. var infos:[O2App] = []
  181. let sql = "select * from OOAppsInfo where categorytype = 0 ORDER BY `order`"
  182. queue?.inDatabase({ (db) in
  183. let resultSet = db?.executeQuery(sql, withArgumentsIn: nil)
  184. while (resultSet?.next())!{
  185. let app = O2App()
  186. app.appId = resultSet?.string(forColumn: "appid")
  187. app.title = resultSet?.string(forColumn: "title")
  188. app.storyBoard = resultSet?.string(forColumn: "storyboard")
  189. app.vcName = resultSet?.string(forColumn: "vcname")
  190. app.segueIdentifier = resultSet?.string(forColumn: "segueidentifier")
  191. app.normalIcon = resultSet?.string(forColumn: "normalicon")
  192. app.selectedIcon = resultSet?.string(forColumn: "selectedicon")
  193. app.order = Int((resultSet?.int(forColumn: "order"))!)
  194. app.mainOrder = Int((resultSet?.int(forColumn: "mainorder"))!)
  195. infos.append(app)
  196. }
  197. })
  198. return infos
  199. }
  200. func queryMainData() -> [O2App] {
  201. if queue == nil {
  202. _createDB()
  203. let _ = queryMainData()
  204. }
  205. var infos:[O2App] = []
  206. let sql = "select * from OOAppsInfo where categorytype = 1 ORDER BY `mainorder`"
  207. queue?.inDatabase({ (db) in
  208. let resultSet = db?.executeQuery(sql, withArgumentsIn: nil)
  209. while (resultSet?.next())!{
  210. let app = O2App()
  211. app.appId = resultSet?.string(forColumn: "appid")
  212. app.title = resultSet?.string(forColumn: "title")
  213. app.storyBoard = resultSet?.string(forColumn: "storyboard")
  214. app.vcName = resultSet?.string(forColumn: "vcname")
  215. app.segueIdentifier = resultSet?.string(forColumn: "segueidentifier")
  216. app.normalIcon = resultSet?.string(forColumn: "normalicon")
  217. app.selectedIcon = resultSet?.string(forColumn: "selectedicon")
  218. app.order = Int((resultSet?.int(forColumn: "order"))!)
  219. app.mainOrder = Int((resultSet?.int(forColumn: "mainorder"))!)
  220. infos.append(app)
  221. }
  222. })
  223. return infos
  224. }
  225. // MARK: - private func
  226. private func createPath(_ path: String) -> Bool {
  227. var isDir: ObjCBool = ObjCBool(false)
  228. if FileManager.default.fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue {
  229. return true
  230. } else {
  231. try! FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
  232. return true
  233. }
  234. }
  235. }