|
|
- //体积不同单位相加
- export function addTj(valueArr, unitArr) {
- let unit = ['pL', 'nL', 'uL', 'mL', 'L']
-
- //计算最小单位
- let mixIndex = unit.length - 1
- for (let i = 0; i < unitArr.length; i++) {
- let thisIndex = unit.indexOf(unitArr[i])
- mixIndex = thisIndex < mixIndex ? thisIndex : mixIndex
- }
-
- let total = 0
- for (let i = 0; i < unitArr.length; i++) {
- let thisIndex = unit.indexOf(unitArr[i])
- total = addDecimals(
- total,
- multiplyDecimals(
- parseFloat(valueArr[i]),
- Math.pow(1000, thisIndex - mixIndex)
- )
- )
- // total += parseFloat(valueArr[i]) * Math.pow(1000, thisIndex - mixIndex)
- }
- return {
- total: total,
- unit: unit[mixIndex]
- }
- }
-
- //体积不同单位相减
- export function subTj(valueArr, unitArr) {
- let unit = ['pL', 'nL', 'uL', 'mL', 'L']
-
- //计算最小单位
- let mixIndex = unit.length - 1
- for (let i = 0; i < unitArr.length; i++) {
- let thisIndex = unit.indexOf(unitArr[i])
- mixIndex = thisIndex < mixIndex ? thisIndex : mixIndex
- }
-
- let total = multiplyDecimals(
- parseFloat(valueArr[0]),
- Math.pow(1000, unit.indexOf(unitArr[0]) - mixIndex)
- )
-
- let sub = multiplyDecimals(
- parseFloat(valueArr[1]),
- Math.pow(1000, unit.indexOf(unitArr[1]) - mixIndex)
- )
- return {
- total: subDecimals(total, sub),
- unit: unit[mixIndex]
- }
- }
-
- export function addDecimals(a, b) {
- if (Number.isNaN(a)) {
- a = 0
- }
- if (Number.isNaN(b)) {
- b = 0
- }
- const strA = a.toString()
- const strB = b.toString()
-
- // 获取小数位数
- const getDecimals = (str) => (str.split('.')[1] || '').length
- const maxLen = Math.max(getDecimals(strA), getDecimals(strB))
-
- // 相加并格式化为字符串
- const result = (parseFloat(a) + parseFloat(b)).toFixed(maxLen)
-
- // 去掉末尾的0
- return result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
- }
-
- export function subDecimals(a, b) {
- if (Number.isNaN(a)) {
- a = 0
- }
- if (Number.isNaN(b)) {
- b = 0
- }
- const strA = a.toString()
- const strB = b.toString()
-
- // 获取小数位数
- const getDecimals = (str) => (str.split('.')[1] || '').length
- const maxLen = Math.max(getDecimals(strA), getDecimals(strB))
-
- // 相加并格式化为字符串
- const result = (parseFloat(a) - parseFloat(b)).toFixed(maxLen)
-
- // 去掉末尾的0
- return result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
- }
-
- export function multiplyDecimals(a, b) {
- if (Number.isNaN(a) || Number.isNaN(b)) {
- return 0
- }
- const strA = a.toString()
- const strB = b.toString()
-
- const decA = (strA.split('.')[1] || '').length
- const decB = (strB.split('.')[1] || '').length
- const totalDecimals = decA + decB
-
- const numA = BigInt(strA.replace('.', ''))
- const numB = BigInt(strB.replace('.', ''))
-
- const product = numA * numB
- const productStr = product.toString()
-
- const isNegative = productStr.startsWith('-')
- const absStr = isNegative ? productStr.substring(1) : productStr
-
- let result = ''
- if (totalDecimals === 0) {
- result = absStr
- } else if (absStr.length <= totalDecimals) {
- result = '0.' + absStr.padStart(totalDecimals, '0')
- } else {
- result =
- absStr.substring(0, absStr.length - totalDecimals) +
- '.' +
- absStr.substring(absStr.length - totalDecimals)
- }
-
- result = result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
- return (isNegative ? '-' : '') + result
- }
-
- //模板物资去重
- export function uniqeResource(oldList, newList) {
- for (var i = 0; i < newList.length; i++) {
- let _index = _.findIndex(oldList, function (item) {
- return (
- item.bh == newList[i].bh &&
- (item.type == newList[i].type ||
- item.elnType == newList[i].type ||
- item.elnType == newList[i].elnType ||
- item.type == newList[i].elnType)
- )
- })
- if (_index > -1) {
- const { total, unit } = addTj(
- [oldList[_index].syl, newList[i].syl],
- [oldList[_index].syldw, newList[i].syldw]
- )
- oldList[_index].syl = total
- oldList[_index].syldw = unit
- } else {
- oldList.push(newList[i])
- }
- }
- return oldList
- }
- //模板物资去重
- export function uniqeResourceOne(newList) {
- let resultList = []
- for (var i = 0; i < newList.length; i++) {
- let _index = _.findIndex(resultList, function (item) {
- return (
- item.bh == newList[i].bh &&
- (item.type == newList[i].type ||
- item.elnType == newList[i].type ||
- item.elnType == newList[i].elnType ||
- item.type == newList[i].elnType)
- )
- })
- if (_index > -1) {
- const { total, unit } = addTj(
- [resultList[_index].syl, newList[i].syl],
- [resultList[_index].syldw, newList[i].syldw]
- )
- resultList[_index].syl = total
- resultList[_index].syldw = unit
- } else {
- resultList.push(newList[i])
- }
- }
- return resultList
- }
|