ShoppingCarDetailView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // ShoppingCarDetailView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/11/1.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. class ShoppingCarDetailView: UIView {
  11. /// 购物车类型
  12. var shopType : String?{
  13. didSet{
  14. self.viewModel.HomeTimeGetModel.shopType = shopType
  15. }
  16. }
  17. /// 购物车ID
  18. var shopcartId : NSNumber?{
  19. didSet{
  20. self.viewModel.getShoppingCarMsgResultMeal.shopcartId = shopcartId
  21. self.viewModel.editShoppingCarRequestModel.shopcartId = shopcartId
  22. self.viewModel.cleanShopcarModel.id = shopcartId
  23. }
  24. }
  25. /// 购物车时间ID
  26. var shopcartTimeId : NSNumber?{
  27. didSet{
  28. self.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId = shopcartTimeId
  29. }
  30. }
  31. @IBOutlet weak var blackView: UIView!
  32. let dateView = Bundle.main.loadNibNamed("HomeDateView", owner: nil, options: nil)?.last as? HomeDateView
  33. let editorDeliveryTimeView = Bundle.main.loadNibNamed("EditorDeliveryView", owner: nil, options: nil)?.last as? EditorDeliveryView
  34. let editPersonNumView = Bundle.main.loadNibNamed("EditPersonNumView", owner: nil, options: nil)?.last as? EditPersonNumView
  35. let adhAV = Bundle.main.loadNibNamed("ADHAlertView", owner: nil, options: nil)?.last as? ADHAlertView
  36. @IBOutlet weak var priceLab: UILabel!
  37. @IBOutlet weak var tableView: UITableView!
  38. var editModel : ShopMealMsgDetailModel?
  39. /// 当前选择的时间
  40. var nowTimeModel : HomeTimeItem?
  41. let viewModel : ShoppingCarViewModel = ShoppingCarViewModel.init()
  42. let diposableBag = DisposeBag()
  43. var isRefresh = true
  44. lazy var hud : MCHud! = {
  45. return MCHud()
  46. }()
  47. var reFreshBlock : (()->Void)?
  48. var checkOrderBlock : (()->Void)?
  49. override func awakeFromNib() {
  50. super.awakeFromNib()
  51. tableView.register(withType: MenuTableViewCell.self)
  52. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  53. self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in
  54. self.isRefresh = true
  55. self.getShopCarDetail()
  56. });
  57. self.tableView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: "暂无数据", detailStr: "", btnTitleStr: "点击刷新", btnClick: {[weak self] in
  58. self?.isRefresh = true
  59. self?.getShopCarDetail()
  60. })
  61. self.dateView?.deletBtn.isHidden = false
  62. self.dateView?.timeBtn.isHidden = true
  63. self.dateView?.titleLab.text = LanguagesUtil.createTextBy(Ctext: "已选菜品", Etext: "selected foods")
  64. self.addSubview(self.dateView!)
  65. self.dateView!.mas_remakeConstraints { make in
  66. make?.left.right().mas_equalTo()(self)
  67. make?.top.mas_equalTo()(self)?.offset()(200)
  68. make?.height.offset()(180)
  69. }
  70. self.dateView?.selectBlock = {[weak self] model in
  71. self?.nowTimeModel = model
  72. self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId = self?.nowTimeModel!.shopcartTimeId
  73. self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId = model.shopcartTimeId
  74. self?.getShopCarDetail()
  75. self?.reloadDateView()
  76. }
  77. self.dateView?.deletActionBlock = {[weak self] in
  78. self?.adhAV?.show()
  79. }
  80. self.adhAV?.sureActionBlock = {[weak self] in
  81. //清空购物车
  82. self?.viewModel.signalCleanShopCar()?.on(success: { dd in
  83. NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil)
  84. self?.hidden()
  85. }, error: { error in
  86. self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  87. })
  88. }
  89. self.editorDeliveryTimeView?.selectTimeBlock = {[weak self] model in
  90. self?.viewModel.editShoppingCarRequestModel.shopMealId = self?.editModel?.shopMealId
  91. self?.viewModel.editShoppingCarRequestModel.mealType = self?.editModel?.mealType
  92. self?.viewModel.editShoppingCarRequestModel.shopcartTimeId = self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId
  93. self?.viewModel.editShoppingCarRequestModel.deliveryId = model.id
  94. self?.viewModel.signalEditShopCar()?.on(success: { dd in
  95. self?.getShopCarDetail()
  96. self?.getShopCarPrice()
  97. }, error: { error in
  98. self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  99. })
  100. }
  101. self.editPersonNumView?.inputBlock = {[weak self] num in
  102. if num < Int(self?.editModel?.lowestNum ?? "1"){
  103. self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self?.editModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self?.editModel?.lowestNum ?? "1")"))
  104. return
  105. }
  106. self?.viewModel.editShoppingCarRequestModel.shopMealId = self?.editModel?.shopMealId
  107. self?.viewModel.editShoppingCarRequestModel.mealType = self?.editModel?.mealType
  108. self?.viewModel.editShoppingCarRequestModel.shopcartTimeId = self?.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId
  109. self?.viewModel.editShoppingCarRequestModel.numPeople = num
  110. self?.viewModel.signalEditShopCar()?.on(success: { dd in
  111. self?.getShopCarDetail()
  112. self?.getShopCarPrice()
  113. }, error: { error in
  114. self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  115. })
  116. }
  117. }
  118. //MARK: 获取时间范围
  119. func getTimeRangDetail(){
  120. self.hud.showLoading()
  121. self.viewModel.signalGetHomeTime()?.on(success: { dd in
  122. for item in self.viewModel.TimeRangDetail!.timeVoList! {
  123. let model : HomeTimeItem = item
  124. if model.day == self.nowTimeModel?.day{
  125. model.isSelect = true
  126. }
  127. }
  128. self.getShopCarPrice()
  129. self.reloadDateView()
  130. self.hud.hidden()
  131. }, error: { error in
  132. self.hud.showFailure("获取时间范围失败")
  133. })
  134. }
  135. //MARK: 获取日期下购物车详情
  136. func getShopCarDetail(){
  137. if isRefresh{
  138. self.hud.showLoading()
  139. NSLog("刷新")
  140. }else{
  141. NSLog("计算")
  142. }
  143. self.viewModel.signalgetShoppingCarMsg()?.on(success: { dd in
  144. self.tableView.reloadData()
  145. self.hud.hidden()
  146. self.tableView.mj_header.endRefreshing()
  147. }, error: { error in
  148. self.hud.showFailure("获取失败")
  149. self.tableView.mj_header.endRefreshing()
  150. })
  151. }
  152. //MARK: 获取购物车价格详情
  153. func getShopCarPrice(){
  154. self.viewModel.getShopCarPriceRModel.id = self.viewModel.TimeRangDetail?.shopcartId
  155. self.viewModel.signalGetShopCarPrice()?.on(success: { dd in
  156. self.priceLab.text = "\(self.viewModel.shopCarPriceDetail?.price ?? "")"
  157. }, error: { error in
  158. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  159. })
  160. }
  161. //MARK: 获取配送时段
  162. func getDeliveryTime(){
  163. self.viewModel.signalGetDeliveryTime()?.on(success: { dd in
  164. }, error: { error in
  165. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  166. })
  167. }
  168. func reloadDateView(){
  169. self.dateView!.configModel(dateModel: self.viewModel.TimeRangDetail!)
  170. }
  171. @IBAction func gotoJIesuan(_ sender: UIButton) {
  172. if self.checkOrderBlock != nil{
  173. self.checkOrderBlock!()
  174. }
  175. self.hidden()
  176. }
  177. public func show(){
  178. self.getTimeRangDetail()
  179. self.getShopCarDetail()
  180. self.getDeliveryTime()
  181. kAppDelegateWindow.addSubview(self)
  182. self.blackView.alpha = 0
  183. self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
  184. UIView.animate(withDuration: 0.5) {
  185. self.frame = CGRect.init(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
  186. self.layoutIfNeeded()
  187. }
  188. DELAY(0.5) {
  189. self.blackView.alpha = 0.3
  190. }
  191. }
  192. public func hidden(){
  193. self.blackView.alpha = 0
  194. UIView.animate(withDuration: 0.5) {
  195. self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
  196. self.layoutIfNeeded()
  197. }
  198. DELAY(0.5) {
  199. self.removeFromSuperview()
  200. }
  201. }
  202. @IBAction func backTouchAction(_ sender: UIButton) {
  203. if self.reFreshBlock != nil{
  204. self.reFreshBlock!()
  205. }
  206. self.hidden()
  207. }
  208. func add(){
  209. if (self.editModel?.numPeople ?? "0") < Int(self.editModel?.lowestNum ?? "1"){
  210. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self.editModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self.editModel?.lowestNum ?? "1")") ,delay: 1.5)
  211. self.getShopCarDetail()
  212. self.getShopCarPrice()
  213. }else{
  214. self.viewModel.editShoppingCarRequestModel.shopMealId = self.editModel?.shopMealId
  215. self.viewModel.editShoppingCarRequestModel.mealType = self.editModel?.mealType
  216. self.viewModel.editShoppingCarRequestModel.shopcartTimeId = self.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId
  217. self.viewModel.editShoppingCarRequestModel.numPeople = self.editModel?.numPeople
  218. self.viewModel.signalEditShopCar()?.on(success: { dd in
  219. self.getShopCarDetail()
  220. self.getShopCarPrice()
  221. }, error: { error in
  222. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  223. })
  224. }
  225. }
  226. func deleteMeal(){
  227. self.viewModel.editShoppingCarRequestModel.shopMealId = self.editModel?.shopMealId
  228. self.viewModel.editShoppingCarRequestModel.mealType = self.editModel?.mealType
  229. self.viewModel.editShoppingCarRequestModel.shopcartTimeId = self.viewModel.getShoppingCarMsgResultMeal.shopcartTimeId
  230. self.viewModel.editShoppingCarRequestModel.del = "1"
  231. self.viewModel.signalEditShopCar()?.on(success: { dd in
  232. self.getTimeRangDetail()
  233. self.getShopCarDetail()
  234. self.getShopCarPrice()
  235. }, error: { error in
  236. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  237. })
  238. }
  239. }
  240. extension ShoppingCarDetailView : UITableViewDelegate,UITableViewDataSource{
  241. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  242. return self.viewModel.ShopCarAllModelList?[section].shopMealMsgVos?.count ?? 0
  243. }
  244. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  245. let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell;
  246. let batchModel = self.viewModel.ShopCarAllModelList?[indexPath.section]
  247. let model = (batchModel?.shopMealMsgVos?[indexPath.row])!
  248. var isfirst = indexPath.row == 0 ? true : false
  249. cell.configModel(model: model, isfirstLine: isfirst)
  250. //加上左滑事件
  251. cell.addLeftSlideAction()
  252. cell.addOrSubtractionBlock = {[weak self] isAdd in
  253. self?.editModel = model
  254. self?.isRefresh = false
  255. self?.add()
  256. }
  257. cell.selectTimeActionBlock = {[weak self] in
  258. self?.editorDeliveryTimeView?.configModel(dataSource: self?.viewModel.deliveryTimeList)
  259. self?.editModel = model
  260. }
  261. cell.inPutNumBlock = {[weak self] in
  262. self?.editModel = model
  263. self?.editPersonNumView?.showWithNum(model.numPeople!)
  264. }
  265. cell.deleteBlock = {[weak self] in
  266. self?.editModel = model
  267. self?.deleteMeal()
  268. }
  269. return cell
  270. }
  271. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  272. return 0.01
  273. }
  274. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  275. return 0.01
  276. }
  277. //
  278. func numberOfSections(in tableView: UITableView) -> Int {
  279. return self.viewModel.ShopCarAllModelList?.count ?? 0
  280. }
  281. }