SingleMealDetailController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // SingleMealDetailController.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/29.
  6. //
  7. import UIKit
  8. //import MJRefresh
  9. import RxSwift
  10. import RxCocoa
  11. import RxAlamofire
  12. import Alamofire
  13. import HandyJSON
  14. class SingleMealDetailController: ADHBaseVC {
  15. /// 展示更多
  16. var showMore = false
  17. /// 菜品/套餐id
  18. var mealId : NSNumber?{
  19. didSet{
  20. self.viewModel.getSMDetailRequestModel.mealId = mealId
  21. }
  22. }
  23. /// /// 菜品类型,1为单品,2为套餐
  24. var mealType : String?{
  25. didSet{
  26. self.viewModel.getSMDetailRequestModel.mealType = mealType
  27. }
  28. }
  29. /// 供餐类型(早餐、中餐、晚餐)
  30. var supplyTypeId : String?{
  31. didSet{
  32. self.viewModel.getSMDetailRequestModel.supplyTypeId = supplyTypeId
  33. self.viewModel.addShoppingRequestModel.supplyTypeId = supplyTypeId
  34. }
  35. }
  36. /// 菜品分类
  37. var runTypeId : String?{
  38. didSet{
  39. self.viewModel.getSMDetailRequestModel.runTypeId = runTypeId
  40. self.viewModel.addShoppingRequestModel.runTypeId = runTypeId
  41. }
  42. }
  43. /// 时间
  44. var shopcartTime : String?{
  45. didSet{
  46. self.viewModel.addShoppingRequestModel.shopcartTime = shopcartTime
  47. }
  48. }
  49. /// 时间id
  50. var shopcartTimeId : NSNumber?{
  51. didSet{
  52. self.viewModel.addShoppingRequestModel.shopcartTimeId = shopcartTimeId
  53. self.viewModel.getTimeRequestModel.id = shopcartTimeId
  54. }
  55. }
  56. /// 购物车id
  57. var shopcartId : NSNumber?{
  58. didSet{
  59. self.viewModel.addShoppingRequestModel.shopcartId = shopcartId
  60. }
  61. }
  62. @IBOutlet weak var bottonView: UIView!
  63. @IBOutlet weak var priceLab: UILabel!
  64. @IBOutlet weak var attentionLab: UILabel!
  65. @IBOutlet weak var addShopBtn: UIButton!
  66. //HeadView
  67. let mealHeadView = Bundle.main.loadNibNamed("MealHeadView", owner: nil, options: nil)?.last as? MealHeadView
  68. //评价
  69. let mealDescribeView = Bundle.main.loadNibNamed("MealDescribeView", owner: nil, options: nil)?.last as? MealDescribeView
  70. //配送时间选择
  71. let deliveryTimeSetView = Bundle.main.loadNibNamed("DeliveryTimeSetView", owner: nil, options: nil)?.last as? DeliveryTimeSetView
  72. //需知
  73. let needKonwView = Bundle.main.loadNibNamed("NeedKonwView", owner: nil, options: nil)?.last as? NeedKonwView
  74. let viewModel : SetMealViewModel = SetMealViewModel.init()
  75. let diposableBag = DisposeBag()
  76. //子视图列表
  77. var viewList : [Any]?
  78. override func viewWillAppear(_ animated: Bool) {
  79. super.viewWillAppear(animated)
  80. }
  81. override func viewDidLoad() {
  82. super.viewDidLoad()
  83. self.view.backgroundColor = UIColor.white
  84. // Do any additional setup after loading the view.
  85. self.navBar = MCNavBarView.init(title:"", subTitle: nil,style: nil)
  86. self.navBar?.insertNavRightBtn(nil, image: "分享图标", tap: {[unowned self] in
  87. }, semantic: nil, at: 0)
  88. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  89. self.addShopBtn.isUserInteractionEnabled = false
  90. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time")
  91. //MARK: 获取详情
  92. self.hud.showLoading()
  93. self.viewModel.signalGetSetMealDetail()?.on(success: { dd in
  94. self.mealHeadView?.configModel(model: self.viewModel.SMDetailModel!)
  95. self.mealDescribeView!.configDescribLab(text: (self.viewModel.SMDetailModel!.introduce ?? ""))
  96. self.resetConstraint()
  97. self.viewModel.addShoppingRequestModel.activityId = self.viewModel.SMDetailModel?.activityId
  98. self.viewModel.addShoppingRequestModel.discountId = self.viewModel.SMDetailModel?.discountId
  99. self.viewModel.addShoppingRequestModel.isGeneral = self.viewModel.SMDetailModel?.isGeneral
  100. self.viewModel.addShoppingRequestModel.mealType = self.viewModel.SMDetailModel?.mealType
  101. self.viewModel.addShoppingRequestModel.isFixed = self.viewModel.SMDetailModel?.isFixed
  102. self.viewModel.addShoppingRequestModel.mealId = self.viewModel.SMDetailModel?.mealId
  103. // self.viewModel.addShoppingRequestModel.runTypeId = self.viewModel.SMDetailModel?.runTypeId
  104. // self.viewModel.addShoppingRequestModel.supplyTypeId = self.viewModel.SMDetailModel?.supplyTypeId
  105. self.viewModel.addShoppingRequestModel.mealType = self.viewModel.SMDetailModel?.mealType
  106. self.priceLab.text = "\(self.viewModel.SMDetailModel?.price ?? "")"
  107. self.hud.hidden()
  108. }, error: { error in
  109. self.hud.showFailure("\(error.msg ?? "")")
  110. })
  111. //MARK: 获取配送时段
  112. self.viewModel.signalGetDeliveryTime()?.on(success: { dd in
  113. self.deliveryTimeSetView?.configModel(dataSource: self.viewModel.deliveryTimeList!)
  114. self.resetConstraint()
  115. }, error: { error in
  116. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  117. })
  118. self.createUI()
  119. }
  120. //MARK: createUI
  121. func createUI(){
  122. self.view.addSubview(self.scrollView)
  123. self.scrollView.backgroundColor = UIColor.clear
  124. self.scrollView.mas_remakeConstraints { make in
  125. make?.left.right().mas_equalTo()(self.view)
  126. make?.top.mas_equalTo()(self.view)?.offset()(kNavAndStatuHeight)
  127. make?.bottom.mas_equalTo()(self.bottonView.mas_top)
  128. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight - 76 - kNavAndStatuHeight)
  129. make?.width.offset()(kSCREEN_WIDTH)
  130. }
  131. self.viewList = [self.mealHeadView!,self.mealDescribeView!,self.deliveryTimeSetView!]
  132. self.scrollView.addSubview(self.mealHeadView!)
  133. self.scrollView.addSubview(self.mealDescribeView!)
  134. self.scrollView.addSubview(self.deliveryTimeSetView!)
  135. self.resetConstraint()
  136. //查看评论
  137. self.mealHeadView?.evaluateActionBlock = {[weak self] in
  138. let vc = MealEvaluateListVC()
  139. vc.mealId = self?.mealId
  140. self?.navigationController?.pushViewController(vc, animated: true)
  141. }
  142. self.deliveryTimeSetView?.selectTimeBlock = {[weak self] model in
  143. self?.viewModel.addShoppingRequestModel.deliveryStart = model.startTime
  144. self?.viewModel.addShoppingRequestModel.deliveryEnd = model.endTime
  145. self?.viewModel.addShoppingRequestModel.deliveryId = model.id
  146. if (self?.chenkSaveData() ?? false){
  147. }
  148. }
  149. self.deliveryTimeSetView?.inputNumBlock = {[weak self] numStr in
  150. self?.viewModel.addShoppingRequestModel.numPeople = numStr
  151. let price : String = numStr ** "\(self?.viewModel.SMDetailModel?.price ?? "")"
  152. self?.priceLab.text = "\(price ?? "")"
  153. if (self?.chenkSaveData() ?? false){
  154. }
  155. }
  156. }
  157. //MARK: 重设约束
  158. func resetConstraint(){
  159. var contentSizeH : CGFloat = 0
  160. for i in 0..<viewList!.count{
  161. let itme : UIView = viewList![i] as! UIView
  162. itme.mas_remakeConstraints { make in
  163. make?.left.right().mas_equalTo()(self.scrollView)
  164. if i == 0{
  165. make?.top.mas_equalTo()(self.scrollView)
  166. make?.width.offset()(kSCREEN_WIDTH)
  167. }
  168. if i == 1{
  169. make?.top.mas_equalTo()(self.mealHeadView?.mas_bottom)?.offset()(-34)
  170. make?.width.offset()(kSCREEN_WIDTH)
  171. }
  172. if i == 2{
  173. make?.top.mas_equalTo()(self.mealDescribeView?.mas_bottom)
  174. make?.width.offset()(kSCREEN_WIDTH)
  175. //self.viewModel.deliveryTimeList?.count date数量 2两列 取整数取余数
  176. let lin1 = (self.viewModel.deliveryTimeList?.count ?? 0)/3
  177. var lin2 = (self.viewModel.deliveryTimeList?.count ?? 0)%3
  178. if lin2 > 0 {
  179. lin2 = 1
  180. }
  181. make?.height.offset()(154 + 44 * CGFloat(lin1 + lin2) - 12)
  182. make?.bottom.mas_equalTo()(self.scrollView)
  183. }
  184. }
  185. contentSizeH = contentSizeH + itme.frame.size.height
  186. }
  187. scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH)
  188. }
  189. //MARK: scrollView
  190. private var scrollView : UIScrollView = {
  191. let scrollView = UIScrollView.init(frame: CGRect.zero)
  192. scrollView.alwaysBounceVertical = true
  193. scrollView.isScrollEnabled = true
  194. scrollView.showsVerticalScrollIndicator = false
  195. scrollView.backgroundColor = kTBackgroundColor
  196. scrollView.contentInsetAdjustmentBehavior = .never
  197. return scrollView
  198. }()
  199. func chenkSaveData() ->(Bool){
  200. if self.viewModel.addShoppingRequestModel.numPeople.isEmptyStr{
  201. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请先设置用餐人数", Etext: "Please set the number of diners first")
  202. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  203. self.addShopBtn.isUserInteractionEnabled = false
  204. return false
  205. }
  206. if self.viewModel.addShoppingRequestModel.numPeople < Int(self.viewModel.SMDetailModel?.lowestNum ?? "1"){
  207. 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")")
  208. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  209. self.addShopBtn.isUserInteractionEnabled = false
  210. return false
  211. }
  212. if (self.viewModel.addShoppingRequestModel.numPeople > Int(self.viewModel.SMDetailModel?.lowestNum ?? "1") || self.viewModel.addShoppingRequestModel.numPeople === Int(self.viewModel.SMDetailModel?.lowestNum ?? "1")) && self.viewModel.addShoppingRequestModel.numPeople.isNotEmptyStr{
  213. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "", Etext: "")
  214. }
  215. if self.viewModel.addShoppingRequestModel.deliveryId.isNil{
  216. self.attentionLab.text = LanguagesUtil.createTextBy(Ctext: "请选择配送时间", Etext: "Please select a delivery time")
  217. self.addShopBtn.setBackColor(kThemeColorAlpha, forState: .normal)
  218. self.addShopBtn.isUserInteractionEnabled = false
  219. return false
  220. }
  221. self.addShopBtn.setBackColor(kThemeColor, forState: .normal)
  222. self.addShopBtn.isUserInteractionEnabled = true
  223. return true
  224. }
  225. @IBAction func addToShoppingcart(_ sender: UIButton) {
  226. if self.viewModel.addShoppingRequestModel.deliveryEnd.isEmptyStr{
  227. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time"))
  228. return
  229. }
  230. if self.viewModel.addShoppingRequestModel.numPeople.isEmptyStr{
  231. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请输入就餐人数", Etext: "Please enter the number of diners"))
  232. return
  233. }
  234. if self.viewModel.addShoppingRequestModel.numPeople < Int(self.viewModel.SMDetailModel?.lowestNum ?? "1"){
  235. 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")"))
  236. return
  237. }
  238. //MARK: 加入购物车
  239. self.viewModel.signalAddShopping()?.on(success: { dd in
  240. self.hud.showSuccess()
  241. NotificationCenter.default.post(name: NSNotification.Name("刷新首页"), object: self, userInfo:nil)
  242. self.navigationController?.popViewController(animated: true)
  243. }, error: { error in
  244. self.hud.showFailure("\(error.msg ?? "")")
  245. })
  246. }
  247. }