-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.go
543 lines (457 loc) · 17.3 KB
/
db.go
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
// MySQL
type MySQL struct {
db *sql.DB
}
func initializeDB() *sql.DB {
dsn := fmt.Sprintf("%v:%v@tcp(10.0.0.33)/%v?parseTime=true", os.Getenv("MYSQL_LOGIN"),
os.Getenv("MYSQL_PASS"), os.Getenv("DB_NAME"))
db, err := sql.Open("mysql", dsn)
if err != nil {
log.Fatal(err)
}
db.SetConnMaxLifetime(2 * time.Minute)
db.SetMaxIdleConns(10)
db.SetMaxOpenConns(10)
return db
}
// Payment struct
type Payment struct {
Receipt string `json:"receipt" csv:"Квитанция"`
Method string `json:"method" csv:"Способ"`
Admin string `json:"admin" csv:"Кто вносил"`
Sum int `json:"sum" csv:"Сумма"`
Date time.Time `json:"date" csv:"Дата"`
}
// Operation struct
type Operation struct {
Admin string `json:"admin"`
Type string `json:"type"`
Date time.Time `json:"date"`
}
// Tariff struct
type Tariff struct {
ID int `json:"id"`
Price int `json:"price"`
Name string `json:"name"`
}
// User struct
type User struct {
ID uint `json:"id"`
Balance int `json:"balance"`
Activity bool `json:"activity"`
Paid bool `json:"paid"`
Name string `json:"name"`
Room string `json:"room"`
Mac string `json:"mac"`
Login string `json:"login"`
Phone string `json:"phone"`
Comment string `json:"comment"`
ExtIP string `json:"ext_ip"`
InnerIP string `json:"inner_ip"`
Tariff Tariff `json:"tariff"`
Payments []Payment `json:"payments,omitempty"`
Operations []Operation `json:"operations,omitempty"`
Agreement string `json:"agreement"`
// separate for a more beautiful view
IsDeactivated bool `json:"is_deactivated"`
IsEmployee bool `json:"is_employee"`
IsArchived bool `json:"is_archived"`
IsLimited bool `json:"is_limited"`
ExpiredDate time.Time `json:"expired_date"`
AgreementConclusionDate time.Time `json:"agreement_conclusion_date"`
ConnectionPlace string `json:"connection_place"`
}
func (u User) hasEnoughMoneyForPayment() bool {
return !(u.Balance < u.Tariff.Price)
}
// GetAllUsers returns all users from db
func (mysql MySQL) GetAllUsers() ([]User, error) {
rows, err := mysql.db.Query(`SELECT users.id, balance, users.name, mac, login, agreement, expired_date,
connection_place, activity, paid, comment, is_archived, phone, rooms.name, tariffs.id AS tariff_id, tariffs.name,
price, ips.ip, ext_ip
FROM ((( users
INNER JOIN ips ON users.ip_id = ips.id)
INNER JOIN rooms ON users.room_id = rooms.id)
INNER JOIN tariffs ON users.tariff = tariffs.id)`)
if err != nil {
return nil, fmt.Errorf("cannot get all users: %v", err)
}
defer rows.Close()
var user User
users := make([]User, 0)
for rows.Next() {
err := rows.Scan(&user.ID, &user.Balance, &user.Name, &user.Mac, &user.Login, &user.Agreement, &user.ExpiredDate,
&user.ConnectionPlace, &user.Activity, &user.Paid, &user.Comment, &user.IsArchived, &user.Phone, &user.Room,
&user.Tariff.ID, &user.Tariff.Name, &user.Tariff.Price, &user.InnerIP, &user.ExtIP)
if err != nil {
return nil, fmt.Errorf("cannot get one row: %v", err)
}
users = append(users, user)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("something happened with rows: %v", err)
}
return users, nil
}
// GetUserByID returns user from db
func (mysql MySQL) GetUserByID(id int) (User, error) {
var user User
err := mysql.db.QueryRow(`SELECT users.id, balance, users.name, mac, login, agreement, expired_date,
connection_place, activity, paid, comment, is_deactivated, is_employee, is_archived, is_limited, phone,
agreement_conclusion_date, rooms.name, tariffs.id AS tariff_id, tariffs.name AS tariff_name, price, ips.ip, ext_ip
FROM ((( users
INNER JOIN ips ON users.ip_id = ips.id)
INNER JOIN rooms ON users.room_id = rooms.id)
INNER JOIN tariffs ON users.tariff = tariffs.id)
WHERE users.id = ?`, id).Scan(&user.ID, &user.Balance, &user.Name, &user.Mac, &user.Login, &user.Agreement,
&user.ExpiredDate, &user.ConnectionPlace, &user.Activity, &user.Paid, &user.Comment, &user.IsDeactivated,
&user.IsEmployee, &user.IsArchived, &user.IsLimited, &user.Phone, &user.AgreementConclusionDate, &user.Room,
&user.Tariff.ID, &user.Tariff.Name, &user.Tariff.Price, &user.InnerIP, &user.ExtIP)
if err != nil {
return user, fmt.Errorf("cannot get user with id=%v: %v", id, err)
}
payments, err := mysql.GetPaymentsByID(id)
if err != nil {
return user, fmt.Errorf("cannot get payments with id=%v: %v", id, err)
}
user.Payments = payments
operations, err := mysql.GetOperationsByID(id)
if err != nil {
return user, fmt.Errorf("cannot get operations with user id=%v: %v", id, err)
}
user.Operations = operations
return user, nil
}
// GetUserIDbyLogin returns user id from db
func (mysql MySQL) GetUserIDbyLogin(login string) (uint, error) {
var id uint
err := mysql.db.QueryRow(`SELECT id FROM users WHERE login = ?`, login).Scan(&id)
if err != nil {
return 0, fmt.Errorf("cannot get user id by login: %v", err)
}
return id, nil
}
// GetPaymentsByID returns info about user payments. Fresh records first
func (mysql MySQL) GetPaymentsByID(userID int) ([]Payment, error) {
rows, err := mysql.db.Query(`SELECT admin, receipt, method, sum, date FROM payments WHERE user_id=? ORDER BY date DESC`, userID)
if err != nil {
return nil, fmt.Errorf("cannot get payments by id: %v", err)
}
var payment Payment
payments := make([]Payment, 0)
for rows.Next() {
err := rows.Scan(&payment.Admin, &payment.Receipt, &payment.Method, &payment.Sum, &payment.Date)
if err != nil {
return nil, fmt.Errorf("cannot scan from row: %v", err)
}
payments = append(payments, payment)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("something happened with rows: %v", err)
}
return payments, nil
}
func (mysql MySQL) GetOperationsByID(userID int) ([]Operation, error) {
rows, err := mysql.db.Query(`SELECT admin, type, date FROM operations WHERE user_id = ? ORDER BY date DESC`, userID)
if err != nil {
return nil, fmt.Errorf("cannot get operations by user id: %v", err)
}
var operation Operation
operations := make([]Operation, 0)
for rows.Next() {
err := rows.Scan(&operation.Admin, &operation.Type, &operation.Date)
if err != nil {
return nil, fmt.Errorf("cannot scan from row: %v", err)
}
operations = append(operations, operation)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("something happened with rows: %v", err)
}
return operations, nil
}
// AddUser adds user to db
func (mysql MySQL) AddUser(user User) (int, error) {
innerIPid, err := mysql.getUnusedInnerIPid()
if err != nil {
return 0, fmt.Errorf("cannot get unused id of inner ip: %v", err)
}
roomID, err := mysql.getRoomIDByName(user.Room)
if err != nil {
return 0, fmt.Errorf("cannot get room id: %v", err)
}
_, err = mysql.db.Exec(`UPDATE rooms SET vacant_esockets=vacant_esockets-1 WHERE id = ? AND vacant_esockets > 0`, roomID)
if err != nil {
return 0, fmt.Errorf("cannot decrease vacant ethernet sockets in the room - %v: %v", user.Room, err)
}
res, err := mysql.db.Exec(`INSERT INTO users (balance, paid, name, is_employee, comment, mac, login, phone, room_id,
ext_ip, ip_id, agreement_conclusion_date, tariff, agreement, connection_place, expired_date)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, user.Balance, user.Paid, user.Name, user.IsEmployee, user.Comment,
user.Mac, user.Login, user.Phone, roomID, "82.200.46.10", innerIPid, user.AgreementConclusionDate, user.Tariff.ID,
user.Agreement, user.ConnectionPlace, user.ExpiredDate)
if err != nil {
return 0, fmt.Errorf("cannot insert values in db: %v", err)
}
id, err := res.LastInsertId()
if err != nil {
return 0, fmt.Errorf("cannot get last insert id: %v", err)
}
return int(id), nil
}
func (mysql MySQL) getUnusedInnerIPid() (int, error) {
var innerIPid int
err := mysql.db.QueryRow(`SELECT id FROM ips WHERE used = 0`).Scan(&innerIPid)
if err != nil {
return 0, fmt.Errorf("cannot scan id from db to variable: %v", err)
}
_, err = mysql.db.Exec(`UPDATE ips SET used=1 WHERE id = ?`, innerIPid)
if err != nil {
return 0, fmt.Errorf("cannot set inner ip as used: %v", err)
}
return innerIPid, nil
}
func (mysql MySQL) getRoomIDByName(name string) (int, error) {
var roomID int64
err := mysql.db.QueryRow(`SELECT id FROM rooms WHERE name = ?`, name).Scan(&roomID)
if err != nil {
res, err := mysql.db.Exec(`INSERT INTO rooms (name) VALUES (?)`, name)
if err != nil {
return 0, fmt.Errorf("cannot add new room in the table: %v", err)
}
roomID, _ = res.LastInsertId()
}
return int(roomID), nil
}
func (mysql MySQL) FreePaymentForOneYear(id int) error {
_, err := mysql.db.Exec(`UPDATE users SET paid=1, expired_date=? WHERE id=?`,
time.Now().AddDate(1, 0, 0), id)
if err != nil {
return fmt.Errorf("cannot update values in db: %v", err)
}
return nil
}
func (mysql MySQL) ResetFreePaymentForOneYear(id int) error {
_, err := mysql.db.Exec(`UPDATE users SET paid=0, expired_date=? WHERE id=?`,
time.Now().AddDate(0, 0, -1), id)
if err != nil {
return fmt.Errorf("cannot update values in db: %v", err)
}
return nil
}
func (mysql MySQL) UpdateUser(user User) error {
roomID, err := mysql.getRoomIDByName(user.Room)
if err != nil {
return fmt.Errorf("cannot get room id: %v", err)
}
_, err = mysql.db.Exec(`UPDATE users SET name=?, agreement=?, is_employee=?, mac=?, login=?, tariff=?, phone=?,
room_id=?, comment=?, connection_place=?, agreement_conclusion_date=?, expired_date=? WHERE id=?`,
user.Name, user.Agreement, user.IsEmployee, user.Mac, user.Login, user.Tariff.ID, user.Phone, roomID,
user.Comment, user.ConnectionPlace, user.AgreementConclusionDate, user.ExpiredDate, user.ID)
if err != nil {
return fmt.Errorf("cannot update user fields: %v", err)
}
return nil
}
// ProcessPayment updates balance and insert record into payments table
func (mysql MySQL) ProcessPayment(userID, sum int, method, receipt, admin string) error {
_, err := mysql.db.Exec(`UPDATE users SET balance=balance+? WHERE id=?`, sum, userID)
if err != nil {
return fmt.Errorf("cannot increase balance field: %v", err)
}
_, err = mysql.db.Exec(`INSERT INTO payments (user_id, admin, receipt, method, sum, date) VALUES (?,?,?,?,?,?)`,
userID, admin, receipt, method, sum, time.Now())
if err != nil {
return fmt.Errorf("cannot insert record about payment: %v", err)
}
return nil
}
// PayForNextMonth activates user for next month
func (mysql MySQL) PayForNextMonth(user User) (time.Time, error) {
t := time.Now().AddDate(0, 1, 0)
_, err := mysql.db.Exec(`UPDATE users SET expired_date=?, paid=1, balance=balance-? WHERE id=?`,
t, user.Tariff.Price, user.ID)
if err != nil {
return t, fmt.Errorf("cannot update user's info after payment: %v", err)
}
return t, nil
}
func (mysql MySQL) UnlimitUserByID(id int, admin string) error {
_, err := mysql.db.Exec(`UPDATE users SET is_limited=0 WHERE id=?`, id)
if err != nil {
return fmt.Errorf("cannot unlimit user: %v", err)
}
_, err = mysql.db.Exec(`INSERT INTO operations (user_id, admin, type, date) VALUES (?,?,?,?)`, id, admin, "unlimit", time.Now())
if err != nil {
return fmt.Errorf("cannot add 'unlimit' operation: %v", err)
}
return nil
}
func (mysql MySQL) LimitUserByID(id int, admin string) error {
_, err := mysql.db.Exec(`UPDATE users SET is_limited=1 WHERE id=?`, id)
if err != nil {
return fmt.Errorf("cannot limit user: %v", err)
}
_, err = mysql.db.Exec(`INSERT INTO operations (user_id, admin, type, date) VALUES (?,?,?,?)`, id, admin, "limit", time.Now())
if err != nil {
return fmt.Errorf("cannot add 'limit' operation: %v", err)
}
return nil
}
func (mysql MySQL) ActivateUserByID(id int, admin string) error {
_, err := mysql.db.Exec(`UPDATE users SET is_deactivated=0 WHERE id=?`, id)
if err != nil {
return fmt.Errorf("cannot activate user: %v", err)
}
_, err = mysql.db.Exec(`INSERT INTO operations (user_id, admin, type, date) VALUES (?,?,?,?)`, id, admin, "activate", time.Now())
if err != nil {
return fmt.Errorf("cannot add 'activate' operation: %v", err)
}
user, err := mysql.GetUserByID(id)
if err != nil {
return fmt.Errorf("cannot get user by id: %v", err)
}
operations, err := mysql.GetOperationsByID(id)
if err != nil {
return fmt.Errorf("cannot get operations by user id: %v", err)
}
// Берем предпоследнюю запись, так как последняя запись включения
remainDurationForUser := user.ExpiredDate.Sub(operations[len(operations)-2].Date)
_, err = mysql.db.Exec(`UPDATE users SET expired_date=? WHERE id=?`, operations[len(operations)-1].Date.Add(remainDurationForUser), id)
if err != nil {
return fmt.Errorf("cannot update expired_date: %v", err)
}
return nil
}
func (mysql MySQL) DeactivateUserByID(id int, admin string) error {
_, err := mysql.db.Exec(`UPDATE users SET is_deactivated=1 WHERE id=?`, id)
if err != nil {
return fmt.Errorf("cannot deactivate user: %v", err)
}
_, err = mysql.db.Exec(`INSERT INTO operations (user_id, admin, type, date) VALUES (?,?,?,?)`, id, admin, "deactivate", time.Now())
if err != nil {
return fmt.Errorf("cannot add 'deactivate' operation: %v", err)
}
return nil
}
func (mysql MySQL) ArchiveUserByID(id int) error {
_, err := mysql.db.Exec(`UPDATE users SET is_archived=1 WHERE id=?`, id)
if err != nil {
return fmt.Errorf("cannot archive user: %v", err)
}
_, err = mysql.db.Exec(`UPDATE rooms INNER JOIN users ON rooms.id = users.room_id
SET vacant_esockets=vacant_esockets+1 WHERE users.id = ? AND connection_place != ''`, id)
if err != nil {
return fmt.Errorf("cannot restore vacant ethernet socket: %v", err)
}
return nil
}
func (mysql MySQL) RestoreUserByID(id int) error {
_, err := mysql.db.Exec(`UPDATE users SET is_archived=0 WHERE id=?`, id)
if err != nil {
return fmt.Errorf("cannot restore user: %v", err)
}
_, err = mysql.db.Exec(`UPDATE rooms INNER JOIN users ON rooms.id = users.room_id
SET vacant_esockets=vacant_esockets-1 WHERE users.id = ? AND connection_place != '' AND vacant_esockets > 0`, id)
if err != nil {
return fmt.Errorf("cannot decrease vacant ethernet socket: %v", err)
}
return nil
}
func (mysql MySQL) GetCountOfActiveUsers() (int, error) {
var count int
err := mysql.db.QueryRow(`SELECT COUNT(*) FROM users WHERE activity=true AND is_archived=false`).Scan(&count)
if err != nil {
return 0, fmt.Errorf("cannot do queryRow: %v", err)
}
return count, nil
}
func (mysql MySQL) GetCountOfInactiveUsers() (int, error) {
var count int
err := mysql.db.QueryRow(`SELECT COUNT(*) FROM users WHERE activity=false AND is_archived=false`).Scan(&count)
if err != nil {
return 0, fmt.Errorf("cannot do queryRow: %v", err)
}
return count, nil
}
func (mysql MySQL) GetCountOfArchivedUsers() (int, error) {
var count int
err := mysql.db.QueryRow(`SELECT COUNT(*) FROM users WHERE activity=false AND is_archived=true`).Scan(&count)
if err != nil {
return 0, fmt.Errorf("cannot do queryRow: %v", err)
}
return count, nil
}
func (mysql MySQL) GetAllMoneyWeHave() (int, error) {
var sum int
err := mysql.db.QueryRow(`SELECT SUM(money) FROM ( SELECT SUM(payments.sum) AS money FROM payments
WHERE payments.user_id IN (SELECT id FROM users WHERE users.is_employee=0) ) AS a`).Scan(&sum)
if err != nil {
return 0, fmt.Errorf("cannot do queryRow: %v", err)
}
return sum, nil
}
func (mysql MySQL) GetIncomeForPeriod(from, to string) (int, error) {
var sum int
err := mysql.db.QueryRow(`SELECT SUM(money) FROM ( SELECT SUM(payments.sum) AS money FROM payments
WHERE payments.user_id IN (SELECT id FROM users WHERE users.is_employee=0)
AND payments.date >= ? AND payments.date < ? ) AS a`, from, to).Scan(&sum)
if err != nil {
return 0, fmt.Errorf("cannot do queryRow. From: %v, To: %v. Error: %v", from, to, err)
}
return sum, nil
}
type PaymentRecord struct {
Name string `csv:"ФИО"`
Payment
}
func (mysql MySQL) GetPaymentsRecords(from, to string) ([]PaymentRecord, error) {
rows, err := mysql.db.Query(`SELECT users.name, receipt, method, sum, admin, date
FROM ( users INNER JOIN payments ON users.id = payments.user_id )
WHERE date >= ? AND date < ? ORDER BY date DESC`, from, to)
if err != nil {
return nil, fmt.Errorf("cannot get payments records: %v", err)
}
var payment PaymentRecord
payments := make([]PaymentRecord, 0)
for rows.Next() {
err := rows.Scan(&payment.Name, &payment.Receipt, &payment.Method, &payment.Sum, &payment.Admin, &payment.Date)
if err != nil {
return nil, fmt.Errorf("cannot scan from row: %v", err)
}
payments = append(payments, payment)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("something happened with rows: %v", err)
}
return payments, nil
}
// GetNextAgreement returns next agreement
func (mysql MySQL) GetNextAgreement() (string, error) {
var lastAgreement string
err := mysql.db.QueryRow(`SELECT MAX(agreement) FROM users`).Scan(&lastAgreement)
if err != nil {
return "", fmt.Errorf("cannot select max agreement: %v", err)
}
agreementParts := strings.Split(lastAgreement, "-")
agreementID, err := strconv.Atoi(agreementParts[1])
if err != nil {
return "", fmt.Errorf("cannot convert agreement from string to int: %v", err)
}
agreementID += 1
return fmt.Sprintf("%v-%03d", agreementParts[0], agreementID), nil
}