User.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // User.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 28/09/21.
  6. //
  7. import Foundation
  8. import FMDB
  9. public class User: Model {
  10. public var pin: String
  11. public var firstName: String
  12. public var lastName: String
  13. public var thumb: String
  14. public var official: String?
  15. public var userType: String?
  16. public var privacy_flag: String?
  17. public var offline_mode: String?
  18. public var ex_block: String?
  19. public var ex_offmp: String?
  20. public var device_id: String
  21. public var isSelected: Bool = false
  22. public init(pin: String) {
  23. self.pin = pin
  24. self.firstName = ""
  25. self.lastName = ""
  26. self.thumb = ""
  27. self.userType = ""
  28. self.privacy_flag = ""
  29. self.offline_mode = ""
  30. self.ex_block = ""
  31. self.ex_offmp = ""
  32. self.device_id = ""
  33. }
  34. public init(pin: String, firstName: String, lastName: String, thumb: String, userType: String = "0", privacy_flag: String = "", offline_mode: String = "", ex_block: String = "", official: String = "", ex_offmp: String = "", device_id: String = "") {
  35. self.pin = pin
  36. self.firstName = firstName
  37. self.lastName = lastName
  38. self.thumb = thumb
  39. self.userType = userType
  40. self.privacy_flag = privacy_flag
  41. self.offline_mode = offline_mode
  42. self.official = official
  43. self.ex_block = ex_block
  44. self.ex_offmp = ex_offmp
  45. self.device_id = device_id
  46. }
  47. public static func == (lhs: User, rhs: User) -> Bool {
  48. return lhs.pin == rhs.pin
  49. }
  50. public var description: String {
  51. return "\(pin) \(firstName) \(lastName) \(thumb)"
  52. }
  53. public var fullName: String {
  54. return "\(firstName) \(lastName)".trimmingCharacters(in: .whitespaces)
  55. }
  56. public static func isOfficial(official_account: String) -> Bool {
  57. return official_account == "1"
  58. }
  59. public static func isVerified(official_account: String) -> Bool {
  60. return official_account == "2"
  61. }
  62. public static func isOfficialRegular(official_account: String) -> Bool {
  63. return official_account == "3"
  64. }
  65. public static func isInternal(userType: String) -> Bool {
  66. return userType == "23"
  67. }
  68. public static func isCallCenter(userType: String) -> Bool {
  69. return userType == "24"
  70. }
  71. public static func getMyPin() -> String? {
  72. return UserDefaults.standard.string(forKey: "me")
  73. }
  74. public static func getData(pin: String?, fmdb: FMDatabase? = nil) -> User? {
  75. guard let pin = pin else {
  76. return nil
  77. }
  78. var user: User?
  79. if fmdb != nil {
  80. if let cursor = Database.shared.getRecords(fmdb: fmdb!, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
  81. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  82. firstName: cursor.string(forColumnIndex: 1) ?? "",
  83. lastName: cursor.string(forColumnIndex: 2) ?? "",
  84. thumb: cursor.string(forColumnIndex: 3) ?? "",
  85. userType: cursor.string(forColumnIndex: 4) ?? "",
  86. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  87. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  88. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  89. official: cursor.string(forColumnIndex: 9) ?? "",
  90. device_id: cursor.string(forColumnIndex: 8) ?? "")
  91. cursor.close()
  92. } else {
  93. user = User(pin: pin,
  94. firstName: "User".localized(),
  95. lastName: "",
  96. thumb: "",
  97. userType: "",
  98. privacy_flag: "",
  99. offline_mode: "",
  100. ex_block: "")
  101. }
  102. } else {
  103. Database.shared.database?.inTransaction({ fmdb, rollback in
  104. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
  105. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  106. firstName: cursor.string(forColumnIndex: 1) ?? "",
  107. lastName: cursor.string(forColumnIndex: 2) ?? "",
  108. thumb: cursor.string(forColumnIndex: 3) ?? "",
  109. userType: cursor.string(forColumnIndex: 4) ?? "",
  110. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  111. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  112. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  113. official: cursor.string(forColumnIndex: 9) ?? "",
  114. device_id: cursor.string(forColumnIndex: 8) ?? "")
  115. cursor.close()
  116. } else {
  117. user = User(pin: pin,
  118. firstName: "User".localized(),
  119. lastName: "",
  120. thumb: "",
  121. userType: "",
  122. privacy_flag: "",
  123. offline_mode: "",
  124. ex_block: "")
  125. }
  126. })
  127. }
  128. return user
  129. }
  130. public static func getDataCanNil(pin: String?, fmdb: FMDatabase? = nil) -> User? {
  131. guard let pin = pin else {
  132. return nil
  133. }
  134. var user: User?
  135. if fmdb != nil {
  136. if let cursor = Database.shared.getRecords(fmdb: fmdb!, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
  137. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  138. firstName: cursor.string(forColumnIndex: 1) ?? "",
  139. lastName: cursor.string(forColumnIndex: 2) ?? "",
  140. thumb: cursor.string(forColumnIndex: 3) ?? "",
  141. userType: cursor.string(forColumnIndex: 4) ?? "",
  142. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  143. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  144. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  145. official: cursor.string(forColumnIndex: 9) ?? "",
  146. device_id: cursor.string(forColumnIndex: 8) ?? "")
  147. cursor.close()
  148. }
  149. } else {
  150. Database.shared.database?.inTransaction({ fmdb, rollback in
  151. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
  152. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  153. firstName: cursor.string(forColumnIndex: 1) ?? "",
  154. lastName: cursor.string(forColumnIndex: 2) ?? "",
  155. thumb: cursor.string(forColumnIndex: 3) ?? "",
  156. userType: cursor.string(forColumnIndex: 4) ?? "",
  157. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  158. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  159. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  160. official: cursor.string(forColumnIndex: 9) ?? "",
  161. device_id: cursor.string(forColumnIndex: 8) ?? "")
  162. cursor.close()
  163. }
  164. })
  165. }
  166. return user
  167. }
  168. public static func getDataFromNameCanNil(name: String?, fmdb: FMDatabase? = nil) -> User? {
  169. guard let name = name else {
  170. return nil
  171. }
  172. let listName = name.components(separatedBy: " ")
  173. let firstName = listName[0]
  174. var lastName = ""
  175. print("firstName: \(firstName) <> lastName: \(lastName)")
  176. if listName.count > 1 {
  177. for i in 1..<listName.count {
  178. if lastName.isEmpty {
  179. lastName = listName[i]
  180. } else {
  181. lastName = lastName + " " + listName[i]
  182. }
  183. }
  184. }
  185. var user: User?
  186. if fmdb != nil {
  187. if let cursor = Database.shared.getRecords(fmdb: fmdb!, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where LOWER(first_name) = '\(firstName.lowercased())' AND LOWER(last_name) = '\(lastName.lowercased())'"), cursor.next() {
  188. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  189. firstName: cursor.string(forColumnIndex: 1) ?? "",
  190. lastName: cursor.string(forColumnIndex: 2) ?? "",
  191. thumb: cursor.string(forColumnIndex: 3) ?? "",
  192. userType: cursor.string(forColumnIndex: 4) ?? "",
  193. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  194. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  195. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  196. official: cursor.string(forColumnIndex: 9) ?? "",
  197. device_id: cursor.string(forColumnIndex: 8) ?? "")
  198. cursor.close()
  199. }
  200. } else {
  201. Database.shared.database?.inTransaction({ fmdb, rollback in
  202. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where LOWER(first_name) = '\(firstName.lowercased())' AND LOWER(last_name) = '\(lastName.lowercased())'"), cursor.next() {
  203. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  204. firstName: cursor.string(forColumnIndex: 1) ?? "",
  205. lastName: cursor.string(forColumnIndex: 2) ?? "",
  206. thumb: cursor.string(forColumnIndex: 3) ?? "",
  207. userType: cursor.string(forColumnIndex: 4) ?? "",
  208. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  209. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  210. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  211. official: cursor.string(forColumnIndex: 9) ?? "",
  212. device_id: cursor.string(forColumnIndex: 8) ?? "")
  213. cursor.close()
  214. }
  215. })
  216. }
  217. return user
  218. }
  219. }