@dazitech/cli 3.0.7 → 3.0.9

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 (70) hide show
  1. package/README.md +1 -1
  2. package/dist/clis/dazi-app.js +1 -1
  3. package/dist/clis/dazi-flow.js +1 -1
  4. package/dist/clis/dazi-onto.js +73 -22
  5. package/dist/clis/dazi.js +266 -171
  6. package/dist/docs/flow/flow-project-guide.md +1 -1
  7. package/dist/docs/guides/quickstart.md +18 -4
  8. package/dist/docs/guides/troubleshooting.md +12 -1
  9. package/dist/docs/guides/workspace-v3.md +43 -23
  10. package/dist/docs/index.json +28 -3
  11. package/dist/docs/onto/action-guide.md +3 -3
  12. package/dist/docs/onto/dazi_script_sdk_reference.md +244 -174
  13. package/dist/docs/onto/dazi_script_seed_data_guide.md +158 -155
  14. package/dist/docs/onto/function-guide.md +82 -27
  15. package/dist/docs/onto/space-management.md +3 -1
  16. package/dist/docs/onto//346/234/254/344/275/223/345/210/206/347/261/273/350/247/204/345/210/222/344/270/216SDK/346/211/251/345/261/225/346/226/271/346/241/210.md +168 -0
  17. package/dist/docs/onto//346/234/254/344/275/223/345/221/275/345/220/215/350/247/204/350/214/203_/347/211/251/347/220/206/350/241/250Cube/344/270/216/345/257/271/350/261/241.md +402 -0
  18. 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 +200 -34
  19. package/dist/docs/onto//346/234/254/344/275/223/350/247/204/345/210/222/346/214/207/345/215/227.md +188 -38
  20. package/dist/docs/onto//350/204/232/346/234/254/350/277/220/350/241/214/347/272/240/351/224/231_/345/225/206/345/212/241/346/210/220/346/234/254/346/226/271/346/241/210/345/274/200/345/217/221/350/277/207/347/250/213.md +213 -0
  21. 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 +620 -0
  22. 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 +680 -541
  23. package/dist/examples/index.json +208 -22
  24. package/dist/examples/onto/README.md +51 -0
  25. package/dist/examples/onto/_templates/ontology_function_template.py +50 -0
  26. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_account_breakdown.py +62 -0
  27. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_budget_vs_actual.py +69 -0
  28. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_cost_center_profit.py +64 -0
  29. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_get_summary.py +61 -0
  30. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_mom_analysis.py +82 -0
  31. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_top_accounts.py +61 -0
  32. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_yoy_analysis.py +79 -0
  33. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/save_test_arguments.ps1 +38 -0
  34. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.account_breakdown.json +1 -0
  35. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.budget_vs_actual.json +1 -0
  36. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.cost_center_profit.json +1 -0
  37. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.get_summary.json +1 -0
  38. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.mom_analysis.json +1 -0
  39. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.top_accounts.json +1 -0
  40. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.yoy_analysis.json +1 -0
  41. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_ontology_init.py +679 -0
  42. package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_seed_data.py +216 -0
  43. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_channel_mix.py +89 -0
  44. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_customer_segmentation.py +121 -0
  45. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_get_summary.py +78 -0
  46. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_mom_analysis.py +89 -0
  47. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_region_breakdown.py +84 -0
  48. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_top_products.py +98 -0
  49. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_yoy_analysis.py +87 -0
  50. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/save_test_arguments.ps1 +38 -0
  51. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.channel_mix.json +5 -0
  52. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.customer_segmentation.json +5 -0
  53. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.get_summary.json +5 -0
  54. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.mom_analysis.json +5 -0
  55. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.region_breakdown.json +5 -0
  56. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.top_products.json +5 -0
  57. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.yoy_analysis.json +5 -0
  58. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_ontology_init.py +539 -0
  59. package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_seed_data.py +163 -0
  60. package/dist/prompts/index.json +2 -2
  61. package/dist/prompts/onto/action-design.md +4 -1
  62. package/dist/prompts/onto/function-design.md +46 -19
  63. package/dist/prompts/onto/rule-seed.md +5 -1
  64. package/dist/prompts/onto/script-publish-run.md +87 -25
  65. package/package.json +1 -1
  66. package/dist/examples/onto/function/profit_fn_customer_segmentation.py +0 -117
  67. package/dist/examples/onto/function/profit_fn_mom_analysis.py +0 -89
  68. package/dist/examples/onto/function/profit_fn_top_products.py +0 -89
  69. package/dist/examples/onto/function/profit_fn_yoy_analysis.py +0 -89
  70. package/dist/examples/onto/setup/profit_ontology_init.py +0 -388
@@ -1,541 +1,680 @@
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
+ **文档 ID**: `onto/planning-example-profit`
4
+
5
+ > 表/Cube/对象命名遵循 [本体命名规范](./本体命名规范_物理表Cube与对象.md)。
6
+ > 工程模式参考:[规划示例_产品销售本体规划方案.md](./规划示例_产品销售本体规划方案.md)
7
+
8
+ **占位符**(复制到工作区 `plans/` 后替换为实际值):
9
+
10
+ | 占位符 | 含义 | 权威来源 |
11
+ | ------ | ---- | -------- |
12
+ | `<space-id>` | 数据空间 ID | `项目/<业务名>/本体/ontos/<实现名>/README.md` |
13
+ | `<业务名>` | 业务项目目录名 | 工作区 `项目/` 下文件夹 |
14
+ | `<实现名>` | 本体实现单元名 | 如 `利润分析本体方案` |
15
+ | `<item-path>` | 实现单元根路径 | `项目/<业务名>/本体/ontos/<实现名>` |
16
+
17
+ ---
18
+
19
+ ## 一、空间概述
20
+
21
+ | 项目 | 说明 |
22
+ | -------- | ---- |
23
+ | 空间 ID | `<space-id>`(示例:`space__acme`) |
24
+ | 空间名称 | `<空间名称>`(与平台数据空间显示名一致) |
25
+ | 空间状态 | 可与销售等其他域**共用**同一 `<space-id>`;利润域表/Cube/对象独立建设 |
26
+ | 规划日期 | (填写定稿日期) |
27
+ | 实现单元 | `<item-path>/` |
28
+
29
+ > **共存说明**:本空间可能已有销售域 `dim_product`、`fact_sales_order_line` 等表。利润域 init **仅**创建/注册本域表;若 **`dim_date`** 已存在则 **ensure** 不重复建表。
30
+ > **与销售域关系**:销售收入可通过科目映射与实际分录对齐;勾稽函数可 LEFT JOIN `fact_sales_order_line`,**本体对象不以订单为主锚点**。
31
+
32
+ ---
33
+
34
+ ## 二、需求分析
35
+
36
+ ### 2.1 业务背景
37
+
38
+ 某制造企业(示例)财务利润分析场景,需构建**利润分析本体**,以业务语言描述「会计科目」「实际分录」「预算」及其关系,支撑:
39
+
40
+ - **损益总览**:收入、成本、费用、营业利润、利润率
41
+ - **科目结构**:按科目层级展开收入/成本/费用构成
42
+ - **实际 vs 预算**:按期间、科目、成本中心的预算执行率与差异分析
43
+ - **组织维度**:按成本中心/部门贡献与超支预警
44
+ - **趋势分析**:同比、环比利润与关键科目走势
45
+ - **归因分析**:利润变动由哪些科目/组织驱动
46
+
47
+ ### 2.2 核心业务问题
48
+
49
+ | 问题 | 说明 |
50
+ | --------------- | ------------------------------ |
51
+ | 利润是多少 | 期间营业利润、利润率 |
52
+ | 钱从哪来/到哪去 | 收入、成本、费用科目结构 |
53
+ | 预算执行如何 | 预算额、实际额、差异额、执行率 |
54
+ | 哪个部门拖后腿 | 成本中心维度利润与预算偏差 |
55
+ | 哪些科目异常 | 科目 Top N、同比/环比异动 |
56
+ | 趋势如何 | 按月/季/年利润与关键科目走势 |
57
+
58
+ ### 2.3 规划范围说明
59
+
60
+ **物理表清单**(命名见 [命名规范 §2](./本体命名规范_物理表Cube与对象.md#2-物理表类别与前缀)):
61
+
62
+ | 表名 | 前缀类别 | **347 分类** | 显示名 | 说明 | 角色 |
63
+ | ---- | -------- | ------------ | ------ | ---- | ---- |
64
+ | **`dim_date`** | 时间维(**强制**) | **时间维** | 日期维表 | 与 sales 域共用;PK **`date_key`** | 时间维 |
65
+ | `dim_account` | dim | **维度表** | 科目维表 | 会计科目主数据 | dim |
66
+ | `dim_cost_center` | dim | **维度表** | 成本中心维表 | 组织/利润中心主数据 | dim |
67
+ | **`fact_gl_journal_entry`** | fact | **事实表** | 总账实际分录 | 凭证行粒度损益流水;含 **date_key** | fact |
68
+ | **`fact_budget_entry`** | fact | **事实表** | 预算明细 | 预算行粒度编制数据;含 **date_key** | fact |
69
+
70
+ > 规划原则:先定义 **Account、JournalEntry、BudgetLine** 等业务对象与链接,再反推 Cube 与物理表。旧版 `profit_analysis_fact`(订单粒度)**不作为主模型**。
71
+ > init 脚本 `资源/examples/onto/利润示例/setup/profit_ontology_init.py` 顶部 **`TABLE_REGISTRY`** 与上表及 §3.x 字段 **显示名/说明** 一一对应,经 `s.tables.register_with_meta` 注册;**`CATEGORY_REGISTRY`** 见 [附录 B](#附录-b347-分类挂载对照表category_registry)。
72
+
73
+ ## 三、物理层设计
74
+
75
+ ### 3.0 时间维:dim_date(强制)
76
+
77
+ 与销售域共用;字段定义见 [销售规划示例 §3.0](./规划示例_产品销售本体规划方案.md#30-时间维dim_date强制)。分录 **`date_key = toYYYYMMDD(posting_date)`**;预算行可用 **`toYYYYMMDD(toDate(concat(toString(fiscal_year), '-', lpad(toString(fiscal_period), 2, '0'), '-01')))`** 或 period 末代表日,规划须写清口径。
78
+
79
+ ### 3.1 维度表:dim_account
80
+
81
+ **用途**:会计科目主数据;科目树与损益口径。
82
+
83
+ | 字段名 | 类型 | 显示名 | 说明 |
84
+ | ------ | ---- | ------ | ---- |
85
+ | account_id | String | 科目 ID | 主键 |
86
+ | account_code | String | 科目编码 | |
87
+ | account_name | String | 科目名称 | |
88
+ | account_type | String | 科目类型 | 资产/负债/权益/收入/成本/费用 |
89
+ | pl_category | String | 损益大类 | |
90
+ | parent_account_id | String | 上级科目 | 关联 dim_account |
91
+ | account_level | Int32 | 层级 | |
92
+ | is_leaf | Boolean | 末级 | |
93
+ | normal_balance | String | 余额方向 | 借/贷 |
94
+ | status | String | 状态 | 启用/停用 |
95
+ | created_at | DateTime | 创建时间 | |
96
+
97
+ **排序键**:`ORDER BY (account_code)`
98
+
99
+ ### 3.2 维度表:dim_cost_center
100
+
101
+ | 字段名 | 类型 | 显示名 | 说明 |
102
+ | ------ | ---- | ------ | ---- |
103
+ | cost_center_id | String | 成本中心 ID | 主键 |
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
+ ### 3.3 事实表:fact_gl_journal_entry
113
+
114
+ **用途**:总账凭证行实际发生额;ActualCube / JournalEntry 主源。
115
+
116
+ | 字段名 | 类型 | 显示名 | 说明 |
117
+ | ------ | ---- | ------ | ---- |
118
+ | entry_id | String | 凭证 ID | |
119
+ | line_id | String | 凭证行 ID | 主键组合 |
120
+ | **date_key** | Int32 | 日期键 | 关联 dim_date,YYYYMMDD |
121
+ | posting_date | Date | 记账日期 | |
122
+ | fiscal_year | Int32 | 会计年度 | |
123
+ | fiscal_period | Int32 | 会计期间 | 1–12 |
124
+ | account_id | String | 科目 ID | 关联 dim_account |
125
+ | account_code | String | 科目编码 | 冗余 |
126
+ | account_name | String | 科目名称 | 冗余 |
127
+ | account_type | String | 科目类型 | 冗余 |
128
+ | pl_category | String | 损益大类 | 冗余 |
129
+ | account_level | Int32 | 科目层级 | 冗余 |
130
+ | cost_center_id | String | 成本中心 ID | 关联 dim_cost_center |
131
+ | cost_center_name | String | 成本中心 | 冗余 |
132
+ | department | String | 部门 | 冗余 |
133
+ | profit_center | String | 利润中心 | 冗余 |
134
+ | debit_amount | Float64 | 借方 | |
135
+ | credit_amount | Float64 | 贷方 | |
136
+ | amount_signed | Float64 | 损益金额 | 收入为正、成本费用为负 |
137
+ | currency | String | 币种 | |
138
+ | voucher_no | String | 凭证号 | |
139
+ | source_system | String | 来源系统 | |
140
+ | description | String | 摘要 | 凭证行摘要文本 |
141
+ | created_at | DateTime | 创建时间 | |
142
+
143
+ **排序键**:`ORDER BY (date_key, entry_id, line_id)`
144
+
145
+ **损益符号 `amount_signed`**(seed 统一计算):
146
+
147
+ | account_type | 规则 | 含义 |
148
+ | ------------ | ---- | ---- |
149
+ | 收入 | credit - debit | 正=收入增加 |
150
+ | 成本/费用 | debit - credit | 正=成本/费用发生 |
151
+
152
+ ### 3.4 事实表:fact_budget_entry
153
+
154
+ | 字段名 | 类型 | 显示名 | 说明 |
155
+ | ------ | ---- | ------ | ---- |
156
+ | budget_id | String | 预算批次 | |
157
+ | line_id | String | 预算行 ID | 主键组合 |
158
+ | **date_key** | Int32 | 日期键 | 关联 dim_date(期间代表日) |
159
+ | budget_version | String | 预算版本 | |
160
+ | fiscal_year | Int32 | 预算年度 | |
161
+ | fiscal_period | Int32 | 预算期间 | 1–12;0=全年 |
162
+ | account_id | String | 科目 ID | 关联 dim_account |
163
+ | account_code | String | 科目编码 | 冗余 |
164
+ | account_name | String | 科目名称 | 冗余 |
165
+ | account_type | String | 科目类型 | 冗余 |
166
+ | pl_category | String | 损益大类 | 冗余 |
167
+ | cost_center_id | String | 成本中心 ID | 关联 dim_cost_center |
168
+ | cost_center_name | String | 成本中心 | 冗余 |
169
+ | department | String | 部门 | 冗余 |
170
+ | budget_amount | Float64 | 预算金额 | |
171
+ | currency | String | 币种 | |
172
+ | status | String | 状态 | 草稿/已发布 |
173
+ | created_at | DateTime | 创建时间 | |
174
+
175
+ **排序键**:`ORDER BY (fiscal_year, fiscal_period, account_id, line_id)`
176
+
177
+ ### 3.5 表间关系设计(物理层 · 数据空间)
178
+
179
+ > **与本体链接的区别**:本节描述 **ClickHouse 物理表 FK/JOIN**,供侧栏 **数据资源 → 表间关系** 与 AI 多表 JOIN。本体链接见第五节。
180
+
181
+ **关系模型**:
182
+
183
+ ```text
184
+ dim_date
185
+ (date_key PK)
186
+ ▲ ▲
187
+ │ │
188
+ date_key │ │ date_key
189
+ │ │
190
+ dim_account ◄────┼────┼────► dim_cost_center
191
+ (account_id PK) │ │ (cost_center_id PK)
192
+ ▲ │ │ ▲
193
+ │ │ │ │
194
+ │ fact_gl_journal_entry │
195
+ │ (分录行) │
196
+ │ │ │ │
197
+ │ │ fact_budget_entry
198
+ │ │ (预算行)
199
+ │ └─────────┘
200
+
201
+ dim_account.parent_account_id ──► dim_account(自关联)
202
+ ```
203
+
204
+ **关系清单(8 条)**:
205
+
206
+ | 关系 ID | **347 分类** | 类型 | 从表 | 到表 | join_keys | 说明 |
207
+ | ------- | ------------ | ---- | ---- | ---- | --------- | ---- |
208
+ | rel_journal_date | **时间关联** | many_to_one | fact_gl_journal_entry | dim_date | date_key → date_key | 分录关联日历 |
209
+ | rel_budget_date | **时间关联** | many_to_one | fact_budget_entry | dim_date | date_key → date_key | 预算关联日历 |
210
+ | rel_journal_account | **主数据关联** | many_to_one | fact_gl_journal_entry | dim_account | account_id → account_id | 分录→科目 |
211
+ | rel_journal_cost_center | **主数据关联** | many_to_one | fact_gl_journal_entry | dim_cost_center | cost_center_id → cost_center_id | 分录→成本中心 |
212
+ | rel_budget_account | **主数据关联** | many_to_one | fact_budget_entry | dim_account | account_id → account_id | 预算→科目 |
213
+ | rel_budget_cost_center | **主数据关联** | many_to_one | fact_budget_entry | dim_cost_center | cost_center_id → cost_center_id | 预算→成本中心 |
214
+ | rel_account_parent | **层级自关联** | many_to_one | dim_account | dim_account | parent_account_id → account_id | 科目树 |
215
+ | rel_budget_actual | **预实关联** | many_to_one | fact_budget_entry | fact_gl_journal_entry | account_id+cost_center_id+fiscal_period | 预实同维(语义 JOIN;**init 待补**) |
216
+
217
+ **实施 API**(`setup/profit_ontology_init.py`):
218
+
219
+ ```python
220
+ s.tables.add_relationship(
221
+ from_table="fact_gl_journal_entry",
222
+ to_table="dim_account",
223
+ join_sql="fact_gl_journal_entry.account_id = dim_account.account_id",
224
+ join_keys=[{"from": "account_id", "to": "account_id"}],
225
+ relationship_type="many_to_one",
226
+ description="实际分录关联会计科目",
227
+ )
228
+ ```
229
+
230
+ **与本体链接对照**:
231
+
232
+ | 物理表关系 | 对应本体链接(业务层) |
233
+ | ---------------------- | ------------------------- |
234
+ | rel_journal_account | entry_belongs_account |
235
+ | rel_journal_cost_center | entry_belongs_cost_center |
236
+ | rel_budget_account | budget_for_account |
237
+ | rel_budget_cost_center | budget_for_cost_center |
238
+ | rel_account_parent | account_has_parent |
239
+ | rel_budget_actual | budget_compared_to_actual |
240
+
241
+ ---
242
+
243
+ ## 四、Cube 层设计
244
+
245
+ ### 4.0 Cube 清单与类别
246
+
247
+ | Cube | 类别 | **347 分类** | 事实源 | 时间维 | 支撑对象 |
248
+ | ---- | ---- | ------------ | ------ | ------ | -------- |
249
+ | **ActualCube** | Process | **流程型** | fact_gl_journal_entry | dim_date + fiscal_* | JournalEntry, ProfitAnalysis |
250
+ | **AccountActualCube** | Subject | **主体型** | fact_gl_journal_entry | 同上 | Account |
251
+ | **CostCenterActualCube** | Subject | **主体型** | fact_gl_journal_entry | 同上 | CostCenter |
252
+ | **BudgetCube** | Process | **流程型** | fact_budget_entry | 同上 | BudgetLine, BudgetAnalysis |
253
+ | **TimeActualCube** | Subject | **主体型** | fact_gl_journal_entry | 同上 | (时间切片;**init 已注册**,347 建议后续并入 Process) |
254
+ | **BudgetVsActualCube** | Comparison | **对比型** | fact_budget_entry + fact_gl_journal_entry | fiscal_year/period | BudgetAnalysis(预实;**规划目标,init 待补**) |
255
+
256
+ > 347 §4.2 建议时间增强不单独建 Cube;当前示例 init 仍含 **TimeActualCube** 并归 **主体型**,后续可合并进 ActualCube 后从注册表移除。
257
+
258
+ ### 4.1 ActualCube(Process · 实际发生主 Cube)
259
+
260
+ **事实源**:`fact_gl_journal_entry`
261
+
262
+ **维度**:entry_id, line_id, date_key, posting_date, fiscal_year, fiscal_period, year, month, account_id, account_code, account_name, account_type, pl_category, cost_center_id, cost_center_name, department, voucher_no, source_system
263
+
264
+ **度量**:
265
+
266
+ | 度量名 | 列名 | 聚合 | 说明 |
267
+ | ------------ | ------------- | ----- | ---------- |
268
+ | debit_total | debit_amount | sum | 借方合计 |
269
+ | credit_total | credit_amount | sum | 贷方合计 |
270
+ | net_amount | amount_signed | sum | 损益符号额 |
271
+ | line_count | line_id | count | 分录行数 |
272
+
273
+ **派生度量**(按科目类型切片,表达式示意):
274
+
275
+ | 度量名 | 表达式(示意) | 说明 |
276
+ | ---------------- | ------------------------------------------ | -------- |
277
+ | revenue | sumIf(amount_signed, account_type='收入') | 收入 |
278
+ | cost | sumIf(amount_signed, account_type='成本') | 营业成本 |
279
+ | expense | sumIf(amount_signed, account_type='费用') | 期间费用 |
280
+ | operating_profit | revenue - cost - expense | 营业利润 |
281
+ | profit_margin | if(revenue>0, operating_profit/revenue, 0) | 利润率 |
282
+
283
+ ### 4.2 AccountActualCube(Subject · 科目实际)
284
+
285
+ **目的**:Account 对象 bind_source
286
+
287
+ **维度**:account_id, account_code, account_name, account_type, pl_category, account_level, fiscal_year, fiscal_period, year, month
288
+
289
+ **度量**:net_amount, line_count
290
+
291
+ **派生度量**:revenue, cost, expense
292
+
293
+ ### 4.3 CostCenterActualCube(Subject · 成本中心实际)
294
+
295
+ **维度**:cost_center_id, cost_center_name, department, profit_center, fiscal_year, fiscal_period, year, month
296
+
297
+ **度量**:net_amount, line_count
298
+
299
+ **派生度量**:revenue, cost, expense, operating_profit, profit_margin
300
+
301
+ ### 4.4 BudgetCube(Process · 预算)
302
+
303
+ **事实源**:`fact_budget_entry`
304
+
305
+ **维度**:budget_version, fiscal_year, fiscal_period, date_key, account_id, account_code, account_type, pl_category, cost_center_id, department
306
+
307
+ **度量**:budget_amount(sum), budget_lines(count)
308
+
309
+ **派生度量**:budget_revenue / budget_cost / budget_expense(按 account_type 切片)
310
+
311
+ ### 4.5 BudgetVsActualCube(Comparison · 预实对比)
312
+
313
+ **目的**:`profit.fn.budget_vs_actual` 读模型;JOIN budget 与 actual 同维(account + cost_center + fiscal_period)
314
+
315
+ **维度**:fiscal_year, fiscal_period, account_id, cost_center_id, budget_version
316
+
317
+ **度量**:budget_amount, actual_amount(来自 ActualCube 聚合), variance, execution_rate(派生)
318
+
319
+ ---
320
+
321
+ ## 五、本体层设计
322
+
323
+ ### 5.1 对象类型(6 种)
324
+
325
+ | code | 分类 | **347 分类** | 名称 | bind_source | 主物理支撑 |
326
+ | ---- | ---- | ------------ | ---- | ------------- | ---------- |
327
+ | Account | Master | **主数据** | 会计科目 | AccountActualCube | dim_account |
328
+ | CostCenter | Master | **主数据** | 成本中心 | CostCenterActualCube | dim_cost_center |
329
+ | JournalEntry | Transaction | **事务** | 实际分录 | ActualCube | fact_gl_journal_entry |
330
+ | BudgetLine | Transaction | **事务** | 预算行 | BudgetCube | fact_budget_entry |
331
+ | ProfitAnalysis | Analytical | **分析** | 利润分析 | ActualCube | fact_gl_journal_entry |
332
+ | BudgetAnalysis | Analytical | **分析** | 预算分析 | BudgetCube / BudgetVsActualCube | fact_budget_entry |
333
+
334
+ > 对象 **`code` 无前缀**;分类见 [命名规范 §5.3](./本体命名规范_物理表Cube与对象.md#53-对象分类无-code-前缀)。
335
+
336
+ ### 5.2 对象属性(摘要)
337
+
338
+ #### Account(会计科目)
339
+
340
+ | 属性 code | 名称 | semantic_role | qualified_name |
341
+ | ----------- | -------- | ------------- | ------------------------------- |
342
+ | id | 科目 ID | dimension | AccountActualCube.account_id |
343
+ | code | 科目编码 | dimension | AccountActualCube.account_code |
344
+ | name | 科目名称 | dimension | AccountActualCube.account_name |
345
+ | type | 科目类型 | dimension | AccountActualCube.account_type |
346
+ | pl_category | 损益大类 | dimension | AccountActualCube.pl_category |
347
+ | level | 科目层级 | dimension | AccountActualCube.account_level |
348
+ | net_amount | 实际发生 | measure | AccountActualCube.net_amount |
349
+ | revenue | 收入 | measure | AccountActualCube.revenue |
350
+ | cost | 成本 | measure | AccountActualCube.cost |
351
+ | expense | 费用 | measure | AccountActualCube.expense |
352
+
353
+ #### JournalEntry(实际分录)
354
+
355
+ | 属性 code | 名称 | semantic_role | qualified_name |
356
+ | ------------- | -------- | ------------- | ------------------------ |
357
+ | id | 行 ID | dimension | ActualCube.line_id |
358
+ | posting_date | 记账日期 | dimension | ActualCube.posting_date |
359
+ | fiscal_period | 会计期间 | dimension | ActualCube.fiscal_period |
360
+ | net_amount | 损益金额 | measure | ActualCube.net_amount |
361
+ | debit | 借方 | measure | ActualCube.debit_total |
362
+ | credit | 贷方 | measure | ActualCube.credit_total |
363
+
364
+ #### BudgetLine(预算行)
365
+
366
+ | 属性 code | 名称 | semantic_role | qualified_name |
367
+ | ------------- | --------- | ------------- | ------------------------- |
368
+ | id | 预算行 ID | dimension | BudgetCube.line_id |
369
+ | version | 预算版本 | dimension | BudgetCube.budget_version |
370
+ | fiscal_period | 预算期间 | dimension | BudgetCube.fiscal_period |
371
+ | budget_amount | 预算金额 | measure | BudgetCube.budget_amount |
372
+
373
+ #### CostCenter / ProfitAnalysis / BudgetAnalysis
374
+
375
+ Cube 维/度量映射一致(约 **42** 个属性);实施参照 `利润示例/setup/profit_ontology_init.py`(表名按本规划迁移)。
376
+
377
+ ### 5.3 链接类型(10 种)
378
+
379
+ | 链接 code | **347 分类** | 名称 | from | to | 说明 |
380
+ | ------------------------------ | ------------ | ---------------- | -------------- | -------------- | ---------------- |
381
+ | entry_belongs_account | **归属关系** | 分录归属科目 | JournalEntry | Account | 分录行对应科目 |
382
+ | entry_belongs_cost_center | **归属关系** | 分录归属成本中心 | JournalEntry | CostCenter | 分录行对应组织 |
383
+ | budget_for_account | **归属关系** | 预算对应科目 | BudgetLine | Account | 预算行对应科目 |
384
+ | budget_for_cost_center | **归属关系** | 预算对应成本中心 | BudgetLine | CostCenter | 预算行对应组织 |
385
+ | account_has_parent | **层级关系** | 科目上级 | Account | Account | 科目树父级 |
386
+ | budget_compared_to_actual | **对比关系** | 预算对比实际 | BudgetAnalysis | ProfitAnalysis | 预实差异分析 |
387
+ | analysis_by_account | **分析归因** | 分析归因科目 | ProfitAnalysis | Account | 指标按科目切片 |
388
+ | analysis_by_cost_center | **分析归因** | 分析归因成本中心 | ProfitAnalysis | CostCenter | 指标按组织切片 |
389
+ | account_contributes_profit | **分析归因** | 科目贡献利润 | Account | ProfitAnalysis | 科目损益贡献 |
390
+ | cost_center_contributes_profit | **分析归因** | 组织贡献利润 | CostCenter | ProfitAnalysis | 成本中心利润贡献 |
391
+
392
+ ### 5.4 三层对照总表
393
+
394
+ | 对象 code | 分类 | Cube | 主 fact / dim |
395
+ | --------- | ---- | ---- | ------------- |
396
+ | Account | Master | AccountActualCube | dim_account |
397
+ | CostCenter | Master | CostCenterActualCube | dim_cost_center |
398
+ | JournalEntry | Transaction | ActualCube | fact_gl_journal_entry |
399
+ | BudgetLine | Transaction | BudgetCube | fact_budget_entry |
400
+ | ProfitAnalysis | Analytical | ActualCube | fact_gl_journal_entry |
401
+ | BudgetAnalysis | Analytical | BudgetVsActualCube | fact_budget_entry + fact_gl_journal_entry |
402
+ | (时间) | | ActualCube / BudgetCube | dim_date |
403
+
404
+ ---
405
+
406
+ ## 六、本体函数设计
407
+
408
+ ### 6.1 函数清单
409
+
410
+ | 函数 ID | **347 分类** | 功能 | 参数 |
411
+ | ---------------------------- | ------------ | ------------ | ------------------------------------------------------------------ |
412
+ | profit.fn.get_summary | **总览分析** | 利润总览 | start_date, end_date, cost_center_id(可选) |
413
+ | profit.fn.yoy_analysis | **趋势分析** | 同比分析 | start_date, end_date, pl_category(可选) |
414
+ | profit.fn.mom_analysis | **趋势分析** | 环比分析 | start_date, end_date, pl_category(可选) |
415
+ | profit.fn.budget_vs_actual | **预实分析** | **预实对比** | fiscal_year, fiscal_period, budget_version, cost_center_id(可选) |
416
+ | profit.fn.account_breakdown | **结构分析** | **科目结构** | start_date, end_date, account_level, pl_category(可选) |
417
+ | profit.fn.cost_center_profit | **组织分析** | **组织利润** | start_date, end_date, department(可选) |
418
+ | profit.fn.top_accounts | **结构分析** | 科目 Top N | limit, metric, start_date, end_date, account_type(可选) |
419
+
420
+ > 旧示例中的 `profit.fn.top_products`、`profit.fn.customer_segmentation` 属**订单/产品域**,本方案以 GL 为主,**不纳入**默认函数集;若需产品利润,可单独增加 `profit.fn.sales_reconciliation` 勾稽销售表。
421
+
422
+ ### 6.2 函数示例:profit.fn.get_summary
423
+
424
+ **功能**:期间损益总览(基于实际分录)
425
+
426
+ **返回值**:
427
+
428
+ | 字段名 | 类型 | 说明 |
429
+ | ---------------- | ------- | -------- |
430
+ | total_revenue | Float64 | 总收入 |
431
+ | total_cost | Float64 | 总成本 |
432
+ | total_expense | Float64 | 总费用 |
433
+ | operating_profit | Float64 | 营业利润 |
434
+ | profit_margin | Float64 | 利润率 |
435
+ | line_count | Int | 分录行数 |
436
+
437
+ ### 6.3 函数示例:profit.fn.budget_vs_actual
438
+
439
+ **功能**:按科目/成本中心对比预算与实际,输出差异与执行率
440
+
441
+ **参数**:fiscal_year, fiscal_period(可选 0=全年), budget_version, cost_center_id(可选)
442
+
443
+ **返回值**:
444
+
445
+ | 字段名 | 类型 | 说明 |
446
+ | ---------------- | ------- | ---------------------------------------- |
447
+ | account_code | String | 科目编码 |
448
+ | account_name | String | 科目名称 |
449
+ | pl_category | String | 损益大类 |
450
+ | cost_center_name | String | 成本中心 |
451
+ | budget_amount | Float64 | 预算额 |
452
+ | actual_amount | Float64 | 实际额 |
453
+ | variance | Float64 | 差异(实际-预算,收入/利润类为正向有利) |
454
+ | execution_rate | Float64 | 执行率(实际/预算) |
455
+
456
+ ### 6.4 函数示例:profit.fn.account_breakdown
457
+
458
+ **功能**:按科目层级展开损益结构(树形或扁平)
459
+
460
+ **返回值**:account_code, account_name, account_level, pl_category, revenue, cost, expense, net_impact, share_pct
461
+
462
+ ### 6.5 测试参数(test_arguments)
463
+
464
+ | 函数 ID | test_arguments 文件 | 默认 arguments 要点 |
465
+ | ---------------------------- | ----------------------------------------------------- | --------------------------------------------- |
466
+ | profit.fn.get_summary | `functions/test_arguments/profit.fn.get_summary.json` | 2025-01-01 ~ 2026-06-30 |
467
+ | profit.fn.yoy_analysis | `.../profit.fn.yoy_analysis.json` | 同上 |
468
+ | profit.fn.mom_analysis | `.../profit.fn.mom_analysis.json` | 同上 |
469
+ | profit.fn.budget_vs_actual | `.../profit.fn.budget_vs_actual.json` | fiscal_year=2026, budget_version=2026年度预算 |
470
+ | profit.fn.account_breakdown | `.../profit.fn.account_breakdown.json` | account_level=2 |
471
+ | profit.fn.cost_center_profit | `.../profit.fn.cost_center_profit.json` | 日期范围 |
472
+ | profit.fn.top_accounts | `.../profit.fn.top_accounts.json` | limit=10, metric=net_impact |
473
+
474
+ > 内置示例路径:`资源/examples/onto/利润示例/functions/test_arguments/`(复制到实现单元 `functions/test_arguments/`)。
475
+
476
+ 批量入库(实现单元 `functions/` 下,dazi-work 根):
477
+
478
+ ```powershell
479
+ .\<item-path>\functions\save_test_arguments.ps1
480
+ ```
481
+
482
+ > 参考实现:`资源/examples/onto/利润示例/`(init + seed + 7 个 `profit_fn_*.py` + test_arguments)。
483
+
484
+ ---
485
+
486
+ ## 七、实施计划
487
+
488
+ > **dazi-vscode v3** · 空间 `<space-id>` · 脚本目录 `setup/`、`functions/`
489
+ > 发布示例:
490
+ > `dazi onto script publish <item-path>/setup/profit_ontology_init.py --space <space-id> --type setup`
491
+
492
+ ### 7.1 阶段一:物理层(高)
493
+
494
+ | 步骤 | 任务 | 说明 |
495
+ | ---- | ---- | ---- |
496
+ | 1 | **dim_date** | 共用或 ensure |
497
+ | 2 | dim_account | 科目维 |
498
+ | 3 | dim_cost_center | 成本中心维 |
499
+ | 4 | fact_gl_journal_entry | 含 **date_key** |
500
+ | 5 | fact_budget_entry | 含 **date_key** |
501
+ | 6 | **TABLE_REGISTRY** + `register_with_meta` | 表/列 display_name、description |
502
+ | 7 | **表间关系(8 条)** | 含两条 fact→dim_date |
503
+
504
+ ### 7.2 阶段二:Cube 层(高)
505
+
506
+ | 步骤 | 任务 |
507
+ | ---- | ---- |
508
+ | 8 | 注册 **5 个 Cube**(含 BudgetVsActualCube;无 Time Cube) |
509
+ | 9 | 派生度量 revenue/cost/expense/operating_profit |
510
+
511
+ ### 7.3 阶段三:本体层(高)
512
+
513
+ | 步骤 | 任务 |
514
+ | ---- | ---- |
515
+ | 10 | define_object_type(6 种) |
516
+ | 11 | bind_source |
517
+ | 12 | define_property(约 42 个) |
518
+ | 13 | define_link_type(10 种) |
519
+ | 14 | sync_metric_refs |
520
+ | 15 | **CATEGORY_REGISTRY** + `s.categories.apply_registry`(347 对齐) |
521
+
522
+ ### 7.4 阶段四:函数与数据(中)
523
+
524
+ | 步骤 | 任务 |
525
+ | ---- | ---- |
526
+ | 15 | profit_ontology_init.py、profit_seed_data.py(含 dim_date / date_key) |
527
+ | 16 | 7 个 profit_fn_*.py |
528
+ | 17 | publish + run → save-test-arguments |
529
+
530
+ ### 7.5 快速实施路径
531
+
532
+ | 来源 | 可复用内容 | 需改造 |
533
+ | ---------------------------------------------------------- | --------------------------------- | ------------------------------ |
534
+ | `资源/examples/onto/利润示例/` | init/seed/函数完整示例 | 复制到 `<item-path>/` 后改 `<space-id>` |
535
+ | [规划示例:产品销售](./规划示例_产品销售本体规划方案.md) | 工程模式对照 | 表结构与函数逻辑不同 |
536
+
537
+ **seed 数据建议**:
538
+
539
+ - 科目:一级 4–6 个(收入/成本/费用),二级 15–20
540
+ - 成本中心:5–8 个(生产/销售/管理/财务等)
541
+ - 实际分录:2025-01 ~ 2026-06 按月随机凭证,2000+ 行
542
+ - 预算:同科目×成本中心×12 期,与 actual 同维便于预实对比
543
+
544
+ ---
545
+
546
+ ## 八、风险与验收
547
+
548
+ ### 8.1 风险
549
+
550
+ | 等级 | 风险 | 缓解措施 |
551
+ | ---- | ------------------------- | ------------------------------------------- |
552
+ | 中 | 多域共用 `<space-id>` | init 仅创建利润域表;**dim_date** 共用 ensure |
553
+ | 中 | 借贷方向与符号口径不一致 | seed 统一计算 `amount_signed`;文档约定口径 |
554
+ | 中 | 预实 JOIN 期间粒度不一致 | budget/actual 均含 fiscal_year/period |
555
+ | 低 | 旧示例订单模型混淆 | 规划与函数 ID 明确以 GL 为准 |
556
+
557
+ ### 8.2 验收标准
558
+
559
+ | 验收项 | 标准 |
560
+ | ------ | ---- |
561
+ | dim_date | 可查询;fact 的 date_key 可 JOIN |
562
+ | dim_account | 树形 parent 可解析 |
563
+ | fact_gl_journal_entry | 损益汇总与手工 SQL 一致 |
564
+ | fact_budget_entry | 与 actual 同维可 JOIN |
565
+ | 表/列元数据 | 侧栏与表预览显示名、说明与 §3.x 一致 |
566
+ | **表间关系** | **8 条** |
567
+ | Cube | **5 个** + 派生度量 |
568
+ | 对象 | 6 种 + 分类;10 种链接 |
569
+ | **347 平台分类** | 侧栏分类名与附录 B 一致;`category_mounts` 非空 |
570
+ | 函数 | get_summary、budget_vs_actual、account_breakdown 可运行 |
571
+ | 数据 | 种子灌入后预实差异可解释 |
572
+
573
+ ---
574
+
575
+ ## 九、预期成果
576
+
577
+ | 分析能力 | 说明 |
578
+ | ------------ | ------------------------------ |
579
+ | 损益总览 | 收入/成本/费用/营业利润/利润率 |
580
+ | 科目结构 | 按层级与 pl_category 展开 |
581
+ | **预实分析** | 预算、实际、差异、执行率 |
582
+ | 组织利润 | 成本中心/部门贡献 |
583
+ | 趋势分析 | 同比、环比 |
584
+ | 科目排行 | Top N 异动科目 |
585
+ | 业务语义 | 科目—分录—预算—组织链接可导航 |
586
+
587
+ ---
588
+
589
+ ## 附录 B:347 分类挂载对照表(CATEGORY_REGISTRY)
590
+
591
+ 与 `利润示例/setup/profit_ontology_init.py` 顶部 **`CATEGORY_REGISTRY`** 一一对应(见 [349 方案](./本体分类规划与SDK扩展方案.md))。
592
+
593
+ ### B.1 物理表(table)
594
+
595
+ | 347 分类 | 表名 |
596
+ | -------- | ---- |
597
+ | 维度表 | dim_account, dim_cost_center |
598
+ | 事实表 | fact_gl_journal_entry, fact_budget_entry |
599
+
600
+ > `dim_date` 为 **时间维**,多域共用;由销售域 init 创建时,利润域 `CATEGORY_REGISTRY` 可不重复挂载。
601
+
602
+ ### B.2 Cube(cube)
603
+
604
+ | 347 分类 | Cube |
605
+ | -------- | ---- |
606
+ | 流程型 | ActualCube, BudgetCube |
607
+ | 主体型 | AccountActualCube, CostCenterActualCube, TimeActualCube |
608
+
609
+ ### B.3 对象类型(object)
610
+
611
+ | 347 分类 | code |
612
+ | -------- | ---- |
613
+ | 主数据 | Account, CostCenter |
614
+ | 事务 | JournalEntry, BudgetLine |
615
+ | 分析 | ProfitAnalysis, BudgetAnalysis |
616
+
617
+ ### B.4 表间关系(relation)
618
+
619
+ | 347 分类 | 从表 → 到表 |
620
+ | -------- | ----------- |
621
+ | 时间关联 | fact_gl_journal_entry → dim_date;fact_budget_entry → dim_date |
622
+ | 主数据关联 | fact_gl_journal_entry → dim_account / dim_cost_center;fact_budget_entry → dim_account / dim_cost_center |
623
+ | 层级自关联 | dim_account → dim_account |
624
+
625
+ ### B.5 链接类型(link)
626
+
627
+ | 347 分类 | link code |
628
+ | -------- | --------- |
629
+ | 归属关系 | entry_belongs_account, entry_belongs_cost_center, budget_for_account, budget_for_cost_center |
630
+ | 分析归因 | analysis_by_account, analysis_by_cost_center, account_contributes_profit, cost_center_contributes_profit |
631
+ | 层级关系 | account_has_parent |
632
+ | 对比关系 | budget_compared_to_actual |
633
+
634
+ ### B.6 本体函数(function)
635
+
636
+ init **不**注册函数;`apply_registry(..., skip_missing=True)` 跳过未发布函数。建议分类:
637
+
638
+ | 347 分类 | function_id |
639
+ | -------- | ----------- |
640
+ | 总览分析 | profit.fn.get_summary |
641
+ | 趋势分析 | profit.fn.yoy_analysis, profit.fn.mom_analysis |
642
+ | 结构分析 | profit.fn.account_breakdown, profit.fn.top_accounts |
643
+ | 预实分析 | profit.fn.budget_vs_actual |
644
+ | 组织分析 | profit.fn.cost_center_profit |
645
+
646
+ ---
647
+
648
+ ## 附录:规划完整性自检清单
649
+
650
+ 定稿前逐项勾选(完整说明见 [本体规划指南 · 规划文档完整性自检清单](./本体规划指南.md#规划文档完整性自检清单))。
651
+
652
+ | # | 检查项 | 本方案 |
653
+ | - | ------ | ------ |
654
+ | 1 | 业务场景 | ☑ |
655
+ | 2 | 物理表 | ☑(dim_date + 2 dim + 2 fact) |
656
+ | 2a–2c | 命名规范 | ☑ |
657
+ | 3 | 表间关系 | ☑(**8 条**) |
658
+ | 4 | Cube 层 | ☑(**5 个**,§4.0) |
659
+ | 5 | 对象 + 分类 | ☑(§5.1、§5.4) |
660
+ | 6–10 | 同指南 | ☑ |
661
+ | 6a–6e | 347 平台分类 | ☑(附录 B + init 步骤 10) |
662
+
663
+ ---
664
+
665
+ ## 十、相关文档
666
+
667
+ | 文档 | 路径 |
668
+ | ---- | ---- |
669
+ | 本体命名规范 | [本体命名规范_物理表Cube与对象.md](./本体命名规范_物理表Cube与对象.md) |
670
+ | 本体规划指南 | [本体规划指南.md](./本体规划指南.md) |
671
+ | 规划示例(产品销售) | [规划示例_产品销售本体规划方案.md](./规划示例_产品销售本体规划方案.md) |
672
+ | **内置脚本示例** | `资源/examples/onto/利润示例/`(`dazi examples sync`;表名迁移中) |
673
+ | 349 · 分类方案 | [本体分类规划与 SDK 扩展方案](./本体分类规划与SDK扩展方案.md) |
674
+
675
+ ---
676
+
677
+ **方案版本**:v2.2(+347 分类列与附录 B CATEGORY_REGISTRY)
678
+ **创建日期**:(定稿日)
679
+ **适用**:复制到 `<item-path>/plans/`;`<space-id>` 以该实现单元 `README.md` 为准
680
+ **状态**:规划示例(公共模板)