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
|
#include <tc/tc.h>
#include <item/item.h>
#include <aom/aom.h>
#include <emh/emh.h>
typedef struct {
char item_id[50];
char object_string[200];
char object_desc[500];
double weight;
char material[100];
} ItemData;
int import_items_from_csv(const char* csv_file) {
int status = ITK_ok;
int total = 0, success = 0, failed = 0;
FILE* fp = fopen(csv_file, "r");
if (!fp) {
printf("无法打开文件: %s\n", csv_file);
return ITK_error;
}
// 跳过表头
char line[1024];
fgets(line, sizeof(line), fp);
while (fgets(line, sizeof(line), fp)) {
ItemData data;
memset(&data, 0, sizeof(data));
// 解析 CSV 行
sscanf(line, "%[^,],%[^,],%[^,],%lf,%s",
data.item_id, data.object_string,
data.object_desc, &data.weight,
data.material);
total++;
// 创建或更新 Item
status = import_single_item(&data);
if (status == ITK_ok) {
success++;
printf("[OK] %s - %s\n", data.item_id, data.object_string);
} else {
failed++;
printf("[FAIL] %s - %s (error: %d)\n",
data.item_id, data.object_string, status);
}
// 每 100 条提交一次事务
if (total % 100 == 0) {
EMH_ask_errors();
printf("进度: %d 条,成功 %d,失败 %d\n",
total, success, failed);
}
}
fclose(fp);
printf("\n导入完成: 总计 %d,成功 %d,失败 %d\n",
total, success, failed);
return ITK_ok;
}
int import_single_item(ItemData* data) {
tag_t item_tag = NULLTAG;
tag_t rev_tag = NULLTAG;
int status = ITK_ok;
// 检查是否已存在
status = ITEM_find_item(data->item_id, &item_tag);
if (status != ITK_ok || item_tag == NULLTAG) {
// 创建新 Item
status = ITEM_create_item("Item", data->item_id, &item_tag);
if (status != ITK_ok) return status;
// 创建 Revision
status = ITEM_create_rev(item_tag, "A", &rev_tag);
if (status != ITK_ok) return status;
} else {
// 获取 Revision
status = ITEM_find_rev(item_tag, "A", &rev_tag);
if (status != ITK_ok) return status;
}
// 设置属性
status = AOM_set_string_property(rev_tag, "object_string", data->object_string);
if (status != ITK_ok) return status;
status = AOM_set_string_property(rev_tag, "object_desc", data->object_desc);
if (status != ITK_ok) return status;
status = AOM_set_double_property(rev_tag, "POM_part_weight", data->weight);
if (status != ITK_ok) return status;
status = AOM_set_string_property(rev_tag, "POM_material", data->material);
if (status != ITK_ok) return status;
// 保存
status = AOM_save(rev_tag);
if (status != ITK_ok) return status;
AOM_refresh(rev_tag, true);
return ITK_ok;
}
|