ShoppingCarDetailView.swift 14 KB

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