LAPTOP-SNT8I5JK\Boounion
2025-09-19 81df82be1f5f401b80780f65e5811685d9a8e4c4
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
165
166
167
168
169
170
#pragma once
// GlassLogDb.h - µ¥Àý·â×°£ºSQLite Ð´Èë/²éѯ/·ÖÒ³/ͳ¼Æ/CSV µ¼³ö£¨ÒѶԽӠSERVO::CGlass£©
 
#include <string>
#include <vector>
#include <mutex>
#include <optional>
#include <chrono>
#include <cstdint>
#include <memory>   // std::unique_ptr
 
#include "CGlass.h"   // ÐèÒª SERVO::CGlass
 
// ¿ÉÔÚ±àÒëÃüÁî»ò±¾Í·Îļþǰ×Ô¶¨Òå±íÃû£º#define GLASS_LOG_TABLE "your_table_name"
#ifndef GLASS_LOG_TABLE
#define GLASS_LOG_TABLE "glass_log"
#endif
 
// Ç°ÖÃÉùÃ÷£¬±ÜÃâÍ·ÎļþÒÀÀµ sqlite3.h
struct sqlite3;
struct sqlite3_stmt;
 
class GlassLogDb {
public:
    // ====== µ¥Àý½Ó¿Ú ======
    // ³õʼ»¯£¨»òÇл»£©Êý¾Ý¿âÎļþ·¾¶£»µÚÒ»´Îµ÷Óûᴴ½¨ÊµÀý
    static void Init(const std::string& dbPath);
    // »ñȡȫ¾ÖʵÀý£»Èôδ³õʼ»¯»áÅ×Òì³£
    static GlassLogDb& Instance();
    // ÊÇ·ñÒѳõʼ»¯
    static bool IsInitialized() noexcept;
    // ÔËÐÐÖÐÇл»µ½ÁíÒ»·ÝÊý¾Ý¿âÎļþ£¨µÈͬ Init£¬²»´æÔÚÔò´´½¨£©
    static void Reopen(const std::string& dbPath);
    // µ±Ç°Êý¾Ý¿âÎļþ·¾¶£¨Î´³õʼ»¯·µ»Ø¿Õ´®£©
    static std::string CurrentPath();
 
    // ====== ÊÂÎñ¿ØÖÆ ======
    void beginTransaction();
    void commit();
    void rollback();
 
    // ====== Ð´Èë ======
    // Ö±½Ó´Ó SERVO::CGlass Ð´È루עÒ⣺²ÎÊýΪ ·Ç const ÒýÓã¬ÒòΪÄãµÄÈô¸É getter ·Ç const£©
    long long insertFromCGlass(SERVO::CGlass& g);
 
    // ÏÔʽ²ÎÊý²åÈ루tStart/tEnd ¿É¿Õ£»¾ùдÈë UTC ISO8601 Îı¾»ò NULL£©
    long long insertExplicit(
        int cassetteSeqNo,
        int jobSeqNo,
        const std::string& classId,
        int materialType,
        int state,
        std::optional<std::chrono::system_clock::time_point> tStart,
        std::optional<std::chrono::system_clock::time_point> tEnd,
        const std::string& buddyId,
        int aoiResult,
        const std::string& pathDesc,
        const std::string& paramsDesc,
        const std::string& prettyString);
 
    // ====== ²éѯ·µ»Ø½á¹¹ ======
    struct Row {
        long long id = 0;
        int cassetteSeqNo = 0;
        int jobSeqNo = 0;
        std::string classId;
        int materialType = 0;
        int state = 0;
        std::string tStart;   // ISO8601£¨¿âÖÐΪ NULL ÔòΪ¿Õ´®£©
        std::string tEnd;     // ISO8601£¨¿âÖÐΪ NULL ÔòΪ¿Õ´®£©
        std::string buddyId;
        int aoiResult = 0;
        std::string path;
        std::string params;
        std::string pretty;
    };
 
    // ====== ¹ýÂËÌõ¼þ ======
    struct Filters {
        std::optional<std::string> classId;       // ¾«È·Æ¥Åä class_id
        std::optional<int> cassetteSeqNo;
        std::optional<int> jobSeqNo;
 
        // ¹Ø¼ü×ÖÄ£ºý£º¶Ô class_id / buddy_id / path / params / pretty ×ö OR LIKE
        std::optional<std::string> keyword;
 
        // Ê±¼ä·¶Î§£º¶Ô t_start ¹ýÂË£¬º¬±ß½ç£¨ISO8601 Îı¾±È½Ï£©
        std::optional<std::chrono::system_clock::time_point> tStartFrom;
        std::optional<std::chrono::system_clock::time_point> tStartTo;
    };
 
    // ====== ²éѯ / Í³¼Æ / ·ÖÒ³ ======
    // ·ÖÒ³²éѯ£¨limit/offset£©£¬°´ id DESC
    std::vector<Row> query(
        const Filters& filters = {},
        int limit = 50,
        int offset = 0);
 
    // Í³¼ÆÓë filters ÏàͬÌõ¼þµÄ×ÜÊý
    long long count(const Filters& filters = {});
 
    struct Page {
        std::vector<Row> items;  // µ±Ç°Ò³Êý¾Ý
        long long total = 0;     // ·ûºÏÌõ¼þµÄ×ܼǼÊý
        int limit = 0;           // ±¾´Î²éѯµÄÒ³ÈÝÁ¿
        int offset = 0;          // ±¾´Î²éѯµÄÆðÊ¼Æ«ÒÆ
    };
    // Ò»´ÎÈ¡»ØÁбí + ×ÜÊý
    Page queryPaged(
        const Filters& filters = {},
        int limit = 50,
        int offset = 0);
 
    // ====== µ¼³ö ======
    // µ¼³öÂú×ã filters µÄ¡°È«²¿¼Ç¼¡±£¨²»ÊÜ·ÖÒ³ÏÞÖÆ£©Îª CSV£¨UTF-8£©
    // ·µ»ØÐ´³öµÄÊý¾ÝÐÐÊý£¨²»º¬±íÍ·£©
    long long exportCsv(const std::string& csvPath, const Filters& filters = {});
 
    // ====== Îö¹¹ / ¿½±´¿ØÖÆ ======
    ~GlassLogDb();
    GlassLogDb(const GlassLogDb&) = delete;
    GlassLogDb& operator=(const GlassLogDb&) = delete;
 
private:
    // ½öÔÊÐíͨ¹ýµ¥Àý½Ó¿Ú¹¹Ôì
    explicit GlassLogDb(const std::string& dbPath);
 
    // ÄÚ²¿´ò¿ª/¹Ø±Õ/ÖØ¿ª
    void openDb(const std::string& dbPath);
    void closeDb() noexcept;
    void reopenInternal(const std::string& dbPath);
 
    // ½¨±í / Ô¤±àÒë / ÊÍ·Å
    void ensureSchema();
    void prepareStatements();
    void finalizeStatements() noexcept;
 
    // ¹¤¾ß£ºtime_point -> "YYYY-MM-DDTHH:MM:SSZ"£¨UTC£©
    static std::string toIso8601Utc(std::chrono::system_clock::time_point tp);
 
    // Êµ¼Ê²åÈëÖ´ÐУ¨Ö§³Ö¿É¿Õʱ¼ä£©
    long long doInsert(
        int cassetteSeqNo,
        int jobSeqNo,
        const std::string& classId,
        int materialType,
        int state,
        std::optional<std::chrono::system_clock::time_point> tStart,
        std::optional<std::chrono::system_clock::time_point> tEnd,
        const std::string& buddyId,
        int aoiResult,
        const std::string& pathDesc,
        const std::string& paramsDesc,
        const std::string& prettyString);
 
private:
    // SQLite ¾ä±ú
    sqlite3* db_ = nullptr;
    sqlite3_stmt* stmtInsert_ = nullptr;
 
    // ÊµÀýÄÚ²¢·¢±£»¤£¨Í¬Ò»Á¬½ÓÉϵÄд/¶Á´®Ðл¯£©
    std::mutex mtx_;
 
    // µ±Ç°Êý¾Ý¿âÎļþ·¾¶
    std::string dbPath_;
 
    // µ¥Àý¾²Ì¬¶ÔÏóÓëÆä»¥³â
    static std::unique_ptr<GlassLogDb> s_inst;
    static std::mutex s_instMtx;
};