LAPTOP-SNT8I5JK\Boounion
2025-09-20 5909ef662dca45ce0fd9c2217f7c185225c2bf0c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "stdafx.h"
#include "ProductionLogManager.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <iomanip>
 
const std::string PRODUCTION_DB_FILE = R"(ProductionLog.db)";
std::mutex ProductionLogManager::m_mutex;
 
ProductionLogManager& ProductionLogManager::getInstance() {
    static ProductionLogManager instance;
    return instance;
}
 
ProductionLogManager::ProductionLogManager() {
    m_pDB = new BL::SQLiteDatabase();
}
 
ProductionLogManager::~ProductionLogManager() {
    if (m_pDB) {
        delete m_pDB;
        m_pDB = nullptr;
    }
}
 
bool ProductionLogManager::initProductionTable() {
    char path[MAX_PATH];
    GetModuleFileName(NULL, path, MAX_PATH);
    std::string exePath(path);
    std::string dbFileDir = exePath.substr(0, exePath.find_last_of("\\/")) + "\\DB";
    if (!CreateDirectory(dbFileDir.c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
        throw std::runtime_error("Failed to create DB directory.");
    }
 
    std::string dbFilePath = dbFileDir + "\\" + PRODUCTION_DB_FILE;
    if (!m_pDB->connect(dbFilePath, true)) {
        throw std::runtime_error("Failed to connect to production database.");
    }
 
    const std::string createTableQuery = R"(
        CREATE TABLE IF NOT EXISTS production_log (
            step_id INTEGER PRIMARY KEY AUTOINCREMENT,
            product_id TEXT NOT NULL,
            batch_no TEXT NOT NULL,
            device_id INTEGER NOT NULL,
            prev_device_id INTEGER,
            next_device_id INTEGER,
            operator_name TEXT,
            start_time DATETIME,
            end_time DATETIME,
            yield INTEGER,
            good_count INTEGER,
            bad_count INTEGER,
            status TEXT,
            note TEXT
        )
    )";
    return m_pDB->executeQuery(createTableQuery);
}
 
void ProductionLogManager::termProductionTable() {
    if (m_pDB) {
        m_pDB->disconnect();
    }
}
 
bool ProductionLogManager::destroyProductionTable() {
    if (!m_pDB) return false;
    const std::string query = "DROP TABLE IF EXISTS production_log";
    return m_pDB->executeQuery(query);
}
 
void ProductionLogManager::insertMockData() {
    // TODO: ÊµÏÖÄ£ÄâÊý¾Ý²åÈëÂß¼­£¨Ê¹Óàstd::ostringstream£©
    ProductionStep step;
    step.strProductId = "P888";
    step.strBatchNo = "B999";
    step.nDeviceId = 3;
    step.nPrevDeviceId = 2;
    step.nNextDeviceId = 4;
    step.strOperator = "MockUser";
    step.strStartTime = "2025-04-02 10:00:00";
    step.strEndTime = "2025-04-02 10:20:00";
    step.nYield = 100;
    step.nGoodCount = 98;
    step.nBadCount = 2;
    step.strStatus = "²âÊÔ";
    step.strNote = "ÕâÊÇÄ£ÄâÂÄÀú";
    int id = 0;
    addProductionStep(id, step);
}
 
bool ProductionLogManager::addProductionStep(int stepId, const ProductionStep& stepData) {
    std::ostringstream query;
    query << "INSERT INTO production_log (product_id, batch_no, device_id, prev_device_id, next_device_id, operator_name, start_time, end_time, yield, good_count, bad_count, status, note) "
        << "VALUES ('" << stepData.strProductId << "', '" << stepData.strBatchNo << "', " << stepData.nDeviceId << ", "
        << stepData.nPrevDeviceId << ", " << stepData.nNextDeviceId << ", '" << stepData.strOperator << "', '"
        << stepData.strStartTime << "', '" << stepData.strEndTime << "', " << stepData.nYield << ", "
        << stepData.nGoodCount << ", " << stepData.nBadCount << ", '" << stepData.strStatus << "', '"
        << stepData.strNote << "') RETURNING step_id;";
 
    std::lock_guard<std::mutex> lock(m_mutex);
    auto results = m_pDB->fetchResults(query.str());
    if (!results.empty() && !results[0].empty()) {
        try {
            stepId = std::stoi(results[0][0]);
            m_mapStepCache[stepId] = stepData;
            return true;
        }
        catch (...) {
            return false;
        }
    }
    return false;
}
 
std::vector<ProductionStep> ProductionLogManager::getAllSteps() {
    const std::string query = R"(
        SELECT step_id, product_id, batch_no, device_id, prev_device_id, next_device_id,
               operator_name, start_time, end_time, yield, good_count, bad_count, status, note
        FROM production_log
    )";
 
    auto results = m_pDB->fetchResults(query);
    std::vector<ProductionStep> steps;
    for (const auto& row : results) {
        ProductionStep step;
        step.nStepId = std::stoi(row[0]);
        step.strProductId = row[1];
        step.strBatchNo = row[2];
        step.nDeviceId = std::stoi(row[3]);
        step.nPrevDeviceId = std::stoi(row[4]);
        step.nNextDeviceId = std::stoi(row[5]);
        step.strOperator = row[6];
        step.strStartTime = row[7];
        step.strEndTime = row[8];
        step.nYield = std::stoi(row[9]);
        step.nGoodCount = std::stoi(row[10]);
        step.nBadCount = std::stoi(row[11]);
        step.strStatus = row[12];
        step.strNote = row[13];
        steps.push_back(step);
    }
    return steps;
}
 
std::vector<ProductionStep> ProductionLogManager::getStepsByProductId(const std::string& productId) {
    std::ostringstream query;
    query << "SELECT * FROM production_log WHERE product_id = '" << productId << "'";
    auto results = m_pDB->fetchResults(query.str());
    std::vector<ProductionStep> steps;
    for (const auto& row : results) {
        ProductionStep step;
        step.nStepId = std::stoi(row[0]);
        step.strProductId = row[1];
        step.strBatchNo = row[2];
        step.nDeviceId = std::stoi(row[3]);
        step.nPrevDeviceId = std::stoi(row[4]);
        step.nNextDeviceId = std::stoi(row[5]);
        step.strOperator = row[6];
        step.strStartTime = row[7];
        step.strEndTime = row[8];
        step.nYield = std::stoi(row[9]);
        step.nGoodCount = std::stoi(row[10]);
        step.nBadCount = std::stoi(row[11]);
        step.strStatus = row[12];
        step.strNote = row[13];
        steps.push_back(step);
    }
    return steps;
}
 
std::vector<ProductionStep> ProductionLogManager::getStepsByBatchNo(const std::string& batchNo) {
    std::ostringstream query;
    query << "SELECT * FROM production_log WHERE batch_no = '" << batchNo << "'";
    auto results = m_pDB->fetchResults(query.str());
    std::vector<ProductionStep> steps;
    for (const auto& row : results) {
        ProductionStep step;
        step.nStepId = std::stoi(row[0]);
        step.strProductId = row[1];
        step.strBatchNo = row[2];
        step.nDeviceId = std::stoi(row[3]);
        step.nPrevDeviceId = std::stoi(row[4]);
        step.nNextDeviceId = std::stoi(row[5]);
        step.strOperator = row[6];
        step.strStartTime = row[7];
        step.strEndTime = row[8];
        step.nYield = std::stoi(row[9]);
        step.nGoodCount = std::stoi(row[10]);
        step.nBadCount = std::stoi(row[11]);
        step.strStatus = row[12];
        step.strNote = row[13];
        steps.push_back(step);
    }
    return steps;
}
 
std::vector<ProductionStep> ProductionLogManager::getStepsByDeviceId(int nDeviceId) {
    std::ostringstream query;
    query << "SELECT * FROM production_log WHERE device_id = " << nDeviceId;
    auto results = m_pDB->fetchResults(query.str());
    std::vector<ProductionStep> steps;
    for (const auto& row : results) {
        ProductionStep step;
        step.nStepId = std::stoi(row[0]);
        step.strProductId = row[1];
        step.strBatchNo = row[2];
        step.nDeviceId = std::stoi(row[3]);
        step.nPrevDeviceId = std::stoi(row[4]);
        step.nNextDeviceId = std::stoi(row[5]);
        step.strOperator = row[6];
        step.strStartTime = row[7];
        step.strEndTime = row[8];
        step.nYield = std::stoi(row[9]);
        step.nGoodCount = std::stoi(row[10]);
        step.nBadCount = std::stoi(row[11]);
        step.strStatus = row[12];
        step.strNote = row[13];
        steps.push_back(step);
    }
    return steps;
}
 
std::vector<ProductionStep> ProductionLogManager::getStepsByTimeRange(const std::string& startTime, const std::string& endTime) {
    std::ostringstream query;
    query << "SELECT * FROM production_log WHERE start_time >= '" << startTime << "' AND end_time <= '" << endTime << "'";
    auto results = m_pDB->fetchResults(query.str());
    std::vector<ProductionStep> steps;
    for (const auto& row : results) {
        ProductionStep step;
        step.nStepId = std::stoi(row[0]);
        step.strProductId = row[1];
        step.strBatchNo = row[2];
        step.nDeviceId = std::stoi(row[3]);
        step.nPrevDeviceId = std::stoi(row[4]);
        step.nNextDeviceId = std::stoi(row[5]);
        step.strOperator = row[6];
        step.strStartTime = row[7];
        step.strEndTime = row[8];
        step.nYield = std::stoi(row[9]);
        step.nGoodCount = std::stoi(row[10]);
        step.nBadCount = std::stoi(row[11]);
        step.strStatus = row[12];
        step.strNote = row[13];
        steps.push_back(step);
    }
    return steps;
}
 
std::vector<ProductionStep> ProductionLogManager::getFilteredSteps(
    const std::string& productId,
    const std::string& batchNo,
    const std::string& deviceId,
    const std::string& operatorName,
    const std::string& status,
    const std::string& startTime,
    const std::string& endTime,
    int pageNumber,
    int pageSize) {
 
    std::ostringstream query;
    query << "SELECT * FROM production_log WHERE 1=1";
 
    if (!productId.empty()) {
        query << " AND product_id LIKE '%" << productId << "%'";
    }
    if (!batchNo.empty()) {
        query << " AND batch_no LIKE '%" << batchNo << "%'";
    }
    if (!deviceId.empty()) {
        query << " AND device_id = " << deviceId;
    }
    if (!operatorName.empty()) {
        query << " AND operator_name LIKE '%" << operatorName << "%'";
    }
    if (!status.empty()) {
        query << " AND status LIKE '%" << status << "%'";
    }
    if (!startTime.empty()) {
        query << " AND start_time >= '" << startTime << "'";
    }
    if (!endTime.empty()) {
        query << " AND end_time <= '" << endTime << "'";
    }
 
    int offset = (pageNumber - 1) * pageSize;
    query << " ORDER BY start_time DESC LIMIT " << pageSize << " OFFSET " << offset;
 
    auto results = m_pDB->fetchResults(query.str());
    std::vector<ProductionStep> steps;
    for (const auto& row : results) {
        ProductionStep step;
        step.nStepId = std::stoi(row[0]);
        step.strProductId = row[1];
        step.strBatchNo = row[2];
        step.nDeviceId = std::stoi(row[3]);
        step.nPrevDeviceId = std::stoi(row[4]);
        step.nNextDeviceId = std::stoi(row[5]);
        step.strOperator = row[6];
        step.strStartTime = row[7];
        step.strEndTime = row[8];
        step.nYield = std::stoi(row[9]);
        step.nGoodCount = std::stoi(row[10]);
        step.nBadCount = std::stoi(row[11]);
        step.strStatus = row[12];
        step.strNote = row[13];
        steps.push_back(step);
    }
    return steps;
}
 
int ProductionLogManager::getTotalStepCount(
    const std::string& productId,
    const std::string& batchNo,
    const std::string& deviceId,
    const std::string& operatorName,
    const std::string& status,
    const std::string& startTime,
    const std::string& endTime) {
 
    std::ostringstream query;
    query << "SELECT COUNT(*) FROM production_log WHERE 1=1";
 
    if (!productId.empty()) {
        query << " AND product_id LIKE '%" << productId << "%'";
    }
    if (!batchNo.empty()) {
        query << " AND batch_no LIKE '%" << batchNo << "%'";
    }
    if (!deviceId.empty()) {
        query << " AND device_id = " << deviceId;
    }
    if (!operatorName.empty()) {
        query << " AND operator_name LIKE '%" << operatorName << "%'";
    }
    if (!status.empty()) {
        query << " AND status LIKE '%" << status << "%'";
    }
    if (!startTime.empty()) {
        query << " AND start_time >= '" << startTime << "'";
    }
    if (!endTime.empty()) {
        query << " AND end_time <= '" << endTime << "'";
    }
 
    auto results = m_pDB->fetchResults(query.str());
    if (!results.empty() && !results[0].empty()) {
        return std::stoi(results[0][0]);
    }
    return 0;
}
 
bool ProductionLogManager::updateStepEndTime(int nStepId, const std::string& newEndTime) {
    std::ostringstream query;
    query << "UPDATE production_log SET end_time = '" << newEndTime << "' WHERE step_id = " << nStepId;
    return m_pDB->executeQuery(query.str());
}
 
bool ProductionLogManager::saveProductionFile(const std::string& filename) {
    std::ofstream file(filename);
    if (!file.is_open()) return false;
    file << "StepID,ProductID,BatchNo,DeviceID,PrevDeviceID,NextDeviceID,Operator,StartTime,EndTime,Yield,Good,Bad,Status,Note\n";
    for (auto it = m_mapStepCache.begin(); it != m_mapStepCache.end(); ++it) {
        const int id = it->first;
        const ProductionStep& step = it->second;
 
        file << id << "," << step.strProductId << "," << step.strBatchNo << ","
            << step.nDeviceId << "," << step.nPrevDeviceId << "," << step.nNextDeviceId << ","
            << step.strOperator << "," << step.strStartTime << "," << step.strEndTime << ","
            << step.nYield << "," << step.nGoodCount << "," << step.nBadCount << ","
            << step.strStatus << "," << step.strNote << "\n";
    }
    file.close();
    return true;
}
 
bool ProductionLogManager::readProductionFile(const std::string& filename) {
    std::ifstream file(filename);
    if (!file.is_open()) return false;
    std::string line;
    bool first = true;
    while (std::getline(file, line)) {
        if (first) { first = false; continue; }
        std::stringstream ss(line);
        std::string cell;
        ProductionStep step;
        std::getline(ss, cell, ','); step.nStepId = std::stoi(cell);
        std::getline(ss, step.strProductId, ',');
        std::getline(ss, step.strBatchNo, ',');
        std::getline(ss, cell, ','); step.nDeviceId = std::stoi(cell);
        std::getline(ss, cell, ','); step.nPrevDeviceId = std::stoi(cell);
        std::getline(ss, cell, ','); step.nNextDeviceId = std::stoi(cell);
        std::getline(ss, step.strOperator, ',');
        std::getline(ss, step.strStartTime, ',');
        std::getline(ss, step.strEndTime, ',');
        std::getline(ss, cell, ','); step.nYield = std::stoi(cell);
        std::getline(ss, cell, ','); step.nGoodCount = std::stoi(cell);
        std::getline(ss, cell, ','); step.nBadCount = std::stoi(cell);
        std::getline(ss, step.strStatus, ',');
        std::getline(ss, step.strNote);
        m_mapStepCache[step.nStepId] = step;
    }
    return true;
}