Showing
1 changed file
with
50 additions
and
36 deletions
| @@ -65,7 +65,6 @@ Page({ | @@ -65,7 +65,6 @@ Page({ | ||
| 65 | // console.log('listlistlist',JSON.stringify(list)); | 65 | // console.log('listlistlist',JSON.stringify(list)); |
| 66 | // list= list.concat(monidata) | 66 | // list= list.concat(monidata) |
| 67 | list.forEach(detail => { | 67 | list.forEach(detail => { |
| 68 | - detail.stringifyUse = detail.use ? JSON.stringify(detail.use) : ''; | ||
| 69 | detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : ''; | 68 | detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : ''; |
| 70 | detail.arule = detail.arule ? util.determine(detail.arule) : ''; | 69 | detail.arule = detail.arule ? util.determine(detail.arule) : ''; |
| 71 | if (detail.arule) { | 70 | if (detail.arule) { |
| @@ -128,13 +127,13 @@ Page({ | @@ -128,13 +127,13 @@ Page({ | ||
| 128 | const values = e.detail.value | 127 | const values = e.detail.value |
| 129 | let arr = [] | 128 | let arr = [] |
| 130 | let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2 | 129 | let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2 |
| 131 | - let inputValue = this.data.inputValue * 1 | 130 | + let inputValue = parseFloat(this.data.inputValue) |
| 132 | let val = 0; | 131 | let val = 0; |
| 133 | for (let i = 0, lenI = list.length; i < lenI; ++i) { | 132 | for (let i = 0, lenI = list.length; i < lenI; ++i) { |
| 134 | list[i].checked = false; | 133 | list[i].checked = false; |
| 135 | for (let j = 0, lenJ = values.length; j < lenJ; ++j) { | 134 | for (let j = 0, lenJ = values.length; j < lenJ; ++j) { |
| 136 | if (list[i].activity_id === values[j]) { | 135 | if (list[i].activity_id === values[j]) { |
| 137 | - val += list[i].price * 1 | 136 | + val += parseFloat(list[i].price) |
| 138 | arr.push({ | 137 | arr.push({ |
| 139 | ...list[i] | 138 | ...list[i] |
| 140 | }) | 139 | }) |
| @@ -172,7 +171,11 @@ Page({ | @@ -172,7 +171,11 @@ Page({ | ||
| 172 | //选的可叠加券 其他所有的不可叠加券都不可选 | 171 | //选的可叠加券 其他所有的不可叠加券都不可选 |
| 173 | if (currentCoupon == '1') { | 172 | if (currentCoupon == '1') { |
| 174 | list.forEach(item => { | 173 | list.forEach(item => { |
| 175 | - if (item.accumulationShow == "0" || (remaining - item.price < 0 && !item.checked)) { | 174 | + let kyNum = Math.floor(parseFloat(this.data.inputValue)/parseFloat(item.desc)) |
| 175 | + let count = list.reduce((accumulator, obj) => { | ||
| 176 | + return item.aid === obj.aid&&obj.checked ? accumulator + 1 : accumulator; | ||
| 177 | + }, 0); | ||
| 178 | + if (item.accumulationShow == "0" || ((remaining - item.price < 0) && !item.checked)||(!item.checked&&count>=kyNum)) { | ||
| 176 | item.showCpupon = '0'; | 179 | item.showCpupon = '0'; |
| 177 | } else if (item.accumulationShow == "1" && item.canCheck == '1') { | 180 | } else if (item.accumulationShow == "1" && item.canCheck == '1') { |
| 178 | item.showCpupon = '1'; | 181 | item.showCpupon = '1'; |
| @@ -208,10 +211,10 @@ Page({ | @@ -208,10 +211,10 @@ Page({ | ||
| 208 | }) | 211 | }) |
| 209 | let price = 0; | 212 | let price = 0; |
| 210 | list.forEach(i => { | 213 | list.forEach(i => { |
| 211 | - price += i.price * 1 | 214 | + price += parseFloat(i.price) |
| 212 | }) | 215 | }) |
| 213 | console.log('优惠金额----', price); | 216 | console.log('优惠金额----', price); |
| 214 | - let val = this.data.inputValue * 1; | 217 | + let val = parseFloat(this.data.inputValue); |
| 215 | let actualPrice = (val - price).toFixed(2) | 218 | let actualPrice = (val - price).toFixed(2) |
| 216 | price = price.toFixed(2) | 219 | price = price.toFixed(2) |
| 217 | this.setData({ | 220 | this.setData({ |
| @@ -354,7 +357,7 @@ Page({ | @@ -354,7 +357,7 @@ Page({ | ||
| 354 | selectedValue = todayArr.selectMor; | 357 | selectedValue = todayArr.selectMor; |
| 355 | } | 358 | } |
| 356 | selectedValue = !selectedValue | 359 | selectedValue = !selectedValue |
| 357 | - console.log(coupon.aname, coupon.exp_date.week_show, `今天是 ${todayArr.title},选择值为: ${selectedValue}`); | 360 | + // console.log(coupon.aname, coupon.exp_date.week_show, `今天是 ${todayArr.title},选择值为: ${selectedValue}`); |
| 358 | // 根据 isshow 判断法定假日是否可用 | 361 | // 根据 isshow 判断法定假日是否可用 |
| 359 | if (obj.isshow === "1") { | 362 | if (obj.isshow === "1") { |
| 360 | if (obj.can === "晚餐") { | 363 | if (obj.can === "晚餐") { |
| @@ -366,7 +369,7 @@ Page({ | @@ -366,7 +369,7 @@ Page({ | ||
| 366 | } | 369 | } |
| 367 | str = !str | 370 | str = !str |
| 368 | } | 371 | } |
| 369 | - console.log(coupon.aname, '法定节假期', `,选择值为: ${str}`); | 372 | + // console.log(coupon.aname, '法定节假期', `,选择值为: ${str}`); |
| 370 | } | 373 | } |
| 371 | let availableDate = true; | 374 | let availableDate = true; |
| 372 | if (str || selectedValue) { | 375 | if (str || selectedValue) { |
| @@ -401,28 +404,43 @@ Page({ | @@ -401,28 +404,43 @@ Page({ | ||
| 401 | let finalList = []; //最终用的优惠券组合 | 404 | let finalList = []; //最终用的优惠券组合 |
| 402 | // 计算可叠加的组合 | 405 | // 计算可叠加的组合 |
| 403 | let maxDiscount = 0; | 406 | let maxDiscount = 0; |
| 404 | - | ||
| 405 | - function backtrack(startIndex, currentCombination, currentDiscount, remainingValue) { | ||
| 406 | - // 如果当前组合的优惠大于最大优惠且剩余金额不小于0,更新最大优惠和最佳组合 | ||
| 407 | - if (currentDiscount > maxDiscount && remainingValue >= 0) { | ||
| 408 | - maxDiscount = currentDiscount; | 407 | + let numIndex=0 |
| 408 | + let closestSum = -Infinity; // 最接近或等于输入值的和 | ||
| 409 | + function backtrack(startIndex, currentCombination, currentSum,couponCount) { | ||
| 410 | + // 如果当前和等于输入值,直接返回 | ||
| 411 | + if (currentSum == amount) { | ||
| 412 | + closestSum = currentSum; | ||
| 413 | + list1 = [...currentCombination]; | ||
| 414 | + return; | ||
| 415 | + } | ||
| 416 | + // 如果当前和小于等于输入值且更接近输入值,更新最优解 | ||
| 417 | + if (currentSum <= amount && currentSum > closestSum) { | ||
| 418 | + closestSum = currentSum; | ||
| 409 | list1 = [...currentCombination]; | 419 | list1 = [...currentCombination]; |
| 410 | } | 420 | } |
| 421 | + // 遍历数组 | ||
| 411 | for (let i = startIndex; i < validCoupons.length; i++) { | 422 | for (let i = startIndex; i < validCoupons.length; i++) { |
| 412 | - const coupon = validCoupons[i]; | ||
| 413 | - if (coupon.accumulationShow == '1') { // 仅考虑可叠加的优惠券 | ||
| 414 | - const discountAmount = parseFloat(coupon.price); | ||
| 415 | - if (remainingValue - discountAmount >= 0) { // 确保剩余金额足够 | ||
| 416 | - currentCombination.push(coupon); | ||
| 417 | - backtrack(i + 1, currentCombination, currentDiscount + discountAmount, remainingValue - discountAmount); // 递归时从 i + 1 开始 | ||
| 418 | - currentCombination.pop(); // 回溯 | ||
| 419 | - } | 423 | + const obj = validCoupons[i]; |
| 424 | + if (obj.accumulationShow == '1') { // 仅考虑可叠加的优惠券 | ||
| 425 | + if (currentSum*1 + obj.price*1 > amount) continue; // 如果当前和已经超过输入值,跳过 | ||
| 426 | + let kyNum = Math.floor(amount/parseFloat(obj.desc)) | ||
| 427 | + if (amount - obj.price*1 >= 0&&(!couponCount[obj.aid]||couponCount[obj.aid] < kyNum)) { | ||
| 428 | + couponCount[obj.aid] = (couponCount[obj.aid] || 0) + 1; // 更新使用次数 | ||
| 429 | + currentCombination.push(obj); // 添加当前数字到组合 | ||
| 430 | + backtrack(i + 1, currentCombination, currentSum*1 + obj.price*1,couponCount); // 递归 | ||
| 431 | + currentCombination.pop(); // 回溯 | ||
| 432 | + couponCount[obj.aid]--; // 回溯时减少使用次数 | ||
| 433 | + } | ||
| 434 | + // 提前退出:如果当前和加上当前数字已经小于输入值,后续的数字也会更小 | ||
| 435 | + if (currentSum*1 + obj.price*1 < amount) break; | ||
| 420 | } | 436 | } |
| 437 | + | ||
| 421 | } | 438 | } |
| 422 | } | 439 | } |
| 423 | - backtrack(0, [], 0, amount); // 开始回溯 | ||
| 424 | - console.log('最佳可叠加优惠券组合:', list1); | 440 | + const couponCount = {}; |
| 441 | + backtrack(0, [], 0,couponCount); // 开始回溯 | ||
| 425 | // 遍历有效优惠券,寻找不可叠加的优惠券 | 442 | // 遍历有效优惠券,寻找不可叠加的优惠券 |
| 443 | + let maxNonStackableValue = 0 | ||
| 426 | validCoupons.forEach(coupon => { | 444 | validCoupons.forEach(coupon => { |
| 427 | if (coupon.accumulationShow === '0') { | 445 | if (coupon.accumulationShow === '0') { |
| 428 | const discountAmount = parseFloat(coupon.price); | 446 | const discountAmount = parseFloat(coupon.price); |
| @@ -430,26 +448,22 @@ Page({ | @@ -430,26 +448,22 @@ Page({ | ||
| 430 | const finalAmount = amount - discountAmount; | 448 | const finalAmount = amount - discountAmount; |
| 431 | // 确保实际支付金额不小于0,并且是最优惠的 | 449 | // 确保实际支付金额不小于0,并且是最优惠的 |
| 432 | if (finalAmount >= 0) { | 450 | if (finalAmount >= 0) { |
| 433 | - if (!list2.length || discountAmount > parseFloat(coupon.price)) { | ||
| 434 | - list2.push({ | ||
| 435 | - ...coupon | ||
| 436 | - }); // 更新最优惠的不可叠加券 | 451 | + if (maxNonStackableValue<discountAmount) { |
| 452 | + maxNonStackableValue = discountAmount | ||
| 453 | + list2=[{...coupon}] | ||
| 437 | } | 454 | } |
| 438 | } | 455 | } |
| 439 | } | 456 | } |
| 440 | }); | 457 | }); |
| 441 | - console.log('可叠加的优惠券组合2', list1); | 458 | + console.log('可叠加优惠券组合', list1); |
| 442 | console.log('不可叠加的优惠券', list2); | 459 | console.log('不可叠加的优惠券', list2); |
| 443 | // 计算两个列表的最大优惠值 | 460 | // 计算两个列表的最大优惠值 |
| 444 | let maxStackableValue = list1.reduce((total, coupon) => total + parseFloat(coupon.price), 0); | 461 | let maxStackableValue = list1.reduce((total, coupon) => total + parseFloat(coupon.price), 0); |
| 445 | - let maxNonStackableValue = list2.reduce((total, coupon) => total + parseFloat(coupon.price), 0); | ||
| 446 | 462 | ||
| 447 | // 比较两个最大值并输出结果 | 463 | // 比较两个最大值并输出结果 |
| 448 | if (maxStackableValue > maxNonStackableValue) { | 464 | if (maxStackableValue > maxNonStackableValue) { |
| 449 | - console.log(`可叠加优惠券组合的最大优惠值更高: ${maxStackableValue}`); | ||
| 450 | finalList = list1 | 465 | finalList = list1 |
| 451 | } else if (maxStackableValue < maxNonStackableValue) { | 466 | } else if (maxStackableValue < maxNonStackableValue) { |
| 452 | - console.log(`不可叠加券的最大优惠值更高: ${maxNonStackableValue}`); | ||
| 453 | finalList = list2 | 467 | finalList = list2 |
| 454 | } else { | 468 | } else { |
| 455 | console.log(`两者的优惠值相同: ${maxStackableValue}`); | 469 | console.log(`两者的优惠值相同: ${maxStackableValue}`); |
| @@ -470,11 +484,11 @@ Page({ | @@ -470,11 +484,11 @@ Page({ | ||
| 470 | let discountPrice = 0; | 484 | let discountPrice = 0; |
| 471 | // 更新选中的优惠券 | 485 | // 更新选中的优惠券 |
| 472 | finalList.forEach(coupon => { | 486 | finalList.forEach(coupon => { |
| 473 | - let target = arr.find(obj => obj.activity_id === coupon.activity_id); | ||
| 474 | - if (target) { | ||
| 475 | - target.showCpupon = 1; | ||
| 476 | - target.checked = true; | ||
| 477 | - discountPrice += coupon.price * 1; // 使用 parseFloat 确保类型正确 | 487 | + let target = arr.findIndex(obj => obj.activity_id === coupon.activity_id); |
| 488 | + if (target>-1) { | ||
| 489 | + arr[target].showCpupon = 1; | ||
| 490 | + arr[target].checked = true; | ||
| 491 | + discountPrice += parseFloat(coupon.price); // 使用 parseFloat 确保类型正确 | ||
| 478 | } | 492 | } |
| 479 | console.log('选中的券', { | 493 | console.log('选中的券', { |
| 480 | ...coupon | 494 | ...coupon |
-
Please register or login to post a comment