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

算法修改

parent c7dec44f
......@@ -124,7 +124,7 @@ class YHSurveyMatchingViewController: YHBaseViewController {
self.requestState()
}
})
//requestState()
// requestState()
}
private func requestState() {
......
......@@ -82,65 +82,105 @@ extension String {
extension YHMakePlanModel {
mutating func toggleAddCartStatus(for productId: Int) {
// Search and toggle in basic array
for i in 0..<basic.count {
if basic[i].product_id == productId {
basic[i].is_add_cart = !basic[i].is_add_cart
// If enabling, set all sub products to false except the first one
if basic[i].is_add_cart {
for j in 0..<basic[i].sub_product.count {
basic[i].sub_product[j].is_add_cart = (j == 0)
}
}
var localBasic = basic
var localWork = work
var localLive = live
var localInvest = invest
for i in 0..<localBasic.count {
if localBasic[i].product_id != 0 && localBasic[i].product_id == productId {
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
} 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
toggleInArray(&work, productId: productId)
toggleInArray(&live, productId: productId)
toggleInArray(&invest, productId: productId)
for i in 0..<localLive.count {
if localLive[i].product_id != 0 && localLive[i].product_id == productId {
localLive[i].is_add_cart = !localLive[i].is_add_cart
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) {
for i in 0..<array.count {
if array[i].product_id == productId {
array[i].is_add_cart = !array[i].is_add_cart
// If enabling, set all sub products to false except the first one
if array[i].is_add_cart {
for j in 0..<array[i].sub_product.count {
array[i].sub_product[j].is_add_cart = (j == 0)
}
}
return
} 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
}
private mutating func updateSubProducts(for mainModel: inout YHMakePlanMainModel) {
if mainModel.is_add_cart {
// Find the subproduct with the lowest price
if let cheapestIndex = mainModel.sub_product.enumerated().min(by: {
let price1 = Double($0.element.price) ?? 0
let price2 = Double($1.element.price) ?? 0
return price1 < price2
})?.offset {
// Set only the cheapest to true, others to false
for i in 0..<mainModel.sub_product.count {
mainModel.sub_product[i].is_add_cart = (i == cheapestIndex)
}
}
} 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 {
let allCategories = [basic, work, live, invest]
var total: Double = 0
......
......@@ -189,6 +189,8 @@ class YHMakePlanCardView: UIView {
for i in 0 ..< count {
if tag == self.data?.sub_product[i].product_id {
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())
......@@ -197,7 +199,20 @@ class YHMakePlanCardView: UIView {
@objc func handleAction() {
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
......
......@@ -321,7 +321,6 @@ class YHMakePlanLabelCell: UITableViewCell {
}
func updateAllViews() {
titleLabel.text = "基础身份续签"
mainItemView.removeSubviews()
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