// // IHHistoryCityModel.swift // Inhealth // // Created by weclouds on 2020/3/23. // Copyright © 2020 weclouds. All rights reserved. // import UIKit //struct IHHistoryCityModel { // var id:String? // var name:String? //} /* 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) */ struct IHHistoryHotelModel { var id:String? //酒店id var name:String? //酒店名称 var countryId:String? var countryName:String? var provinceId:String? var provinceName:String? var cityId:String? var cityName:String? var buildId:String? var buildName:String? } class IHHistroyPlist: NSObject { var filePath = "" // 文件路径 static let share = IHHistroyPlist() private override init() { super.init() createPlistFile() } //判断文件是否存在改plist ,不存在创建 func createPlistFile() { //获取路径 let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] filePath = path + "/historyhotel.plist" //###注意加横杠“/”,否则真机测试崩溃,无法写入#### if FileManager.default.fileExists(atPath: filePath) == false { let rootArr = NSMutableArray() rootArr.write(toFile: filePath, atomically: true) print("plist创建成功 - \(filePath)") }else{ print("文件已存在 - \(filePath)") } } //保存数据 func addHotel(_ hotel:IHHistoryHotelModel) { let rootArr = NSMutableArray(contentsOfFile: filePath)//根 let newdict = ["id":hotel.id, "name":hotel.name, "countryId":hotel.countryId, "countryName":hotel.countryName, "provinceId,":hotel.provinceId, "provinceName":hotel.provinceName, "cityId":hotel.cityId, "cityName":hotel.cityName, "buildId":hotel.buildId, "buildName":hotel.buildName] if rootArr?.count == 0 { //如果数组为空,则直接添加 rootArr?.add(newdict) }else{ //判断是否已经存在 if rootArr!.count < 6 { //包含了这个个数据 if rootArr?.contains(newdict) == true { let index = rootArr?.index(of: newdict) log.debug("当前的下标 - \(index)") rootArr?.removeObject(at: index!) rootArr?.insert(newdict, at: 0) }else{ rootArr?.insert(newdict, at: 0)//如果不为空 ,则添加到第0个 } }else if rootArr?.count == 6{//最多存放六个 if rootArr?.contains(newdict) == true { let index = rootArr?.index(of: newdict) log.debug("当前的下标 - \(index)") rootArr?.removeObject(at: index!) rootArr?.insert(newdict, at: 0) }else{ rootArr?.removeLastObject()//删除最后一个 // 在添加 rootArr?.insert(newdict, at: 0) } } } rootArr?.write(toFile: filePath, atomically: true) // print("保存成功 -- \(filePath) - 保存内容 \(NSMutableArray(contentsOfFile: filePath)) ") } //获取数据 func getAllHistoryHotelData()->[IHHistoryHotelModel] { var dataArr = [IHHistoryHotelModel]() let rootArr = NSMutableArray(contentsOfFile: filePath)! if rootArr.count > 0 { for item in rootArr { let hoteldict = item as! [String:String] // let hotel = IHHistoryHotelModel(id: hoteldict["id"], name: hoteldict["name"]) 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"]) dataArr.append(hotel) } for city in dataArr { log.debug("城市名称 \(city.name), 城市id- \(city.id)") } } else { log.debug("未查到无数据") } return dataArr } //通过hotel 获取整个model func getHotelMessage(_ hotelID :String,hotelName:String) -> IHHistoryHotelModel? { let dataArr = getAllHistoryHotelData() var hotels = [IHHistoryHotelModel]() hotels = dataArr.filter { (model) -> Bool in return model.id == hotelID && model.name == hotelName } return hotels.first } }