zhangwencui
6 天以前 7619c19a67c2ac824f803090bab753fc5ea14408
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * WMS 展示格式化和表单配置
 */
 
type DecimalValue = null | number | string | undefined;
 
/** 数量小数位 */
export const QUANTITY_PRECISION = 2;
/** 金额小数位 */
export const PRICE_PRECISION = 2;
/** 重量小数位 */
export const WEIGHT_PRECISION = 3;
/** 长宽高小数位 */
export const DIMENSION_PRECISION = 1;
 
function isNullOrUndefined(value: unknown) {
  return value === null || value === undefined;
}
 
function toFiniteDecimal(value: DecimalValue) {
  if (isNullOrUndefined(value)) {
    return undefined;
  }
  if (typeof value === 'string' && value.trim() === '') {
    return undefined;
  }
  const decimalValue = typeof value === 'string' ? Number(value) : value;
  if (!Number.isFinite(decimalValue)) {
    return undefined;
  }
  return decimalValue;
}
 
function sumDecimal<T>(list: T[], getter: (item: T) => DecimalValue) {
  let sum = 0;
  for (const item of list) {
    const decimalValue = toFiniteDecimal(getter(item));
    if (decimalValue !== undefined) {
      sum += decimalValue;
    }
  }
  return sum;
}
 
/** 格式化数量 */
export function formatQuantity(value?: null | number | string) {
  const decimalValue = toFiniteDecimal(value);
  return decimalValue === undefined
    ? ''
    : decimalValue.toFixed(QUANTITY_PRECISION);
}
 
/** 格式化金额 */
export function formatPrice(value?: null | number | string) {
  const decimalValue = toFiniteDecimal(value);
  return decimalValue === undefined
    ? ''
    : decimalValue.toFixed(PRICE_PRECISION);
}
 
/** 金额四舍五入 */
export function roundPrice(value: number) {
  return Number.isFinite(value)
    ? Number(value.toFixed(PRICE_PRECISION))
    : undefined;
}
 
/** 亏损数字样式 */
export function getLossClass(value?: null | number | string) {
  const decimalValue = toFiniteDecimal(value);
  return decimalValue !== undefined && decimalValue < 0 ? 'text-red-500' : '';
}
 
/** 数量 * 单价,计算金额 */
export function multiplyPrice(quantity?: number, price?: number) {
  if (
    quantity === undefined ||
    quantity === null ||
    price === undefined ||
    price === null
  ) {
    return undefined;
  }
  return roundPrice(Number(quantity) * Number(price));
}
 
/** 金额 / 数量,反算单价 */
export function dividePrice(totalPrice?: number, quantity?: number) {
  if (totalPrice === undefined || totalPrice === null || !quantity) {
    return undefined;
  }
  return roundPrice(Number(totalPrice) / Number(quantity));
}
 
/** 汇总数量 */
export function sumQuantity<T>(list: T[], getter: (item: T) => DecimalValue) {
  return sumDecimal(list, getter);
}
 
/** 汇总金额 */
export function sumPrice<T>(list: T[], getter: (item: T) => DecimalValue) {
  return sumDecimal(list, getter);
}
 
/** 格式化汇总数量 */
export function formatSumQuantity<T>(
  list: T[],
  getter: (item: T) => DecimalValue,
) {
  return formatQuantity(sumQuantity(list, getter));
}
 
/** 格式化汇总金额 */
export function formatSumPrice<T>(
  list: T[],
  getter: (item: T) => DecimalValue,
) {
  return formatPrice(sumPrice(list, getter));
}
 
/** 格式化重量 */
export function formatWeight(value?: null | number | string) {
  const decimalValue = toFiniteDecimal(value);
  return decimalValue === undefined
    ? ''
    : decimalValue.toFixed(WEIGHT_PRECISION);
}
 
/** 格式化长宽高 */
export function formatDimension(value?: null | number | string) {
  const decimalValue = toFiniteDecimal(value);
  return decimalValue === undefined
    ? ''
    : decimalValue.toFixed(DIMENSION_PRECISION);
}
 
/** 格式化长宽高组合 */
export function formatDimensionText(
  length?: null | number | string,
  width?: null | number | string,
  height?: null | number | string,
) {
  if (
    !isNullOrUndefined(length) &&
    !isNullOrUndefined(width) &&
    !isNullOrUndefined(height)
  ) {
    return [
      formatDimension(length),
      formatDimension(width),
      formatDimension(height),
    ].join(' * ');
  }
  return [
    isNullOrUndefined(length) ? undefined : `长:${formatDimension(length)}`,
    isNullOrUndefined(width) ? undefined : `宽:${formatDimension(width)}`,
    isNullOrUndefined(height) ? undefined : `高:${formatDimension(height)}`,
  ]
    .filter(Boolean)
    .join(' ');
}