Commit 2c34284e authored by pete谢兆麟's avatar pete谢兆麟

算法修改

parent 0818af1b
......@@ -173,9 +173,26 @@ extension YHMakePlanModel {
private mutating func checkSubProducts(in models: inout [YHMakePlanMainModel], for productId: Int) {
for i in 0..<models.count {
if models[i].product_id == 0 {
var hasFind = false
for j in 0..<models[i].sub_product.count {
if models[i].sub_product[j].product_id == productId {
if models[i].sub_product[j].is_add_cart {
models[i].sub_product[j].is_add_cart = false
models[i].is_add_cart = false
} else {
models[i].sub_product[j].is_add_cart = true
models[i].is_add_cart = true
}
hasFind = true
}
}
if hasFind {
for j in 0..<models[i].sub_product.count {
models[i].sub_product[j].is_add_cart = (models[i].sub_product[j].product_id == productId)
if models[i].sub_product[j].product_id == productId {
} else {
models[i].sub_product[j].is_add_cart = false
}
}
}
}
......@@ -229,3 +246,14 @@ extension Array where Element == YHMakePlanSubModel {
self.filter { $0.is_add_cart }
}
}
extension Array where Element == YHMakePlanSubModel {
func indexOfLowestPrice() -> Int? {
return self.enumerated().reduce(into: (index: 0, price: Double.infinity)) { result, current in
let (currentIndex, model) = current
if let currentPrice = Double(model.price), currentPrice < result.price {
result = (currentIndex, currentPrice)
}
}.index
}
}
......@@ -202,7 +202,7 @@ class YHMakePlanCardView: UIView {
var product = self.data?.product_id ?? 0
let count = self.data?.sub_product.count ?? 0
if count != 0 {
var index = 0
var index = self.data?.sub_product.indexOfLowestPrice() ?? 0
for i in 0 ..< count {
let flag = self.data?.sub_product[i].is_add_cart ?? false
if flag {
......@@ -224,7 +224,7 @@ class YHMakePlanCardView: UIView {
if data.price == "" {
storeButton.isHidden = false
let count = self.data?.sub_product.count ?? 0
var index = 0
var index = self.data?.sub_product.indexOfLowestPrice() ?? 0
for i in 0 ..< count {
let flag = self.data?.sub_product[i].is_add_cart ?? false
if flag {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment