Browse Source

购物车选择逻辑

Chris 6 years ago
parent
commit
97ad74eaf5

+ 21 - 3
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartAccountView.swift

@@ -26,6 +26,27 @@ class ShoppingCartAccountView: BaseView {
         }
     }
     
+    var tPrice: Int? {
+        didSet {
+            priceLabel.text = "¥\(tPrice ?? 0)"
+        }
+    }
+    
+    var tNumber: Int? {
+        didSet {
+            accountBtn.setTitle("结算(\(tNumber ?? 0))", for: UIControl.State.normal)
+            if tNumber == 0 {
+                accountBtn.backgroundColor = ke6e6e6Color
+                accountBtn.setTitleColor(k333333Color, for: UIControl.State.normal)
+                accountBtn.isEnabled = false
+            } else {
+                accountBtn.setTitleColor(kffffffColor, for: UIControl.State.normal)
+                accountBtn.backgroundColor = kFFA42FColor
+                accountBtn.isEnabled = true
+            }
+        }
+    }
+    
     override func setupViews() {
         self.backgroundColor = kffffffColor
         addSubview(allSelectBtn)
@@ -83,9 +104,7 @@ class ShoppingCartAccountView: BaseView {
     
     private lazy var accountBtn: UIButton = {
         let accountBtn = UIButton(type: UIButton.ButtonType.custom)
-        accountBtn.backgroundColor = kFFA42FColor
         accountBtn.setTitle("结算(0)", for: UIControl.State.normal)
-        accountBtn.setTitleColor(kffffffColor, for: UIControl.State.normal)
         accountBtn.titleLabel?.font = kScaleRegularFont15
         accountBtn.cornerRadius = 18
         accountBtn.masksToBounds = true
@@ -99,7 +118,6 @@ class ShoppingCartAccountView: BaseView {
     
     private lazy var priceLabel: UILabel = {
         let priceLabel = UILabel()
-        priceLabel.text = "¥0.00"
         priceLabel.textColor = kFE352BColor
         priceLabel.font = kScaleBoldFont18
         return priceLabel

+ 49 - 3
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartView.swift

@@ -15,6 +15,7 @@ class ShoppingCartView: BaseView {
         didSet {
             tableView.reloadData()
             self.judgeAllSelectedStatus()
+            self.refreshAccountView()
         }
     }
     
@@ -26,7 +27,14 @@ class ShoppingCartView: BaseView {
         }
     }
     
-    typealias OrderPayTransBlock = () -> Void
+    // 已选商品总价
+    private var totalPrice: Int = 0
+    // 已选商品件数
+    private var totalNum: Int = 0
+    // 已选商品ModelArr
+    private var selModelArr: Array<CartProductListModel> = []
+    
+    typealias OrderPayTransBlock = (_ selMdlArr: Array<CartProductListModel>) -> Void
     var orderPayTransBlock : OrderPayTransBlock?
     
     override func setupViews() {
@@ -56,12 +64,13 @@ class ShoppingCartView: BaseView {
         accountView.allSelectBlock = { [weak self]
             (isAllSelected) in
             self?.allSelectedAction(isAllSelected)
+            self?.refreshAccountView()
         }
         
         accountView.orderPayBlock = {
             [weak self] in
             if let orderPayTransBlock = self?.orderPayTransBlock {
-                orderPayTransBlock()
+                orderPayTransBlock(self!.selModelArr)
             }
         }
         
@@ -133,7 +142,7 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
                     
                     self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
                     self?.judgeAllSelectedStatus()
-                    
+                    self?.refreshAccountView()
                 }
                 
                 cell.changeProductBlock = { [weak self]
@@ -183,6 +192,7 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
                 (isSectionSel) in
                 self?.shopSelectedAction(isSectionSel, section: section)
                 self?.judgeAllSelectedStatus()
+                self?.refreshAccountView()
             }
             return headerView
             
@@ -262,4 +272,40 @@ extension ShoppingCartView {
         self.accountView.isAllSelected = 1
     }
     
+    // 计算结算数据,刷新结算View
+    func refreshAccountView() {
+        totalPrice = 0
+        totalNum = 0
+        selModelArr = []
+        
+//        var cartListCopyArr :Array<CartProductListModel> = []
+        
+        for listModel in cartListModelArr! {
+            let copyListMdl: CartProductListModel = listModel.copy() as! CartProductListModel
+            selModelArr.append(copyListMdl)
+        }
+        print(selModelArr)
+        
+        
+        for (secIdx, cartProListMdl) in (cartListModelArr?.enumerated())! {
+            var subTag: Int = 0
+            for (rowIdx, productMdl) in cartProListMdl.productList!.enumerated() {
+                if productMdl.isSelect == 1 {
+                    totalPrice += productMdl.skuPrice ?? 0
+                    totalNum += 1
+                    
+                } else {
+                    if !cartListModelArr![secIdx].productList!.isEmpty {
+                        selModelArr[secIdx].productList!.remove(at: rowIdx-subTag)
+                        subTag += 1
+                    }
+                }
+            }
+        }
+        print(selModelArr)
+        
+        self.accountView.tPrice = totalPrice
+        self.accountView.tNumber = totalNum
+    }
+    
 }

+ 2 - 1
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/ViewController/ShoppingCartViewController.swift

@@ -66,8 +66,9 @@ class ShoppingCartViewController: BaseViewController {
     private lazy var shoppingCartView: ShoppingCartView = {
         let shoppingCartView = ShoppingCartView()
         shoppingCartView.orderPayTransBlock = {
-            [weak self] in
+            [weak self] (selMdlArr) in
             let vc = ShoppingCartPayOrderController()
+            vc.selListModelArr = selMdlArr
             self?.navigationController?.pushViewController(vc, animated: true)
         }
         return shoppingCartView

+ 8 - 6
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCartPayOrder/ViewController/ShoppingCartPayOrderController.swift

@@ -11,6 +11,13 @@ import RxSwift
 
 class ShoppingCartPayOrderController: BaseViewController {
     
+    // 已选商品ModelArr
+    var selListModelArr : Array<CartProductListModel>? {
+        didSet {
+            self.setOrderPayView()
+        }
+    }
+    
     override func viewDidLoad() {
         super.viewDidLoad()
         setupViews()
@@ -28,12 +35,7 @@ class ShoppingCartPayOrderController: BaseViewController {
     }
     
     override func setupData() {
-        //TODO:获取订单列表
-        //        SwiftMoyaNetWorkServiceCMS.shared().cmsTemplateSetTemplateNameApi {
-        //            [weak self] (data) -> (Void) in
-        //            self?.setShoppingCartView()
-        self.setOrderPayView()
-        //        }
+        
     }
     
     /// 添加view

+ 30 - 2
RainbowPlanet/RainbowPlanet/Service/Model/ProductModel/ProductCartListModel.swift

@@ -10,7 +10,7 @@ import Foundation
 import ObjectMapper
 
 
-class ProductCartListData : NSObject, Mappable{
+class ProductCartListData : NSObject, Mappable, NSCopying, NSMutableCopying{
     
     var data : [CartProductListModel]?
     
@@ -26,9 +26,21 @@ class ProductCartListData : NSObject, Mappable{
         data <- map["data"]
     }
     
+    func copy(with zone: NSZone? = nil) -> Any {
+        let dataModel = ProductCartListData.init()
+        dataModel.data = self.data
+        return dataModel
+    }
+    
+    func mutableCopy(with zone: NSZone? = nil) -> Any {
+        let dataModel = ProductCartListData.init()
+        dataModel.data = self.data
+        return dataModel
+    }
+    
 }
 
-class CartProductListModel : NSObject, Mappable{
+class CartProductListModel : NSObject, Mappable, NSCopying, NSMutableCopying{
     
     var productList : [ProductModel]?
     var shopId : Int?
@@ -48,4 +60,20 @@ class CartProductListModel : NSObject, Mappable{
         shopName <- map["shop_name"]
     }
     
+    func copy(with zone: NSZone? = nil) -> Any {
+        let listModel = CartProductListModel.init()
+        listModel.productList = self.productList
+        listModel.shopId = self.shopId
+        listModel.shopName = self.shopName
+        return listModel
+    }
+    
+    func mutableCopy(with zone: NSZone? = nil) -> Any {
+        let listModel = CartProductListModel.init()
+        listModel.productList = self.productList
+        listModel.shopId = self.shopId
+        listModel.shopName = self.shopName
+        return listModel
+    }
+    
 }

+ 119 - 0
RainbowPlanet/RainbowPlanet/Service/Model/ProductModel/ProductModel.swift

@@ -134,3 +134,122 @@ class ProductModel : NSObject, Mappable{
 	}
 
 }
+
+extension ProductModel: NSCopying,NSMutableCopying {
+    
+    func copy(with zone: NSZone? = nil) -> Any {
+        let pModel = ProductModel.init()
+        pModel.attributeCategoryId = self.attributeCategoryId
+        pModel.bigImg = self.bigImg
+        pModel.categoryId1 = self.categoryId1
+        pModel.categoryId2 = self.categoryId2
+        pModel.categoryId3 = self.categoryId3
+        pModel.cityId = self.cityId
+        pModel.commentNumber = self.commentNumber
+        pModel.commentScore = self.commentScore
+        pModel.commentStar = self.commentStar
+        pModel.createdAt = self.createdAt
+        pModel.deletedAt = self.deletedAt
+        pModel.deliverType = self.deliverType
+        pModel.desc = self.desc
+        pModel.id = self.id
+        pModel.img = self.img
+        pModel.isConfirmSale = self.isConfirmSale
+        pModel.isMain = self.isMain
+        pModel.limitNumber = self.limitNumber
+        pModel.limitType = self.limitType
+        pModel.name = self.name
+        pModel.originPrice = self.originPrice
+        pModel.otherCode = self.otherCode
+        pModel.price = self.price
+        pModel.receiveTime = self.receiveTime
+        pModel.receiveType = self.receiveType
+        pModel.saleName = self.saleName
+        pModel.saleStatus = self.saleStatus
+        pModel.shopId = self.shopId
+        pModel.shopName = self.shopName
+        pModel.skuCode = self.skuCode
+        pModel.skuId = self.skuId
+        pModel.skuName = self.skuName
+        pModel.sort = self.sort
+        pModel.spuCode = self.spuCode
+        pModel.status = self.status
+        pModel.stock = self.stock
+        pModel.storeTypeIds = self.storeTypeIds
+        pModel.subtitle = self.subtitle
+        pModel.totalCount = self.totalCount
+        pModel.totalStock = self.totalStock
+        pModel.upStatus = self.upStatus
+        pModel.updatedAt = self.updatedAt
+        pModel.video = self.video
+        pModel.wasteStatus = self.wasteStatus
+        // 购物车
+        pModel.amount = self.amount
+        pModel.isSelect = self.isSelect
+        pModel.productId = self.productId
+        pModel.productImg = self.productImg
+        pModel.productName = self.productName
+        pModel.productPrice = self.productPrice
+        pModel.skuPrice = self.skuPrice
+        pModel.uid = self.uid
+        return pModel
+    }
+    
+    func mutableCopy(with zone: NSZone? = nil) -> Any {
+        let pModel = ProductModel.init()
+        pModel.attributeCategoryId = self.attributeCategoryId
+        pModel.bigImg = self.bigImg
+        pModel.categoryId1 = self.categoryId1
+        pModel.categoryId2 = self.categoryId2
+        pModel.categoryId3 = self.categoryId3
+        pModel.cityId = self.cityId
+        pModel.commentNumber = self.commentNumber
+        pModel.commentScore = self.commentScore
+        pModel.commentStar = self.commentStar
+        pModel.createdAt = self.createdAt
+        pModel.deletedAt = self.deletedAt
+        pModel.deliverType = self.deliverType
+        pModel.desc = self.desc
+        pModel.id = self.id
+        pModel.img = self.img
+        pModel.isConfirmSale = self.isConfirmSale
+        pModel.isMain = self.isMain
+        pModel.limitNumber = self.limitNumber
+        pModel.limitType = self.limitType
+        pModel.name = self.name
+        pModel.originPrice = self.originPrice
+        pModel.otherCode = self.otherCode
+        pModel.price = self.price
+        pModel.receiveTime = self.receiveTime
+        pModel.receiveType = self.receiveType
+        pModel.saleName = self.saleName
+        pModel.saleStatus = self.saleStatus
+        pModel.shopId = self.shopId
+        pModel.shopName = self.shopName
+        pModel.skuCode = self.skuCode
+        pModel.skuId = self.skuId
+        pModel.skuName = self.skuName
+        pModel.sort = self.sort
+        pModel.spuCode = self.spuCode
+        pModel.status = self.status
+        pModel.stock = self.stock
+        pModel.storeTypeIds = self.storeTypeIds
+        pModel.subtitle = self.subtitle
+        pModel.totalCount = self.totalCount
+        pModel.totalStock = self.totalStock
+        pModel.upStatus = self.upStatus
+        pModel.updatedAt = self.updatedAt
+        pModel.video = self.video
+        pModel.wasteStatus = self.wasteStatus
+        // 购物车
+        pModel.amount = self.amount
+        pModel.isSelect = self.isSelect
+        pModel.productId = self.productId
+        pModel.productImg = self.productImg
+        pModel.productName = self.productName
+        pModel.productPrice = self.productPrice
+        pModel.skuPrice = self.skuPrice
+        pModel.uid = self.uid
+        return pModel
+    }
+}