SetMealDetailVC.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //
  2. // SetMealDetailVC.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/26.
  6. //
  7. import UIKit
  8. //import MJRefresh
  9. import RxSwift
  10. import RxCocoa
  11. import RxAlamofire
  12. import Alamofire
  13. import HandyJSON
  14. class SetMealDetailVC: ADHBaseVC {
  15. /// 菜品/套餐id
  16. var mealId : NSNumber?{
  17. didSet{
  18. self.viewModel.getSMDetailRequestModel.mealId = mealId
  19. }
  20. }
  21. /// /// 菜品类型,1为单品,2为套餐
  22. var mealType : String?{
  23. didSet{
  24. self.viewModel.getSMDetailRequestModel.mealType = mealType
  25. }
  26. }
  27. /// 供餐类型(早餐、中餐、晚餐)
  28. var supplyTypeId : String?{
  29. didSet{
  30. self.viewModel.getSMDetailRequestModel.supplyTypeId = supplyTypeId
  31. self.viewModel.addShoppingRequestModel.supplyTypeId = supplyTypeId
  32. }
  33. }
  34. /// 菜品分类
  35. var runTypeId : String?{
  36. didSet{
  37. self.viewModel.getSMDetailRequestModel.runTypeId = runTypeId
  38. self.viewModel.addShoppingRequestModel.runTypeId = runTypeId
  39. }
  40. }
  41. /// 时间
  42. var shopcartTime : String?{
  43. didSet{
  44. self.viewModel.addShoppingRequestModel.shopcartTime = shopcartTime
  45. }
  46. }
  47. /// 时间id
  48. var shopcartTimeId : NSNumber?{
  49. didSet{
  50. self.viewModel.addShoppingRequestModel.shopcartTimeId = shopcartTimeId
  51. self.viewModel.getTimeRequestModel.id = shopcartTimeId
  52. }
  53. }
  54. /// 购物车id
  55. var shopcartId : NSNumber?{
  56. didSet{
  57. self.viewModel.addShoppingRequestModel.shopcartId = shopcartId
  58. }
  59. }
  60. @IBOutlet weak var addShopBtn: UIButton!
  61. @IBOutlet weak var priceLab: UILabel!
  62. @IBOutlet weak var attentionLab: UILabel!
  63. /// 展示更多
  64. var showMore = false
  65. @IBOutlet weak var bottonView: UIView!
  66. //HeadView
  67. let mealHeadView = Bundle.main.loadNibNamed("MealHeadView", owner: nil, options: nil)?.last as? MealHeadView
  68. //固定套餐列表
  69. let mealCollectionView = Bundle.main.loadNibNamed("MealCollectionView", owner: nil, options: nil)?.last as? MealCollectionView
  70. //非固定套餐列表
  71. let changeMealCollectionView = Bundle.main.loadNibNamed("ChangeMealCollectionView", owner: nil, options: nil)?.last as? ChangeMealCollectionView
  72. //配送时间选择
  73. let deliveryTimeSetView = Bundle.main.loadNibNamed("DeliveryTimeSetView", owner: nil, options: nil)?.last as? DeliveryTimeSetView
  74. //需知
  75. let needKonwView = Bundle.main.loadNibNamed("NeedKonwView", owner: nil, options: nil)?.last as? NeedKonwView
  76. //替换菜品
  77. let replaceFoodView = Bundle.main.loadNibNamed("ReplaceFoodView", owner: nil, options: nil)?.last as? ReplaceFoodView
  78. let viewModel : SetMealViewModel = SetMealViewModel.init()
  79. let diposableBag = DisposeBag()
  80. /// 当前更换的Model
  81. var willReplaceModel : MealFoodMsgModel?
  82. //子视图列表
  83. var viewList : [Any]?
  84. override func viewWillAppear(_ animated: Bool) {
  85. super.viewWillAppear(animated)
  86. }
  87. override func viewDidLoad() {
  88. super.viewDidLoad()
  89. self.view.backgroundColor = UIColor.white
  90. // Do any additional setup after loading the view.
  91. self.navBar = MCNavBarView.init(title:"", subTitle: nil,style: nil)
  92. self.navBar?.insertNavRightBtn(nil, image: "分享图标", tap: {[unowned self] in
  93. }, semantic: nil, at: 0)
  94. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  95. self.addShopBtn.isUserInteractionEnabled = false
  96. //MARK: 获取详情
  97. self.hud.showLoading()
  98. self.viewModel.signalGetSetMealDetail()?.on(success: { dd in
  99. self.mealHeadView?.configModel(model: self.viewModel.SMDetailModel!)
  100. self.mealCollectionView!.configModel(model: self.viewModel.SMDetailModel!)
  101. self.changeMealCollectionView!.configModel(model: self.viewModel.SMDetailModel!)
  102. self.viewModel.addShoppingRequestModel.activityId = self.viewModel.SMDetailModel?.activityId
  103. self.viewModel.addShoppingRequestModel.discountId = self.viewModel.SMDetailModel?.discountId
  104. self.viewModel.addShoppingRequestModel.isGeneral = self.viewModel.SMDetailModel?.isGeneral
  105. self.viewModel.addShoppingRequestModel.mealType = self.viewModel.SMDetailModel?.mealType
  106. self.viewModel.addShoppingRequestModel.isFixed = self.viewModel.SMDetailModel?.isFixed
  107. self.viewModel.addShoppingRequestModel.mealId = self.viewModel.SMDetailModel?.mealId
  108. // self.viewModel.addShoppingRequestModel.runTypeId = self.viewModel.SMDetailModel?.runTypeId
  109. // self.viewModel.addShoppingRequestModel.supplyTypeId = self.viewModel.SMDetailModel?.supplyTypeId
  110. self.viewModel.addShoppingRequestModel.mealType = self.viewModel.SMDetailModel?.mealType
  111. self.setPriceShow()
  112. if self.chenkSaveData(){
  113. }
  114. self.hud.hidden()
  115. self.getDeliveryTime()
  116. }, error: { error in
  117. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  118. })
  119. }
  120. func getDeliveryTime(){
  121. //MARK: 获取配送时段
  122. self.viewModel.signalGetDeliveryTime()?.on(success: { dd in
  123. self.deliveryTimeSetView?.configModel(dataSource: self.viewModel.deliveryTimeList!)
  124. self.createUI()
  125. }, error: { error in
  126. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  127. })
  128. }
  129. //MARK: createUI
  130. func createUI(){
  131. self.view.addSubview(self.scrollView)
  132. self.scrollView.backgroundColor = UIColor.clear
  133. self.scrollView.mas_remakeConstraints { make in
  134. make?.left.right().mas_equalTo()(self.view)
  135. make?.top.mas_equalTo()(self.view)?.offset()(kNavAndStatuHeight)
  136. make?.bottom.mas_equalTo()(self.bottonView.mas_top)
  137. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight - 76 - kNavAndStatuHeight)
  138. make?.width.offset()(kSCREEN_WIDTH)
  139. }
  140. if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{
  141. self.viewList = [self.mealHeadView!,self.changeMealCollectionView!,self.deliveryTimeSetView!]
  142. }else{
  143. self.viewList = [self.mealHeadView!,self.mealCollectionView!,self.deliveryTimeSetView!]
  144. }
  145. self.scrollView.addSubview(self.mealHeadView!)
  146. if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{
  147. self.scrollView.addSubview(self.changeMealCollectionView!)
  148. }else{
  149. self.scrollView.addSubview(self.mealCollectionView!)
  150. }
  151. self.scrollView.addSubview(self.deliveryTimeSetView!)
  152. self.resetConstraint()
  153. //查看评论
  154. self.mealHeadView?.evaluateActionBlock = {[weak self] in
  155. let vc = MealEvaluateListVC()
  156. vc.mealId = self?.mealId
  157. self?.navigationController?.pushViewController(vc, animated: true)
  158. }
  159. self.mealCollectionView?.showMoreActionBlock = {[weak self] isShow in
  160. self?.showMore = isShow
  161. self?.resetConstraint()
  162. }
  163. self.mealCollectionView?.needKnowBlock = {[weak self] in
  164. self?.needKonwView?.showWithString(detail: self?.viewModel.SMDetailModel?.introduce)
  165. }
  166. self.mealCollectionView?.changeFoodBlock = {[weak self] in
  167. self?.setPriceShow()
  168. if (self?.chenkSaveData() ?? false){
  169. }
  170. }
  171. //MARK: 获取配送时段
  172. self.changeMealCollectionView?.changeFoodBlock = {[weak self] model , row in
  173. self?.viewModel.getReplaceFoodRequestModel.poolId = model.replacePoolId
  174. self?.viewModel.signalGetReplaceFood()?.on(success: { dd in
  175. self?.replaceFoodView?.showWithModel(models: self?.viewModel.replaceFoodModels, row: row, foodName: model.name ?? "")
  176. }, error: { error in
  177. self?.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  178. })
  179. }
  180. self.deliveryTimeSetView?.selectTimeBlock = {[weak self] model in
  181. self?.viewModel.addShoppingRequestModel.deliveryStart = model.startTime
  182. self?.viewModel.addShoppingRequestModel.deliveryEnd = model.endTime
  183. self?.viewModel.addShoppingRequestModel.deliveryId = model.id
  184. if (self?.chenkSaveData() ?? false){
  185. }
  186. }
  187. self.deliveryTimeSetView?.inputNumBlock = {[weak self] numStr in
  188. self?.viewModel.addShoppingRequestModel.numPeople = numStr
  189. self?.setPriceShow()
  190. if (self?.chenkSaveData() ?? false){
  191. }
  192. }
  193. //替换菜品的替换ID
  194. self.replaceFoodView?.selectBlock = {[weak self] model , row in
  195. let oldModel : MealFoodMsgModel = (self?.viewModel.SMDetailModel?.mealFoodMsgs?[row])!
  196. oldModel.link = model.link
  197. oldModel.name = model.name
  198. oldModel.replaceId = model.id
  199. self?.changeMealCollectionView?.collectView.reloadData()
  200. }
  201. }
  202. func setPriceShow(){
  203. if (self.viewModel.SMDetailModel?.isFixed ?? "0") === 1{
  204. self.priceLab.text = self.viewModel.SMDetailModel?.price ** self.viewModel.addShoppingRequestModel.numPeople
  205. }else{
  206. var foods : [NSNumber]? = []
  207. for item in self.viewModel.SMDetailModel!.mealFoodMsgs! {
  208. let model : MealFoodMsgModel = item
  209. if model.selected{
  210. foods?.append(model.id!)
  211. }
  212. }
  213. if let _ = self.viewModel.SMDetailModel?.mealPrices{
  214. for item in self.viewModel.SMDetailModel!.mealPrices!{
  215. let model : MealPricesModel = item
  216. if model.quantity === (foods?.count ?? 0){
  217. self.priceLab.text = model.amount ** self.viewModel.addShoppingRequestModel.numPeople
  218. break
  219. }else{
  220. self.priceLab.text = ""
  221. }
  222. }
  223. }
  224. }
  225. }
  226. func chenkSaveData() ->(Bool){
  227. if self.viewModel.addShoppingRequestModel.numPeople.isEmptyStr{
  228. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请先设置用餐人数", Etext: "Please set the number of diners first")
  229. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  230. self.addShopBtn.isUserInteractionEnabled = false
  231. return false
  232. }
  233. if self.viewModel.addShoppingRequestModel.numPeople < Int(self.viewModel.SMDetailModel?.lowestNum ?? "1"){
  234. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self.viewModel.SMDetailModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self.viewModel.SMDetailModel?.lowestNum ?? "1")")
  235. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  236. self.addShopBtn.isUserInteractionEnabled = false
  237. return false
  238. }
  239. if self.viewModel.addShoppingRequestModel.deliveryId.isNil{
  240. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请选择配送时间", Etext: "Please select a delivery time")
  241. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  242. self.addShopBtn.isUserInteractionEnabled = false
  243. return false
  244. }
  245. if ((self.viewModel.SMDetailModel?.isFixed ?? "") == "0"){
  246. //非固定套餐
  247. var fixedFoods : [NSNumber]? = []
  248. for item in self.viewModel.SMDetailModel!.mealFoodMsgs! {
  249. let model : MealFoodMsgModel = item
  250. if model.selected{
  251. fixedFoods?.append(model.id!)
  252. }
  253. }
  254. var isPeopleNUmOK = false
  255. if let _ = self.viewModel.SMDetailModel?.mealPrices{
  256. for item in self.viewModel.SMDetailModel!.mealPrices! {
  257. let priceModel : MealPricesModel = item
  258. if priceModel.quantity === fixedFoods?.count{
  259. isPeopleNUmOK = true
  260. }
  261. }
  262. }else{
  263. isPeopleNUmOK = false
  264. }
  265. if isPeopleNUmOK{
  266. self.addShopBtn.setBackColor(kThemeColor, forState: .normal)
  267. self.addShopBtn.isUserInteractionEnabled = true
  268. }else{
  269. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  270. self.addShopBtn.isUserInteractionEnabled = false
  271. }
  272. return isPeopleNUmOK
  273. }
  274. self.addShopBtn.setBackColor(kThemeColor, forState: .normal)
  275. self.addShopBtn.isUserInteractionEnabled = true
  276. return true
  277. }
  278. //MARK: 重设约束
  279. func resetConstraint(){
  280. var contentSizeH : CGFloat = 0
  281. for i in 0..<viewList!.count{
  282. let itme : UIView = viewList![i] as! UIView
  283. itme.mas_remakeConstraints { make in
  284. make?.left.right().mas_equalTo()(self.scrollView)
  285. if i == 0{
  286. make?.top.mas_equalTo()(self.scrollView)
  287. make?.width.offset()(kSCREEN_WIDTH)
  288. }
  289. if i == 1{
  290. make?.top.mas_equalTo()(self.mealHeadView?.mas_bottom)?.offset()(-34)
  291. make?.width.offset()(kSCREEN_WIDTH)
  292. let lin1 = (self.viewModel.SMDetailModel?.mealFoodMsgs?.count ?? 0)/3
  293. var lin2 = (self.viewModel.SMDetailModel?.mealFoodMsgs?.count ?? 0)%3
  294. if lin2 > 0 {
  295. lin2 = 1
  296. }
  297. if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{
  298. make?.height.offset()(56 + 170 * CGFloat(lin1 + lin2) + 25)
  299. }else{
  300. //self.viewModel.deliveryTimeList?.count date数量 2两列 取整数取余数
  301. if (self.viewModel.SMDetailModel?.mealFoodMsgs?.count ?? 0) > 9{
  302. if showMore{
  303. make?.height.offset()(93 + 29 + 138 * CGFloat(lin1 + lin2) - 12)
  304. }else{
  305. make?.height.offset()(93 + 29 + 138 * 3 - 12)
  306. }
  307. }else{
  308. make?.height.offset()(93 + 29 + 138 * CGFloat(lin1 + lin2) - 12)
  309. }
  310. }
  311. }
  312. if i == 2{
  313. if (self.viewModel.SMDetailModel?.isFixed ?? "0") == "1"{
  314. make?.top.mas_equalTo()(self.changeMealCollectionView?.mas_bottom)
  315. }else{
  316. make?.top.mas_equalTo()(self.mealCollectionView?.mas_bottom)
  317. }
  318. make?.width.offset()(kSCREEN_WIDTH)
  319. let lin1 = (self.viewModel.deliveryTimeList?.count ?? 0)/3
  320. var lin2 = (self.viewModel.deliveryTimeList?.count ?? 0)%3
  321. if lin2 > 0 {
  322. lin2 = 1
  323. }
  324. make?.height.offset()(154 + 44 * CGFloat(lin1 + lin2) - 12)
  325. make?.bottom.mas_equalTo()(self.scrollView)
  326. }
  327. }
  328. contentSizeH = contentSizeH + itme.frame.size.height
  329. }
  330. scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH)
  331. }
  332. //MARK: scrollView
  333. private var scrollView : UIScrollView = {
  334. let scrollView = UIScrollView.init(frame: CGRect.zero)
  335. scrollView.alwaysBounceVertical = true
  336. scrollView.isScrollEnabled = true
  337. scrollView.showsVerticalScrollIndicator = false
  338. scrollView.backgroundColor = kTBackgroundColor
  339. scrollView.contentInsetAdjustmentBehavior = .never
  340. return scrollView
  341. }()
  342. func getReplaceFoods(){
  343. // //MARK: 获取配送时段
  344. // self.viewModel.getReplaceFoodRequestModel.poolId = self.willReplaceModel?.replacePoolId
  345. // self.viewModel.signalGetReplaceFood()?.on(success: { dd in
  346. // self.replaceFoodView?.showWithModel(models: self.viewModel.replaceFoodModels!)
  347. // }, error: { error in
  348. // self.hud.showFailure("网络错误!")
  349. // })
  350. }
  351. @IBAction func addShoppingAction(_ sender: UIButton) {
  352. if self.viewModel.addShoppingRequestModel.deliveryStart.isEmptyStr{
  353. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time"))
  354. return
  355. }
  356. if self.viewModel.addShoppingRequestModel.deliveryEnd.isEmptyStr{
  357. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time"))
  358. return
  359. }
  360. if self.viewModel.addShoppingRequestModel.numPeople.isEmptyStr{
  361. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请输入就餐人数", Etext: "Please enter the number of diners"))
  362. return
  363. }
  364. if self.viewModel.addShoppingRequestModel.numPeople < Int(self.viewModel.SMDetailModel?.lowestNum ?? "1"){
  365. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "就餐人数不能低于\(self.viewModel.SMDetailModel?.lowestNum ?? "1")人", Etext: "The number of diners should not be lower \(self.viewModel.SMDetailModel?.lowestNum ?? "1")"))
  366. return
  367. }
  368. if ((self.viewModel.SMDetailModel?.isFixed ?? "") == "1"){
  369. //固定套餐
  370. self.viewModel.addShoppingRequestModel.mealFoodMsgs = self.viewModel.SMDetailModel?.mealFoodMsgs
  371. }else{
  372. //非固定套餐
  373. var fixedFoods : [NSNumber]? = []
  374. for item in self.viewModel.SMDetailModel!.mealFoodMsgs! {
  375. let model : MealFoodMsgModel = item
  376. if model.selected{
  377. fixedFoods?.append(model.id!)
  378. }
  379. }
  380. self.viewModel.addShoppingRequestModel.fixedFoods = fixedFoods
  381. var isPeopleNUmOK = false
  382. if let _ = self.viewModel.SMDetailModel?.mealPrices{
  383. for item in self.viewModel.SMDetailModel!.mealPrices! {
  384. let priceModel : MealPricesModel = item
  385. if priceModel.quantity === fixedFoods?.count{
  386. isPeopleNUmOK = true
  387. }
  388. }
  389. }else{
  390. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "固定套餐规则为空", Etext: "固定套餐规则为空"))
  391. return
  392. }
  393. if isPeopleNUmOK{
  394. }else{
  395. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "固定套餐规则为空", Etext: "固定套餐规则为空"))
  396. return
  397. }
  398. }
  399. // self.viewModel.addShoppingRequestModel.mealFoodMsgs =
  400. //MARK: 加入购物车
  401. self.viewModel.signalAddShopping()?.on(success: { dd in
  402. self.hud.showSuccess()
  403. NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil)
  404. self.navigationController?.popViewController(animated: true)
  405. }, error: { error in
  406. self.hud.showFailure("\(error.msg ?? "")")
  407. })
  408. }
  409. }