优化
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
const positive = value => ['number', 'string'].includes(typeof value)
|
||||
&& String(value).trim() !== '' && Number.isFinite(Number(value)) && Number(value) > 0
|
||||
|
||||
export function frozenOuterMeasurement(item) {
|
||||
const outer = [item?.calculatedOuterLengthCm, item?.calculatedOuterWidthCm, item?.calculatedOuterHeightCm]
|
||||
if (!outer.every(positive)) throw Error('测量缺少已冻结的计费外尺寸;请准备当前基线的新样本')
|
||||
const volume = outer.reduce((total, value) => total * Number(value), 1) / 1_000_000
|
||||
return { outer: outer.map(Number), unitVolumeCbm: Math.round((volume + Number.EPSILON) * 1000) / 1000 }
|
||||
}
|
||||
|
||||
export function publishedMeasurementItemsMatch(published, platform) {
|
||||
const exactId = item => typeof item?.id === 'string' && /^[1-9]\d*$/.test(item.id)
|
||||
if (!Array.isArray(published) || !Array.isArray(platform) || platform.length === 0
|
||||
|| !published.every(exactId) || !platform.every(exactId) || published.length !== platform.length
|
||||
|| new Set(platform.map(item => String(item.id))).size !== platform.length
|
||||
|| new Set(published.map(item => String(item.id))).size !== published.length) return false
|
||||
return published.every(item => {
|
||||
const source = platform.find(row => String(row.id) === String(item.id))
|
||||
if (!source) return false
|
||||
const frozen = frozenOuterMeasurement(source)
|
||||
const numbers = { packageCount: source.packageCount, lengthCm: frozen.outer[0], widthCm: frozen.outer[1],
|
||||
heightCm: frozen.outer[2], unitVolumeCbm: source.unitVolumeCbm,
|
||||
totalVolumeCbm: source.totalVolumeCbm, volumeWeightKg: source.volumeWeightKg }
|
||||
return Object.entries(numbers).every(([key, expected]) => positive(item[key]) && positive(expected)
|
||||
&& Math.abs(Number(item[key]) - Number(expected)) <= 0.000001)
|
||||
&& String(item.chargeRuleId ?? '') === String(source.chargeRuleId ?? '')
|
||||
&& item.coefficientMode === source.coefficientMode && item.calculationRuleVersion === source.calculationRuleVersion
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user