export function isTui(item) { return item.status == "return" || item.status == "refund" || item.status == "refunding"; } //是否使用会员价 export function isUseVipPrice(vipUser, goods) { return vipUser.id && vipUser.isVip; } //计算商品券优惠价格 export function returnProductCouponPrice(coup, goodsArr, vipUser) { const item = goodsArr.find((v) => v.productId == coup.proId); if (!item) { return 0; } const discountSaleAmount = item.discountSaleAmount * 1; if (discountSaleAmount > 0) { return discountSaleAmount * coup.num; } const memberPrice = item.memberPrice ? item.memberPrice : item.price; const price = item ? (isUseVipPrice(vipUser, item) ? memberPrice : item.price) : 0; return price * coup.num; } //返回新的商品列表,过滤掉退菜的,退单的商品 export function returnNewGoodsList(arr) { let goodsMap = {}; return arr.filter((v) => !isTui(v)); } //根据当前购物车商品以及数量,已选券对应商品数量,判断该商品券是否可用 export function returnCoupCanUse(goodsArr = [], coup, selCoupArr = []) { // if(!coup.use){ // return false // } const findGoods = goodsArr.filter((v) => v.productId == coup.proId); if (!findGoods.length) { return false; } const findGoodsTotalNumber = findGoods.reduce((prve, cur) => { const num = cur.num - cur.refundNum - cur.returnNum; return prve + (num <= 0 ? 0 : num); }, 0); const selCoupNumber = selCoupArr .filter((v) => v.proId == coup.proId) .reduce((prve, cur) => { return prve + cur.num * 1; }, 0); if (selCoupNumber >= findGoodsTotalNumber) { return false; } return findGoodsTotalNumber < selCoupNumber ? false : true; } //查找购物车商品根据购物车商品数据返回商品券信息(抵扣价格以及是否满足可用需求) export function returnProductCoupon(coup, goodsArr, vipUser, selCoupArr = []) { const newGoodsArr = returnNewGoodsList(goodsArr); const item = newGoodsArr.find((v) => v.productId == coup.proId); if (!item) { return { ...coup, discountAmount: 0, use: false, }; } const discountSaleAmount = item.discountSaleAmount * 1; const memberPrice = item.memberPrice ? item.memberPrice : item.price; let price = item ? (isUseVipPrice(vipUser, item) ? memberPrice : item.price) : 0; price = discountSaleAmount > 0 ? discountSaleAmount : price; const discountAmount = (price * coup.num).toFixed(2); // const canUse = !coup.use ? false : (discountAmount > 0 && returnCoupCanUse(goodsArr, coup, selCoupArr)) // const canUse=discountAmount>0 const canUse = coup.use; return { ...coup, discountAmount: discountAmount, use: canUse, }; } /** * 根据购物车商品计算商品券抵扣价格以及是否满足可用需求 * 1.商品券对应商品数量大于购物车对应商品数量不可用 * 2.未在购物车找到相关商品不可用 * @param {*} coupArr * @param {*} goodsArr * @param {*} vipUser * @returns */ export function returnProductAllCoup(coupArr, goodsArr, vipUser) { return coupArr.map((v) => { return returnProductCoupon(v, goodsArr, vipUser); }); } //返回商品实际支付价格 export function returnProductPayPrice(goods, vipUser) { const discountSaleAmount = goods.discountSaleAmount * 1; const memberPrice = goods.memberPrice ? goods.memberPrice : goods.price; const price = isUseVipPrice(vipUser, goods) ? memberPrice : goods.price; if (discountSaleAmount) { return discountSaleAmount; } return price; } //返回商品券抵扣的商品价格 export function returnProductCoupAllPrice(productPriceArr, startIndex, num = 1, isVip = true) { num = num || 1; return productPriceArr.slice(startIndex, startIndex + num).reduce((prve, cur) => { let curPrice = 0; if (typeof cur === "object") { if (cur.discountSaleAmount * 1 > 0) { curPrice = cur.discountSaleAmount * 1; } else { curPrice = isVip ? cur.memberPrice * 1 : cur.price; } } else { curPrice = cur * 1; } return prve + curPrice; }, 0); } //返回商品券可抵扣的商品数量 export function returnProductCanUseNum(productPriceArr, startIndex, num) { let n = 0; for (let i = 0; i < num; i++) { if (productPriceArr[startIndex * 1 + i]) { n += 1; } else { break; } } return n; } //返回同类商品券在同类商品价格数组里的开始位置 export function returnProCoupStartIndex(coupArr, index) { return coupArr.slice(0, index).reduce((prve, cur) => { return prve + cur.num * 1; }, 0); } //返回商品数量从0到n每一个对应的价格对照表 export function returnGoodsPayPriceMap(goodsArr) { return goodsArr.reduce((prve, cur) => { if (!prve.hasOwnProperty(cur.productId)) { prve[cur.productId] = []; } const n = cur.num - cur.returnNum; const arr = new Array(n <= 0 ? 0 : n).fill(cur).map((v) => { return { memberPrice: v.memberPrice ? v.memberPrice : v.price, price: v.price, discountSaleAmount: v.discountSaleAmount * 1, }; }); prve[cur.productId].push(...arr); return prve; }, {}); } //计算商品券总优惠价格 export function returnProductCouponAllPrice(coupArr, goodsArr, vipUser) { if (coupArr.length == 0) { return 0; } //商品分组 const goodsMap = {}; //商品数量从0到n每一个对应的价格 const goodsPayPriceMap = {}; //商品券分组 let coupMap = {}; for (let i in coupArr) { const coup = coupArr[i]; if (coupMap.hasOwnProperty(coup.proId)) { coupMap[coup.proId].push(coup); } else { coupMap[coup.proId] = [coup]; } } let total = 0; for (let key in coupMap) { const arr = coupMap[key]; for (let i in arr) { const coup = arr[i]; if (!goodsMap.hasOwnProperty(coup.proId)) { goodsMap[coup.proId] = goodsArr .filter((v) => v.productId == coup.proId) .map((v) => { return { ...v, payPrice: returnProductPayPrice(v, vipUser), }; }) .sort((a, b) => { const aPrice = a.payPrice; const bPrice = b.payPrice; return aPrice - bPrice; }); goodsPayPriceMap[coup.proId] = goodsMap[coup.proId].reduce((prve, cur) => { const arr = new Array(cur.num).fill(cur.payPrice); prve.push(...arr); return prve; }, []); } const proCoupStartIndex = returnProCoupStartIndex(arr, i); const coupNum = Math.min(goodsPayPriceMap[coup.proId].length, coup.num); total += returnProductCoupAllPrice(goodsPayPriceMap[coup.proId], proCoupStartIndex, coupNum); } } return total.toFixed(2); } //计算满减券总优惠价格 export function returnFullReductionCouponAllPrice(coupArr) { if (coupArr.length == 0) { return 0; } return coupArr .filter((v) => v.type == 1) .reduce((a, b) => { const price = b.discountAmount; return a + price; }, 0) .toFixed(2); } //计算优惠券总价格 export function returnCouponAllPrice(coupArr, goodsArr, vipUser) { const poductAllprice = returnProductCouponAllPrice(coupArr, goodsArr, vipUser); const pointAllPrice = returnFullReductionCouponAllPrice(coupArr); return (poductAllprice * 1 + pointAllPrice * 1).toFixed(2); } //返回当前满减券列表可用状态 export function returnCanUseFullReductionCoupon(coupArr, payPrice, selCoup) { return coupArr .map((v) => { if (v.id == selCoup.id) { return { ...v, use: true }; } const isfullAmount = payPrice * 1 >= v.fullAmount * 1; if (payPrice <= 0) { return { ...v, use: false, }; } return { ...v, use: v.use && isfullAmount, }; }) .filter((v) => v.use); } //根据商品数量还有商品券数量返回优惠券可以使用的数量数组 export function returnCanUseNumProductCoup(coupArr) { let productCoup = coupArr.filter((v) => v.type == 2); //商品券分组 let coupMap = {}; for (let i in productCoup) { const coup = productCoup[i]; if (coupMap.hasOwnProperty(coup.proId)) { coupMap[coup.proId].push(coup); } else { coupMap[coup.proId] = [coup]; } } return arr; }