-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
298 lines (223 loc) · 13 KB
/
db.py
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
import sqlite3
import datetime as DT
class DataBase:
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.cursor = self.connection.cursor()
def user_exists(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT * FROM 'users' WHERE user_id = ?",
(user_id,)).fetchall() # Проверяем на наличие в БД
return bool(len(result))
def add_user(self, user_id, name, start_time, end_time, refer_id=None):
with self.connection:
if refer_id != None:
return self.cursor.execute(
"INSERT INTO 'users' ('user_id', 'name', 'start_time', 'end_time', 'refer_id')"
" VALUES (?, ?, ?, ?, ?)", (user_id, name, start_time, end_time, refer_id))
else:
return self.cursor.execute(
"INSERT INTO 'users' ('user_id', 'name', 'start_time', 'end_time') VALUES (?, ?, ?, ?)",
(user_id, name, start_time, end_time)) # Добавляем значение в БД
def count_refer(self, user_id):
with self.connection:
return \
self.cursor.execute("SELECT COUNT('id') as count FROM 'users' WHERE refer_id = ?", (user_id,)).fetchone()[0]
def pro(self, active, user_id):
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?",
(active, user_id,)) # Обновляем значение
def end_time(self, user_id):
with self.connection:
return self.cursor.execute("SELECT end_time FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
def start_time(self, user_id):
with self.connection:
return self.cursor.execute("SELECT start_time FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
def get_vip_status(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT vip FROM 'users' WHERE user_id = ?",
(user_id,)).fetchone() # Достаём ОДНО значение
return result[0]
def get_active_status(self, user_id):
with self.connection:
return self.cursor.execute("SELECT active FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
def get_all_user_id(self):
with self.connection:
return self.cursor.execute("SELECT user_id FROM 'users'").fetchall() # Получить все user_id
def get_users(self):
with self.connection:
return self.cursor.execute("SELECT user_id, active FROM 'users'").fetchall()
def change_seb_3day(self, vip_status, end_time):
now_3 = DT.datetime.now()
# now_3 = now + DT.timedelta(days=3)
last_year_3 = str(now_3)[:4]
last_month_3 = str(now_3)[5:7]
last_day_3 = str(now_3)[8:10]
if bool(vip_status) and int(end_time[:2]) == int(last_day_3) and end_time[3:5] == last_month_3 and\
end_time[6:11] == last_year_3:
return 'ok'
if bool(vip_status) and int(end_time[:2]) - 2 == int(last_day_3) and end_time[3:5] == last_month_3 and\
end_time[6:11] == last_year_3:
return 2
if bool(vip_status) and int(end_time[:2]) - 1 == int(last_day_3) and end_time[3:5] == last_month_3 and\
end_time[6:11] == last_year_3:
return 1
def change_seb_week(self, vip_status, end_time):
now_7 = DT.datetime.now()
# now_7 = now + DT.timedelta(days=7)
last_year_7 = str(now_7)[:4]
last_month_7 = str(now_7)[5:7]
last_day_7 = str(now_7)[8:10]
if bool(vip_status) and int(end_time[:2]) == int(last_day_7) and end_time[3:5] == last_month_7 and\
end_time[6:11] == last_year_7:
return 'ok'
if bool(vip_status) and int(end_time[:2]) - 4 == int(last_day_7) and end_time[3:5] == last_month_7 and\
end_time[6:11] == last_year_7:
return 4
if bool(vip_status) and int(end_time[:2]) - 2 == int(last_day_7) and end_time[3:5] == last_month_7 and\
end_time[6:11] == last_year_7:
return 2
if bool(vip_status) and int(end_time[:2]) - 1 == int(last_day_7) and end_time[3:5] == last_month_7 and\
end_time[6:11] == last_year_7:
return 1
def change_seb_month(self, vip_status, end_time):
now_month = DT.datetime.now()
# now_month = now + DT.timedelta(days=31)
last_year_month = str(now_month)[:4]
last_month_month = str(now_month)[5:7]
last_day_month = str(now_month)[8:10]
if bool(vip_status) and int(end_time[:2]) == int(last_day_month) and end_time[3:5] == last_month_month and\
end_time[6:11] == last_year_month:
return 'ok'
if bool(vip_status) and int(end_time[:2]) - 4 == int(last_day_month) and end_time[3:5] == last_month_month and\
end_time[6:11] == last_year_month:
return 4
if bool(vip_status) and int(end_time[:2]) - 2 == int(last_day_month) and end_time[3:5] == last_month_month and\
end_time[6:11] == last_year_month:
return 2
if bool(vip_status) and int(end_time[:2]) - 1 == int(last_day_month) and end_time[3:5] == last_month_month and\
end_time[6:11] == last_year_month:
return 1
def change_seb_midle_year(self, vip_status, end_time):
now_midle_year = DT.datetime.now()
# now_midle_year = now + DT.timedelta(days=183)
last_year_midle_year = str(now_midle_year)[:4]
last_month_midle_year = str(now_midle_year)[5:7]
last_day_midle_year = str(now_midle_year)[8:10]
if bool(vip_status) and int(end_time[:2]) == int(last_day_midle_year) and end_time[3:5] == last_month_midle_year\
and end_time[6:11] == last_year_midle_year:
return 'ok'
if bool(vip_status) and int(end_time[:2]) - 4 == int(last_day_midle_year) and\
end_time[3:5] == last_month_midle_year and end_time[6:11] == last_year_midle_year:
return 4
if bool(vip_status) and int(end_time[:2]) - 2 == int(last_day_midle_year) and\
end_time[3:5] == last_month_midle_year and end_time[6:11] == last_year_midle_year:
return 2
if bool(vip_status) and int(end_time[:2]) - 1 == int(last_day_midle_year) and\
end_time[3:5] == last_month_midle_year and end_time[6:11] == last_year_midle_year:
return 1
def change_seb_year(self, vip_status, end_time):
now_year = DT.datetime.now()
# now_year = now + DT.timedelta(days=366)
last_year_year = str(now_year)[:4]
last_month_year = str(now_year)[5:7]
last_day_year = str(now_year)[8:10]
if bool(vip_status) and int(end_time[:2]) == int(last_day_year) and end_time[3:5] == last_month_year and\
end_time[6:11] == last_year_year:
return 'ok'
if bool(vip_status) and int(end_time[:2]) - 4 == int(last_day_year) and end_time[3:5] == last_month_year and\
end_time[6:11] == last_year_year:
return 4
if bool(vip_status) and int(end_time[:2]) - 2 == int(last_day_year) and end_time[3:5] == last_month_year and\
end_time[6:11] == last_year_year:
return 2
if bool(vip_status) and int(end_time[:2]) - 1 == int(last_day_year) and end_time[3:5] == last_month_year and\
end_time[6:11] == last_year_year:
return 1
# ************************************** УДАЛЯТЬ И СТАВИТЬ ПОДПИСКУ ******************************
def unviped(self, user_id):
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?", (0, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'start_time' = ? WHERE user_id = ?", (0, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'end_time' = ? WHERE user_id = ?", (0, user_id,))
def viped_3(self, user_id):
now = DT.datetime.now()
start_time = now.strftime("%d-%m-%Y")
now = now + DT.timedelta(days=3)
last_year = str(now)[:4]
last_month = str(now)[5:7]
last_day = str(now)[8:10]
end_time = last_day + '-' + last_month + '-' + last_year
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?", (1, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'start_time' = ? WHERE user_id = ?", (start_time, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'end_time' = ? WHERE user_id = ?", (end_time, user_id,))
def viped_week(self, user_id):
now = DT.datetime.now()
start_time = now.strftime("%d-%m-%Y")
now = now + DT.timedelta(days=7)
last_year = str(now)[:4]
last_month = str(now)[5:7]
last_day = str(now)[8:10]
end_time = last_day + '-' + last_month + '-' + last_year
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?", (1, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'start_time' = ? WHERE user_id = ?", (start_time, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'end_time' = ? WHERE user_id = ?", (end_time, user_id,))
def viped_month(self, user_id):
now = DT.datetime.now()
start_time = now.strftime("%d-%m-%Y")
now = now + DT.timedelta(days=31)
last_year = str(now)[:4]
last_month = str(now)[5:7]
last_day = str(now)[8:10]
end_time = last_day + '-' + last_month + '-' + last_year
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?", (1, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'start_time' = ? WHERE user_id = ?", (start_time, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'end_time' = ? WHERE user_id = ?", (end_time, user_id,))
def viped_midle_year(self, user_id):
now = DT.datetime.now()
start_time = now.strftime("%d-%m-%Y")
now = now + DT.timedelta(days=183)
last_year = str(now)[:4]
last_month = str(now)[5:7]
last_day = str(now)[8:10]
end_time = last_day + '-' + last_month + '-' + last_year
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?", (1, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'start_time' = ? WHERE user_id = ?", (start_time, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'end_time' = ? WHERE user_id = ?", (end_time, user_id,))
def viped_year(self, user_id):
now = DT.datetime.now()
start_time = now.strftime("%d-%m-%Y")
now = now + DT.timedelta(days=366)
last_year = str(now)[:4]
last_month = str(now)[5:7]
last_day = str(now)[8:10]
end_time = last_day + '-' + last_month + '-' + last_year
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'vip' = ? WHERE user_id = ?", (1, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'start_time' = ? WHERE user_id = ?", (start_time, user_id,)), \
self.cursor.execute("UPDATE 'users' SET 'end_time' = ? WHERE user_id = ?", (end_time, user_id,))
def add_bill_id(self, bill_id, user_id):
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'bill_id' = ? WHERE user_id = ?", (bill_id, user_id,))
def get_bill_id(self, user_id):
with self.connection:
return self.cursor.execute("SELECT bill_id FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
def get_info_users(self, user_id):
with self.connection:
name = self.cursor.execute("SELECT name FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
id = self.cursor.execute("SELECT user_id FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
sub = self.cursor.execute("SELECT vip FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
start_time = self.cursor.execute("SELECT start_time FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[
0]
end_time = self.cursor.execute("SELECT end_time FROM 'users' WHERE user_id = ?", (user_id,)).fetchone()[0]
return name, id, sub, start_time, end_time
def set_active(self, user_id, active):
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'active' = ? WHERE user_id = ?", (active, user_id,))
def set_proffessional(self, user_id):
with self.connection:
return self.cursor.execute("UPDATE 'users' SET 'professional' = ? WHERE user_id = ?", (0, user_id,))