Commit 3058597b authored by pete谢兆麟's avatar pete谢兆麟

算法修改

parent c7dec44f
...@@ -124,7 +124,7 @@ class YHSurveyMatchingViewController: YHBaseViewController { ...@@ -124,7 +124,7 @@ class YHSurveyMatchingViewController: YHBaseViewController {
self.requestState() self.requestState()
} }
}) })
//requestState() // requestState()
} }
private func requestState() { private func requestState() {
......
...@@ -82,65 +82,105 @@ extension String { ...@@ -82,65 +82,105 @@ extension String {
extension YHMakePlanModel { extension YHMakePlanModel {
mutating func toggleAddCartStatus(for productId: Int) { mutating func toggleAddCartStatus(for productId: Int) {
// Search and toggle in basic array var localBasic = basic
for i in 0..<basic.count { var localWork = work
if basic[i].product_id == productId { var localLive = live
basic[i].is_add_cart = !basic[i].is_add_cart var localInvest = invest
// If enabling, set all sub products to false except the first one
if basic[i].is_add_cart { for i in 0..<localBasic.count {
for j in 0..<basic[i].sub_product.count { if localBasic[i].product_id != 0 && localBasic[i].product_id == productId {
basic[i].sub_product[j].is_add_cart = (j == 0) localBasic[i].is_add_cart = !localBasic[i].is_add_cart
} updateSubProducts(for: &localBasic[i])
} // Update the original arrays and return
basic = localBasic
work = localWork
live = localLive
invest = localInvest
return
}
}
for i in 0..<localWork.count {
if localWork[i].product_id != 0 && localWork[i].product_id == productId {
localWork[i].is_add_cart = !localWork[i].is_add_cart
updateSubProducts(for: &localWork[i])
// Update the original arrays and return
basic = localBasic
work = localWork
live = localLive
invest = localInvest
return return
} else {
// Search in sub products
for j in 0..<basic[i].sub_product.count {
if basic[i].sub_product[j].product_id == productId {
basic[i].sub_product[j].is_add_cart = !basic[i].sub_product[j].is_add_cart
// When a sub product is selected, set its parent to true
if basic[i].sub_product[j].is_add_cart {
basic[i].is_add_cart = true
}
return
}
}
} }
} }
// Repeat the same logic for work, live, and invest arrays for i in 0..<localLive.count {
toggleInArray(&work, productId: productId) if localLive[i].product_id != 0 && localLive[i].product_id == productId {
toggleInArray(&live, productId: productId) localLive[i].is_add_cart = !localLive[i].is_add_cart
toggleInArray(&invest, productId: productId) updateSubProducts(for: &localLive[i])
// Update the original arrays and return
basic = localBasic
work = localWork
live = localLive
invest = localInvest
return
}
}
for i in 0..<localInvest.count {
if localInvest[i].product_id != 0 && localInvest[i].product_id == productId {
localInvest[i].is_add_cart = !localInvest[i].is_add_cart
updateSubProducts(for: &localInvest[i])
// Update the original arrays and return
basic = localBasic
work = localWork
live = localLive
invest = localInvest
return
}
}
// If we get here, we need to check sub_products (product_id == 0 cases)
checkSubProducts(in: &localBasic, for: productId)
checkSubProducts(in: &localWork, for: productId)
checkSubProducts(in: &localLive, for: productId)
checkSubProducts(in: &localInvest, for: productId)
basic = localBasic
work = localWork
live = localLive
invest = localInvest
} }
private func toggleInArray(_ array: inout [YHMakePlanMainModel], productId: Int) { private mutating func updateSubProducts(for mainModel: inout YHMakePlanMainModel) {
for i in 0..<array.count { if mainModel.is_add_cart {
if array[i].product_id == productId { // Find the subproduct with the lowest price
array[i].is_add_cart = !array[i].is_add_cart if let cheapestIndex = mainModel.sub_product.enumerated().min(by: {
// If enabling, set all sub products to false except the first one let price1 = Double($0.element.price) ?? 0
if array[i].is_add_cart { let price2 = Double($1.element.price) ?? 0
for j in 0..<array[i].sub_product.count { return price1 < price2
array[i].sub_product[j].is_add_cart = (j == 0) })?.offset {
} // Set only the cheapest to true, others to false
} for i in 0..<mainModel.sub_product.count {
return mainModel.sub_product[i].is_add_cart = (i == cheapestIndex)
} else {
// Search in sub products
for j in 0..<array[i].sub_product.count {
if array[i].sub_product[j].product_id == productId {
array[i].sub_product[j].is_add_cart = !array[i].sub_product[j].is_add_cart
// When a sub product is selected, set its parent to true
if array[i].sub_product[j].is_add_cart {
array[i].is_add_cart = true
}
return
}
} }
} }
} else {
// If main model is not in cart, set all subproducts to false
for i in 0..<mainModel.sub_product.count {
mainModel.sub_product[i].is_add_cart = false
}
} }
} }
private mutating func checkSubProducts(in models: inout [YHMakePlanMainModel], for productId: Int) {
for i in 0..<models.count {
if models[i].product_id == 0 {
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)
}
}
}
}
func calculateTotalPrice() -> String { func calculateTotalPrice() -> String {
let allCategories = [basic, work, live, invest] let allCategories = [basic, work, live, invest]
var total: Double = 0 var total: Double = 0
......
...@@ -189,6 +189,8 @@ class YHMakePlanCardView: UIView { ...@@ -189,6 +189,8 @@ class YHMakePlanCardView: UIView {
for i in 0 ..< count { for i in 0 ..< count {
if tag == self.data?.sub_product[i].product_id { if tag == self.data?.sub_product[i].product_id {
self.data?.sub_product[i].is_add_cart = true self.data?.sub_product[i].is_add_cart = true
} else {
self.data?.sub_product[i].is_add_cart = false
} }
} }
self.configure(with: self.data ?? YHMakePlanMainModel()) self.configure(with: self.data ?? YHMakePlanMainModel())
...@@ -197,7 +199,20 @@ class YHMakePlanCardView: UIView { ...@@ -197,7 +199,20 @@ class YHMakePlanCardView: UIView {
@objc func handleAction() { @objc func handleAction() {
selectButton.isSelected = !selectButton.isSelected selectButton.isSelected = !selectButton.isSelected
self.actionHandler?(self.data?.product_id ?? 0) var product = self.data?.product_id ?? 0
let count = self.data?.sub_product.count ?? 0
if count != 0 {
var index = 0
for i in 0 ..< count {
let flag = self.data?.sub_product[i].is_add_cart ?? false
if flag {
index = i
}
}
let subModel = data?.sub_product[index]
product = subModel?.product_id ?? 0
}
self.actionHandler?(product)
} }
// MARK: - Configuration // MARK: - Configuration
......
...@@ -321,7 +321,6 @@ class YHMakePlanLabelCell: UITableViewCell { ...@@ -321,7 +321,6 @@ class YHMakePlanLabelCell: UITableViewCell {
} }
func updateAllViews() { func updateAllViews() {
titleLabel.text = "基础身份续签"
mainItemView.removeSubviews() mainItemView.removeSubviews()
let filteredModel = dataSource?.filteredByCartStatus() ?? [] let filteredModel = dataSource?.filteredByCartStatus() ?? []
......
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