|
...
|
...
|
@@ -65,7 +65,6 @@ Page({ |
|
|
|
// console.log('listlistlist',JSON.stringify(list));
|
|
|
|
// list= list.concat(monidata)
|
|
|
|
list.forEach(detail => {
|
|
|
|
detail.stringifyUse = detail.use ? JSON.stringify(detail.use) : '';
|
|
|
|
detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : '';
|
|
|
|
detail.arule = detail.arule ? util.determine(detail.arule) : '';
|
|
|
|
if (detail.arule) {
|
|
...
|
...
|
@@ -128,13 +127,13 @@ Page({ |
|
|
|
const values = e.detail.value
|
|
|
|
let arr = []
|
|
|
|
let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2
|
|
|
|
let inputValue = this.data.inputValue * 1
|
|
|
|
let inputValue = parseFloat(this.data.inputValue)
|
|
|
|
let val = 0;
|
|
|
|
for (let i = 0, lenI = list.length; i < lenI; ++i) {
|
|
|
|
list[i].checked = false;
|
|
|
|
for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
|
|
|
|
if (list[i].activity_id === values[j]) {
|
|
|
|
val += list[i].price * 1
|
|
|
|
val += parseFloat(list[i].price)
|
|
|
|
arr.push({
|
|
|
|
...list[i]
|
|
|
|
})
|
|
...
|
...
|
@@ -172,7 +171,11 @@ Page({ |
|
|
|
//选的可叠加券 其他所有的不可叠加券都不可选
|
|
|
|
if (currentCoupon == '1') {
|
|
|
|
list.forEach(item => {
|
|
|
|
if (item.accumulationShow == "0" || (remaining - item.price < 0 && !item.checked)) {
|
|
|
|
let kyNum = Math.floor(parseFloat(this.data.inputValue)/parseFloat(item.desc))
|
|
|
|
let count = list.reduce((accumulator, obj) => {
|
|
|
|
return item.aid === obj.aid&&obj.checked ? accumulator + 1 : accumulator;
|
|
|
|
}, 0);
|
|
|
|
if (item.accumulationShow == "0" || ((remaining - item.price < 0) && !item.checked)||(!item.checked&&count>=kyNum)) {
|
|
|
|
item.showCpupon = '0';
|
|
|
|
} else if (item.accumulationShow == "1" && item.canCheck == '1') {
|
|
|
|
item.showCpupon = '1';
|
|
...
|
...
|
@@ -208,10 +211,10 @@ Page({ |
|
|
|
})
|
|
|
|
let price = 0;
|
|
|
|
list.forEach(i => {
|
|
|
|
price += i.price * 1
|
|
|
|
price += parseFloat(i.price)
|
|
|
|
})
|
|
|
|
console.log('优惠金额----', price);
|
|
|
|
let val = this.data.inputValue * 1;
|
|
|
|
let val = parseFloat(this.data.inputValue);
|
|
|
|
let actualPrice = (val - price).toFixed(2)
|
|
|
|
price = price.toFixed(2)
|
|
|
|
this.setData({
|
|
...
|
...
|
@@ -354,7 +357,7 @@ Page({ |
|
|
|
selectedValue = todayArr.selectMor;
|
|
|
|
}
|
|
|
|
selectedValue = !selectedValue
|
|
|
|
console.log(coupon.aname, coupon.exp_date.week_show, `今天是 ${todayArr.title},选择值为: ${selectedValue}`);
|
|
|
|
// console.log(coupon.aname, coupon.exp_date.week_show, `今天是 ${todayArr.title},选择值为: ${selectedValue}`);
|
|
|
|
// 根据 isshow 判断法定假日是否可用
|
|
|
|
if (obj.isshow === "1") {
|
|
|
|
if (obj.can === "晚餐") {
|
|
...
|
...
|
@@ -366,7 +369,7 @@ Page({ |
|
|
|
}
|
|
|
|
str = !str
|
|
|
|
}
|
|
|
|
console.log(coupon.aname, '法定节假期', `,选择值为: ${str}`);
|
|
|
|
// console.log(coupon.aname, '法定节假期', `,选择值为: ${str}`);
|
|
|
|
}
|
|
|
|
let availableDate = true;
|
|
|
|
if (str || selectedValue) {
|
|
...
|
...
|
@@ -401,28 +404,43 @@ Page({ |
|
|
|
let finalList = []; //最终用的优惠券组合
|
|
|
|
// 计算可叠加的组合
|
|
|
|
let maxDiscount = 0;
|
|
|
|
|
|
|
|
function backtrack(startIndex, currentCombination, currentDiscount, remainingValue) {
|
|
|
|
// 如果当前组合的优惠大于最大优惠且剩余金额不小于0,更新最大优惠和最佳组合
|
|
|
|
if (currentDiscount > maxDiscount && remainingValue >= 0) {
|
|
|
|
maxDiscount = currentDiscount;
|
|
|
|
let numIndex=0
|
|
|
|
let closestSum = -Infinity; // 最接近或等于输入值的和
|
|
|
|
function backtrack(startIndex, currentCombination, currentSum,couponCount) {
|
|
|
|
// 如果当前和等于输入值,直接返回
|
|
|
|
if (currentSum == amount) {
|
|
|
|
closestSum = currentSum;
|
|
|
|
list1 = [...currentCombination];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 如果当前和小于等于输入值且更接近输入值,更新最优解
|
|
|
|
if (currentSum <= amount && currentSum > closestSum) {
|
|
|
|
closestSum = currentSum;
|
|
|
|
list1 = [...currentCombination];
|
|
|
|
}
|
|
|
|
// 遍历数组
|
|
|
|
for (let i = startIndex; i < validCoupons.length; i++) {
|
|
|
|
const coupon = validCoupons[i];
|
|
|
|
if (coupon.accumulationShow == '1') { // 仅考虑可叠加的优惠券
|
|
|
|
const discountAmount = parseFloat(coupon.price);
|
|
|
|
if (remainingValue - discountAmount >= 0) { // 确保剩余金额足够
|
|
|
|
currentCombination.push(coupon);
|
|
|
|
backtrack(i + 1, currentCombination, currentDiscount + discountAmount, remainingValue - discountAmount); // 递归时从 i + 1 开始
|
|
|
|
currentCombination.pop(); // 回溯
|
|
|
|
}
|
|
|
|
const obj = validCoupons[i];
|
|
|
|
if (obj.accumulationShow == '1') { // 仅考虑可叠加的优惠券
|
|
|
|
if (currentSum*1 + obj.price*1 > amount) continue; // 如果当前和已经超过输入值,跳过
|
|
|
|
let kyNum = Math.floor(amount/parseFloat(obj.desc))
|
|
|
|
if (amount - obj.price*1 >= 0&&(!couponCount[obj.aid]||couponCount[obj.aid] < kyNum)) {
|
|
|
|
couponCount[obj.aid] = (couponCount[obj.aid] || 0) + 1; // 更新使用次数
|
|
|
|
currentCombination.push(obj); // 添加当前数字到组合
|
|
|
|
backtrack(i + 1, currentCombination, currentSum*1 + obj.price*1,couponCount); // 递归
|
|
|
|
currentCombination.pop(); // 回溯
|
|
|
|
couponCount[obj.aid]--; // 回溯时减少使用次数
|
|
|
|
}
|
|
|
|
// 提前退出:如果当前和加上当前数字已经小于输入值,后续的数字也会更小
|
|
|
|
if (currentSum*1 + obj.price*1 < amount) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
backtrack(0, [], 0, amount); // 开始回溯
|
|
|
|
console.log('最佳可叠加优惠券组合:', list1);
|
|
|
|
const couponCount = {};
|
|
|
|
backtrack(0, [], 0,couponCount); // 开始回溯
|
|
|
|
// 遍历有效优惠券,寻找不可叠加的优惠券
|
|
|
|
let maxNonStackableValue = 0
|
|
|
|
validCoupons.forEach(coupon => {
|
|
|
|
if (coupon.accumulationShow === '0') {
|
|
|
|
const discountAmount = parseFloat(coupon.price);
|
|
...
|
...
|
@@ -430,26 +448,22 @@ Page({ |
|
|
|
const finalAmount = amount - discountAmount;
|
|
|
|
// 确保实际支付金额不小于0,并且是最优惠的
|
|
|
|
if (finalAmount >= 0) {
|
|
|
|
if (!list2.length || discountAmount > parseFloat(coupon.price)) {
|
|
|
|
list2.push({
|
|
|
|
...coupon
|
|
|
|
}); // 更新最优惠的不可叠加券
|
|
|
|
if (maxNonStackableValue<discountAmount) {
|
|
|
|
maxNonStackableValue = discountAmount
|
|
|
|
list2=[{...coupon}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log('可叠加的优惠券组合2', list1);
|
|
|
|
console.log('可叠加优惠券组合', list1);
|
|
|
|
console.log('不可叠加的优惠券', list2);
|
|
|
|
// 计算两个列表的最大优惠值
|
|
|
|
let maxStackableValue = list1.reduce((total, coupon) => total + parseFloat(coupon.price), 0);
|
|
|
|
let maxNonStackableValue = list2.reduce((total, coupon) => total + parseFloat(coupon.price), 0);
|
|
|
|
|
|
|
|
// 比较两个最大值并输出结果
|
|
|
|
if (maxStackableValue > maxNonStackableValue) {
|
|
|
|
console.log(`可叠加优惠券组合的最大优惠值更高: ${maxStackableValue}`);
|
|
|
|
finalList = list1
|
|
|
|
} else if (maxStackableValue < maxNonStackableValue) {
|
|
|
|
console.log(`不可叠加券的最大优惠值更高: ${maxNonStackableValue}`);
|
|
|
|
finalList = list2
|
|
|
|
} else {
|
|
|
|
console.log(`两者的优惠值相同: ${maxStackableValue}`);
|
|
...
|
...
|
@@ -470,11 +484,11 @@ Page({ |
|
|
|
let discountPrice = 0;
|
|
|
|
// 更新选中的优惠券
|
|
|
|
finalList.forEach(coupon => {
|
|
|
|
let target = arr.find(obj => obj.activity_id === coupon.activity_id);
|
|
|
|
if (target) {
|
|
|
|
target.showCpupon = 1;
|
|
|
|
target.checked = true;
|
|
|
|
discountPrice += coupon.price * 1; // 使用 parseFloat 确保类型正确
|
|
|
|
let target = arr.findIndex(obj => obj.activity_id === coupon.activity_id);
|
|
|
|
if (target>-1) {
|
|
|
|
arr[target].showCpupon = 1;
|
|
|
|
arr[target].checked = true;
|
|
|
|
discountPrice += parseFloat(coupon.price); // 使用 parseFloat 确保类型正确
|
|
|
|
}
|
|
|
|
console.log('选中的券', {
|
|
|
|
...coupon
|
...
|
...
|
|