Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (136 loc) · 4.39 KB
/
Copy pathmain.cpp
File metadata and controls
165 lines (136 loc) · 4.39 KB
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
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "sqlite3.h"
using namespace std;
struct orderEntry {
int id;
string hp;
int amount;
string date;
orderEntry(int id, string hp, int amount, string date)
: id(id), hp(hp), amount(amount), date(date) {}
};
class DB {
private:
sqlite3* db;
std::string dbPath;
static int callback(void* data, int argc, char** argv, char** azColName) {
auto* records = static_cast<vector<orderEntry>*>(data);
records->emplace_back(atoi(argv[0]), argv[1], atoi(argv[2]), argv[3]);
return 0;
}
string yyyymmdd() {
time_t now = time(nullptr);
tm* ltm = localtime(&now);
stringstream ss;
ss << 1900 + ltm->tm_year;
ss << std::setw(2) << std::setfill('0') << 1 + ltm->tm_mon;
ss << std::setw(2) << std::setfill('0') << ltm->tm_mday;
return ss.str();
}
public:
DB(const std::string& dbPath) : dbPath(dbPath), db(nullptr) {
if (sqlite3_open(dbPath.c_str(), &db) != SQLITE_OK) {
cerr << "Can't open database: " << sqlite3_errmsg(db) << endl;
exit(0);
}
}
~DB() {
if (db) {
sqlite3_close(db);
}
}
vector<orderEntry> getRecordsByHp(const string hpValue) {
vector<orderEntry> records;
string sql = "SELECT * FROM orderLog WHERE hp = '" + hpValue + "'";
char* errMsg = nullptr;
if (sqlite3_exec(db, sql.c_str(), callback, &records, &errMsg) !=
SQLITE_OK) {
cerr << "SQL error: " << errMsg << endl;
sqlite3_free(errMsg);
}
return records;
}
unsigned int getCouponsAvailable(const string hpValue) {
unsigned int coupon_count = 0;
string currentDate = yyyymmdd(); // 현재 날짜 가져오기
// 7일 전 날짜 계산
tm ltm = {};
strptime(currentDate.c_str(), "%Y%m%d", <m); // 문자열을 tm 구조체로 변환
ltm.tm_mday -= 7;
mktime(<m);
stringstream ss;
ss << 1900 + ltm.tm_year;
ss << std::setw(2) << std::setfill('0') << 1 + ltm.tm_mon;
ss << std::setw(2) << std::setfill('0') << ltm.tm_mday;
string sevenDaysAgoDate = ss.str(); // 7일 전 날짜 문자열로 변환
string sql = "SELECT COUNT(*) FROM couponBook WHERE hp = '" + hpValue + "' AND exp >= '" + sevenDaysAgoDate + "'";
char* errMsg = nullptr;
if (sqlite3_exec(db, sql.c_str(), callback, &coupon_count, &errMsg) !=
SQLITE_OK) {
cerr << "SQL error: " << errMsg << endl;
sqlite3_free(errMsg);
}
return coupon_count;
}
unsigned int makeCoupon(const string hpValue) {
unsigned int coupon_count = 0;
unsigned int total = 0;
vector<orderEntry> record = getRecordsByHp(hpValue);
vector<orderEntry>::iterator iter;
vector<orderEntry> deleteEntries;
struct orderEntry editEntry;
string sql;
string date = yyyymmdd();
char* errMsg = nullptr;
// 현재 주문 총액 구하기
for (iter = record.begin(); iter != record.end(); iter++) {
total += iter->amount;
}
// 쿠폰 추가하기
coupon_count = total / 10000;
sql = "INSERT INTO couponBook (hp, exp) VALUES (" + hpValue + ", " + date +
")";
for (int i = 0; i < coupon_count; i++) {
if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) {
cerr << "SQL error: " << errMsg << endl;
sqlite3_free(errMsg);
}
}
// 삭제할 Entry와 수정할 Entry를 찾기
for (iter = record.begin(); iter != record.end(); iter++) {
if ((total - iter->amount) < 0) {
editEntry = *iter;
editEntry.amount -= total;
break;
}
deleteEntries.emplace_back(*iter);
total -= iter->amount;
}
// 삭제할 Entry 삭제하기
for (iter = deleteEntries.begin(); iter != deleteEntries.end(); iter++) {
sql = "DELETE FROM orderLog WHERE id = " + to_string(iter->id);
if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) {
cerr << "SQL error: " << errMsg << endl;
sqlite3_free(errMsg);
}
}
// 수정할 Entry 수정하기
sql = "UPDATE orderLog SET amount " + to_string(editEntry.amount) +
" WHERE id = " + to_string(editEntry.id);
if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) {
cerr << "SQL error: " << errMsg << endl;
sqlite3_free(errMsg);
}
return coupon_count;
}
};
int main() {
DB db("database.db");
return 0;
}