@dazitech/cli 3.0.6 → 3.0.8

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.
Files changed (76) hide show
  1. package/README.md +1 -1
  2. package/dist/clis/dazi-app.js +1 -1
  3. package/dist/clis/dazi-flow.js +43 -24
  4. package/dist/clis/dazi-onto.js +73 -22
  5. package/dist/clis/dazi.js +266 -171
  6. package/dist/docs/flow/ai-workflow-playbook.md +4 -2
  7. package/dist/docs/flow/flow-project-guide.md +9 -5
  8. package/dist/docs/flow/flows-guide.md +2 -2
  9. package/dist/docs/flow/node-code-guide.md +408 -401
  10. package/dist/docs/flow/run-guide.md +13 -6
  11. package/dist/docs/flow/variables-guide.md +407 -406
  12. package/dist/docs/guides/quickstart.md +18 -4
  13. package/dist/docs/guides/troubleshooting.md +1 -1
  14. package/dist/docs/guides/workspace-v3.md +43 -23
  15. package/dist/docs/index.json +9 -3
  16. package/dist/docs/onto/action-guide.md +3 -3
  17. package/dist/docs/onto/dazi_script_sdk_reference.md +178 -174
  18. package/dist/docs/onto/dazi_script_seed_data_guide.md +158 -155
  19. package/dist/docs/onto/function-guide.md +37 -10
  20. package/dist/docs/onto/space-management.md +3 -1
  21. package/dist/docs/onto//346/234/254/344/275/223/350/204/232/346/234/254/347/274/226/345/206/231/346/214/207/345/215/227.md +138 -34
  22. package/dist/docs/onto//346/234/254/344/275/223/350/247/204/345/210/222/346/214/207/345/215/227.md +73 -31
  23. package/dist/docs/onto//350/247/204/345/210/222/347/244/272/344/276/213_/344/272/247/345/223/201/351/224/200/345/224/256/346/234/254/344/275/223/350/247/204/345/210/222/346/226/271/346/241/210.md +497 -0
  24. package/dist/docs/onto//350/247/204/345/210/222/347/244/272/344/276/213_/345/210/251/346/266/246/345/210/206/346/236/220/346/234/254/344/275/223/346/226/271/346/241/210.md +597 -541
  25. package/dist/examples/index.json +202 -22
  26. package/dist/examples/onto/README.md +43 -0
  27. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_account_breakdown.py +99 -0
  28. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_budget_vs_actual.py +116 -0
  29. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_cost_center_profit.py +85 -0
  30. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_get_summary.py +76 -0
  31. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_mom_analysis.py +86 -0
  32. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_top_accounts.py +103 -0
  33. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/profit_fn_yoy_analysis.py +86 -0
  34. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/save_test_arguments.ps1 +27 -0
  35. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.account_breakdown.json +10 -0
  36. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.budget_vs_actual.json +10 -0
  37. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.cost_center_profit.json +9 -0
  38. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.get_summary.json +9 -0
  39. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.mom_analysis.json +9 -0
  40. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.top_accounts.json +11 -0
  41. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/function/test_arguments/profit.fn.yoy_analysis.json +9 -0
  42. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_ontology_init.py +521 -0
  43. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_seed_data.py +213 -0
  44. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/README.md +25 -0
  45. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_channel_mix.py +86 -0
  46. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_customer_segmentation.py +123 -0
  47. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_get_summary.py +81 -0
  48. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_mom_analysis.py +90 -0
  49. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_region_breakdown.py +85 -0
  50. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_top_products.py +101 -0
  51. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_yoy_analysis.py +90 -0
  52. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/save_test_arguments.ps1 +25 -0
  53. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.channel_mix.json +8 -0
  54. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.customer_segmentation.json +10 -0
  55. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.get_summary.json +8 -0
  56. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.mom_analysis.json +8 -0
  57. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.region_breakdown.json +8 -0
  58. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.top_products.json +10 -0
  59. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.yoy_analysis.json +8 -0
  60. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/README.md +5 -0
  61. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_ontology_init.py +403 -0
  62. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_seed_data.py +124 -0
  63. package/dist/prompts/flow/ai-workflow-playbook.md +4 -2
  64. package/dist/prompts/flow/flow-design.md +12 -9
  65. package/dist/prompts/flow/run-fix-loop.md +37 -22
  66. package/dist/prompts/index.json +2 -2
  67. package/dist/prompts/onto/action-design.md +4 -1
  68. package/dist/prompts/onto/function-design.md +9 -2
  69. package/dist/prompts/onto/rule-seed.md +5 -1
  70. package/dist/prompts/onto/script-publish-run.md +72 -24
  71. package/package.json +1 -1
  72. package/dist/examples/onto/function/profit_fn_customer_segmentation.py +0 -117
  73. package/dist/examples/onto/function/profit_fn_mom_analysis.py +0 -89
  74. package/dist/examples/onto/function/profit_fn_top_products.py +0 -89
  75. package/dist/examples/onto/function/profit_fn_yoy_analysis.py +0 -89
  76. package/dist/examples/onto/setup/profit_ontology_init.py +0 -388
@@ -1,541 +1,597 @@
1
- # 规划示例\_利润分析本体方案
2
-
3
- ---
4
-
5
- ## 一、空间概述
6
-
7
- | 项目 | 说明 |
8
- | -------- | -------------------------------- |
9
- | 空间ID | space\_\_profit0520 |
10
- | 空间名称 | 利润分析0520 |
11
- | 空间状态 | 空白空间(无表、无Cube、无本体) |
12
- | 规划日期 | 2026-05-20 |
13
-
14
- ---
15
-
16
- ## 二、需求分析
17
-
18
- ### 2.1 业务背景
19
-
20
- 构建利润分析本体,实现多维度、多层级的利润分析能力,支持:
21
-
22
- - 按产品、客户、时间等多维度利润分析
23
- - 同比、环比趋势分析
24
- - 产品利润排行与客户分层
25
- - 利润归因与成本结构分析
26
-
27
- ### 2.2 核心业务问题
28
-
29
- | 问题 | 说明 |
30
- | -------------- | ------------------ |
31
- | 利润从哪里来 | 收入来源结构分析 |
32
- | 成本消耗在哪里 | 成本去向与结构分析 |
33
- | 利润变化原因 | 影响利润的关键因素 |
34
- | 如何提升利润 | 优化方向与建议 |
35
-
36
- ---
37
-
38
- ## 三、物理层设计
39
-
40
- ### 3.1 表设计
41
-
42
- #### 3.1.1 事实表:profit_analysis_fact(利润分析事实表)
43
-
44
- **用途**:存储订单维度的收入、成本、利润数据
45
-
46
- **表结构**:
47
-
48
- | 字段名 | 类型 | 说明 | 主键/索引 |
49
- | ---------------- | ------- | -------- | --------- |
50
- | order_id | String | 订单ID | PK |
51
- | product_id | String | 产品ID | |
52
- | customer_id | String | 客户ID | |
53
- | order_date | Date | 订单日期 | |
54
- | product_category | String | 产品类别 | |
55
- | customer_region | String | 客户区域 | |
56
- | customer_type | String | 客户类型 | |
57
- | quantity | Int32 | 销售数量 | |
58
- | unit_price | Float64 | 单价 | |
59
- | revenue | Float64 | 收入 | |
60
- | cost | Float64 | 成本 | |
61
- | profit | Float64 | 利润 | |
62
-
63
- **排序键**:`ORDER BY (order_date, order_id)`
64
- **引擎**:`MergeTree()`
65
-
66
- #### 3.1.2 维度表:product_dimension(产品维度表)
67
-
68
- **用途**:存储产品主数据
69
-
70
- **表结构**:
71
-
72
- | 字段名 | 类型 | 说明 |
73
- | ---------------- | -------- | ---------- |
74
- | product_id | String | 产品ID |
75
- | product_name | String | 产品名称 |
76
- | product_category | String | 产品类别 |
77
- | cost_price | Float64 | 成本单价 |
78
- | margin_ratio | Float64 | 毛利率目标 |
79
- | created_at | DateTime | 创建时间 |
80
-
81
- **排序键**:`ORDER BY (product_id)`
82
-
83
- #### 3.1.3 维度表:customer_dimension(客户维度表)
84
-
85
- **用途**:存储客户主数据
86
-
87
- **表结构**:
88
-
89
- | 字段名 | 类型 | 说明 |
90
- | --------------- | -------- | -------------------- |
91
- | customer_id | String | 客户ID |
92
- | customer_name | String | 客户名称 |
93
- | customer_region | String | 客户区域 |
94
- | customer_type | String | 客户类型(VIP/普通) |
95
- | credit_level | String | 信用等级 |
96
- | created_at | DateTime | 创建时间 |
97
-
98
- **排序键**:`ORDER BY (customer_id)`
99
-
100
- ---
101
-
102
- ## 四、Cube 层设计
103
-
104
- ### 4.1 ProfitCube(利润分析主Cube)
105
-
106
- **目的**:支持全局利润分析
107
-
108
- **维度**:
109
-
110
- | 维度名 | 列名 | 类型 | 说明 |
111
- | ---------------- | ---------------- | ------ | -------- |
112
- | order_id | order_id | string | 订单ID |
113
- | order_date | order_date | date | 订单日期 |
114
- | product_id | product_id | string | 产品ID |
115
- | product_category | product_category | string | 产品类别 |
116
- | customer_id | customer_id | string | 客户ID |
117
- | customer_region | customer_region | string | 客户区域 |
118
- | customer_type | customer_type | string | 客户类型 |
119
-
120
- **度量**:
121
-
122
- | 度量名 | 列名 | 聚合 | 说明 |
123
- | ----------- | -------- | ---- | -------- |
124
- | quantity | quantity | sum | 销售数量 |
125
- | revenue | revenue | sum | 收入 |
126
- | cost | cost | sum | 成本 |
127
- | order_count | order_id | uniq | 订单数量 |
128
-
129
- **派生度量**:
130
-
131
- | 度量名 | 表达式 | 说明 |
132
- | ------------- | ------------------------------------ | ------ |
133
- | profit | revenue - cost | 利润 |
134
- | profit_margin | if(revenue > 0, profit / revenue, 0) | 利润率 |
135
-
136
- ### 4.2 ProductCube(产品分析Cube)
137
-
138
- **目的**:支持按产品维度的利润分析
139
-
140
- **维度**:
141
-
142
- | 维度名 | 列名 | 类型 | 说明 |
143
- | ---------------- | ---------------- | ------ | -------- |
144
- | product_id | product_id | string | 产品ID |
145
- | product_category | product_category | string | 产品类别 |
146
-
147
- **度量**:
148
-
149
- | 度量名 | 列名 | 聚合 | 说明 |
150
- | ----------- | -------- | ---- | -------- |
151
- | revenue | revenue | sum | 收入 |
152
- | cost | cost | sum | 成本 |
153
- | order_count | order_id | uniq | 订单数量 |
154
-
155
- **派生度量**:
156
-
157
- | 度量名 | 表达式 | 说明 |
158
- | ------------- | ------------------------------------ | ------ |
159
- | profit | revenue - cost | 利润 |
160
- | profit_margin | if(revenue > 0, profit / revenue, 0) | 利润率 |
161
-
162
- ### 4.3 CustomerCube(客户分析Cube)
163
-
164
- **目的**:支持按客户维度的利润分析
165
-
166
- **维度**:
167
-
168
- | 维度名 | 列名 | 类型 | 说明 |
169
- | --------------- | --------------- | ------ | -------- |
170
- | customer_id | customer_id | string | 客户ID |
171
- | customer_region | customer_region | string | 客户区域 |
172
- | customer_type | customer_type | string | 客户类型 |
173
-
174
- **度量**:
175
-
176
- | 度量名 | 列名 | 聚合 | 说明 |
177
- | ----------- | -------- | ---- | -------- |
178
- | revenue | revenue | sum | 收入 |
179
- | cost | cost | sum | 成本 |
180
- | order_count | order_id | uniq | 订单数量 |
181
-
182
- **派生度量**:
183
-
184
- | 度量名 | 表达式 | 说明 |
185
- | ------------- | ------------------------------------ | ------ |
186
- | profit | revenue - cost | 利润 |
187
- | profit_margin | if(revenue > 0, profit / revenue, 0) | 利润率 |
188
-
189
- ### 4.4 TimeCube(时间维度Cube)
190
-
191
- **目的**:支持同比、环比分析
192
-
193
- **维度**:
194
-
195
- | 维度名 | 列名 | 类型 | 说明 |
196
- | ---------- | ---------- | ------ | ---- |
197
- | year | order_date | date | 年份 |
198
- | year_month | order_date | string | 年月 |
199
- | quarter | order_date | string | 季度 |
200
-
201
- **度量**:
202
-
203
- | 度量名 | 列名 | 聚合 | 说明 |
204
- | ------- | ------- | ---- | ---- |
205
- | revenue | revenue | sum | 收入 |
206
- | cost | cost | sum | 成本 |
207
-
208
- **派生度量**:
209
-
210
- | 度量名 | 表达式 | 说明 |
211
- | ------------- | ------------------------------------ | ------ |
212
- | profit | revenue - cost | 利润 |
213
- | profit_margin | if(revenue > 0, profit / revenue, 0) | 利润率 |
214
-
215
- ---
216
-
217
- ## 五、本体层设计
218
-
219
- ### 5.1 对象类型(4种)
220
-
221
- | 对象类型code | 名称 | 说明 | bind_source |
222
- | -------------- | -------- | ------------ | ------------ |
223
- | Order | 订单 | 订单业务对象 | ProfitCube |
224
- | Product | 产品 | 产品业务对象 | ProductCube |
225
- | Customer | 客户 | 客户业务对象 | CustomerCube |
226
- | ProfitAnalysis | 利润分析 | 利润聚合对象 | ProfitCube |
227
-
228
- ### 5.2 对象属性
229
-
230
- #### 5.2.1 Order(订单)属性
231
-
232
- | 属性code | 名称 | semantic_role | qualified_name |
233
- | ------------- | -------- | ------------- | ------------------------ |
234
- | id | 订单ID | dimension | ProfitCube.order_id |
235
- | date | 订单日期 | dimension | ProfitCube.order_date |
236
- | quantity | 销售数量 | measure | ProfitCube.quantity |
237
- | revenue | 收入 | measure | ProfitCube.revenue |
238
- | cost | 成本 | measure | ProfitCube.cost |
239
- | profit | 利润 | measure | ProfitCube.profit |
240
- | profit_margin | 利润率 | measure | ProfitCube.profit_margin |
241
-
242
- #### 5.2.2 Product(产品)属性
243
-
244
- | 属性code | 名称 | semantic_role | qualified_name |
245
- | ------------- | -------- | ------------- | ---------------------------- |
246
- | id | 产品ID | dimension | ProductCube.product_id |
247
- | category | 产品类别 | dimension | ProductCube.product_category |
248
- | revenue | 累计收入 | measure | ProductCube.revenue |
249
- | cost | 累计成本 | measure | ProductCube.cost |
250
- | profit | 累计利润 | measure | ProductCube.profit |
251
- | profit_margin | 利润率 | measure | ProductCube.profit_margin |
252
- | order_count | 订单数 | measure | ProductCube.order_count |
253
-
254
- #### 5.2.3 Customer(客户)属性
255
-
256
- | 属性code | 名称 | semantic_role | qualified_name |
257
- | ------------- | -------- | ------------- | ---------------------------- |
258
- | id | 客户ID | dimension | CustomerCube.customer_id |
259
- | region | 客户区域 | dimension | CustomerCube.customer_region |
260
- | type | 客户类型 | dimension | CustomerCube.customer_type |
261
- | revenue | 累计收入 | measure | CustomerCube.revenue |
262
- | cost | 累计成本 | measure | CustomerCube.cost |
263
- | profit | 累计利润 | measure | CustomerCube.profit |
264
- | profit_margin | 利润率 | measure | CustomerCube.profit_margin |
265
- | order_count | 订单数 | measure | CustomerCube.order_count |
266
-
267
- #### 5.2.4 ProfitAnalysis(利润分析)属性
268
-
269
- | 属性code | 名称 | semantic_role | qualified_name |
270
- | ---------------- | -------- | ------------- | --------------------------- |
271
- | date | 日期 | dimension | ProfitCube.order_date |
272
- | product_category | 产品类别 | dimension | ProfitCube.product_category |
273
- | customer_region | 客户区域 | dimension | ProfitCube.customer_region |
274
- | customer_type | 客户类型 | dimension | ProfitCube.customer_type |
275
- | revenue | 收入 | measure | ProfitCube.revenue |
276
- | cost | 成本 | measure | ProfitCube.cost |
277
- | profit | 利润 | measure | ProfitCube.profit |
278
- | profit_margin | 利润率 | measure | ProfitCube.profit_margin |
279
-
280
- ### 5.3 链接类型(6种)
281
-
282
- | 链接code | 名称 | from | to | 说明 |
283
- | ------------------ | ------------ | -------------- | -------------- | -------------- |
284
- | order_to_product | 订单关联产品 | Order | Product | 订单包含的产品 |
285
- | order_to_customer | 订单关联客户 | Order | Customer | 订单归属客户 |
286
- | profit_to_product | 利润关联产品 | ProfitAnalysis | Product | 利润归因到产品 |
287
- | profit_to_customer | 利润关联客户 | ProfitAnalysis | Customer | 利润归因到客户 |
288
- | product_to_profit | 产品归属利润 | Product | ProfitAnalysis | 产品利润聚合 |
289
- | customer_to_profit | 客户归属利润 | Customer | ProfitAnalysis | 客户利润聚合 |
290
-
291
- ---
292
-
293
- ## 六、本体函数设计
294
-
295
- ### 6.1 函数清单
296
-
297
- | 函数ID | 功能 | 参数 |
298
- | ------------------------------- | ------------ | ------------------------------------ |
299
- | profit.fn.get_summary | 利润总览 | - |
300
- | profit.fn.yoy_analysis | 同比分析 | start_date, end_date |
301
- | profit.fn.mom_analysis | 环比分析 | start_date, end_date |
302
- | profit.fn.top_products | 产品利润排行 | limit, metric, start_date, end_date |
303
- | profit.fn.customer_segmentation | 客户分层 | metric, method, start_date, end_date |
304
-
305
- ### 6.2 函数详细设计
306
-
307
- #### 6.2.1 profit.fn.get_summary(利润总览)
308
-
309
- **功能**:获取利润总览数据
310
-
311
- **参数**:无
312
-
313
- **返回值**:
314
-
315
- | 字段名 | 类型 | 说明 |
316
- | ------------- | ------- | -------- |
317
- | total_revenue | Float64 | 总收入 |
318
- | total_cost | Float64 | 总成本 |
319
- | total_profit | Float64 | 总利润 |
320
- | profit_margin | Float64 | 利润率 |
321
- | order_count | Int | 订单数量 |
322
-
323
- #### 6.2.2 profit.fn.yoy_analysis(同比分析)
324
-
325
- **功能**:计算指定时间范围内的年度同比分析
326
-
327
- **参数**:
328
-
329
- | 参数名 | 类型 | 默认值 | 说明 |
330
- | ---------- | ------ | ------ | -------- |
331
- | start_date | String | - | 开始日期 |
332
- | end_date | String | - | 结束日期 |
333
-
334
- **返回值**:
335
-
336
- | 字段名 | 类型 | 说明 |
337
- | ------------- | ------- | ---------- |
338
- | year | Int | 年份 |
339
- | revenue | Float64 | 收入 |
340
- | cost | Float64 | 成本 |
341
- | profit | Float64 | 利润 |
342
- | profit_margin | Float64 | 利润率 |
343
- | yoy_growth | Float64 | 同比增长率 |
344
-
345
- #### 6.2.3 profit.fn.mom_analysis(环比分析)
346
-
347
- **功能**:计算指定时间范围内的月度环比分析
348
-
349
- **参数**:
350
-
351
- | 参数名 | 类型 | 默认值 | 说明 |
352
- | ---------- | ------ | ------ | -------- |
353
- | start_date | String | - | 开始日期 |
354
- | end_date | String | - | 结束日期 |
355
-
356
- **返回值**:
357
-
358
- | 字段名 | 类型 | 说明 |
359
- | ------------- | ------- | ---------- |
360
- | year_month | String | 年月 |
361
- | revenue | Float64 | 收入 |
362
- | cost | Float64 | 成本 |
363
- | profit | Float64 | 利润 |
364
- | profit_margin | Float64 | 利润率 |
365
- | mom_growth | Float64 | 环比增长率 |
366
-
367
- #### 6.2.4 profit.fn.top_products(产品利润排行)
368
-
369
- **功能**:按利润或利润率排序,获取Top N产品
370
-
371
- **参数**:
372
-
373
- | 参数名 | 类型 | 默认值 | 说明 |
374
- | ---------- | ------ | ------ | ------------------------- |
375
- | limit | Int | 10 | 返回数量 |
376
- | metric | String | profit | 排序指标(profit/margin) |
377
- | start_date | String | - | 开始日期(可选) |
378
- | end_date | String | - | 结束日期(可选) |
379
-
380
- **返回值**:
381
-
382
- | 字段名 | 类型 | 说明 |
383
- | ---------------- | ------- | -------- |
384
- | rank | Int | 排名 |
385
- | product_id | String | 产品ID |
386
- | product_category | String | 产品类别 |
387
- | revenue | Float64 | 收入 |
388
- | cost | Float64 | 成本 |
389
- | profit | Float64 | 利润 |
390
- | profit_margin | Float64 | 利润率 |
391
-
392
- #### 6.2.5 profit.fn.customer_segmentation(客户分层)
393
-
394
- **功能**:基于利润贡献对客户进行分层
395
-
396
- **参数**:
397
-
398
- | 参数名 | 类型 | 默认值 | 说明 |
399
- | ---------- | ------ | -------- | ------------------------------- |
400
- | metric | String | profit | 分层指标(profit/revenue) |
401
- | method | String | quartile | 分层方法(quartile/percentile) |
402
- | start_date | String | - | 开始日期(可选) |
403
- | end_date | String | - | 结束日期(可选) |
404
-
405
- **返回值**:
406
-
407
- | 字段名 | 类型 | 说明 |
408
- | --------------- | ------- | --------------------------- |
409
- | customer_id | String | 客户ID |
410
- | customer_region | String | 客户区域 |
411
- | revenue | Float64 | 收入 |
412
- | profit | Float64 | 利润 |
413
- | segment | String | 分层(VIP/High/Medium/Low) |
414
-
415
- ---
416
-
417
- ## 七、实施计划
418
-
419
- > **dazi-vscode v3 命令对照**(`space_id` 见本文「一、空间概述」;项目目录 `项目/onto_<名称>/` 在新建项目时绑定该空间)
420
- > 环境:`dazi doctor` · 现网表/Cube:**数据资源** 侧栏 ·
421
- > 发布:`dazi onto script publish 项目/onto_<名称>/脚本/<file>.py --space space__profit0520 --register-function-id <id>` ·
422
- > 运行:`dazi onto function run <function_id> --space space__profit0520 --params '{}'`
423
- > 细则见 **`本体规划指南.md`**「LLM 执行指引」。
424
-
425
- ### 7.1 阶段一:物理层(优先级:高)
426
-
427
- | 步骤 | 任务 | 说明 |
428
- | ---- | ------------------------- | ---------------------- |
429
- | 1 | 创建 profit_analysis_fact | 利润分析事实表 |
430
- | 2 | 创建 product_dimension | 产品维度表 |
431
- | 3 | 创建 customer_dimension | 客户维度表 |
432
- | 4 | 注册表到空间 | 使用 s.tables.register |
433
-
434
- ### 7.2 阶段二:Cube层(优先级:高)
435
-
436
- | 步骤 | 任务 | 说明 |
437
- | ---- | ----------------- | --------------------- |
438
- | 5 | 注册 ProfitCube | 主分析Cube |
439
- | 6 | 注册 ProductCube | 产品分析Cube |
440
- | 7 | 注册 CustomerCube | 客户分析Cube |
441
- | 8 | 注册 TimeCube | 时间维度Cube |
442
- | 9 | 添加派生度量 | profit, profit_margin |
443
-
444
- ### 7.3 阶段三:本体层(优先级:高)
445
-
446
- | 步骤 | 任务 | 说明 |
447
- | ---- | ------------ | ---------------------------------------- |
448
- | 10 | 定义对象类型 | Order, Product, Customer, ProfitAnalysis |
449
- | 11 | 绑定数据源 | bind_source |
450
- | 12 | 定义属性 | 属性映射 |
451
- | 13 | 定义链接 | 6种链接关系 |
452
- | 14 | 同步指标引用 | sync_metric_refs |
453
-
454
- ### 7.4 阶段四:函数层(优先级:中)
455
-
456
- | 步骤 | 任务 | 说明 |
457
- | ---- | -------------------------- | ----------------------------------------------------------------------------- |
458
- | 15 | 开发 get_summary | 利润总览函数 |
459
- | 16 | 开发 yoy_analysis | 同比分析函数 |
460
- | 17 | 开发 mom_analysis | 环比分析函数 |
461
- | 18 | 开发 top_products | 产品排行函数 |
462
- | 19 | 开发 customer_segmentation | 客户分层函数 |
463
- | 20 | 发布并注册函数 | `dazi onto script publish … --space space__profit0520 --register-function-id` |
464
-
465
- ### 7.5 阶段五:数据层(优先级:中)
466
-
467
- | 步骤 | 任务 | 说明 |
468
- | ---- | ------------ | ------------------------ |
469
- | 21 | 生成模拟数据 | 2025年1-6月、2026年1-6月 |
470
- | 22 | 数据验证 | 验证查询结果 |
471
-
472
- ### 7.6 阶段六:验证与交付(优先级:高)
473
-
474
- | 步骤 | 任务 | 说明 |
475
- | ---- | -------- | --------------------------------------------------------------------------------------- |
476
- | 23 | 验收核对 | 侧栏 **数据资源** 查表/Cube;`dazi onto function list --space space__profit0520` 查函数 |
477
- | 24 | 函数测试 | 验证函数功能 |
478
- | 25 | 文档归档 | 更新规划文档 |
479
-
480
- ---
481
-
482
- ## 八、风险与验收
483
-
484
- ### 8.1 风险识别
485
-
486
- | 风险等级 | 风险描述 | 缓解措施 |
487
- | -------- | ------------------ | --------------------------- |
488
- | 中 | 数据量不足影响分析 | 生成足够的模拟数据 |
489
- | 低 | 派生度量表达式错误 | 先测试SQL,再迁移到派生度量 |
490
- | | 函数查询性能 | 优化SQL,使用索引 |
491
-
492
- ### 8.2 验收标准
493
-
494
- | 验收项 | 验收标准 |
495
- | -------- | ------------------------------ |
496
- | 物理表 | 3张表创建成功,可正常查询 |
497
- | Cube | 4个Cube正确注册,维度/度量完整 |
498
- | 对象类型 | 4种对象类型定义正确 |
499
- | 链接类型 | 6种链接关系定义正确 |
500
- | 属性 | 25个属性定义正确 |
501
- | 本体函数 | 5个函数可正常执行 |
502
- | 数据 | 可查询正确利润数据 |
503
-
504
- ---
505
-
506
- ## 九、预期成果
507
-
508
- ### 9.1 能力矩阵
509
-
510
- | 分析能力 | 说明 |
511
- | ---------- | ----------------------- |
512
- | 多维度分析 | 产品、客户、时间多维度 |
513
- | 趋势分析 | 同比、环比增长率 |
514
- | 排行分析 | Top N产品利润排行 |
515
- | 客户分层 | VIP/High/Medium/Low分层 |
516
- | 利润归因 | 产品/客户利润贡献 |
517
-
518
- ### 9.2 业务价值
519
-
520
- - **决策支持**:提供多维度利润数据支持经营决策
521
- - **趋势洞察**:识别利润变化趋势和异常
522
- - **客户价值**:识别高价值客户,优化客户策略
523
- - **产品优化**:识别高利润产品,优化产品组合
524
-
525
- ---
526
-
527
- ## 十、相关文档
528
-
529
- | 文档路径 | 说明 |
530
- | ------------------------------------------- | ---------------------------------------------- |
531
- | `本体规划指南.md` | 规划方法、空间约束、**LLM 执行指引(v3 CLI)** |
532
- | `本体脚本编写指南.md` | 项目内 `脚本/` 目录约定 |
533
- | `本体脚本编写指南.md` / `function-guide.md` | `dazi onto` 脚本发布与函数生命周期 |
534
- | `guides/workspace-v3.md` | 工作区 `项目/`、`资源/` 布局 |
535
-
536
- ---
537
-
538
- **方案版本**:v1.0
539
- **创建日期**:2026-05-20
540
- **适用空间**:space\_\_profit0520
541
- **状态**:待评审
1
+ # 利润分析本体规划方案
2
+
3
+ ---
4
+
5
+ ## 一、空间概述
6
+
7
+ | 项目 | 说明 |
8
+ | -------- | ---------------------------------------------------------------------------- |
9
+ | 空间 ID | `space__zlj` |
10
+ | 空间名称 | 数据空间zlj |
11
+ | 空间状态 | 与「产品销售」「员工培训」等**共用同一数据空间**;利润域表/Cube/对象独立建设 |
12
+ | 规划日期 | 2026-06-06 |
13
+ | 实现单元 | `项目/潘达石化/本体/ontos/利润分析本体方案/` |
14
+
15
+ > **共存说明**:本空间可能已有 `product_master`、`sales_order_fact`、`training_record` 等表。利润分析域以 **会计科目 + 实际分录 + 预算** 为读模型根基,init 脚本**仅**创建/注册利润域表,不覆盖其他域资产。
16
+ > **与销售域关系**:销售收入可通过科目映射(如「6001 主营业务收入」)与实际分录对齐;必要时函数层 LEFT JOIN `sales_order_fact` 做勾稽,但**本体对象不以订单为主锚点**。
17
+
18
+ ---
19
+
20
+ ## 二、需求分析
21
+
22
+ ### 2.1 业务背景
23
+
24
+ 潘达石化财务利润分析场景,需构建**利润分析本体**,以业务语言描述「会计科目」「实际分录」「预算」及其关系,支撑:
25
+
26
+ - **损益总览**:收入、成本、费用、营业利润、利润率
27
+ - **科目结构**:按科目层级展开收入/成本/费用构成
28
+ - **实际 vs 预算**:按期间、科目、成本中心的预算执行率与差异分析
29
+ - **组织维度**:按成本中心/部门贡献与超支预警
30
+ - **趋势分析**:同比、环比利润与关键科目走势
31
+ - **归因分析**:利润变动由哪些科目/组织驱动
32
+
33
+ ### 2.2 核心业务问题
34
+
35
+ | 问题 | 说明 |
36
+ | --------------- | ------------------------------ |
37
+ | 利润是多少 | 期间营业利润、利润率 |
38
+ | 钱从哪来/到哪去 | 收入、成本、费用科目结构 |
39
+ | 预算执行如何 | 预算额、实际额、差异额、执行率 |
40
+ | 哪个部门拖后腿 | 成本中心维度利润与预算偏差 |
41
+ | 哪些科目异常 | 科目 Top N、同比/环比异动 |
42
+ | 趋势如何 | 按月/季/年利润与关键科目走势 |
43
+
44
+ ### 2.3 规划范围说明
45
+
46
+ **核心物理表(本方案重点)**:
47
+
48
+ | 表名 | 中文名 | 角色 |
49
+ | ---------------------- | -------------- | ------------------------------- |
50
+ | `account_master` | **科目表** | 会计科目主数据(维表) |
51
+ | `actual_journal_entry` | **实际分录表** | 总账/财务实际记账流水(事实表) |
52
+ | `budget_entry` | **预算表** | 预算编制明细(事实表) |
53
+
54
+ **辅助维表**:
55
+
56
+ | 表名 | 中文名 | 角色 |
57
+ | ----------------------- | ------------ | ------------------------ |
58
+ | `cost_center_dimension` | 成本中心维表 | 组织/部门/利润中心主数据 |
59
+
60
+ > 规划原则:先定义**业务对象**(Account、JournalEntry、BudgetLine 等)与**链接**,再反推物理表。
61
+ > **实际分录**承载「实际发生」;**预算表**承载「计划数」;**科目表**统一语义与损益口径(收入/成本/费用)。
62
+ > 旧版示例中的 `profit_analysis_fact`(订单粒度收入/成本)**不作为本方案主模型**;若需订单维度利润,可另建与销售域的映射函数,不替代 GL 分录。
63
+
64
+ ---
65
+
66
+ ## 三、物理层设计
67
+
68
+ ### 3.1 科目表:account_master
69
+
70
+ **用途**:存储会计科目主数据,定义损益口径与层级,供实际分录、预算关联及科目树分析。
71
+
72
+ **表结构**:
73
+
74
+ | 字段名 | 类型 | 说明 | 主键/索引 |
75
+ | ----------------- | -------- | ------------------------------------------- | --------- |
76
+ | account_id | String | 科目 ID | PK |
77
+ | account_code | String | 科目编码(如 6001、6401) | |
78
+ | account_name | String | 科目名称 | |
79
+ | account_type | String | 科目类型(资产/负债/权益/收入/成本/费用) | |
80
+ | pl_category | String | 损益大类(营业收入/营业成本/期间费用/其他) | |
81
+ | parent_account_id | String | 上级科目 ID(根节点为空) | FK 语义 |
82
+ | account_level | Int32 | 科目层级(1=一级,2=二级…) | |
83
+ | is_leaf | Boolean | 是否末级科目 | |
84
+ | normal_balance | String | 余额方向(借/贷) | |
85
+ | status | String | 状态(启用/停用) | |
86
+ | created_at | DateTime | 创建时间 | |
87
+
88
+ **排序键**:`ORDER BY (account_code)`
89
+ **引擎**:`MergeTree()`
90
+
91
+ **业务规则**:
92
+
93
+ - 损益分析默认过滤 `account_type IN ('收入','成本','费用')`
94
+ - `pl_category` 用于利润表行项目归集;函数层可按一级科目 + `pl_category` 双轨展示
95
+ - 收入类科目:`amount_signed` 为正表示增加利润;成本/费用类为负向影响(见 §3.3 符号约定)
96
+
97
+ ### 3.2 成本中心维表:cost_center_dimension
98
+
99
+ **用途**:组织/部门/利润中心主数据,支撑分录与预算的多维切片。
100
+
101
+ | 字段名 | 类型 | 说明 |
102
+ | ---------------- | -------- | ----------------- |
103
+ | cost_center_id | String | 成本中心 ID(PK) |
104
+ | cost_center_code | String | 编码 |
105
+ | cost_center_name | String | 名称 |
106
+ | department | String | 所属部门 |
107
+ | company_code | String | 公司代码 |
108
+ | profit_center | String | 利润中心 |
109
+ | status | String | 状态 |
110
+ | created_at | DateTime | 创建时间 |
111
+
112
+ **排序键**:`ORDER BY (cost_center_id)`
113
+
114
+ ### 3.3 实际分录表:actual_journal_entry
115
+
116
+ **用途**:存储总账凭证行粒度实际发生额,是 ActualCube / JournalEntry 对象的主要事实来源。
117
+
118
+ **表结构**:
119
+
120
+ | 字段名 | 类型 | 说明 | 主键/索引 |
121
+ | ---------------- | -------- | ----------------------------------- | --------- |
122
+ | entry_id | String | 凭证 ID | |
123
+ | line_id | String | 凭证行 ID | PK 组合 |
124
+ | posting_date | Date | 记账日期 | |
125
+ | fiscal_year | Int32 | 会计年度 | |
126
+ | fiscal_period | Int32 | 会计期间(1–12) | |
127
+ | account_id | String | 科目 ID account_master | FK 语义 |
128
+ | account_code | String | 科目编码(冗余) | |
129
+ | account_name | String | 科目名称(冗余) | |
130
+ | account_type | String | 科目类型(冗余) | |
131
+ | pl_category | String | 损益大类(冗余) | |
132
+ | cost_center_id | String | 成本中心 ID → cost_center_dimension | FK 语义 |
133
+ | cost_center_name | String | 成本中心名称(冗余) | |
134
+ | department | String | 部门(冗余) | |
135
+ | debit_amount | Float64 | 借方金额 | |
136
+ | credit_amount | Float64 | 贷方金额 | |
137
+ | amount_signed | Float64 | **损益符号金额**(见下) | |
138
+ | currency | String | 币种 | |
139
+ | voucher_no | String | 凭证号 | |
140
+ | source_system | String | 来源系统(GL/ERP) | |
141
+ | description | String | 摘要 | |
142
+ | created_at | DateTime | 创建时间 | |
143
+
144
+ **排序键**:`ORDER BY (posting_date, entry_id, line_id)`
145
+ **引擎**:`MergeTree()`
146
+
147
+ **损益符号约定 `amount_signed`**(写入 ETL/seed 时统一计算,便于 Cube 聚合):
148
+
149
+ | account_type | 计算规则(示例) | 含义 |
150
+ | ------------ | ------------------------------ | ----------- |
151
+ | 收入 | `credit_amount - debit_amount` | 正=收入增加 |
152
+ | 成本 | `debit_amount - credit_amount` | 正=成本发生 |
153
+ | 费用 | `debit_amount - credit_amount` | 正=费用发生 |
154
+
155
+ **分析口径**:
156
+
157
+ - `revenue` = Σ `amount_signed` where `account_type='收入'`
158
+ - `cost` = Σ `amount_signed` where `account_type='成本'`
159
+ - `expense` = Σ `amount_signed` where `account_type='费用'`
160
+ - `operating_profit` = `revenue - cost - expense`
161
+
162
+ ### 3.4 预算表:budget_entry
163
+
164
+ **用途**:存储预算编制明细(年度/月度预算版本),支撑预算 vs 实际分析。
165
+
166
+ **表结构**:
167
+
168
+ | 字段名 | 类型 | 说明 | 主键/索引 |
169
+ | ---------------- | -------- | --------------------------------------- | --------- |
170
+ | budget_id | String | 预算批次 ID | |
171
+ | line_id | String | 预算行 ID | PK 组合 |
172
+ | budget_version | String | 预算版本(如 2026年度预算) | |
173
+ | fiscal_year | Int32 | 预算年度 | |
174
+ | fiscal_period | Int32 | 预算期间(1–12,0=全年) | |
175
+ | account_id | String | 科目 ID → account_master | FK 语义 |
176
+ | account_code | String | 科目编码(冗余) | |
177
+ | account_name | String | 科目名称(冗余) | |
178
+ | account_type | String | 科目类型(冗余) | |
179
+ | pl_category | String | 损益大类(冗余) | |
180
+ | cost_center_id | String | 成本中心 ID | FK 语义 |
181
+ | cost_center_name | String | 成本中心名称(冗余) | |
182
+ | department | String | 部门(冗余) | |
183
+ | budget_amount | Float64 | 预算金额(与 `amount_signed` 同向符号) | |
184
+ | currency | String | 币种 | |
185
+ | status | String | 状态(草稿/已发布) | |
186
+ | created_at | DateTime | 创建时间 | |
187
+
188
+ **排序键**:`ORDER BY (fiscal_year, fiscal_period, account_id, line_id)`
189
+ **引擎**:`MergeTree()`
190
+
191
+ **业务规则**:
192
+
193
+ - 分析默认 `status = '已发布'`
194
+ - 月度预算:`fiscal_period BETWEEN 1 AND 12`;年度汇总:`fiscal_period = 0` 或由函数按月 SUM
195
+
196
+ ### 3.5 表间关系设计(物理层 · 数据空间)
197
+
198
+ > **与本体链接的区别**:本节描述 **ClickHouse 物理表 FK/JOIN**,供侧栏 **数据资源 表间关系** 与 AI 多表 JOIN。本体链接见第五节。
199
+
200
+ **关系模型**:
201
+
202
+ ```text
203
+ account_master
204
+ (account_id PK)
205
+ ▲ ▲
206
+ │ │
207
+ many_to_one │ │ many_to_one
208
+ │ │
209
+ actual_journal_entry budget_entry
210
+ (分录行) (预算行)
211
+
212
+ many_to_one
213
+
214
+ cost_center_dimension
215
+ (cost_center_id PK)
216
+
217
+ account_master.parent_account_id ──► account_master(自关联,科目树)
218
+ ```
219
+
220
+ **关系清单(6 条)**:
221
+
222
+ | 关系 ID(建议) | 类型 | 从表 | 到表 | join_keys | 说明 |
223
+ | ---------------------- | ----------- | -------------------- | --------------------- | --------------------------------------- | ------------------------------------------------- |
224
+ | rel_actual_account | many_to_one | actual_journal_entry | account_master | account_id → account_id | 分录关联科目 |
225
+ | rel_actual_cost_center | many_to_one | actual_journal_entry | cost_center_dimension | cost_center_id → cost_center_id | 分录关联成本中心 |
226
+ | rel_budget_account | many_to_one | budget_entry | account_master | account_id → account_id | 预算关联科目 |
227
+ | rel_budget_cost_center | many_to_one | budget_entry | cost_center_dimension | cost_center_id → cost_center_id | 预算关联成本中心 |
228
+ | rel_account_parent | many_to_one | account_master | account_master | parent_account_id → account_id | 科目上级(树形) |
229
+ | rel_budget_actual | many_to_one | budget_entry | actual_journal_entry | account_id+cost_center_id+fiscal_period | 预算与实际同维对齐(语义关联,JOIN 时加期间条件) |
230
+
231
+ > **说明**:`rel_budget_actual` 为 **同维关联**(非严格 FK),`join_sql` 示例:
232
+ > `budget_entry.account_id = actual_journal_entry.account_id AND budget_entry.cost_center_id = actual_journal_entry.cost_center_id AND budget_entry.fiscal_year = actual_journal_entry.fiscal_year AND budget_entry.fiscal_period = actual_journal_entry.fiscal_period`
233
+ > 函数 `profit.fn.budget_vs_actual` 主要基于此 JOIN。
234
+
235
+ **实施 API**(`setup/profit_ontology_init.py`):
236
+
237
+ ```python
238
+ s.tables.add_relationship(
239
+ from_table="actual_journal_entry",
240
+ to_table="account_master",
241
+ join_sql="actual_journal_entry.account_id = account_master.account_id",
242
+ join_keys=[{"from": "account_id", "to": "account_id"}],
243
+ relationship_type="many_to_one",
244
+ description="实际分录关联会计科目",
245
+ )
246
+ ```
247
+
248
+ **与本体链接对照**:
249
+
250
+ | 物理表关系 | 对应本体链接(业务层) |
251
+ | ---------------------- | ------------------------- |
252
+ | rel_actual_account | entry_belongs_account |
253
+ | rel_actual_cost_center | entry_belongs_cost_center |
254
+ | rel_budget_account | budget_for_account |
255
+ | rel_budget_cost_center | budget_for_cost_center |
256
+ | rel_account_parent | account_has_parent |
257
+ | rel_budget_actual | budget_compared_to_actual |
258
+
259
+ ---
260
+
261
+ ## 四、Cube 层设计
262
+
263
+ ### 4.1 ActualCube(实际发生主 Cube)
264
+
265
+ **事实源**:`actual_journal_entry`
266
+
267
+ **维度**:entry_id, line_id, posting_date, fiscal_year, fiscal_period, account_id, account_code, account_name, account_type, pl_category, cost_center_id, cost_center_name, department, voucher_no, source_system
268
+
269
+ **度量**:
270
+
271
+ | 度量名 | 列名 | 聚合 | 说明 |
272
+ | ------------ | ------------- | ----- | ---------- |
273
+ | debit_total | debit_amount | sum | 借方合计 |
274
+ | credit_total | credit_amount | sum | 贷方合计 |
275
+ | net_amount | amount_signed | sum | 损益符号额 |
276
+ | line_count | line_id | count | 分录行数 |
277
+
278
+ **派生度量**(按科目类型切片,表达式示意):
279
+
280
+ | 度量名 | 表达式(示意) | 说明 |
281
+ | ---------------- | ------------------------------------------ | -------- |
282
+ | revenue | sumIf(amount_signed, account_type='收入') | 收入 |
283
+ | cost | sumIf(amount_signed, account_type='成本') | 营业成本 |
284
+ | expense | sumIf(amount_signed, account_type='费用') | 期间费用 |
285
+ | operating_profit | revenue - cost - expense | 营业利润 |
286
+ | profit_margin | if(revenue>0, operating_profit/revenue, 0) | 利润率 |
287
+
288
+ ### 4.2 AccountActualCube(科目实际 Cube)
289
+
290
+ **目的**:Account 对象 bind_source
291
+
292
+ **维度**:account_id, account_code, account_name, account_type, pl_category, account_level, fiscal_year, fiscal_period
293
+
294
+ **度量**:net_amount, line_count
295
+
296
+ **派生度量**:revenue, cost, expense(同上,按 account_type 聚合)
297
+
298
+ ### 4.3 CostCenterActualCube(成本中心实际 Cube)
299
+
300
+ **维度**:cost_center_id, cost_center_name, department, profit_center, fiscal_year, fiscal_period
301
+
302
+ **度量**:net_amount, line_count
303
+
304
+ **派生度量**:revenue, cost, expense, operating_profit, profit_margin
305
+
306
+ ### 4.4 BudgetCube(预算 Cube)
307
+
308
+ **事实源**:`budget_entry`
309
+
310
+ **维度**:budget_version, fiscal_year, fiscal_period, account_id, account_code, account_type, pl_category, cost_center_id, department
311
+
312
+ **度量**:
313
+
314
+ | 度量名 | 列名 | 聚合 | 说明 |
315
+ | ------------- | ------------- | ----- | -------- |
316
+ | budget_amount | budget_amount | sum | 预算金额 |
317
+ | budget_lines | line_id | count | 预算行数 |
318
+
319
+ **派生度量**:budget_revenue / budget_cost / budget_expense(按 account_type 切片,同 ActualCube 逻辑)
320
+
321
+ ### 4.5 TimeActualCube(时间维度 Cube)
322
+
323
+ **维度**:posting_date, fiscal_year, fiscal_period, year_month, quarter
324
+
325
+ **度量**:net_amount, revenue, cost, expense, operating_profit(派生)
326
+
327
+ ---
328
+
329
+ ## 五、本体层设计
330
+
331
+ ### 5.1 对象类型(6 种)
332
+
333
+ | 对象类型 code | 名称 | 说明 | bind_source |
334
+ | -------------- | -------- | ----------------------- | -------------------- |
335
+ | Account | 会计科目 | 科目业务对象 | AccountActualCube |
336
+ | CostCenter | 成本中心 | 组织/利润中心业务对象 | CostCenterActualCube |
337
+ | JournalEntry | 实际分录 | 凭证行/实际发生业务对象 | ActualCube |
338
+ | BudgetLine | 预算行 | 预算编制明细业务对象 | BudgetCube |
339
+ | ProfitAnalysis | 利润分析 | 多维度损益指标聚合对象 | ActualCube |
340
+ | BudgetAnalysis | 预算分析 | 预算执行与差异分析对象 | BudgetCube |
341
+
342
+ > 对象类型 `code` 使用稳定英文标识;与表名、Cube 名刻意区分。
343
+
344
+ ### 5.2 对象属性(摘要)
345
+
346
+ #### Account(会计科目)
347
+
348
+ | 属性 code | 名称 | semantic_role | qualified_name |
349
+ | ----------- | -------- | ------------- | ------------------------------- |
350
+ | id | 科目 ID | dimension | AccountActualCube.account_id |
351
+ | code | 科目编码 | dimension | AccountActualCube.account_code |
352
+ | name | 科目名称 | dimension | AccountActualCube.account_name |
353
+ | type | 科目类型 | dimension | AccountActualCube.account_type |
354
+ | pl_category | 损益大类 | dimension | AccountActualCube.pl_category |
355
+ | level | 科目层级 | dimension | AccountActualCube.account_level |
356
+ | net_amount | 实际发生 | measure | AccountActualCube.net_amount |
357
+ | revenue | 收入 | measure | AccountActualCube.revenue |
358
+ | cost | 成本 | measure | AccountActualCube.cost |
359
+ | expense | 费用 | measure | AccountActualCube.expense |
360
+
361
+ #### JournalEntry(实际分录)
362
+
363
+ | 属性 code | 名称 | semantic_role | qualified_name |
364
+ | ------------- | -------- | ------------- | ------------------------ |
365
+ | id | ID | dimension | ActualCube.line_id |
366
+ | posting_date | 记账日期 | dimension | ActualCube.posting_date |
367
+ | fiscal_period | 会计期间 | dimension | ActualCube.fiscal_period |
368
+ | net_amount | 损益金额 | measure | ActualCube.net_amount |
369
+ | debit | 借方 | measure | ActualCube.debit_total |
370
+ | credit | 贷方 | measure | ActualCube.credit_total |
371
+
372
+ #### BudgetLine(预算行)
373
+
374
+ | 属性 code | 名称 | semantic_role | qualified_name |
375
+ | ------------- | --------- | ------------- | ------------------------- |
376
+ | id | 预算行 ID | dimension | BudgetCube.line_id |
377
+ | version | 预算版本 | dimension | BudgetCube.budget_version |
378
+ | fiscal_period | 预算期间 | dimension | BudgetCube.fiscal_period |
379
+ | budget_amount | 预算金额 | measure | BudgetCube.budget_amount |
380
+
381
+ #### CostCenter / ProfitAnalysis / BudgetAnalysis
382
+
383
+ Cube 维/度量映射一致(约 **42** 个属性,实施时参照 init 脚本)。
384
+
385
+ ### 5.3 链接类型(10 种)
386
+
387
+ | 链接 code | 名称 | from | to | 说明 |
388
+ | ------------------------------ | ---------------- | -------------- | -------------- | ---------------- |
389
+ | entry_belongs_account | 分录归属科目 | JournalEntry | Account | 分录行对应科目 |
390
+ | entry_belongs_cost_center | 分录归属成本中心 | JournalEntry | CostCenter | 分录行对应组织 |
391
+ | budget_for_account | 预算对应科目 | BudgetLine | Account | 预算行对应科目 |
392
+ | budget_for_cost_center | 预算对应成本中心 | BudgetLine | CostCenter | 预算行对应组织 |
393
+ | account_has_parent | 科目上级 | Account | Account | 科目树父级 |
394
+ | budget_compared_to_actual | 预算对比实际 | BudgetAnalysis | ProfitAnalysis | 预实差异分析 |
395
+ | analysis_by_account | 分析归因科目 | ProfitAnalysis | Account | 指标按科目切片 |
396
+ | analysis_by_cost_center | 分析归因成本中心 | ProfitAnalysis | CostCenter | 指标按组织切片 |
397
+ | account_contributes_profit | 科目贡献利润 | Account | ProfitAnalysis | 科目损益贡献 |
398
+ | cost_center_contributes_profit | 组织贡献利润 | CostCenter | ProfitAnalysis | 成本中心利润贡献 |
399
+
400
+ ---
401
+
402
+ ## 六、本体函数设计
403
+
404
+ ### 6.1 函数清单
405
+
406
+ | 函数 ID | 功能 | 参数 |
407
+ | ---------------------------- | ------------ | ------------------------------------------------------------------ |
408
+ | profit.fn.get_summary | 利润总览 | start_date, end_date, cost_center_id(可选) |
409
+ | profit.fn.yoy_analysis | 同比分析 | start_date, end_date, pl_category(可选) |
410
+ | profit.fn.mom_analysis | 环比分析 | start_date, end_date, pl_category(可选) |
411
+ | profit.fn.budget_vs_actual | **预实对比** | fiscal_year, fiscal_period, budget_version, cost_center_id(可选) |
412
+ | profit.fn.account_breakdown | **科目结构** | start_date, end_date, account_level, pl_category(可选) |
413
+ | profit.fn.cost_center_profit | **组织利润** | start_date, end_date, department(可选) |
414
+ | profit.fn.top_accounts | 科目 Top N | limit, metric, start_date, end_date, account_type(可选) |
415
+
416
+ > 旧示例中的 `profit.fn.top_products`、`profit.fn.customer_segmentation` 属**订单/产品域**,本方案以 GL 为主,**不纳入**默认函数集;若需产品利润,可单独增加 `profit.fn.sales_reconciliation` 勾稽销售表。
417
+
418
+ ### 6.2 函数示例:profit.fn.get_summary
419
+
420
+ **功能**:期间损益总览(基于实际分录)
421
+
422
+ **返回值**:
423
+
424
+ | 字段名 | 类型 | 说明 |
425
+ | ---------------- | ------- | -------- |
426
+ | total_revenue | Float64 | 总收入 |
427
+ | total_cost | Float64 | 总成本 |
428
+ | total_expense | Float64 | 总费用 |
429
+ | operating_profit | Float64 | 营业利润 |
430
+ | profit_margin | Float64 | 利润率 |
431
+ | line_count | Int | 分录行数 |
432
+
433
+ ### 6.3 函数示例:profit.fn.budget_vs_actual
434
+
435
+ **功能**:按科目/成本中心对比预算与实际,输出差异与执行率
436
+
437
+ **参数**:fiscal_year, fiscal_period(可选 0=全年), budget_version, cost_center_id(可选)
438
+
439
+ **返回值**:
440
+
441
+ | 字段名 | 类型 | 说明 |
442
+ | ---------------- | ------- | ---------------------------------------- |
443
+ | account_code | String | 科目编码 |
444
+ | account_name | String | 科目名称 |
445
+ | pl_category | String | 损益大类 |
446
+ | cost_center_name | String | 成本中心 |
447
+ | budget_amount | Float64 | 预算额 |
448
+ | actual_amount | Float64 | 实际额 |
449
+ | variance | Float64 | 差异(实际-预算,收入/利润类为正向有利) |
450
+ | execution_rate | Float64 | 执行率(实际/预算) |
451
+
452
+ ### 6.4 函数示例:profit.fn.account_breakdown
453
+
454
+ **功能**:按科目层级展开损益结构(树形或扁平)
455
+
456
+ **返回值**:account_code, account_name, account_level, pl_category, revenue, cost, expense, net_impact, share_pct
457
+
458
+ ### 6.5 测试参数(test_arguments)
459
+
460
+ | 函数 ID | test_arguments 文件 | 默认 arguments 要点 |
461
+ | ---------------------------- | ----------------------------------------------------- | --------------------------------------------- |
462
+ | profit.fn.get_summary | `functions/test_arguments/profit.fn.get_summary.json` | 2025-01-01 ~ 2026-06-30 |
463
+ | profit.fn.yoy_analysis | `.../profit.fn.yoy_analysis.json` | 同上 |
464
+ | profit.fn.mom_analysis | `.../profit.fn.mom_analysis.json` | 同上 |
465
+ | profit.fn.budget_vs_actual | `.../profit.fn.budget_vs_actual.json` | fiscal_year=2026, budget_version=2026年度预算 |
466
+ | profit.fn.account_breakdown | `.../profit.fn.account_breakdown.json` | account_level=2 |
467
+ | profit.fn.cost_center_profit | `.../profit.fn.cost_center_profit.json` | 日期范围 |
468
+ | profit.fn.top_accounts | `.../profit.fn.top_accounts.json` | limit=10, metric=net_impact |
469
+
470
+ > 内置示例路径:`资源/examples/onto/利润示例/function/test_arguments/`(复制到实现单元 `functions/test_arguments/`)。
471
+
472
+ 批量入库(实现单元 `functions/` 下,dazi-work 根):
473
+
474
+ ```powershell
475
+ .\项目\潘达石化\本体\ontos\利润分析本体方案\functions\save_test_arguments.ps1
476
+ ```
477
+
478
+ > 参考实现:`资源/examples/onto/利润示例/`(init + seed + 7 个 `profit_fn_*.py` + test_arguments)。
479
+
480
+ ---
481
+
482
+ ## 七、实施计划
483
+
484
+ > **dazi-vscode v3** · 空间 `space__zlj` · 脚本目录 `setup/`、`functions/`
485
+ > 发布示例:
486
+ > `dazi onto script publish 项目/潘达石化/本体/ontos/利润分析本体方案/setup/profit_ontology_init.py --space space__zlj --type setup`
487
+
488
+ ### 7.1 阶段一:物理层(高)
489
+
490
+ | 步骤 | 任务 | 说明 |
491
+ | ---- | -------------------------- | ----------------------- |
492
+ | 1 | 创建 account_master | **科目表** |
493
+ | 2 | 创建 cost_center_dimension | 成本中心维表 |
494
+ | 3 | 创建 actual_journal_entry | **实际分录表** |
495
+ | 4 | 创建 budget_entry | **预算表** |
496
+ | 5 | 注册表到空间 | register + sync_columns |
497
+ | 6 | **注册表间关系(6 条)** | add_relationship |
498
+
499
+ ### 7.2 阶段二:Cube 层(高)
500
+
501
+ | 步骤 | 任务 |
502
+ | ---- | -------------------------------------------------- |
503
+ | 7 | 注册 ActualCube / AccountActualCube 等 5 个 Cube |
504
+ | 8 | 配置派生度量 revenue/cost/expense/operating_profit |
505
+
506
+ ### 7.3 阶段三:本体层(高)
507
+
508
+ | 步骤 | 任务 |
509
+ | ---- | --------------------------- |
510
+ | 9 | define_object_type(6 种) |
511
+ | 10 | bind_source |
512
+ | 11 | define_property(约 42 个) |
513
+ | 12 | define_link_type(10 种) |
514
+ | 13 | sync_metric_refs |
515
+
516
+ ### 7.4 阶段四:函数与数据(中)
517
+
518
+ | 步骤 | 任务 |
519
+ | ---- | --------------------------------------------------------------------------------- |
520
+ | 14 | 新建 `profit_ontology_init.py`、`profit_seed_data.py`(科目树+分录+预算演示数据) |
521
+ | 15 | 适配/新建 7 个 `profit_fn_*.py` |
522
+ | 16 | publish + run 验证 → save-test-arguments |
523
+
524
+ ### 7.5 快速实施路径
525
+
526
+ | 来源 | 可复用内容 | 需改造 |
527
+ | ---------------------------------------------------------- | --------------------------------- | ------------------------------ |
528
+ | `资源/examples/onto/利润示例/setup/profit_ontology_init.py` | GL 表/Cube/对象/链接完整 init | 按业务改 `space_id` 与科目口径 |
529
+ | `资源/examples/onto/利润示例/setup/profit_seed_data.py` | 科目树+分录+预算演示数据 | 同上 |
530
+ | `资源/examples/onto/利润示例/function/profit_fn_*.py` | 7 个 GL 分析函数 + test_arguments | 复制到 `functions/` 后发布 |
531
+ | `项目/潘达石化/本体/ontos/产品销售本体方案/` | init/seed/test_arguments 工程模式 | 表结构与函数逻辑不同 |
532
+
533
+ **seed 数据建议**:
534
+
535
+ - 科目:一级 4–6 个(收入/成本/费用),二级 15–20 个
536
+ - 成本中心:5–8 个(生产/销售/管理/财务等)
537
+ - 实际分录:2025-01 ~ 2026-06 按月随机凭证,2000+ 行
538
+ - 预算:同科目×成本中心×12 期,与 actual 同维便于预实对比
539
+
540
+ ---
541
+
542
+ ## 八、风险与验收
543
+
544
+ ### 8.1 风险
545
+
546
+ | 等级 | 风险 | 缓解措施 |
547
+ | ---- | ------------------------- | ------------------------------------------- |
548
+ | 中 | 与销售域共用 space\_\_zlj | init 仅创建利润域 4 表 |
549
+ | 中 | 借贷方向与符号口径不一致 | seed 统一计算 `amount_signed`;文档约定口径 |
550
+ | 中 | 预实 JOIN 期间粒度不一致 | budget/actual 均含 fiscal_year/period |
551
+ | 低 | 旧示例订单模型混淆 | 规划与函数 ID 明确以 GL 为准 |
552
+
553
+ ### 8.2 验收标准
554
+
555
+ | 验收项 | 标准 |
556
+ | ------------ | ------------------------------------------------------- |
557
+ | 科目表 | account_master 可查询,树形 parent 可解析 |
558
+ | 实际分录表 | actual_journal_entry 可查询,损益汇总与手工 SQL 一致 |
559
+ | 预算表 | budget_entry 可查询,与 actual 同维可 JOIN |
560
+ | **表间关系** | **6 条**注册成功 |
561
+ | Cube | 5 个 Cube + 派生度量可算 |
562
+ | 对象类型 | 6 种对象 + 10 种链接 |
563
+ | 函数 | get_summary、budget_vs_actual、account_breakdown 可运行 |
564
+ | 数据 | 种子灌入后预实差异可解释 |
565
+
566
+ ---
567
+
568
+ ## 九、预期成果
569
+
570
+ | 分析能力 | 说明 |
571
+ | ------------ | ------------------------------ |
572
+ | 损益总览 | 收入/成本/费用/营业利润/利润率 |
573
+ | 科目结构 | 按层级与 pl_category 展开 |
574
+ | **预实分析** | 预算、实际、差异、执行率 |
575
+ | 组织利润 | 成本中心/部门贡献 |
576
+ | 趋势分析 | 同比、环比 |
577
+ | 科目排行 | Top N 异动科目 |
578
+ | 业务语义 | 科目—分录—预算—组织链接可导航 |
579
+
580
+ ---
581
+
582
+ ## 十、相关文档
583
+
584
+ | 文档 | 路径 |
585
+ | ---------------------------------- | ----------------------------------------------------------------------------- |
586
+ | 快速启动 | [../快速启动\_利润分析本体方案.md](../快速启动_利润分析本体方案.md) |
587
+ | 本体规划指南 | `资源/docs/onto/本体规划指南.md` |
588
+ | 规划示例(产品销售,推荐工程模式) | `资源/docs/onto/规划示例_产品销售本体规划方案.md` |
589
+ | 规划示例(利润·GL 域) | `资源/docs/onto/规划示例_利润分析本体方案.md` |
590
+ | **内置脚本示例** | `资源/examples/onto/利润示例/`(`dazi examples sync`) |
591
+
592
+ ---
593
+
594
+ **方案版本**:v2.0
595
+ **创建日期**:2026-06-06
596
+ **适用空间**:`space__zlj`
597
+ **状态**:已落地