OrderController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // OrderController.swift
  3. // learnSwift
  4. //
  5. // Created by zyc on 2019/11/20.
  6. // Copyright © 2019 张言超. All rights reserved.
  7. //
  8. import UIKit
  9. class OrderController: BaseViewController {
  10. var myTableView = UITableView.init()
  11. /** 数据源 */
  12. var dataArr: NSMutableArray = NSMutableArray.init()
  13. /** 选择的数据 */
  14. var selectedArr: NSMutableArray = NSMutableArray.init()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. initNavLeftBackButton()
  18. self.navigationItem.title = "购物车"
  19. self.customUI()
  20. self.initData()
  21. }
  22. var footView: YCOrderFootView?
  23. func customUI() {
  24. view.backgroundColor = UIColor.white
  25. self.myTableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-70), style: .grouped)
  26. self.myTableView.delegate = self
  27. self.myTableView.dataSource = self
  28. self.myTableView.separatorStyle = .none
  29. self.myTableView.register(YCOrderShopCartCell.self, forCellReuseIdentifier: "YCOrderShopCartCell")
  30. self.view.addSubview(self.myTableView)
  31. self.footView = YCOrderFootView.init(frame: CGRect(x: 0, y: kScreenHeight-navheight-70, width: kScreenWidth, height: 70))
  32. self.footView?.footDelegate = self
  33. self.footView?.accountBtn?.addTarget(self, action: #selector(accountBtnTouch), for: UIControl.Event.touchUpInside)
  34. self.view.addSubview(self.footView!)
  35. }
  36. @objc func accountBtnTouch() {
  37. self.addToShopCar()
  38. }
  39. @objc func delectGoods(_ sender: UIButton) {
  40. let alert = UIAlertController.init(title: "您确定要删除这个商品吗?", message: "", preferredStyle: .alert)
  41. let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (_) in
  42. let shopmodel: YCOrderShopModel = self.dataArr[0] as! YCOrderShopModel
  43. let arr: [YCOrderGoodsModel] = shopmodel.goodsArr!
  44. if arr.count > 0 {
  45. let goodmodel: YCOrderGoodsModel = arr[sender.tag]
  46. //删除选中的数组
  47. if goodmodel.goodsIsSelect {
  48. let xxtempArr: NSMutableArray = self.selectedArr[0] as! NSMutableArray
  49. if xxtempArr.contains(goodmodel) {
  50. xxtempArr.remove(goodmodel)
  51. }
  52. }
  53. //删除数据源
  54. var tempArr: [YCOrderGoodsModel] = NSMutableArray.init(array: arr) as! [YCOrderGoodsModel]
  55. let xxIndex: Int = tempArr.firstIndex(of: goodmodel)!
  56. var str = ""
  57. let ycmodel = tempArr[xxIndex]
  58. str = ycmodel.Id!
  59. self.delect(str)
  60. tempArr.remove(at: xxIndex)
  61. shopmodel.goodsArr = tempArr
  62. }
  63. //更新数据
  64. self.updateData()
  65. }
  66. let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
  67. alert.addAction(confirmaction)
  68. alert.addAction(cancleAction)
  69. self.present(alert, animated: true, completion: nil)
  70. }
  71. func delect(_ string: String) {
  72. let params = NSMutableDictionary()
  73. params.setValue(string, forKey: "DATA_IDS")
  74. let http = AFHTTPSessionManager()
  75. http.post(RequestURL.deleteProductOrder, parameters: params, progress: { (_) in
  76. }, success: { (operation, json) in
  77. print(json as Any)
  78. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  79. if success == 200 {
  80. SVProgressHUD.showSuccess(withStatus: "删除成功!")
  81. self.initData()
  82. }
  83. }) { (_, _) in
  84. }
  85. }
  86. func initData() {
  87. let params = NSMutableDictionary()
  88. params.setValue(CommonValue.getUserId(), forKey: "userId")
  89. let http = AFHTTPSessionManager()
  90. http.get(RequestURL.getProductOrderList, parameters: params, progress: { (_) in
  91. }, success: { (operation, json) in
  92. print(json as Any)
  93. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  94. if success == 200 {
  95. self.dataArr.removeAllObjects()
  96. self.selectedArr.removeAllObjects()
  97. let dataArr: NSArray = (json as! NSDictionary).object(forKey: "data") as! NSArray
  98. if dataArr.count>0 {
  99. let dict: NSDictionary = dataArr[0] as! NSDictionary
  100. let shopModel = YCOrderShopModel.init()
  101. shopModel.setValue(dataArr, forKey: "commoditys")
  102. shopModel.setValue(dict["shop_id"], forKey: "shopName")
  103. self.dataArr.add(shopModel)
  104. }
  105. self.myTableView.reloadData()
  106. //初始化选择的数组
  107. for _ in 0..<self.dataArr.count {
  108. let tempArr = NSMutableArray.init()
  109. self.selectedArr.add(tempArr)
  110. }
  111. }
  112. }) { (_, _) in
  113. }
  114. }
  115. override func viewWillAppear(_ animated: Bool) {
  116. super.viewWillAppear(animated)
  117. self.tabBarController?.tabBar.backgroundColor = UIColor.white
  118. }
  119. }
  120. extension OrderController: UITableViewDelegate, UITableViewDataSource {
  121. func numberOfSections(in tableView: UITableView) -> Int {
  122. return self.dataArr.count
  123. }
  124. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  125. let shopModel: YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel
  126. return shopModel.goodsArr!.count
  127. }
  128. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  129. let cell: YCOrderShopCartCell = tableView.dequeueReusableCell(withIdentifier: "YCOrderShopCartCell", for: indexPath) as! YCOrderShopCartCell
  130. cell.backgroundColor = UIColor.colorWithCustom(r: 242, g: 242, b: 242)
  131. let shopModel: YCOrderShopModel = self.dataArr[indexPath.section] as! YCOrderShopModel
  132. cell.xgoodModel = shopModel.goodsArr?[indexPath.row]
  133. cell.YCcellDelegate = self
  134. cell.delectBtn?.addTarget(self, action: #selector(delectGoods(_:)), for: UIControl.Event.touchUpInside)
  135. cell.delectBtn!.tag = indexPath.row
  136. return cell
  137. }
  138. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  139. return 100.0
  140. }
  141. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  142. tableView.deselectRow(at: indexPath, animated: false)
  143. }
  144. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  145. // let model:YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel
  146. // return model.goodsArr!.count > 0 ? 50 : 0
  147. return 0
  148. }
  149. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  150. let model: YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel
  151. let headerview = YCOrderHeaderView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 50))
  152. headerview.shopModel = model
  153. headerview.index = section
  154. headerview.YCHeaderDelegate = self
  155. // return model.goodsArr!.count > 0 ? headerview : nil
  156. return nil
  157. }
  158. // func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  159. // if editingStyle == .delete {
  160. // let alert = UIAlertController.init(title: "您确定要删除这个商品吗?", message: "", preferredStyle: .alert)
  161. // let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (UIAlertAction) in
  162. // let shopmodel:YCOrderShopModel = self.dataArr[indexPath.section] as! YCOrderShopModel
  163. // let arr:[YCOrderGoodsModel] = shopmodel.goodsArr!
  164. // if arr.count > 0{
  165. // let goodmodel:YCOrderGoodsModel = arr[indexPath.row]
  166. // //删除选中的数组
  167. // if goodmodel.goodsIsSelect {
  168. // let xxtempArr:NSMutableArray = self.selectedArr[indexPath.section] as! NSMutableArray
  169. // if xxtempArr.contains(goodmodel) {
  170. // xxtempArr.remove(goodmodel)
  171. // }
  172. // }
  173. // //删除数据源
  174. // var tempArr:[YCOrderGoodsModel] = NSMutableArray.init(array: arr) as! [YCOrderGoodsModel]
  175. // let xxIndex:Int = tempArr.firstIndex(of: goodmodel)!
  176. // tempArr.remove(at: xxIndex)
  177. // shopmodel.goodsArr = tempArr
  178. // }
  179. // //更新数据
  180. // self.updateData()
  181. // }
  182. // let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
  183. // alert.addAction(confirmaction)
  184. // alert.addAction(cancleAction)
  185. // self.present(alert, animated: true, completion: nil)
  186. // }
  187. // }
  188. // func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  189. // return "删除"
  190. // }
  191. }
  192. // MARK: - - YCOrderShopCartCellDelegate
  193. extension OrderController: YCOrderShopCartCellDelegate {
  194. func shopCellDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) {
  195. print("点击cell,跳转商品详情")
  196. }
  197. func addBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) {
  198. self.disposeShopData(goodModel: goodModel, cell: cell, isAdd: true)
  199. }
  200. func minusBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) {
  201. self.disposeShopData(goodModel: goodModel, cell: cell, isAdd: false)
  202. }
  203. func leftSelBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell, isSel: Bool) {
  204. var shopModelIndex: Int?
  205. var goodModelIndex: Int?
  206. for shop_Index in 0..<self.dataArr.count {
  207. let shopmodel: YCOrderShopModel = self.dataArr[shop_Index] as! YCOrderShopModel
  208. let arr: [YCOrderGoodsModel] = shopmodel.goodsArr!
  209. if arr.count > 0 {
  210. for good_index in 0..<arr.count {
  211. let good_model: YCOrderGoodsModel = arr[good_index]
  212. if good_model == goodModel {
  213. good_model.goodsIsSelect = isSel
  214. shopModelIndex = self.dataArr.index(of: shopmodel)
  215. goodModelIndex = arr.firstIndex(of: good_model)
  216. let tempArr: NSMutableArray = self.selectedArr[shopModelIndex!] as! NSMutableArray
  217. if isSel {
  218. //装入数组
  219. if !tempArr.contains(goodModel) {
  220. tempArr.add(goodModel)
  221. if tempArr.count == shopmodel.goodsArr?.count {
  222. shopmodel.shopIsAllSelected = true
  223. } else {
  224. shopmodel.shopIsAllSelected = false
  225. }
  226. }
  227. } else {
  228. if tempArr.contains(goodModel) {
  229. tempArr.remove(goodModel)
  230. shopmodel.shopIsAllSelected = false
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. self.updateData()
  238. }
  239. func disposeShopData(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell, isAdd: Bool) {
  240. var shopModelIndex: Int?
  241. var goodModelIndex: Int?
  242. for shop_Index in 0..<self.dataArr.count {
  243. let shopmodel: YCOrderShopModel = self.dataArr[shop_Index] as! YCOrderShopModel
  244. let arr: [YCOrderGoodsModel] = shopmodel.goodsArr!
  245. if arr.count > 0 {
  246. for good_index in 0..<arr.count {
  247. let good_model: YCOrderGoodsModel = arr[good_index]
  248. if good_model == goodModel {
  249. shopModelIndex = self.dataArr.index(of: shopmodel)
  250. goodModelIndex = arr.firstIndex(of: good_model)
  251. let tempArr: NSMutableArray = self.selectedArr[shopModelIndex!] as! NSMutableArray
  252. //是选中的那个
  253. var count: Int = good_model.goodsCount!
  254. if isAdd {
  255. if count == 99 {
  256. return
  257. }
  258. count += 1
  259. } else {
  260. if count == 1 {
  261. return
  262. }
  263. count -= 1
  264. }
  265. //更新选中数组中的个数
  266. if !tempArr.contains(good_model) {
  267. //如果没有加入的,加入选中数组
  268. goodModel.goodsIsSelect = true
  269. tempArr.add(goodModel)
  270. if tempArr.count == shopmodel.goodsArr?.count {
  271. shopmodel.shopIsAllSelected = true
  272. }
  273. }
  274. let xxindex: Int = tempArr.index(of: good_model)
  275. let good: YCOrderGoodsModel = tempArr[xxindex] as! YCOrderGoodsModel
  276. good.goodsCount = count
  277. }
  278. }
  279. }
  280. }
  281. self.updateData()
  282. }
  283. }
  284. // MARK: - - headerViewDelegate
  285. extension OrderController: YC_headerViewDelegate {
  286. func headerViewDidPress(index: NSInteger, shopmodel: YCOrderShopModel, isSel: Bool) {
  287. let selmodel: YCOrderShopModel = self.dataArr[index] as! YCOrderShopModel
  288. selmodel.shopIsAllSelected = isSel
  289. let arr: [YCOrderGoodsModel] = selmodel.goodsArr!
  290. for xindex in 0..<arr.count {
  291. let goodmodel: YCOrderGoodsModel = arr[xindex]
  292. goodmodel.goodsIsSelect = isSel
  293. }
  294. //存入选择数组
  295. let saveArr: NSMutableArray = self.selectedArr[index] as! NSMutableArray
  296. if isSel {
  297. saveArr.setArray(arr)
  298. } else {
  299. saveArr.removeAllObjects()
  300. }
  301. self.updateData()
  302. }
  303. }
  304. // MARK: - - FootViewDelegate
  305. extension OrderController: YC_FootViewDelegate {
  306. func selectedAllBtnDidPress(button: UIButton, isSel: Bool) {
  307. for xindex in 0..<self.dataArr.count {
  308. let shopmodel: YCOrderShopModel = self.dataArr[xindex] as! YCOrderShopModel
  309. let goodarr: NSArray = shopmodel.goodsArr! as NSArray
  310. if goodarr.count > 0 {
  311. for jindex in 0..<goodarr.count {
  312. let goodmodel: YCOrderGoodsModel = goodarr[jindex] as! YCOrderGoodsModel
  313. goodmodel.goodsIsSelect = isSel
  314. }
  315. }
  316. }
  317. //添加到选择数组
  318. if isSel {
  319. for kindex in 0..<self.dataArr.count {
  320. let tempArr: NSMutableArray = self.selectedArr[kindex] as! NSMutableArray
  321. let model: YCOrderShopModel = self.dataArr[kindex] as! YCOrderShopModel
  322. let arr: NSArray = model.goodsArr! as NSArray
  323. tempArr.setArray(arr as! [Any])
  324. }
  325. } else {
  326. for kindex in 0..<self.selectedArr.count {
  327. let tempArr: NSMutableArray = self.selectedArr[kindex] as! NSMutableArray
  328. tempArr.removeAllObjects()
  329. }
  330. }
  331. self.updateData()
  332. }
  333. //更新数据 --价格总计--tableviewUI变化
  334. func updateData() {
  335. self.updatePrice()
  336. self.myTableView.reloadData()
  337. }
  338. //更新价格
  339. func updatePrice() {
  340. //更新底部的价格
  341. self.footView?.updateArr(selectedArr: self.selectedArr, dataArr: self.dataArr)
  342. //判断每个店铺是否全选状态
  343. for lindex in 0..<self.dataArr.count {
  344. let model: YCOrderShopModel = self.dataArr[lindex] as! YCOrderShopModel
  345. let selectarr: NSMutableArray = self.selectedArr[lindex] as! NSMutableArray
  346. model.shopIsAllSelected = model.goodsArr?.count == selectarr.count ? true : false
  347. }
  348. }
  349. func addToShopCar() {
  350. let params = NSMutableDictionary()
  351. let array = self.selectedArr[0]as!NSArray
  352. if array.count == 0 {
  353. SVProgressHUD.showError(withStatus: "请选择商品!")
  354. return
  355. }
  356. var jsonStr = ""
  357. for i in 0..<array.count {
  358. let goodsModel = array[i]as! YCOrderGoodsModel
  359. let tojson = NSMutableDictionary()
  360. tojson.setValue(CommonValue.getUserId(), forKey: "accountId")
  361. tojson.setValue(goodsModel.productId, forKey: "productId")
  362. tojson.setValue(goodsModel.goodsCount, forKey: "num")
  363. tojson.setValue("12345", forKey: "shopId")
  364. tojson.setValue(goodsModel.goodsPrice, forKey: "price")
  365. if i == 0 {
  366. jsonStr = self.dicValueString(tojson as! [String: Any])!
  367. } else {
  368. jsonStr = "\(jsonStr),\(self.dicValueString(tojson as! [String: Any])!)"
  369. }
  370. }
  371. params.setValue("[\(jsonStr)]", forKey: "toJson")
  372. // loadDataList(url: url, params: params, tag: 1001, array: arrType)
  373. print(params)
  374. let http = AFHTTPSessionManager()
  375. http.post(RequestURL.addProductOrder, parameters: params, progress: { (_) in
  376. }, success: { (operation, json) in
  377. print(json as Any)
  378. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  379. if success == 200 {
  380. let vc = CreatShopOrderViewController()
  381. let array = self.selectedArr[0]as!NSArray
  382. vc.goodsArray = array
  383. self.toViewController(viewController: vc)
  384. }
  385. }) { (_, _) in
  386. }
  387. }
  388. func dicValueString(_ dic: [String: Any]) -> String? {
  389. let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
  390. var str = String(data: data!, encoding: String.Encoding.utf8)
  391. str = str!.replacingOccurrences(of: "\"", with: "'")
  392. print(str!)
  393. return str
  394. }
  395. }