华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.8 KiB

  1. //体积不同单位相加
  2. export function addTj(valueArr, unitArr) {
  3. let unit = ['pL', 'nL', 'uL', 'mL', 'L']
  4. //计算最小单位
  5. let mixIndex = unit.length - 1
  6. for (let i = 0; i < unitArr.length; i++) {
  7. let thisIndex = unit.indexOf(unitArr[i])
  8. mixIndex = thisIndex < mixIndex ? thisIndex : mixIndex
  9. }
  10. let total = 0
  11. for (let i = 0; i < unitArr.length; i++) {
  12. let thisIndex = unit.indexOf(unitArr[i])
  13. total = addDecimals(
  14. total,
  15. multiplyDecimals(
  16. parseFloat(valueArr[i]),
  17. Math.pow(1000, thisIndex - mixIndex)
  18. )
  19. )
  20. // total += parseFloat(valueArr[i]) * Math.pow(1000, thisIndex - mixIndex)
  21. }
  22. return {
  23. total: total,
  24. unit: unit[mixIndex]
  25. }
  26. }
  27. //体积不同单位相减
  28. export function subTj(valueArr, unitArr) {
  29. let unit = ['pL', 'nL', 'uL', 'mL', 'L']
  30. //计算最小单位
  31. let mixIndex = unit.length - 1
  32. for (let i = 0; i < unitArr.length; i++) {
  33. let thisIndex = unit.indexOf(unitArr[i])
  34. mixIndex = thisIndex < mixIndex ? thisIndex : mixIndex
  35. }
  36. let total = multiplyDecimals(
  37. parseFloat(valueArr[0]),
  38. Math.pow(1000, unit.indexOf(unitArr[0]) - mixIndex)
  39. )
  40. let sub = multiplyDecimals(
  41. parseFloat(valueArr[1]),
  42. Math.pow(1000, unit.indexOf(unitArr[1]) - mixIndex)
  43. )
  44. return {
  45. total: subDecimals(total, sub),
  46. unit: unit[mixIndex]
  47. }
  48. }
  49. export function addDecimals(a, b) {
  50. if (Number.isNaN(a)) {
  51. a = 0
  52. }
  53. if (Number.isNaN(b)) {
  54. b = 0
  55. }
  56. const strA = a.toString()
  57. const strB = b.toString()
  58. // 获取小数位数
  59. const getDecimals = (str) => (str.split('.')[1] || '').length
  60. const maxLen = Math.max(getDecimals(strA), getDecimals(strB))
  61. // 相加并格式化为字符串
  62. const result = (parseFloat(a) + parseFloat(b)).toFixed(maxLen)
  63. // 去掉末尾的0
  64. return result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
  65. }
  66. export function subDecimals(a, b) {
  67. if (Number.isNaN(a)) {
  68. a = 0
  69. }
  70. if (Number.isNaN(b)) {
  71. b = 0
  72. }
  73. const strA = a.toString()
  74. const strB = b.toString()
  75. // 获取小数位数
  76. const getDecimals = (str) => (str.split('.')[1] || '').length
  77. const maxLen = Math.max(getDecimals(strA), getDecimals(strB))
  78. // 相加并格式化为字符串
  79. const result = (parseFloat(a) - parseFloat(b)).toFixed(maxLen)
  80. // 去掉末尾的0
  81. return result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
  82. }
  83. export function multiplyDecimals(a, b) {
  84. if (Number.isNaN(a) || Number.isNaN(b)) {
  85. return 0
  86. }
  87. const strA = a.toString()
  88. const strB = b.toString()
  89. const decA = (strA.split('.')[1] || '').length
  90. const decB = (strB.split('.')[1] || '').length
  91. const totalDecimals = decA + decB
  92. const numA = BigInt(strA.replace('.', ''))
  93. const numB = BigInt(strB.replace('.', ''))
  94. const product = numA * numB
  95. const productStr = product.toString()
  96. const isNegative = productStr.startsWith('-')
  97. const absStr = isNegative ? productStr.substring(1) : productStr
  98. let result = ''
  99. if (totalDecimals === 0) {
  100. result = absStr
  101. } else if (absStr.length <= totalDecimals) {
  102. result = '0.' + absStr.padStart(totalDecimals, '0')
  103. } else {
  104. result =
  105. absStr.substring(0, absStr.length - totalDecimals) +
  106. '.' +
  107. absStr.substring(absStr.length - totalDecimals)
  108. }
  109. result = result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
  110. return (isNegative ? '-' : '') + result
  111. }
  112. //模板物资去重
  113. export function uniqeResource(oldList, newList) {
  114. for (var i = 0; i < newList.length; i++) {
  115. let _index = _.findIndex(oldList, function (item) {
  116. return (
  117. item.bh == newList[i].bh &&
  118. (item.type == newList[i].type ||
  119. item.elnType == newList[i].type ||
  120. item.elnType == newList[i].elnType ||
  121. item.type == newList[i].elnType)
  122. )
  123. })
  124. if (_index > -1) {
  125. const { total, unit } = addTj(
  126. [oldList[_index].syl, newList[i].syl],
  127. [oldList[_index].syldw, newList[i].syldw]
  128. )
  129. oldList[_index].syl = total
  130. oldList[_index].syldw = unit
  131. } else {
  132. oldList.push(newList[i])
  133. }
  134. }
  135. return oldList
  136. }
  137. //模板物资去重
  138. export function uniqeResourceOne(newList) {
  139. let resultList = []
  140. for (var i = 0; i < newList.length; i++) {
  141. let _index = _.findIndex(resultList, function (item) {
  142. return (
  143. item.bh == newList[i].bh &&
  144. (item.type == newList[i].type ||
  145. item.elnType == newList[i].type ||
  146. item.elnType == newList[i].elnType ||
  147. item.type == newList[i].elnType)
  148. )
  149. })
  150. if (_index > -1) {
  151. const { total, unit } = addTj(
  152. [resultList[_index].syl, newList[i].syl],
  153. [resultList[_index].syldw, newList[i].syldw]
  154. )
  155. resultList[_index].syl = total
  156. resultList[_index].syldw = unit
  157. } else {
  158. resultList.push(newList[i])
  159. }
  160. }
  161. return resultList
  162. }