-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.c
440 lines (386 loc) · 11.2 KB
/
service.c
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
/***********************************************************************
Theater Ticket Management System
Copyright(C) 2017 hepangda
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
3 any later version.
This program is distributed in the hope that it will be useful,
buf WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have recived a copy of the GNU Gereral Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author: hepangda
E-mail: [email protected]
*************************************************************************/
#include"include/global.h"
#include"include/ui.h"
#include"include/service.h"
#include<string.h>
#include<stdlib.h>
#include<time.h>
static int srv_sale_build_schedule_cmp(char *str, schedule_t *this) {
int play = this->play_id;
link_t p = srv_find_play_id(play);
if (p == NULL) {
return -1;
}
play_t *hah = (play_t *)p->data;
return strcmp(hah->name, str) == 0;
}
static int srv_sale_build_ticket_cmp(int id, ticket_t *this) {
link_t p = srv_find_schedule_id(this->schedule_id);
schedule_t *hah = (schedule_t *)p->data;
if (hah == NULL) {
return 0;
}
return hah->id == id;
}
int srv_user_login(user_t loginmsg) {
this_user = loginmsg;
return RET_SUCCEED;
}
int srv_user_check(user_t *loginmsg) {
LINKLIST_ITER(g_user) {
user_t *this = (user_t *)pi->data;
if (strcmp(this->username, loginmsg->username) == 0) {
if (strcmp(this->passwd, loginmsg->passwd) == 0) {
loginmsg->type = this->type;
return RET_SUCCEED;
} else {
return SRV_RET_LOGINFAILED;
}
}
}
return SRV_RET_LOGINFAILED;
}
int srv_sale_build_schedule(linklist_t *schedules, char *question) {
linklist_init(schedules);
LINKLIST_ITER(g_schedule) {
schedule_t *this = (schedule_t *)pi->data;
if (srv_sale_build_schedule_cmp(question, this)) {
linklist_append(schedules, (void *)this);
}
}
}
int srv_sale_build_ticket(linklist_t *tickets, char *question) {
int playid = -1;
sscanf(question, "%d", &playid);
linklist_init(tickets);
LINKLIST_ITER(g_ticket) {
ticket_t *this = (ticket_t *)pi->data;
if (srv_sale_build_ticket_cmp(playid, this)) {
linklist_append(tickets, (void *)this);
}
}
}
link_t srv_find_seat_rc(int studio_id, int r, int c, int set) {
LINKLIST_ITER(g_seat) {
seat_t *this = (seat_t *)pi->data;
if (this->roomid == studio_id && this->row == r && this->col == c) {
this->status = set;
return pi;
}
}
return NULL;
}
link_t srv_find_seat(int studio_id, int r, int c) {
LINKLIST_ITER(g_seat) {
seat_t *this = (seat_t *)pi->data;
if (this->roomid == studio_id && this->row == r && this->col == c) {
return pi;
}
}
return NULL;
}
link_t srv_find_studio_id(int id) {
LINKLIST_ITER(g_studio) {
studio_t *this = (studio_t *)pi->data;
if (this->id == id) {
return pi;
}
}
return NULL;
}
link_t srv_find_schedule_id(int id) {
LINKLIST_ITER(g_schedule) {
schedule_t *this = (schedule_t *)pi->data;
if (this->id == id) {
return pi;
}
}
return NULL;
}
link_t srv_find_play_id(int id) {
LINKLIST_ITER(g_play) {
play_t *this = (play_t *)pi->data;
if (this->id == id) {
return pi;
}
}
return NULL;
}
link_t srv_find_user_name(char *str) {
LINKLIST_ITER(g_user) {
user_t *this = (user_t *)pi->data;
if (strcmp(str, this->username) == 0) {
return pi;
}
}
return NULL;
}
int srv_studio_equid(const void *va, const void *vb) {
studio_t *a = (studio_t *)va,
*b = (studio_t *)vb;
return a->id == b->id;
}
int srv_user_equname(const void *va, const void *vb) {
user_t *a = (user_t *)va;
char *b = (char *)vb;
return strcmp(a->username, b) == 0;
}
int srv_play_equid(const void *va, const void *vb) {
play_t *a = (play_t *)va,
*b = (play_t *)vb;
return a->id == b->id;
}
int srv_studio_add(studio_t which) {
linklist_append(&g_studio, (void *)&which);
return srv_seat_studioadd(which.id, which.rows, which.cols);
}
int srv_play_add(play_t which) {
linklist_append(&g_play, (void *)&which);
return 0;
}
int srv_user_add(user_t which) {
linklist_append(&g_user, (void *)&which);
return 0;
}
int srv_seat_add(seat_t which) {
linklist_append(&g_seat, (void *)&which);
return 0;
}
int srv_build_seatarray(int roomid, int row, int col, int dest[row][col]) {
memset(dest, 0, sizeof(int) * row * col);
LINKLIST_ITER(g_seat) {
seat_t *this = (seat_t *)pi->data;
if (this->roomid == roomid) {
dest[this->row][this->col] = this->status;
}
}
}
int srv_seat_studioadd(int studio_id, int row, int col) {
seat_t this;
this.id = 1;
this.roomid = studio_id;
this.status = SEAT_GOOD;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
this.col = j;
this.row = i;
linklist_append(&g_seat, (void *)&this);
this.id++;
}
}
return 0;
}
int srv_seat_equroomid(const void *va, const void *vb) {
seat_t *a = (seat_t *)va,
*b = (seat_t *)vb;
return a->roomid == b->roomid;
}
int srv_seat_studiodel(int studio_id) {
seat_t c;
c.roomid = studio_id;
linklist_delete(&g_seat, (void *)&c, srv_seat_equroomid);
}
int srv_schedule_equid(const void *va, const void *vb) {
schedule_t *a = (schedule_t *)va,
*b = (schedule_t *)vb;
return a->id == b->id;
}
int srv_schedule_add(schedule_t which) {
linklist_append(&g_schedule, (void *)&which);
return srv_ticket_scheduleadd(which);
}
int srv_ticket_scheduleadd(schedule_t which) {
studio_t *stu = (studio_t *)(srv_find_studio_id(which.studio_id)->data);
ticket_t this;
this.id = 1;
this.schedule_id = which.id;
this.status = 0; //0 not sale,1 saled
play_t *ply = (play_t *)(srv_find_play_id(which.play_id)->data);
this.price = ply->price;
for (int i = 1; i <= stu->rows; i++) {
for (int j = 1; j <= stu->cols; j++) {
this.col = j;
this.row = i;
linklist_append(&g_ticket, (void *)&this);
this.id++;
}
}
return 0;
}
int srv_ticket_scheduleid(const void *va, const void *vb) {
schedule_t *a = (schedule_t *)va,
*b = (schedule_t *)vb;
return a->id == b->id;
}
int srv_ticket_scheduledel(int schedule_id) {
ticket_t c;
c.schedule_id = schedule_id;
linklist_delete(&g_ticket, (void *)&c, srv_ticket_scheduleid);
}
link_t srv_find_ticket(int schedule_id, int row, int col) {
LINKLIST_ITER(g_ticket) {
ticket_t *this = (ticket_t *)pi->data;
if (this->schedule_id == schedule_id && this->row == row && this->col == col) {
return pi;
}
}
return NULL;
}
int srv_return_ticket(ticket_t *which) {
sale_t record;
sale_t *IDFINDER = (sale_t *)g_sale.pend->data;
if (IDFINDER == NULL) {
record.id = 1;
} else {
record.id = IDFINDER->id + 1;
}
strcpy(record.user, this_user.username);
record.ticket_schedule = which->schedule_id;
record.ticket_row = which->row;
record.ticket_col = which->col;
record.date = srv_get_nowdate();
record.time = srv_get_nowtime();
record.value = which->price;
record.type = SALE_RETURN;
which->status = 0;
linklist_append(&g_sale, (void *)&record);
return 0;
}
int srv_sale_ticket(ticket_t *which) {
sale_t record;
sale_t *IDFINDER = (sale_t *)g_sale.pend->data;
if (IDFINDER == NULL) {
record.id = 1;
} else {
record.id = IDFINDER->id + 1;
}
strcpy(record.user, this_user.username);
record.ticket_schedule = which->schedule_id;
record.ticket_row = which->row;
record.ticket_col = which->col;
record.date = srv_get_nowdate();
record.time = srv_get_nowtime();
record.value = which->price;
record.type = SALE_SELL;
which->status = 1;
ll_append(g_sale, record);
return 0;
}
static int srv_sort_helper(const void *a, const void *b) {
sale_analysis_t *va = (sale_analysis_t *)a,
*vb = (sale_analysis_t *)b;
return vb->sales - va->sales;
}
int srv_sort_saleanalysis() {
sale_analysis_t SORT[1000];
int i = 0;
LINKLIST_ITER(g_sale_analysis) {
sale_analysis_t *this = (sale_analysis_t *)pi->data;
SORT[i++] = *this;
}
qsort(SORT, i, sizeof(sale_analysis_t), srv_sort_helper);
ll_init(g_sale_analysis);
for (int j = 0; j < i; j++) {
ll_append(g_sale_analysis, SORT[j]);
}
}
int srv_build_saleanalysis() {
ll_init(g_sale_analysis);
int id = 0;
LINKLIST_ITER(g_play) {
play_t *this = (play_t *)pi->data;
sale_analysis_t thisana;
thisana.id = id++;
thisana.play = *this;
long cnt = 0;
LINKLIST_ITER(g_ticket) {
ticket_t *tt = (ticket_t *)pi->data;
schedule_t *tsc = (schedule_t *)srv_find_schedule_id(tt->schedule_id)->data;
if (tsc->play_id == this->id && tt->status == 1) {
cnt++;
}
}
thisana.totaltickets = cnt;
thisana.sales = cnt * this->price;
ll_append(g_sale_analysis, thisana);
}
return srv_sort_saleanalysis();
}
date_t srv_get_nowdate(void) {
date_t result;
time_t t;
struct tm *lt;
time (&t);
lt = localtime (&t);
result.year=lt->tm_year+1900;
result.month=lt->tm_mon+1;
result.day=lt->tm_mday;
return result;
}
times_t srv_get_nowtime(void) {
times_t result;
time_t t;
struct tm *lt;
time(&t);
lt = localtime(&t);
result.hour=lt->tm_hour;
result.min=lt->tm_min;
result.sec=lt->tm_sec;
return result;
}
int srv_indate(date_t a, date_t b, date_t c) {
int rtn;
if (a.year <= b.year) {
if (c.year <= b.year&&c.year >= a.year) {
if (c.month <= b.month&&c.month >= a.month) {
if (c.day <= b.day&&c.day >= a.day)
rtn = 0;
else
rtn = 0;
}
else rtn = 0;
}
else
rtn = 1;
} else {
if (c.year <= a.year&&c.year >= b.year) {
if (c.month <= a.month&&c.month >= b.month) {
if (c.day <= a.day&&c.day >= b.day)
rtn = 0;
else rtn = 0;
}
else rtn = 0;
}
else
rtn = 1;
}
return rtn;
}
int srv_saler_acc(user_t *tu, date_t begin, date_t end) {
int ret = 0;
LINKLIST_ITER(g_sale) {
sale_t *this = (sale_t *)pi->data;
if (strcmp(this->user, tu->username) == 0 && srv_indate(begin, end, this->date) == 0) {
if (this->type == SALE_RETURN)
ret -= this->value;
else
ret += this->value;
}
}
return ret;
}