@dazitech/cli 3.0.0
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.
- package/LICENSE +21 -0
- package/README.md +104 -0
- package/dist/bin/dazi-app.cjs +4 -0
- package/dist/bin/dazi-flow.cjs +4 -0
- package/dist/bin/dazi-onto.cjs +4 -0
- package/dist/bin/dazi.cjs +4 -0
- package/dist/bin/run-cli-bin.cjs +62 -0
- package/dist/clis/dazi-app.js +16697 -0
- package/dist/clis/dazi-flow.js +6093 -0
- package/dist/clis/dazi-onto.js +3948 -0
- package/dist/clis/dazi.js +5677 -0
- package/dist/docs/app/app-init.md +69 -0
- package/dist/docs/app/build-upload.md +77 -0
- package/dist/docs/app/release-guide.md +51 -0
- package/dist/docs/app//344/270/273/350/246/201/350/264/242/345/212/241/346/214/207/346/240/207/345/244/215/346/235/202/346/212/245/350/241/250/345/274/200/345/217/221/345/256/236/350/267/265.md +261 -0
- package/dist/docs/auth/auth-login.md +41 -0
- package/dist/docs/auth/token-management.md +42 -0
- package/dist/docs/data/cube-guide.md +23 -0
- package/dist/docs/data/data-spaces.md +30 -0
- package/dist/docs/data/table-preview.md +41 -0
- package/dist/docs/flow/flow-project-guide.md +505 -0
- package/dist/docs/flow/flows-guide.md +302 -0
- package/dist/docs/flow/node-code-guide.md +399 -0
- package/dist/docs/flow/plan-guide.md +59 -0
- package/dist/docs/flow/run-guide.md +98 -0
- package/dist/docs/flow/source-guide.md +44 -0
- package/dist/docs/flow/variables-guide.md +406 -0
- package/dist/docs/flow//346/265/201/347/250/213/345/274/200/345/217/221/346/234/200/344/275/263/345/256/236/350/267/265-VS-flow0/346/241/210/344/276/213.md +344 -0
- package/dist/docs/guides/cli-invocation.md +93 -0
- package/dist/docs/guides/cli-reference.md +98 -0
- package/dist/docs/guides/mcp-setup.md +89 -0
- package/dist/docs/guides/migrate-v2-v3.md +54 -0
- package/dist/docs/guides/quickstart.md +77 -0
- package/dist/docs/guides/troubleshooting.md +82 -0
- package/dist/docs/guides/workspace-v3.md +53 -0
- package/dist/docs/index.json +204 -0
- package/dist/docs/onto/action-guide.md +48 -0
- package/dist/docs/onto/dazi_script_sdk_reference.md +168 -0
- package/dist/docs/onto/dazi_script_seed_data_guide.md +155 -0
- package/dist/docs/onto/function-guide.md +68 -0
- package/dist/docs/onto/rule-guide.md +52 -0
- package/dist/docs/onto/space-management.md +46 -0
- 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 +145 -0
- package/dist/docs/onto//346/234/254/344/275/223/350/247/204/345/210/222/346/214/207/345/215/227.md +131 -0
- 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 +541 -0
- package/dist/examples/index.json +36 -0
- package/dist/examples/onto/function/profit_fn_customer_segmentation.py +117 -0
- package/dist/examples/onto/function/profit_fn_mom_analysis.py +89 -0
- package/dist/examples/onto/function/profit_fn_top_products.py +89 -0
- package/dist/examples/onto/function/profit_fn_yoy_analysis.py +89 -0
- package/dist/examples/onto/setup/profit_ontology_init.py +388 -0
- package/dist/prompts/app/ui-design.md +48 -0
- package/dist/prompts/data/data-analysis.md +42 -0
- package/dist/prompts/data/sql-query.md +36 -0
- package/dist/prompts/flow/flow-design.md +77 -0
- package/dist/prompts/flow/plan-generate.md +61 -0
- package/dist/prompts/flow/run-debug.md +66 -0
- package/dist/prompts/flow/run-fix-loop.md +77 -0
- package/dist/prompts/general/ask-dazi.md +30 -0
- package/dist/prompts/general/code-review.md +31 -0
- package/dist/prompts/general/troubleshoot.md +41 -0
- package/dist/prompts/index.json +20 -0
- package/dist/prompts/onto/action-design.md +47 -0
- package/dist/prompts/onto/function-design.md +44 -0
- package/dist/prompts/onto/rule-seed.md +41 -0
- package/dist/prompts/onto/script-publish-run.md +146 -0
- package/package.json +27 -0
|
@@ -0,0 +1,541 @@
|
|
|
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
|
+
> 环境:`.\scripts\dazi.ps1 doctor` · 现网表/Cube:**数据资源** 侧栏 ·
|
|
421
|
+
> 发布:`.\scripts\dazi.ps1 onto script publish 项目/onto_<名称>/脚本/<file>.py --space space__profit0520 --register-function-id <id>` ·
|
|
422
|
+
> 运行:`.\scripts\dazi.ps1 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 | 发布并注册函数 | `.\scripts\dazi.ps1 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;`.\scripts\dazi.ps1 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` | `.\scripts\dazi.ps1 onto` 脚本发布与函数生命周期 |
|
|
534
|
+
| `guides/workspace-v3.md` | 工作区 `项目/`、`资源/` 布局 |
|
|
535
|
+
|
|
536
|
+
---
|
|
537
|
+
|
|
538
|
+
**方案版本**:v1.0
|
|
539
|
+
**创建日期**:2026-05-20
|
|
540
|
+
**适用空间**:space\_\_profit0520
|
|
541
|
+
**状态**:待评审
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "3.0.0",
|
|
3
|
+
"updatedAt": "2026-05-25T00:00:00.000Z",
|
|
4
|
+
"examples": [
|
|
5
|
+
{
|
|
6
|
+
"id": "onto/setup/profit-ontology-init",
|
|
7
|
+
"title": "利润分析本体初始化",
|
|
8
|
+
"category": "onto-setup",
|
|
9
|
+
"file": "onto/setup/profit_ontology_init.py"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"id": "onto/function/profit-fn-yoy",
|
|
13
|
+
"title": "利润同比分析 (YoY)",
|
|
14
|
+
"category": "onto-function",
|
|
15
|
+
"file": "onto/function/profit_fn_yoy_analysis.py"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"id": "onto/function/profit-fn-mom",
|
|
19
|
+
"title": "利润环比分析 (MoM)",
|
|
20
|
+
"category": "onto-function",
|
|
21
|
+
"file": "onto/function/profit_fn_mom_analysis.py"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"id": "onto/function/profit-fn-top-products",
|
|
25
|
+
"title": "产品利润排行",
|
|
26
|
+
"category": "onto-function",
|
|
27
|
+
"file": "onto/function/profit_fn_top_products.py"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"id": "onto/function/profit-fn-customer-segmentation",
|
|
31
|
+
"title": "客户分层",
|
|
32
|
+
"category": "onto-function",
|
|
33
|
+
"file": "onto/function/profit_fn_customer_segmentation.py"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Customer Segmentation Function
|
|
2
|
+
|
|
3
|
+
功能:基于利润贡献对客户进行分层
|
|
4
|
+
参数:metric, method, start_date, end_date
|
|
5
|
+
返回:客户分层数据(VIP/High/Medium/Low)
|
|
6
|
+
|
|
7
|
+
放置位置:spaces/space__profit0520/editorial/scripts/ontology_functions/profit_fn_customer_segmentation.py
|
|
8
|
+
检索关键字:profit customer_segmentation
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _ontology_fn_body(p):
|
|
13
|
+
params = dict(p.get_params() or {})
|
|
14
|
+
|
|
15
|
+
metric = params.get("metric", "profit")
|
|
16
|
+
method = params.get("method", "quartile")
|
|
17
|
+
start_date = params.get("start_date", "")
|
|
18
|
+
end_date = params.get("end_date", "")
|
|
19
|
+
|
|
20
|
+
where_clause = ""
|
|
21
|
+
if start_date and end_date:
|
|
22
|
+
where_clause = f"WHERE order_date >= '{start_date}' AND order_date <= '{end_date}'"
|
|
23
|
+
|
|
24
|
+
sql = f"""
|
|
25
|
+
SELECT
|
|
26
|
+
customer_id,
|
|
27
|
+
customer_region,
|
|
28
|
+
sum(revenue) as revenue,
|
|
29
|
+
sum(cost) as cost,
|
|
30
|
+
sum(revenue) - sum(cost) as profit
|
|
31
|
+
FROM profit_analysis_fact
|
|
32
|
+
{where_clause}
|
|
33
|
+
GROUP BY customer_id, customer_region
|
|
34
|
+
ORDER BY profit DESC
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
result = p.sql.query(sql)
|
|
38
|
+
|
|
39
|
+
if not result:
|
|
40
|
+
return p.function_result(
|
|
41
|
+
columns=["customer_id", "customer_region", "revenue", "cost", "profit", "segment"],
|
|
42
|
+
data=[],
|
|
43
|
+
row_count=0
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
profits = [float(row.get("profit", 0) or 0) for row in result]
|
|
47
|
+
total = len(profits)
|
|
48
|
+
|
|
49
|
+
if total == 0:
|
|
50
|
+
return p.function_result(
|
|
51
|
+
columns=["customer_id", "customer_region", "revenue", "cost", "profit", "segment"],
|
|
52
|
+
data=[],
|
|
53
|
+
row_count=0
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
if method == "percentile":
|
|
57
|
+
sorted_profits = sorted(profits, reverse=True)
|
|
58
|
+
p80_idx = min(int(total * 0.8), total - 1)
|
|
59
|
+
p50_idx = min(int(total * 0.5), total - 1)
|
|
60
|
+
p20_idx = min(int(total * 0.2), total - 1)
|
|
61
|
+
p80 = sorted_profits[p80_idx]
|
|
62
|
+
p50 = sorted_profits[p50_idx]
|
|
63
|
+
p20 = sorted_profits[p20_idx]
|
|
64
|
+
else:
|
|
65
|
+
sorted_profits = sorted(profits, reverse=True)
|
|
66
|
+
q1_idx = min(int(total * 0.25), total - 1)
|
|
67
|
+
q2_idx = min(int(total * 0.5), total - 1)
|
|
68
|
+
q3_idx = min(int(total * 0.75), total - 1)
|
|
69
|
+
p80 = sorted_profits[q1_idx]
|
|
70
|
+
p50 = sorted_profits[q2_idx]
|
|
71
|
+
p20 = sorted_profits[q3_idx]
|
|
72
|
+
|
|
73
|
+
def get_segment(profit_val):
|
|
74
|
+
if profit_val >= p80:
|
|
75
|
+
return "VIP"
|
|
76
|
+
elif profit_val >= p50:
|
|
77
|
+
return "High"
|
|
78
|
+
elif profit_val >= p20:
|
|
79
|
+
return "Medium"
|
|
80
|
+
else:
|
|
81
|
+
return "Low"
|
|
82
|
+
|
|
83
|
+
data = []
|
|
84
|
+
for row in result:
|
|
85
|
+
profit = float(row.get("profit", 0) or 0)
|
|
86
|
+
data.append({
|
|
87
|
+
"customer_id": str(row.get("customer_id", "")),
|
|
88
|
+
"customer_region": str(row.get("customer_region", "")),
|
|
89
|
+
"revenue": round(float(row.get("revenue", 0) or 0), 2),
|
|
90
|
+
"cost": round(float(row.get("cost", 0) or 0), 2),
|
|
91
|
+
"profit": round(profit, 2),
|
|
92
|
+
"segment": get_segment(profit),
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
return p.function_result(
|
|
96
|
+
columns=["customer_id", "customer_region", "revenue", "cost", "profit", "segment"],
|
|
97
|
+
data=data,
|
|
98
|
+
row_count=len(data)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def main():
|
|
103
|
+
s = space.get(ctx.space_id or "")
|
|
104
|
+
|
|
105
|
+
_Ports = type(
|
|
106
|
+
"_Ports",
|
|
107
|
+
(),
|
|
108
|
+
{
|
|
109
|
+
"get_params": lambda self: dict(ctx.params or {}),
|
|
110
|
+
"function_result": lambda self, **kw: onto.function_result(**kw),
|
|
111
|
+
},
|
|
112
|
+
)
|
|
113
|
+
p = _Ports()
|
|
114
|
+
p.space_id = str(ctx.space_id or "")
|
|
115
|
+
p.sql = s.sql
|
|
116
|
+
|
|
117
|
+
return _ontology_fn_body(p)
|