Group.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // Group.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 28/09/21.
  6. //
  7. import Foundation
  8. public class Group: Model {
  9. public let id: String
  10. public let name: String
  11. public var profile: String
  12. public let quote: String
  13. public let by: String
  14. public let date: String
  15. public let parent: String
  16. public let chatId: String
  17. public var isOpen: String
  18. public let official: String
  19. public let isEducation: String
  20. public let groupType: String
  21. public let isLounge: Bool
  22. public var topics: [Topic] = []
  23. public var childs: [Group] = []
  24. public var members: [Member] = []
  25. public let level: String
  26. public var isSelected = false
  27. public init(id: String, name: String, profile: String, quote: String, by: String, date: String, parent: String, chatId: String = "", groupType: String, isOpen: String, official: String, isEducation: String = "", isLounge: Bool = false, level: String = "") {
  28. self.id = id
  29. self.name = name
  30. self.profile = profile
  31. self.quote = quote
  32. self.by = by
  33. self.date = date
  34. self.parent = parent
  35. self.chatId = chatId
  36. self.groupType = groupType
  37. self.isOpen = isOpen
  38. self.official = official
  39. self.isEducation = isEducation
  40. self.isLounge = isLounge
  41. self.level = level
  42. }
  43. var isInternal: Bool {
  44. return isEducation == "2" || isEducation == "3" || isEducation == "4"
  45. }
  46. public var description: String {
  47. return "(\(id), \(name), \(chatId), \(groupType), \(childs)"
  48. }
  49. public static func == (lhs: Group, rhs: Group) -> Bool {
  50. return lhs.id == rhs.id
  51. }
  52. public static func getData(group_id: String?) -> Group? {
  53. guard let group_id = group_id else {
  54. return nil
  55. }
  56. var group: Group?
  57. Database.shared.database?.inTransaction({ fmdb, rollback in
  58. do {
  59. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select group_id, f_name, image_id, quote, created_by, created_date, parent, group_type, is_open, official from GROUPZ where group_id = '\(group_id)'"), cursor.next() {
  60. group = Group(id: cursor.string(forColumnIndex: 0) ?? "",
  61. name: cursor.string(forColumnIndex: 1) ?? "",
  62. profile: cursor.string(forColumnIndex: 2) ?? "",
  63. quote: cursor.string(forColumnIndex: 3) ?? "",
  64. by: cursor.string(forColumnIndex: 4) ?? "",
  65. date: cursor.string(forColumnIndex: 5) ?? "",
  66. parent: cursor.string(forColumnIndex: 6) ?? "",
  67. groupType: cursor.string(forColumnIndex: 7) ?? "",
  68. isOpen: cursor.string(forColumnIndex: 8) ?? "",
  69. official: cursor.string(forColumnIndex: 9) ?? "")
  70. cursor.close()
  71. }
  72. } catch {
  73. rollback.pointee = true
  74. print("Access database error: \(error.localizedDescription)")
  75. }
  76. })
  77. return group
  78. }
  79. }
  80. public class Topic: Model {
  81. public let chatId: String
  82. public let title: String
  83. public let thumb: String
  84. public init(chatId: String, title: String, thumb: String) {
  85. self.chatId = chatId
  86. self.title = title
  87. self.thumb = thumb
  88. }
  89. public static func == (lhs: Topic, rhs: Topic) -> Bool {
  90. return lhs.chatId == rhs.chatId
  91. }
  92. public var description: String {
  93. return ""
  94. }
  95. public static func getData(topic_id: String?) -> Topic? {
  96. guard let topic_id = topic_id else {
  97. return nil
  98. }
  99. var topic: Topic?
  100. Database.shared.database?.inTransaction({ fmdb, rollback in
  101. do {
  102. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select chat_id, title, thumb from DISCUSSION_FORUM where chat_id = '\(topic_id)'"), cursor.next() {
  103. topic = Topic(chatId: cursor.string(forColumnIndex: 0) ?? "",
  104. title: cursor.string(forColumnIndex: 1) ?? "",
  105. thumb: cursor.string(forColumnIndex: 2) ?? "")
  106. cursor.close()
  107. }
  108. } catch {
  109. rollback.pointee = true
  110. print("Access database error: \(error.localizedDescription)")
  111. }
  112. })
  113. return topic
  114. }
  115. }
  116. public class Member: User {
  117. public var position: String
  118. override init(pin: String) {
  119. self.position = "0"
  120. super.init(pin: pin)
  121. }
  122. public init(pin: String, firstName: String, lastName: String, thumb: String, position: String) {
  123. self.position = position
  124. super.init(pin: pin, firstName: firstName, lastName: lastName, thumb: thumb)
  125. }
  126. public static func getAllMember(group_id: String) -> [Member] {
  127. var members: [Member] = []
  128. Database.shared.database?.inTransaction({ fmdb, rollback in
  129. do {
  130. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, thumb_id, position from GROUPZ_MEMBER where group_id = '\(group_id)' OR group_id = (select group_id from DISCUSSION_FORUM where chat_id = '\(group_id)')") {
  131. while cursor.next() {
  132. members.append(Member(pin: cursor.string(forColumnIndex: 0) ?? "",
  133. firstName: cursor.string(forColumnIndex: 1) ?? "",
  134. lastName: cursor.string(forColumnIndex: 2) ?? "",
  135. thumb: cursor.string(forColumnIndex: 3) ?? "",
  136. position: cursor.string(forColumnIndex: 4) ?? ""))
  137. }
  138. cursor.close()
  139. }
  140. } catch {
  141. rollback.pointee = true
  142. print("Access database error: \(error.localizedDescription)")
  143. }
  144. })
  145. return members
  146. }
  147. public static func getMember(f_pin: String) -> Member? {
  148. var member: Member?
  149. Database.shared.database?.inTransaction({ fmdb, rollback in
  150. do {
  151. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, thumb_id, position from GROUPZ_MEMBER where f_pin = '\(f_pin)'") {
  152. while cursor.next() {
  153. member = Member(pin: cursor.string(forColumnIndex: 0) ?? "",
  154. firstName: cursor.string(forColumnIndex: 1) ?? "",
  155. lastName: cursor.string(forColumnIndex: 2) ?? "",
  156. thumb: cursor.string(forColumnIndex: 3) ?? "",
  157. position: cursor.string(forColumnIndex: 4) ?? "")
  158. }
  159. if f_pin == "-997" {
  160. member = Member(pin: "-997",
  161. firstName: "GPT SmartBot",
  162. lastName: "",
  163. thumb: "",
  164. position: "")
  165. }
  166. cursor.close()
  167. }
  168. } catch {
  169. rollback.pointee = true
  170. print("Access database error: \(error.localizedDescription)")
  171. }
  172. })
  173. return member
  174. }
  175. public static func getMemberInGroup(f_pin: String, group_id: String) -> Member? {
  176. var member: Member?
  177. Database.shared.database?.inTransaction({ fmdb, rollback in
  178. do {
  179. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, thumb_id, position from GROUPZ_MEMBER where f_pin = '\(f_pin)' AND (group_id = '\(group_id)' OR group_id = (select group_id from DISCUSSION_FORUM where chat_id = '\(group_id)'))") {
  180. while cursor.next() {
  181. member = Member(pin: cursor.string(forColumnIndex: 0) ?? "",
  182. firstName: cursor.string(forColumnIndex: 1) ?? "",
  183. lastName: cursor.string(forColumnIndex: 2) ?? "",
  184. thumb: cursor.string(forColumnIndex: 3) ?? "",
  185. position: cursor.string(forColumnIndex: 4) ?? "")
  186. }
  187. if f_pin == "-997" {
  188. member = Member(pin: "-997",
  189. firstName: "GPT SmartBot",
  190. lastName: "",
  191. thumb: "",
  192. position: "")
  193. }
  194. cursor.close()
  195. }
  196. } catch {
  197. rollback.pointee = true
  198. print("Access database error: \(error.localizedDescription)")
  199. }
  200. })
  201. return member
  202. }
  203. }