@etsoo/smarterp-core 1.1.68 → 1.1.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,46 @@
1
- import { CoreUtils } from "../src";
1
+ import { CoreUtils, PeriodReportData } from "../src";
2
+ import { TestApp } from "./app/TestApp";
3
+
4
+ const app = new TestApp();
5
+
6
+ test("Test for transformReportData", () => {
7
+ const source: PeriodReportData[] = [
8
+ {
9
+ period: 202601,
10
+ value: 236
11
+ },
12
+ {
13
+ period: 202602,
14
+ value: 99
15
+ },
16
+ {
17
+ period: 202603,
18
+ value: 120
19
+ },
20
+ {
21
+ period: 202604,
22
+ value: 199
23
+ },
24
+ {
25
+ period: 202605,
26
+ value: 230
27
+ },
28
+ {
29
+ period: 202606,
30
+ value: 3
31
+ },
32
+ {
33
+ period: 202505,
34
+ value: 215
35
+ }
36
+ ];
37
+
38
+ const result = CoreUtils.transformReportData(app, source, true, 2026);
39
+
40
+ expect(result.labels.length).toBe(12);
41
+ expect(result.currentYearData[5]).toBe(3);
42
+ expect(result.lastYearData[4]).toBe(215);
43
+ });
2
44
 
3
45
  test("Test for validateJson with string schema", async () => {
4
46
  const schema =
@@ -3,51 +3,10 @@ import { isLocalTest, TestApp } from "./app/TestApp";
3
3
  import { EntityStatus } from "@etsoo/appscript";
4
4
  import { OrgApi } from "../src/OrgApi";
5
5
  import { OrgCreateRQ } from "../src/rq/org/OrgCreateRQ";
6
- import { CoreApp, PeriodReportData } from "../src";
7
6
 
8
7
  const app = new TestApp();
9
8
  await app.changeCulture(app.settings.cultures[0]);
10
9
 
11
- test("Test SysResourceKeyPrefix", () => {
12
- const source: PeriodReportData[] = [
13
- {
14
- period: 202601,
15
- value: 236
16
- },
17
- {
18
- period: 202602,
19
- value: 99
20
- },
21
- {
22
- period: 202603,
23
- value: 120
24
- },
25
- {
26
- period: 202604,
27
- value: 199
28
- },
29
- {
30
- period: 202605,
31
- value: 230
32
- },
33
- {
34
- period: 202606,
35
- value: 3
36
- },
37
- {
38
- period: 202505,
39
- value: 215
40
- }
41
- ];
42
-
43
- const core = new CoreApp(app, app.api);
44
- const result = core.transformReportData(source, true, 2026);
45
-
46
- expect(result.labels.length).toBe(12);
47
- expect(result.currentYearData[5]).toBe(3);
48
- expect(result.lastYearData[4]).toBe(215);
49
- });
50
-
51
10
  test("Test SysResourceKeyPrefix", () => {
52
11
  expect(OrgApi.SysResourceKeyPrefix).toBe("etsoo");
53
12
  });
@@ -11,7 +11,6 @@ import { CoreApiService } from "./dto/org/CoreApiService";
11
11
  import { DocumentApi } from "./DocumentApi";
12
12
  import { DocumentKind } from "./dto/document/DocumentKind";
13
13
  import { ReportApi } from "./ReportApi";
14
- import { PeriodReportData } from "./dto/report/PeriodReportData";
15
14
  type AppData = {
16
15
  id: number;
17
16
  appId?: number;
@@ -129,27 +128,6 @@ export interface ICoreApp {
129
128
  * @returns List
130
129
  */
131
130
  getIdentityFlags(identity?: number | true): ListType[];
132
- /**
133
- * Get report data
134
- * 获取报告数据
135
- * @param data Input data
136
- * @param year Year to calculate
137
- * @returns Result
138
- */
139
- getReportData(data: PeriodReportData[], year: number): number[];
140
- /**
141
- * Transform report data for chart
142
- * 转换报告数据用于图表
143
- * @param data Input data
144
- * @param hasLastYear Has last year data
145
- * @param year Year to calculate, default is current year
146
- * @returns Transformed report data
147
- */
148
- transformReportData(data: PeriodReportData[], hasLastYear?: boolean, year?: number): {
149
- labels: string[];
150
- currentYearData: number[];
151
- lastYearData: number[];
152
- };
153
131
  }
154
132
  /**
155
133
  * Core application
@@ -289,26 +267,5 @@ export declare class CoreApp implements ICoreApp {
289
267
  * @returns List
290
268
  */
291
269
  getIdentityFlags(identity?: number | true): ListType[];
292
- /**
293
- * Get report data
294
- * 获取报告数据
295
- * @param data Input data
296
- * @param year Year to calculate
297
- * @returns Result
298
- */
299
- getReportData(data: PeriodReportData[], year: number): number[];
300
- /**
301
- * Transform report data for chart
302
- * 转换报告数据用于图表
303
- * @param data Input data
304
- * @param hasLastYear Has last year data
305
- * @param year Year to calculate, default is current year
306
- * @returns Transformed report data
307
- */
308
- transformReportData(data: PeriodReportData[], hasLastYear?: boolean, year?: number): {
309
- labels: string[];
310
- currentYearData: number[];
311
- lastYearData: number[];
312
- };
313
270
  }
314
271
  export {};
@@ -229,39 +229,5 @@ class CoreApp {
229
229
  return id;
230
230
  });
231
231
  }
232
- /**
233
- * Get report data
234
- * 获取报告数据
235
- * @param data Input data
236
- * @param year Year to calculate
237
- * @returns Result
238
- */
239
- getReportData(data, year) {
240
- const [start, end] = shared_1.NumberUtils.getMonthPeriodRange(year);
241
- const items = [];
242
- for (let i = start; i <= end; i++) {
243
- const item = data.find((d) => d.period === i);
244
- items.push(item?.value ?? 0);
245
- }
246
- return items;
247
- }
248
- /**
249
- * Transform report data for chart
250
- * 转换报告数据用于图表
251
- * @param data Input data
252
- * @param hasLastYear Has last year data
253
- * @param year Year to calculate, default is current year
254
- * @returns Transformed report data
255
- */
256
- transformReportData(data, hasLastYear, year) {
257
- year ??= new Date().getFullYear();
258
- const months = appscript_1.BusinessUtils.getMonths(this.app.get("months") ?? []);
259
- const labels = months.map((m) => m.label);
260
- const currentYearData = this.getReportData(data, year);
261
- const lastYearData = hasLastYear == null || hasLastYear
262
- ? this.getReportData(data, year - 1)
263
- : [];
264
- return { labels, currentYearData, lastYearData };
265
- }
266
232
  }
267
233
  exports.CoreApp = CoreApp;
@@ -1,4 +1,7 @@
1
+ import { DataTypes } from "@etsoo/shared";
1
2
  import type { ErrorObject } from "ajv";
3
+ import { PeriodReportData } from "./dto/report/PeriodReportData";
4
+ import { IApp } from "@etsoo/appscript";
2
5
  /**
3
6
  * Core utilities
4
7
  */
@@ -10,6 +13,25 @@ export declare namespace CoreUtils {
10
13
  * @returns Styles
11
14
  */
12
15
  function avatarStyles(isOrg?: boolean): React.CSSProperties;
16
+ /**
17
+ * Get report data
18
+ * 获取报告数据
19
+ * @param data Input data
20
+ * @param year Year to calculate
21
+ * @param selector Selector function or number field to extract value from data
22
+ * @returns Result
23
+ */
24
+ function getReportDataBase<D extends {
25
+ period: number;
26
+ }>(data: D[], year: number, selector: ((item: D | undefined | null) => number | undefined | null) | DataTypes.Keys<D, number | null | undefined>): number[];
27
+ /**
28
+ * Get report data
29
+ * 获取报告数据
30
+ * @param data Input data
31
+ * @param year Year to calculate
32
+ * @returns Result
33
+ */
34
+ function getReportData(data: PeriodReportData[], year: number): number[];
13
35
  /**
14
36
  * Merge an array with another array, starting from the end
15
37
  * 合并数组,从末尾开始
@@ -17,6 +39,37 @@ export declare namespace CoreUtils {
17
39
  * @param target Target array
18
40
  */
19
41
  function mergeArray(source: string[], target: string[]): void;
42
+ /**
43
+ * Transform report data for chart
44
+ * 转换报告数据用于图表
45
+ * @param app App instance
46
+ * @param data Input data
47
+ * @param selector Selector function or number field to extract value from data
48
+ * @param hasLastYear Has last year data
49
+ * @param year Year to calculate, default is current year
50
+ * @returns Transformed report data
51
+ */
52
+ function transformReportDataBase<D extends {
53
+ period: number;
54
+ }>(app: IApp, data: D[], selector: ((item: D | undefined | null) => number | undefined | null) | DataTypes.Keys<D, number | null | undefined>, hasLastYear?: boolean, year?: number): {
55
+ labels: string[];
56
+ currentYearData: number[];
57
+ lastYearData: number[];
58
+ };
59
+ /**
60
+ * Transform report data for chart
61
+ * 转换报告数据用于图表
62
+ * @param app App instance
63
+ * @param data Input data
64
+ * @param hasLastYear Has last year data
65
+ * @param year Year to calculate, default is current year
66
+ * @returns Transformed report data
67
+ */
68
+ function transformReportData(app: IApp, data: PeriodReportData[], hasLastYear?: boolean, year?: number): {
69
+ labels: string[];
70
+ currentYearData: number[];
71
+ lastYearData: number[];
72
+ };
20
73
  /**
21
74
  * Validate JSON input against a schema
22
75
  * 验证 JSON 输入是否符合架构
@@ -4,7 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.CoreUtils = void 0;
7
+ const shared_1 = require("@etsoo/shared");
7
8
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
9
+ const appscript_1 = require("@etsoo/appscript");
8
10
  /**
9
11
  * Core utilities
10
12
  */
@@ -24,6 +26,43 @@ var CoreUtils;
24
26
  };
25
27
  }
26
28
  CoreUtils.avatarStyles = avatarStyles;
29
+ /**
30
+ * Get report data
31
+ * 获取报告数据
32
+ * @param data Input data
33
+ * @param year Year to calculate
34
+ * @param selector Selector function or number field to extract value from data
35
+ * @returns Result
36
+ */
37
+ function getReportDataBase(data, year, selector) {
38
+ const [start, end] = shared_1.NumberUtils.getMonthPeriodRange(year);
39
+ const items = [];
40
+ for (let i = start; i <= end; i++) {
41
+ const item = data.find((d) => d.period === i);
42
+ if (typeof selector === "function") {
43
+ items.push(selector(item) ?? 0);
44
+ }
45
+ else {
46
+ if (item == null)
47
+ items.push(0);
48
+ else
49
+ items.push(item[selector] ?? 0);
50
+ }
51
+ }
52
+ return items;
53
+ }
54
+ CoreUtils.getReportDataBase = getReportDataBase;
55
+ /**
56
+ * Get report data
57
+ * 获取报告数据
58
+ * @param data Input data
59
+ * @param year Year to calculate
60
+ * @returns Result
61
+ */
62
+ function getReportData(data, year) {
63
+ return getReportDataBase(data, year, "value");
64
+ }
65
+ CoreUtils.getReportData = getReportData;
27
66
  /**
28
67
  * Merge an array with another array, starting from the end
29
68
  * 合并数组,从末尾开始
@@ -38,6 +77,40 @@ var CoreUtils;
38
77
  }
39
78
  }
40
79
  CoreUtils.mergeArray = mergeArray;
80
+ /**
81
+ * Transform report data for chart
82
+ * 转换报告数据用于图表
83
+ * @param app App instance
84
+ * @param data Input data
85
+ * @param selector Selector function or number field to extract value from data
86
+ * @param hasLastYear Has last year data
87
+ * @param year Year to calculate, default is current year
88
+ * @returns Transformed report data
89
+ */
90
+ function transformReportDataBase(app, data, selector, hasLastYear, year) {
91
+ year ??= new Date().getFullYear();
92
+ const months = appscript_1.BusinessUtils.getMonths(app.get("months") ?? []);
93
+ const labels = months.map((m) => m.label);
94
+ const currentYearData = getReportDataBase(data, year, selector);
95
+ const lastYearData = hasLastYear == null || hasLastYear
96
+ ? getReportDataBase(data, year - 1, selector)
97
+ : [];
98
+ return { labels, currentYearData, lastYearData };
99
+ }
100
+ CoreUtils.transformReportDataBase = transformReportDataBase;
101
+ /**
102
+ * Transform report data for chart
103
+ * 转换报告数据用于图表
104
+ * @param app App instance
105
+ * @param data Input data
106
+ * @param hasLastYear Has last year data
107
+ * @param year Year to calculate, default is current year
108
+ * @returns Transformed report data
109
+ */
110
+ function transformReportData(app, data, hasLastYear, year) {
111
+ return transformReportDataBase(app, data, "value", hasLastYear, year);
112
+ }
113
+ CoreUtils.transformReportData = transformReportData;
41
114
  let ajv = null;
42
115
  /**
43
116
  * Validate JSON input against a schema
@@ -11,7 +11,6 @@ import { CoreApiService } from "./dto/org/CoreApiService";
11
11
  import { DocumentApi } from "./DocumentApi";
12
12
  import { DocumentKind } from "./dto/document/DocumentKind";
13
13
  import { ReportApi } from "./ReportApi";
14
- import { PeriodReportData } from "./dto/report/PeriodReportData";
15
14
  type AppData = {
16
15
  id: number;
17
16
  appId?: number;
@@ -129,27 +128,6 @@ export interface ICoreApp {
129
128
  * @returns List
130
129
  */
131
130
  getIdentityFlags(identity?: number | true): ListType[];
132
- /**
133
- * Get report data
134
- * 获取报告数据
135
- * @param data Input data
136
- * @param year Year to calculate
137
- * @returns Result
138
- */
139
- getReportData(data: PeriodReportData[], year: number): number[];
140
- /**
141
- * Transform report data for chart
142
- * 转换报告数据用于图表
143
- * @param data Input data
144
- * @param hasLastYear Has last year data
145
- * @param year Year to calculate, default is current year
146
- * @returns Transformed report data
147
- */
148
- transformReportData(data: PeriodReportData[], hasLastYear?: boolean, year?: number): {
149
- labels: string[];
150
- currentYearData: number[];
151
- lastYearData: number[];
152
- };
153
131
  }
154
132
  /**
155
133
  * Core application
@@ -289,26 +267,5 @@ export declare class CoreApp implements ICoreApp {
289
267
  * @returns List
290
268
  */
291
269
  getIdentityFlags(identity?: number | true): ListType[];
292
- /**
293
- * Get report data
294
- * 获取报告数据
295
- * @param data Input data
296
- * @param year Year to calculate
297
- * @returns Result
298
- */
299
- getReportData(data: PeriodReportData[], year: number): number[];
300
- /**
301
- * Transform report data for chart
302
- * 转换报告数据用于图表
303
- * @param data Input data
304
- * @param hasLastYear Has last year data
305
- * @param year Year to calculate, default is current year
306
- * @returns Transformed report data
307
- */
308
- transformReportData(data: PeriodReportData[], hasLastYear?: boolean, year?: number): {
309
- labels: string[];
310
- currentYearData: number[];
311
- lastYearData: number[];
312
- };
313
270
  }
314
271
  export {};
@@ -3,9 +3,9 @@ import { OrgApi } from "./OrgApi";
3
3
  import { AppApi } from "./AppApi";
4
4
  import { PublicApi } from "./PublicApi";
5
5
  import { UserApi } from "./UserApi";
6
- import { AuthApi, BusinessUtils, IdentityType, IdentityTypeFlags, UserIdentifierType } from "@etsoo/appscript";
6
+ import { AuthApi, IdentityType, IdentityTypeFlags, UserIdentifierType } from "@etsoo/appscript";
7
7
  import { AuthCodeApi } from "./AuthCodeApi";
8
- import { DataTypes, NumberUtils } from "@etsoo/shared";
8
+ import { DataTypes } from "@etsoo/shared";
9
9
  import { CoreApiService } from "./dto/org/CoreApiService";
10
10
  import { DocumentApi } from "./DocumentApi";
11
11
  import { DocumentKind } from "./dto/document/DocumentKind";
@@ -226,38 +226,4 @@ export class CoreApp {
226
226
  return id;
227
227
  });
228
228
  }
229
- /**
230
- * Get report data
231
- * 获取报告数据
232
- * @param data Input data
233
- * @param year Year to calculate
234
- * @returns Result
235
- */
236
- getReportData(data, year) {
237
- const [start, end] = NumberUtils.getMonthPeriodRange(year);
238
- const items = [];
239
- for (let i = start; i <= end; i++) {
240
- const item = data.find((d) => d.period === i);
241
- items.push(item?.value ?? 0);
242
- }
243
- return items;
244
- }
245
- /**
246
- * Transform report data for chart
247
- * 转换报告数据用于图表
248
- * @param data Input data
249
- * @param hasLastYear Has last year data
250
- * @param year Year to calculate, default is current year
251
- * @returns Transformed report data
252
- */
253
- transformReportData(data, hasLastYear, year) {
254
- year ??= new Date().getFullYear();
255
- const months = BusinessUtils.getMonths(this.app.get("months") ?? []);
256
- const labels = months.map((m) => m.label);
257
- const currentYearData = this.getReportData(data, year);
258
- const lastYearData = hasLastYear == null || hasLastYear
259
- ? this.getReportData(data, year - 1)
260
- : [];
261
- return { labels, currentYearData, lastYearData };
262
- }
263
229
  }
@@ -1,4 +1,7 @@
1
+ import { DataTypes } from "@etsoo/shared";
1
2
  import type { ErrorObject } from "ajv";
3
+ import { PeriodReportData } from "./dto/report/PeriodReportData";
4
+ import { IApp } from "@etsoo/appscript";
2
5
  /**
3
6
  * Core utilities
4
7
  */
@@ -10,6 +13,25 @@ export declare namespace CoreUtils {
10
13
  * @returns Styles
11
14
  */
12
15
  function avatarStyles(isOrg?: boolean): React.CSSProperties;
16
+ /**
17
+ * Get report data
18
+ * 获取报告数据
19
+ * @param data Input data
20
+ * @param year Year to calculate
21
+ * @param selector Selector function or number field to extract value from data
22
+ * @returns Result
23
+ */
24
+ function getReportDataBase<D extends {
25
+ period: number;
26
+ }>(data: D[], year: number, selector: ((item: D | undefined | null) => number | undefined | null) | DataTypes.Keys<D, number | null | undefined>): number[];
27
+ /**
28
+ * Get report data
29
+ * 获取报告数据
30
+ * @param data Input data
31
+ * @param year Year to calculate
32
+ * @returns Result
33
+ */
34
+ function getReportData(data: PeriodReportData[], year: number): number[];
13
35
  /**
14
36
  * Merge an array with another array, starting from the end
15
37
  * 合并数组,从末尾开始
@@ -17,6 +39,37 @@ export declare namespace CoreUtils {
17
39
  * @param target Target array
18
40
  */
19
41
  function mergeArray(source: string[], target: string[]): void;
42
+ /**
43
+ * Transform report data for chart
44
+ * 转换报告数据用于图表
45
+ * @param app App instance
46
+ * @param data Input data
47
+ * @param selector Selector function or number field to extract value from data
48
+ * @param hasLastYear Has last year data
49
+ * @param year Year to calculate, default is current year
50
+ * @returns Transformed report data
51
+ */
52
+ function transformReportDataBase<D extends {
53
+ period: number;
54
+ }>(app: IApp, data: D[], selector: ((item: D | undefined | null) => number | undefined | null) | DataTypes.Keys<D, number | null | undefined>, hasLastYear?: boolean, year?: number): {
55
+ labels: string[];
56
+ currentYearData: number[];
57
+ lastYearData: number[];
58
+ };
59
+ /**
60
+ * Transform report data for chart
61
+ * 转换报告数据用于图表
62
+ * @param app App instance
63
+ * @param data Input data
64
+ * @param hasLastYear Has last year data
65
+ * @param year Year to calculate, default is current year
66
+ * @returns Transformed report data
67
+ */
68
+ function transformReportData(app: IApp, data: PeriodReportData[], hasLastYear?: boolean, year?: number): {
69
+ labels: string[];
70
+ currentYearData: number[];
71
+ lastYearData: number[];
72
+ };
20
73
  /**
21
74
  * Validate JSON input against a schema
22
75
  * 验证 JSON 输入是否符合架构
@@ -1,4 +1,6 @@
1
+ import { NumberUtils } from "@etsoo/shared";
1
2
  import addFormats from "ajv-formats";
3
+ import { BusinessUtils } from "@etsoo/appscript";
2
4
  /**
3
5
  * Core utilities
4
6
  */
@@ -18,6 +20,43 @@ export var CoreUtils;
18
20
  };
19
21
  }
20
22
  CoreUtils.avatarStyles = avatarStyles;
23
+ /**
24
+ * Get report data
25
+ * 获取报告数据
26
+ * @param data Input data
27
+ * @param year Year to calculate
28
+ * @param selector Selector function or number field to extract value from data
29
+ * @returns Result
30
+ */
31
+ function getReportDataBase(data, year, selector) {
32
+ const [start, end] = NumberUtils.getMonthPeriodRange(year);
33
+ const items = [];
34
+ for (let i = start; i <= end; i++) {
35
+ const item = data.find((d) => d.period === i);
36
+ if (typeof selector === "function") {
37
+ items.push(selector(item) ?? 0);
38
+ }
39
+ else {
40
+ if (item == null)
41
+ items.push(0);
42
+ else
43
+ items.push(item[selector] ?? 0);
44
+ }
45
+ }
46
+ return items;
47
+ }
48
+ CoreUtils.getReportDataBase = getReportDataBase;
49
+ /**
50
+ * Get report data
51
+ * 获取报告数据
52
+ * @param data Input data
53
+ * @param year Year to calculate
54
+ * @returns Result
55
+ */
56
+ function getReportData(data, year) {
57
+ return getReportDataBase(data, year, "value");
58
+ }
59
+ CoreUtils.getReportData = getReportData;
21
60
  /**
22
61
  * Merge an array with another array, starting from the end
23
62
  * 合并数组,从末尾开始
@@ -32,6 +71,40 @@ export var CoreUtils;
32
71
  }
33
72
  }
34
73
  CoreUtils.mergeArray = mergeArray;
74
+ /**
75
+ * Transform report data for chart
76
+ * 转换报告数据用于图表
77
+ * @param app App instance
78
+ * @param data Input data
79
+ * @param selector Selector function or number field to extract value from data
80
+ * @param hasLastYear Has last year data
81
+ * @param year Year to calculate, default is current year
82
+ * @returns Transformed report data
83
+ */
84
+ function transformReportDataBase(app, data, selector, hasLastYear, year) {
85
+ year ??= new Date().getFullYear();
86
+ const months = BusinessUtils.getMonths(app.get("months") ?? []);
87
+ const labels = months.map((m) => m.label);
88
+ const currentYearData = getReportDataBase(data, year, selector);
89
+ const lastYearData = hasLastYear == null || hasLastYear
90
+ ? getReportDataBase(data, year - 1, selector)
91
+ : [];
92
+ return { labels, currentYearData, lastYearData };
93
+ }
94
+ CoreUtils.transformReportDataBase = transformReportDataBase;
95
+ /**
96
+ * Transform report data for chart
97
+ * 转换报告数据用于图表
98
+ * @param app App instance
99
+ * @param data Input data
100
+ * @param hasLastYear Has last year data
101
+ * @param year Year to calculate, default is current year
102
+ * @returns Transformed report data
103
+ */
104
+ function transformReportData(app, data, hasLastYear, year) {
105
+ return transformReportDataBase(app, data, "value", hasLastYear, year);
106
+ }
107
+ CoreUtils.transformReportData = transformReportData;
35
108
  let ajv = null;
36
109
  /**
37
110
  * Validate JSON input against a schema
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/smarterp-core",
3
- "version": "1.1.68",
3
+ "version": "1.1.69",
4
4
  "description": "TypeScript APIs for SmartERP Core",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/CoreApp.ts CHANGED
@@ -6,19 +6,17 @@ import { PublicApi } from "./PublicApi";
6
6
  import { UserApi } from "./UserApi";
7
7
  import {
8
8
  AuthApi,
9
- BusinessUtils,
10
9
  IApp,
11
10
  IdentityType,
12
11
  IdentityTypeFlags,
13
12
  UserIdentifierType
14
13
  } from "@etsoo/appscript";
15
14
  import { AuthCodeApi } from "./AuthCodeApi";
16
- import { DataTypes, ListType, ListType1, NumberUtils } from "@etsoo/shared";
15
+ import { DataTypes, ListType, ListType1 } from "@etsoo/shared";
17
16
  import { CoreApiService } from "./dto/org/CoreApiService";
18
17
  import { DocumentApi } from "./DocumentApi";
19
18
  import { DocumentKind } from "./dto/document/DocumentKind";
20
19
  import { ReportApi } from "./ReportApi";
21
- import { PeriodReportData } from "./dto/report/PeriodReportData";
22
20
 
23
21
  type AppData = { id: number; appId?: number; name: string; localName?: string };
24
22
 
@@ -157,33 +155,6 @@ export interface ICoreApp {
157
155
  * @returns List
158
156
  */
159
157
  getIdentityFlags(identity?: number | true): ListType[];
160
-
161
- /**
162
- * Get report data
163
- * 获取报告数据
164
- * @param data Input data
165
- * @param year Year to calculate
166
- * @returns Result
167
- */
168
- getReportData(data: PeriodReportData[], year: number): number[];
169
-
170
- /**
171
- * Transform report data for chart
172
- * 转换报告数据用于图表
173
- * @param data Input data
174
- * @param hasLastYear Has last year data
175
- * @param year Year to calculate, default is current year
176
- * @returns Transformed report data
177
- */
178
- transformReportData(
179
- data: PeriodReportData[],
180
- hasLastYear?: boolean,
181
- year?: number
182
- ): {
183
- labels: string[];
184
- currentYearData: number[];
185
- lastYearData: number[];
186
- };
187
158
  }
188
159
 
189
160
  /**
@@ -427,52 +398,4 @@ export class CoreApp implements ICoreApp {
427
398
  if (id === 0 || (id & identity) > 0) return id;
428
399
  });
429
400
  }
430
-
431
- /**
432
- * Get report data
433
- * 获取报告数据
434
- * @param data Input data
435
- * @param year Year to calculate
436
- * @returns Result
437
- */
438
- getReportData(data: PeriodReportData[], year: number) {
439
- const [start, end] = NumberUtils.getMonthPeriodRange(year);
440
- const items: number[] = [];
441
-
442
- for (let i = start; i <= end; i++) {
443
- const item = data.find((d) => d.period === i);
444
- items.push(item?.value ?? 0);
445
- }
446
-
447
- return items;
448
- }
449
-
450
- /**
451
- * Transform report data for chart
452
- * 转换报告数据用于图表
453
- * @param data Input data
454
- * @param hasLastYear Has last year data
455
- * @param year Year to calculate, default is current year
456
- * @returns Transformed report data
457
- */
458
- transformReportData(
459
- data: PeriodReportData[],
460
- hasLastYear?: boolean,
461
- year?: number
462
- ) {
463
- year ??= new Date().getFullYear();
464
-
465
- const months = BusinessUtils.getMonths(
466
- this.app.get<string[]>("months") ?? []
467
- );
468
- const labels = months.map((m) => m.label);
469
-
470
- const currentYearData = this.getReportData(data, year);
471
- const lastYearData =
472
- hasLastYear == null || hasLastYear
473
- ? this.getReportData(data, year - 1)
474
- : [];
475
-
476
- return { labels, currentYearData, lastYearData };
477
- }
478
401
  }
package/src/CoreUtils.ts CHANGED
@@ -1,6 +1,9 @@
1
+ import { DataTypes, NumberUtils } from "@etsoo/shared";
1
2
  import type Ajv from "ajv";
2
3
  import type { ErrorObject } from "ajv";
3
4
  import addFormats from "ajv-formats";
5
+ import { PeriodReportData } from "./dto/report/PeriodReportData";
6
+ import { BusinessUtils, IApp } from "@etsoo/appscript";
4
7
 
5
8
  /**
6
9
  * Core utilities
@@ -20,6 +23,48 @@ export namespace CoreUtils {
20
23
  };
21
24
  }
22
25
 
26
+ /**
27
+ * Get report data
28
+ * 获取报告数据
29
+ * @param data Input data
30
+ * @param year Year to calculate
31
+ * @param selector Selector function or number field to extract value from data
32
+ * @returns Result
33
+ */
34
+ export function getReportDataBase<D extends { period: number }>(
35
+ data: D[],
36
+ year: number,
37
+ selector:
38
+ | ((item: D | undefined | null) => number | undefined | null)
39
+ | DataTypes.Keys<D, number | null | undefined>
40
+ ) {
41
+ const [start, end] = NumberUtils.getMonthPeriodRange(year);
42
+ const items: number[] = [];
43
+
44
+ for (let i = start; i <= end; i++) {
45
+ const item = data.find((d) => d.period === i);
46
+ if (typeof selector === "function") {
47
+ items.push(selector(item) ?? 0);
48
+ } else {
49
+ if (item == null) items.push(0);
50
+ else items.push((item[selector] as number) ?? 0);
51
+ }
52
+ }
53
+
54
+ return items;
55
+ }
56
+
57
+ /**
58
+ * Get report data
59
+ * 获取报告数据
60
+ * @param data Input data
61
+ * @param year Year to calculate
62
+ * @returns Result
63
+ */
64
+ export function getReportData(data: PeriodReportData[], year: number) {
65
+ return getReportDataBase(data, year, "value");
66
+ }
67
+
23
68
  /**
24
69
  * Merge an array with another array, starting from the end
25
70
  * 合并数组,从末尾开始
@@ -33,6 +78,57 @@ export namespace CoreUtils {
33
78
  }
34
79
  }
35
80
 
81
+ /**
82
+ * Transform report data for chart
83
+ * 转换报告数据用于图表
84
+ * @param app App instance
85
+ * @param data Input data
86
+ * @param selector Selector function or number field to extract value from data
87
+ * @param hasLastYear Has last year data
88
+ * @param year Year to calculate, default is current year
89
+ * @returns Transformed report data
90
+ */
91
+ export function transformReportDataBase<D extends { period: number }>(
92
+ app: IApp,
93
+ data: D[],
94
+ selector:
95
+ | ((item: D | undefined | null) => number | undefined | null)
96
+ | DataTypes.Keys<D, number | null | undefined>,
97
+ hasLastYear?: boolean,
98
+ year?: number
99
+ ) {
100
+ year ??= new Date().getFullYear();
101
+
102
+ const months = BusinessUtils.getMonths(app.get<string[]>("months") ?? []);
103
+ const labels = months.map((m) => m.label);
104
+
105
+ const currentYearData = getReportDataBase(data, year, selector);
106
+ const lastYearData =
107
+ hasLastYear == null || hasLastYear
108
+ ? getReportDataBase(data, year - 1, selector)
109
+ : [];
110
+
111
+ return { labels, currentYearData, lastYearData };
112
+ }
113
+
114
+ /**
115
+ * Transform report data for chart
116
+ * 转换报告数据用于图表
117
+ * @param app App instance
118
+ * @param data Input data
119
+ * @param hasLastYear Has last year data
120
+ * @param year Year to calculate, default is current year
121
+ * @returns Transformed report data
122
+ */
123
+ export function transformReportData(
124
+ app: IApp,
125
+ data: PeriodReportData[],
126
+ hasLastYear?: boolean,
127
+ year?: number
128
+ ) {
129
+ return transformReportDataBase(app, data, "value", hasLastYear, year);
130
+ }
131
+
36
132
  let ajv: Ajv | null = null;
37
133
 
38
134
  /**