Commit da95b8da by 张立顺

fix 订单最低价格校验

1 parent fec45f90
...@@ -456,14 +456,14 @@ Component({ ...@@ -456,14 +456,14 @@ Component({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item [`orderInfo.vdQuotationGoodsList[${index}]`]: item
}) })
} }
this.setGoodsListTotal() this.setGoodsListTotal(false)
}, },
priceBlur(e) { priceBlur(e) {
let { index, childind } = e.currentTarget.dataset let { index, childind } = e.currentTarget.dataset
let thisPage = wx.$router.getPageInstance() let thisPage = wx.$router.getPageInstance()
let { orderInfo } = thisPage.data let { orderInfo } = thisPage.data
let item = orderInfo.vdQuotationGoodsList[index] let item = orderInfo.vdQuotationGoodsList[index]
console.log( index, childind,item,getProductStrategy(item))
if (childind !== undefined) { if (childind !== undefined) {
const subItem = item.packageGoods.goodsList[childind] const subItem = item.packageGoods.goodsList[childind]
subItem.priceInput = null subItem.priceInput = null
...@@ -473,9 +473,38 @@ Component({ ...@@ -473,9 +473,38 @@ Component({
item.goods.priceInput = null item.goods.priceInput = null
} }
const strategy = getProductStrategy(item)
if (strategy) {
if (childind !== undefined && strategy.getSubItemData) {
const subItem = strategy.getSubItemData(childind)
const currentPrice =
subItem.basePrice !== undefined
? subItem.basePrice
: subItem.sellPrice !== undefined
? subItem.sellPrice
: subItem.price
if (!currentPrice && currentPrice !== 0 && subItem.validMinSellPrice === 'Y') {
strategy.updateSubItemPrice(childind, subItem.minSellPrice)
}
} else {
const currentPrice = strategy.getPrice()
const data = strategy.getData()
if (!currentPrice && currentPrice !== 0 && data.validMinSellPrice === 'Y') {
const minPrice = strategy.getMinPrice()
strategy.setPrice(minPrice)
}
}
const validation = strategy.performValidation()
console.log(validation,'performValidation')
if (!validation.valid) {
wx.$util.showToast(validation.message)
}
}
thisPage.setData({ thisPage.setData({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item [`orderInfo.vdQuotationGoodsList[${index}]`]: item
}) })
this.setGoodsListTotal()
}, },
//多规格单价修改 //多规格单价修改
changeSpecific(e) { changeSpecific(e) {
...@@ -517,7 +546,7 @@ Component({ ...@@ -517,7 +546,7 @@ Component({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item [`orderInfo.vdQuotationGoodsList[${index}]`]: item
}) })
} }
this.setGoodsListTotal() this.setGoodsListTotal(false)
}, },
changePricePackage(e) { changePricePackage(e) {
let { value } = e.detail let { value } = e.detail
...@@ -561,7 +590,7 @@ Component({ ...@@ -561,7 +590,7 @@ Component({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item [`orderInfo.vdQuotationGoodsList[${index}]`]: item
}) })
} }
this.setGoodsListTotal() this.setGoodsListTotal(false)
return value return value
}, },
delGoodsList(e) { delGoodsList(e) {
...@@ -584,7 +613,6 @@ Component({ ...@@ -584,7 +613,6 @@ Component({
const strategy = getProductStrategy(item) const strategy = getProductStrategy(item)
if (strategy) { if (strategy) {
strategy.recalculatePrices() strategy.recalculatePrices()
strategy.performValidation()
total = NP.plus(total, strategy.calculateSubtotal()) total = NP.plus(total, strategy.calculateSubtotal())
// Sync package price if it's a package // Sync package price if it's a package
......
...@@ -1414,7 +1414,7 @@ Page({ ...@@ -1414,7 +1414,7 @@ Page({
//套餐 //套餐
if (ele.goodsFlag == 3) { if (ele.goodsFlag == 3) {
let item = ele.packageGoods; let item = ele.packageGoods;
console.log(item, "套餐商品"); // console.log(item, "套餐商品");
ele.packageGoods.goodsList = item.goodsList.map( ele.packageGoods.goodsList = item.goodsList.map(
(item) => { (item) => {
item.price = this.multipliedByHundred( item.price = this.multipliedByHundred(
......
...@@ -73,7 +73,7 @@ ProductStrategy.prototype.validatePrice = function (price) { ...@@ -73,7 +73,7 @@ ProductStrategy.prototype.validatePrice = function (price) {
if ( if (
data.validMinSellPrice === 'Y' && data.validMinSellPrice === 'Y' &&
Number(checkPrice) < Number(data.minSellPrice) NP.divide(Number(checkPrice), 100) < Number(data.minSellPrice)
) { ) {
return { valid: false, message: '单价不可低于商品最低价' } return { valid: false, message: '单价不可低于商品最低价' }
} }
...@@ -162,9 +162,14 @@ ProductStrategy.prototype.setError = function (isError) { ...@@ -162,9 +162,14 @@ ProductStrategy.prototype.setError = function (isError) {
const data = this.getData() const data = this.getData()
if (data) { if (data) {
data.errorTarget = isError data.errorTarget = isError
data.checkError = isError
} }
} }
ProductStrategy.prototype.clearError = function () {
this.setError(false)
}
ProductStrategy.prototype.performValidation = function () { ProductStrategy.prototype.performValidation = function () {
// 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice()) // 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice())
// 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红 // 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红
...@@ -272,16 +277,28 @@ PackageProductStrategy.prototype.performValidation = function () { ...@@ -272,16 +277,28 @@ PackageProductStrategy.prototype.performValidation = function () {
this.item.packageGoods.goodsList.forEach(subItem => { this.item.packageGoods.goodsList.forEach(subItem => {
if ( if (
subItem.validMinSellPrice === 'Y' && subItem.validMinSellPrice === 'Y' &&
Number(subItem.price) < Number(subItem.minSellPrice) NP.divide(Number(subItem.price), 100) < Number(subItem.minSellPrice)
) { ) {
subItem.errorTarget = true subItem.errorTarget = true
subItem.checkError = true
hasError = true hasError = true
} else { } else {
subItem.errorTarget = false subItem.errorTarget = false
subItem.checkError = false
} }
}) })
} }
return { valid: !hasError } return { valid: !hasError, message: hasError ? '部分商品价格低于最低价' : '' }
}
PackageProductStrategy.prototype.clearError = function () {
ProductStrategy.prototype.clearError.call(this)
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach(subItem => {
subItem.errorTarget = false
subItem.checkError = false
})
}
} }
PackageProductStrategy.prototype.recalculatePrices = function () { PackageProductStrategy.prototype.recalculatePrices = function () {
......
...@@ -651,12 +651,14 @@ Page({ ...@@ -651,12 +651,14 @@ Page({
// 以及 validateSubItemPrice 方法进行价格合规性校验(如最低限价检查)。 // 以及 validateSubItemPrice 方法进行价格合规性校验(如最低限价检查)。
// 这样做避免了在业务层直接操作复杂的嵌套数据结构,提高了代码的可读性和可维护性。 // 这样做避免了在业务层直接操作复杂的嵌套数据结构,提高了代码的可读性和可维护性。
if (strategy && strategy.updateSubItemPrice) { if (strategy && strategy.updateSubItemPrice) {
const validation = strategy.validateSubItemPrice(item, number) // 移除输入时的校验,改为失焦校验
if (!validation.valid) { // const validation = strategy.validateSubItemPrice(item, number)
wx.$util.showToast(validation.message) // if (!validation.valid) {
// 强制赋值最低价格 // wx.$util.showToast(validation.message)
number = item.minSellPrice // // 强制赋值最低价格
} // number = item.minSellPrice
// }
strategy.clearError()
// 如果价格有变化,清空折扣输入框 (仅针对折扣单和组合单) // 如果价格有变化,清空折扣输入框 (仅针对折扣单和组合单)
if ( if (
...@@ -696,12 +698,14 @@ Page({ ...@@ -696,12 +698,14 @@ Page({
// 业务层无需关心具体商品类型的内部数据结构差异,实现了逻辑解耦。 // 业务层无需关心具体商品类型的内部数据结构差异,实现了逻辑解耦。
const strategy = getProductStrategy(goodsList[index]) const strategy = getProductStrategy(goodsList[index])
if (strategy) { if (strategy) {
const validation = strategy.validatePrice(number) // 移除输入时的校验,改为失焦校验
if (!validation.valid) { // const validation = strategy.validatePrice(number)
wx.$util.showToast(validation.message) // if (!validation.valid) {
// 强制赋值最低价格 // wx.$util.showToast(validation.message)
number = strategy.getMinPrice() // // 强制赋值最低价格
} // number = strategy.getMinPrice()
// }
strategy.clearError()
if ( if (
this.data.orderObj.djOrderType == ORDER_TYPE.DISCOUNT || this.data.orderObj.djOrderType == ORDER_TYPE.DISCOUNT ||
...@@ -727,21 +731,47 @@ Page({ ...@@ -727,21 +731,47 @@ Page({
return number return number
}, },
priceBlur(e) { priceBlur(e) {
let { index } = e.currentTarget.dataset || e.detail console.log(e,'priceBlur')
let { index, gindex } = e.detail
let { goodsList } = this.data let { goodsList } = this.data
// 策略模式应用: // 策略模式应用:
// 获取策略实例,利用 strategy.getPrice() 和 strategy.getMinPrice() 统一接口访问商品价格数据。 // 获取策略实例,利用 strategy.getPrice() 和 strategy.getMinPrice() 统一接口访问商品价格数据。
// 当输入价格为空时,自动恢复为最低售价,保证数据完整性。 // 当输入价格为空时,自动恢复为最低售价,保证数据完整性。
const strategy = getProductStrategy(goodsList[index]) const strategy = getProductStrategy(goodsList[index])
console.log(strategy,goodsList[index],index,'priceBlur')
if (strategy) { if (strategy) {
const currentPrice = strategy.getPrice() if (gindex !== undefined && gindex !== null && strategy.getSubItemData) {
// 如果价格无效(空字符串等),重置为最低价 // 套餐子商品处理
if (!currentPrice && currentPrice !== 0) { const subItem = strategy.getSubItemData(gindex)
const minPrice = strategy.getMinPrice() const currentPrice =
strategy.setPrice(minPrice) subItem.basePrice !== undefined
this.setGoodsTotal(goodsList) ? subItem.basePrice
: subItem.sellPrice !== undefined
? subItem.sellPrice
: subItem.price
if (!currentPrice && currentPrice !== 0 && subItem.validMinSellPrice === 'Y') {
strategy.updateSubItemPrice(gindex, subItem.minSellPrice)
}
} else {
// 普通/多规格商品处理
const currentPrice = strategy.getPrice()
const data = strategy.getData()
// 如果价格无效(空字符串等),重置为最低价
if (!currentPrice && currentPrice !== 0 && data.validMinSellPrice === 'Y') {
const minPrice = strategy.getMinPrice()
strategy.setPrice(minPrice)
}
}
// 触发校验逻辑(显示红框)
// performValidation 会自动处理套餐内所有子商品的校验
const validation = strategy.performValidation()
if (!validation.valid) {
wx.$util.showToast(validation.message)
} }
this.setGoodsTotal(goodsList)
} }
}, },
setGoodsTotal(goodsList) { setGoodsTotal(goodsList) {
...@@ -754,7 +784,7 @@ Page({ ...@@ -754,7 +784,7 @@ Page({
// 从而消除了原有的大量 if-else 判断,使总价计算逻辑清晰简洁。 // 从而消除了原有的大量 if-else 判断,使总价计算逻辑清晰简洁。
const strategy = getProductStrategy(item) const strategy = getProductStrategy(item)
if (strategy) { if (strategy) {
strategy.performValidation() // strategy.performValidation() // 移除此处校验,避免输入时触发
total = NP.plus(total, strategy.calculateSubtotal()) total = NP.plus(total, strategy.calculateSubtotal())
} }
}) })
......
...@@ -566,6 +566,7 @@ value="{{ orderObj.remark }}" ...@@ -566,6 +566,7 @@ value="{{ orderObj.remark }}"
bind:discountValueInput="inputDiscountValue" bind:discountValueInput="inputDiscountValue"
bind:toggleGift="toggleGift" bind:toggleGift="toggleGift"
bind:showPopup="showPopup" bind:showPopup="showPopup"
bind:priceBlur="priceBlur"
data-item="{{ g }}" data-item="{{ g }}"
data-gindex="{{ gInx }}" data-gindex="{{ gInx }}"
data-index="{{ index }}" data-index="{{ index }}"
......
...@@ -151,9 +151,14 @@ ProductStrategy.prototype.setError = function (isError) { ...@@ -151,9 +151,14 @@ ProductStrategy.prototype.setError = function (isError) {
const data = this.getData() const data = this.getData()
if (data) { if (data) {
data.errorTarget = isError data.errorTarget = isError
data.checkError = isError
} }
} }
ProductStrategy.prototype.clearError = function () {
this.setError(false)
}
ProductStrategy.prototype.performValidation = function () { ProductStrategy.prototype.performValidation = function () {
// 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice()) // 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice())
// 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红 // 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红
...@@ -268,13 +273,25 @@ PackageProductStrategy.prototype.performValidation = function () { ...@@ -268,13 +273,25 @@ PackageProductStrategy.prototype.performValidation = function () {
Number(checkPrice) < Number(subItem.minSellPrice) Number(checkPrice) < Number(subItem.minSellPrice)
) { ) {
subItem.errorTarget = true subItem.errorTarget = true
subItem.checkError = true
hasError = true hasError = true
} else { } else {
subItem.errorTarget = false subItem.errorTarget = false
subItem.checkError = false
} }
}) })
} }
return { valid: !hasError } return { valid: !hasError, message: hasError ? '部分商品价格低于最低价' : '' }
}
PackageProductStrategy.prototype.clearError = function () {
ProductStrategy.prototype.clearError.call(this)
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach(subItem => {
subItem.errorTarget = false
subItem.checkError = false
})
}
} }
PackageProductStrategy.prototype.getSubItemData = function (index) { PackageProductStrategy.prototype.getSubItemData = function (index) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!