ai_xiaopei
9 days ago 1ba86b102ecc01de3191c3861592d9d4d22dfd81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package classify
 
import (
    "encoding/json"
    "fmt"
    "os"
    "path/filepath"
)
 
// Entity 实体定义
type Entity struct {
    ID          string   `json:"id"`
    Name        string   `json:"name"`
    Aliases     []string `json:"aliases"`
    Platform    string   `json:"platform,omitempty"`
    Description string   `json:"description,omitempty"`
    Users       []string `json:"users,omitempty"`
    URL         string   `json:"url,omitempty"`
}
 
// Entities 实体集合
type Entities struct {
    Platforms []Entity `json:"platforms"`
    Apps      []Entity `json:"apps"`
    Devices   []Entity `json:"devices"`
}
 
// Relation 实体关系
type Relation struct {
    From        string `json:"from"`
    To          string `json:"to"`
    Type        string `json:"type"`
    Description string `json:"description"`
}
 
// Relations 关系集合
type Relations struct {
    Relations []Relation `json:"relations"`
}
 
// ExtractEntities 从实体目录提取实体信息
func ExtractEntities(vaultPath string) (*Entities, *Relations, error) {
    entityDir := filepath.Join(vaultPath, "实体")
 
    // 检查目录是否存在
    if _, err := os.Stat(entityDir); os.IsNotExist(err) {
        return nil, nil, fmt.Errorf("实体目录不存在: %s", entityDir)
    }
 
    // 预定义实体(从设计文档)
    entities := &Entities{
        Platforms: []Entity{
            {
                ID:          "elc",
                Name:        "电子秤平台",
                Aliases:     []string{"电子秤后台", "elc平台", "elc.zhiheiot.com"},
                URL:         "https://elc.zhiheiot.com",
                Description: "气站充装作业管理系统",
                Users:       []string{"气站站长", "充装员", "开票员"},
            },
            {
                ID:          "ops",
                Name:        "运营管理平台",
                Aliases:     []string{"运营平台", "运营后台", "rb.zhiheiot.com"},
                URL:         "https://rb.zhiheiot.com",
                Description: "气站运营管理系统",
                Users:       []string{"老板", "管理层", "客服"},
            },
        },
        Apps: []Entity{
            {
                ID:          "safety",
                Name:        "安全用气App",
                Aliases:     []string{"配送App", "电子秤配送App"},
                Platform:    "elc",
                Description: "二维码配送",
            },
            {
                ID:          "delivery",
                Name:        "易配送App",
                Aliases:     []string{"配送App", "运营配送"},
                Platform:    "ops",
                Description: "NFC识别芯片配送",
            },
            {
                ID:          "assistant",
                Name:        "艾信助手App",
                Aliases:     []string{"建档App"},
                Platform:    "both",
                Description: "融合两平台建档",
            },
        },
        Devices: []Entity{
            {
                ID:          "scale",
                Name:        "电子秤",
                Aliases:     []string{"充装电子秤"},
                Platform:    "elc",
                Description: "充装作业设备",
            },
            {
                ID:          "gun",
                Name:        "智能枪",
                Aliases:     []string{"充装枪", "智能充装枪"},
                Platform:    "both",
                Description: "共有硬件",
            },
            {
                ID:          "valve",
                Name:        "智能阀",
                Aliases:     []string{"智能角阀"},
                Platform:    "ops",
                Description: "气瓶阀门",
            },
            {
                ID:          "box",
                Name:        "艾信盒子",
                Aliases:     []string{"4G通信设备"},
                Platform:    "both",
                Description: "共有硬件",
            },
        },
    }
 
    // 预定义关系
    relations := &Relations{
        Relations: []Relation{
            {From: "gun", To: "scale", Type: "connects", Description: "智能枪连接电子秤"},
            {From: "gun", To: "box", Type: "syncs", Description: "充装数据同步到艾信盒子"},
            {From: "box", To: "elc", Type: "uploads", Description: "上传充装记录"},
            {From: "box", To: "ops", Type: "uploads", Description: "上传充装记录"},
            {From: "valve", To: "delivery", Type: "identified_by", Description: "NFC识别"},
            {From: "safety", To: "elc", Type: "belongs_to", Description: "属于电子秤平台"},
            {From: "delivery", To: "ops", Type: "belongs_to", Description: "属于运营管理平台"},
        },
    }
 
    return entities, relations, nil
}
 
// SaveEntities 保存实体和关系到 JSON 文件
func SaveEntities(vaultPath string, entities *Entities, relations *Relations) error {
    // 保存 entities.json
    entitiesPath := filepath.Join(vaultPath, "entities.json")
    entitiesData, err := json.MarshalIndent(entities, "", "  ")
    if err != nil {
        return fmt.Errorf("序列化 entities 失败: %w", err)
    }
    if err := os.WriteFile(entitiesPath, entitiesData, 0644); err != nil {
        return fmt.Errorf("写入 entities.json 失败: %w", err)
    }
 
    // 保存 relations.json
    relationsPath := filepath.Join(vaultPath, "relations.json")
    relationsData, err := json.MarshalIndent(relations, "", "  ")
    if err != nil {
        return fmt.Errorf("序列化 relations 失败: %w", err)
    }
    if err := os.WriteFile(relationsPath, relationsData, 0644); err != nil {
        return fmt.Errorf("写入 relations.json 失败: %w", err)
    }
 
    return nil
}