IHHistoryCityModel.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // IHHistoryCityModel.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/3/23.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. //struct IHHistoryCityModel {
  10. // var id:String?
  11. // var name:String?
  12. //}
  13. /*
  14. Intermediate.countryId, countryName: Intermediate.countryName, provinceId: Intermediate.provinceId, provinceName: Intermediate.provinceName, cityId: Intermediate.cityId, cityName: Intermediate.cityName, hotelId: Intermediate.hotelId, hotelName: Intermediate.hotelName, buildId: Intermediate.buildId, buildName: Intermediate.buildName)
  15. */
  16. struct IHHistoryHotelModel {
  17. var id:String? //酒店id
  18. var name:String? //酒店名称
  19. var countryId:String?
  20. var countryName:String?
  21. var provinceId:String?
  22. var provinceName:String?
  23. var cityId:String?
  24. var cityName:String?
  25. var buildId:String?
  26. var buildName:String?
  27. }
  28. class IHHistroyPlist: NSObject {
  29. var filePath = "" // 文件路径
  30. static let share = IHHistroyPlist()
  31. private override init() {
  32. super.init()
  33. createPlistFile()
  34. }
  35. //判断文件是否存在改plist ,不存在创建
  36. func createPlistFile() {
  37. //获取路径
  38. let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
  39. filePath = path + "/historyhotel.plist"
  40. //###注意加横杠“/”,否则真机测试崩溃,无法写入####
  41. if FileManager.default.fileExists(atPath: filePath) == false {
  42. let rootArr = NSMutableArray()
  43. rootArr.write(toFile: filePath, atomically: true)
  44. print("plist创建成功 - \(filePath)")
  45. }else{
  46. print("文件已存在 - \(filePath)")
  47. }
  48. }
  49. //保存数据
  50. func addHotel(_ hotel:IHHistoryHotelModel) {
  51. let rootArr = NSMutableArray(contentsOfFile: filePath)//根
  52. let newdict = ["id":hotel.id,
  53. "name":hotel.name,
  54. "countryId":hotel.countryId,
  55. "countryName":hotel.countryName,
  56. "provinceId,":hotel.provinceId,
  57. "provinceName":hotel.provinceName,
  58. "cityId":hotel.cityId,
  59. "cityName":hotel.cityName,
  60. "buildId":hotel.buildId,
  61. "buildName":hotel.buildName]
  62. if rootArr?.count == 0 { //如果数组为空,则直接添加
  63. rootArr?.add(newdict)
  64. }else{
  65. //判断是否已经存在
  66. if rootArr!.count < 6 {
  67. //包含了这个个数据
  68. if rootArr?.contains(newdict) == true {
  69. let index = rootArr?.index(of: newdict)
  70. log.debug("当前的下标 - \(index)")
  71. rootArr?.removeObject(at: index!)
  72. rootArr?.insert(newdict, at: 0)
  73. }else{
  74. rootArr?.insert(newdict, at: 0)//如果不为空 ,则添加到第0个
  75. }
  76. }else if rootArr?.count == 6{//最多存放六个
  77. if rootArr?.contains(newdict) == true {
  78. let index = rootArr?.index(of: newdict)
  79. log.debug("当前的下标 - \(index)")
  80. rootArr?.removeObject(at: index!)
  81. rootArr?.insert(newdict, at: 0)
  82. }else{
  83. rootArr?.removeLastObject()//删除最后一个
  84. // 在添加
  85. rootArr?.insert(newdict, at: 0)
  86. }
  87. }
  88. }
  89. rootArr?.write(toFile: filePath, atomically: true)
  90. // print("保存成功 -- \(filePath) - 保存内容 \(NSMutableArray(contentsOfFile: filePath)) ")
  91. }
  92. //获取数据
  93. func getAllHistoryHotelData()->[IHHistoryHotelModel] {
  94. var dataArr = [IHHistoryHotelModel]()
  95. let rootArr = NSMutableArray(contentsOfFile: filePath)!
  96. if rootArr.count > 0 {
  97. for item in rootArr {
  98. let hoteldict = item as! [String:String]
  99. // let hotel = IHHistoryHotelModel(id: hoteldict["id"], name: hoteldict["name"])
  100. let hotel = IHHistoryHotelModel(id: hoteldict["id"], name: hoteldict["name"], countryId: hoteldict["countryId"], countryName: hoteldict["countryName"], provinceId: hoteldict["provinceId"], provinceName: hoteldict["provinceName"], cityId: hoteldict["cityId"], cityName: hoteldict["cityName"], buildId: hoteldict["buildId"], buildName: hoteldict["buildName"])
  101. dataArr.append(hotel)
  102. }
  103. for city in dataArr {
  104. log.debug("城市名称 \(city.name), 城市id- \(city.id)")
  105. }
  106. } else {
  107. log.debug("未查到无数据")
  108. }
  109. return dataArr
  110. }
  111. //通过hotel 获取整个model
  112. func getHotelMessage(_ hotelID :String,hotelName:String) -> IHHistoryHotelModel? {
  113. let dataArr = getAllHistoryHotelData()
  114. var hotels = [IHHistoryHotelModel]()
  115. hotels = dataArr.filter { (model) -> Bool in
  116. return model.id == hotelID && model.name == hotelName
  117. }
  118. return hotels.first
  119. }
  120. }