1#include "/ty_code/common/libyfun/libyfun.h"
2//#include "libyfun.h"
3#include "tjpro.h"
4#include <time.h>
5#include <stdio.h>
6#include <memory.h>
7#include <pthread.h>
8
9GBLPARAM gb;//公有变量
10
11void Init()
12{
13 //得到调试信息
14
15 gb.is_debug = 1;
16 gb.debug_level = 3;
17 strcpy (gb.log_file, "/ty/log/tjpro.log");
18
19}
20
21int gethour()
22{
23 time_t timep;
24 struct tm *p;
25 time(&timep);
26 p= localtime(&timep);
27 return p->tm_hour;
28
29}
30
31int datapro()
32{
33 yCMysql da;
34 if(0 == da.Login("127.0.0.1","","","")) //连接数据库
35 {
36 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro login db fail!!\n");
37 return 0;
38
39 }
40 char sql[500];
41 memset(sql,0,500);
42
43
44 time_t m_ltime;
45 time(&m_ltime);
46 time_t m_btime;
47 time_t m_mtime;
48 m_btime = m_ltime - 86400;
49 m_mtime=m_ltime-300; //显示0点前一天的日期
50 time_t m_tmtime;
51 m_tmtime = m_ltime - 5184000; //60天以前
52
53 //删除60天以前的记录
54 sprintf(sql,"delete from tab_sxxrz where sjsj < %ld",m_tmtime);
55 if(da.QueryWrite(sql) == 0)
56 {
57 da.Logout();
58 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_sxxrz QueryWrite %s fail!!\n",sql);
59 return 0;
60 }
61
62 memset(sql,0,500);
63 sprintf(sql,"delete from tab_dxbj where dxsj < %ld",m_tmtime);
64 if(da.QueryWrite(sql) == 0)
65 {
66 da.Logout();
67 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_dxbj QueryWrite %s fail!!\n",sql);
68 return 0;
69 }
70
71
72 memset(sql,0,500);
73 sprintf(sql,"delete from tab_gkrz where tjsj < %ld",m_tmtime);
74 if(da.QueryWrite(sql) == 0)
75 {
76 da.Logout();
77 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_gkrz QueryWrite %s fail!!\n",sql);
78 return 0;
79 }
80
81 memset(sql,0,500);
82 sprintf(sql,"delete from tab_kkbj where tjsj < %ld",m_tmtime);
83 if(da.QueryWrite(sql) == 0)
84 {
85 da.Logout();
86 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_kkbj QueryWrite %s fail!!\n",sql);
87 return 0;
88 }
89
90
91 ////统计开卡
92 memset(sql,0,500);
93 sprintf(sql,"select dwbh,count(dwbh) as kks from tab_kk where sfyx = 1 and kksj>=%ld and kksj<%ld group by dwbh",m_btime,m_ltime); //统计0点以前24小时
94 if(da.QueryRead(sql) == 0)
95 {
96 da.Logout(); //关闭连接
97 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead1 %s fail!!\n",sql);
98 return 0;
99 }
100 int k = da.GetRowNum();
101 if(k > 0)
102 {
103 char temp[1000][30]; //最高1000家网吧开卡
104 memset(temp,0,1000*30*sizeof(char));
105 for(int i=0;i<k;i++)
106 {
107 da.GetRow();
108 sprintf(temp[i],"%s,%s",da.row[0],da.row[1]);
109 }
110 da.FreeResult();
111 for (int j=0;j<k;j++)
112 {
113 // da.GetRow();
114 char m_dwbh[15] ={0};
115 char m_kks[15] ={0};
116 char *p = temp[j];
117 char *pt = temp[j];
118 while(1)
119 {
120 if(*p ==',')
121 {
122 *p = '\0';
123 sprintf(m_dwbh,"%s",pt);
124 p++;
125 pt = p;
126 }
127 else if(*p == '\0')
128 {
129 sprintf(m_kks,"%s",pt);
130 break;
131 }
132 else
133 p++;
134
135 }
136
137
138 if(atoi(m_kks) > 5) //网吧开卡数大于5张不进行统计,并删除原来当天的报警项
139 {
140 memset(sql,0,500);
141 sprintf(sql,"delete from tab_kkbj where tjsj >= %ld and dwbh = %s",m_btime,m_dwbh);
142 if(da.QueryWrite(sql) == 0)
143 {
144 da.Logout(); //关闭连接
145 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite %s fail!!\n",sql);
146 return 0;
147 }
148 continue;
149 }
150
151 memset(sql,0,500);
152 sprintf(sql,"select dwbh from tab_kkbj where tjsj >= %ld and dwbh = %s",m_btime,m_dwbh); //查询开卡报警表中是否有今天网吧记录
153 if(da.QueryRead(sql) == 0)
154 {
155 da.Logout(); //关闭连接
156 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead2 %s fail!!\n",sql);
157 return 0;
158 }
159 if(da.GetRowNum() >0)
160 {
161 memset(sql,0,500);
162 sprintf(sql,"update tab_kkbj set kks = %s,tjsj = %ld where tjsj >= %ld and dwbh = %s",m_kks,m_mtime,m_btime,m_dwbh);
163 if(da.QueryWrite(sql) == 0)
164 {
165 da.Logout();
166 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite1 %s fail!!\n",sql);
167 return 0;
168 }
169 }
170 else
171 {
172 memset(sql,0,500);
173 sprintf(sql,"insert into tab_kkbj(dwbh,kks,tjsj) values(%s,%s,%ld)",m_dwbh,m_kks,m_mtime);
174 if(da.QueryWrite(sql) == 0)
175 {
176 da.Logout();
177 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite2 %s fail!!\n",sql);
178 return 0;
179 }
180 }
181 da.FreeResult();
182 }
183 }
184
185
186 /*
187 sprintf(sql,"select dwbh from tab_kk where kksj>%ld",m_btime);
188 if(da.QueryRead(sql) == 0)
189 {
190 da.Logout(); //关闭连接
191 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead %s fail!!\n",sql);
192 return 0;
193 }
194
195 if(da.GetRowNum() > 0)
196 {
197 da.FreeResult();
198 memset(sql,0,500);
199 sprintf(sql,"insert into tab_kkbj(dwbh,kks,tjsj) select dwbh,count(dwbh) as kks,%ld as tjsj from tab_kk where kksj>%ld group by dwbh",m_ltime,m_btime);
200 if(da.QueryWrite(sql) == 0)
201 {
202 da.Logout();
203 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite %s fail!!\n",sql);
204 return 0;
205 }
206 }
207*/
208
209
210// 统计公卡日志
211 memset(sql,0,500);
212 sprintf(sql,"select sl from tab_tjpz where mc='gkcs'");
213 if(da.QueryRead(sql) == 0)
214 {
215 da.Logout(); //关闭连接
216 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead %s fail!!\n",sql);
217 return 0;
218 }
219 char gkcs[4] = {0};
220 da.GetRow();
221 sprintf(gkcs,"%s",da.row[0]);
222 da.FreeResult();
223
224
225
226 memset(sql,0,500);
227 sprintf(sql,"select kh,dwbh,count(kh) as sjcs from tab_sxxrz where sjsj>=%ld group by kh,dwbh",m_btime);
228 if(da.QueryRead(sql) == 0)
229 {
230 da.Logout(); //关闭连接
231 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead %s fail!!\n",sql);
232 return 0;
233 }
234 int gk = da.GetRowNum();
235 if(gk > 0)
236 {
237 char tempgk[200000][35]; //最高20万人次
238 memset(tempgk,0,200000*35*sizeof(char));
239 for(int i=0;i<gk;i++)
240 {
241 da.GetRow();
242 sprintf(tempgk[i],"%s,%s:%s",da.row[0],da.row[1],da.row[2]);
243 }
244 da.FreeResult();
245 for (int j=0;j<gk;j++)
246 {
247 // da.GetRow();
248 char m_kh[15] ={0};
249 char m_dwbh[15] ={0};
250 char m_sjcs[15] ={0};
251 char *p = tempgk[j];
252 char *pt = tempgk[j];
253 while(1)
254 {
255 if(*p ==',')
256 {
257 *p = '\0';
258 sprintf(m_kh,"%s",pt);
259 p++;
260 pt = p;
261 }
262 else if(*p == ':')
263 {
264 *p = '\0';
265 sprintf(m_dwbh,"%s",pt);
266 p++;
267 pt = p;
268
269 }
270 else if(*p == '\0')
271 {
272
273 sprintf(m_sjcs,"%s",pt);
274 break;
275 }
276 else
277 p++;
278
279 }
280
281 if(atoi(m_sjcs) < atoi(gkcs))
282 {
283 continue;
284 }
285 sprintf(sql,"insert into tab_gkrz(kh,dwbh,sjcs,tjsj) values('%s',%s,%s,%ld)",m_kh,m_dwbh,m_sjcs,m_mtime);
286 if(da.QueryWrite(sql) == 0)
287 {
288 da.Logout();
289 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite3 %s fail!!\n",sql);
290 return 0;
291 }
292
293
294 }
295 }
296 /*
297 sprintf(sql,"insert into tab_gkrz(kh,dwbh,sjcs,tjsj) select kh,dwbh,count(kh) as sjcs,%ld as tjsj from tab_sxxrz where sjsj>%ld group by kh,dwbh",m_mtime,m_btime);
298 if(da.QueryWrite(sql) == 0)
299 {
300 da.Logout();
301 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite3 %s fail!!\n",sql);
302 return 0;
303 }
304
305
306 */
307
308 da.Logout();
309 return 1;
310
311}
312
313void * dxtjpro(void *arg)
314{
315
316
317 while(1)
318 {
319 sleep(298); //等待5分钟
320 ////////////
321 yCMysql da;
322 if(0 == da.Login("127.0.0.1","","","")) //连接数据库
323 {
324 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro login db fail!!\n");
325 return 0;
326 }
327 char sql[5100];
328 memset(sql,0,5100);
329 time_t m_ltime; //现在的时间
330 time_t m_btime;
331 time(&m_ltime);
332 m_btime = m_ltime - 300; //
333
334
335 memset(sql,0,5100);
336
337 sprintf(sql,"select dwbh from tab_dxbj where ljsj = 0");
338 if(da.QueryRead(sql) == 0)
339 {
340 da.Logout(); //关闭连接
341 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead %s fail!!\n",sql);
342 return 0;
343 }
344 int m = da.GetRowNum();
345 if(m>0)
346 {
347 char tempdwbh[5000] = {0}; //最高500家9位编号
348 int k = 0;
349 for(int n=0;n<m;n++)
350 {
351 da.GetRow();
352 k+=sprintf(tempdwbh+k,"%s,", da.row[0]);
353 }
354 tempdwbh[k-1] ='\0';
355 da.FreeResult();
356 memset(sql,0,5100);
357 sprintf(sql,"insert into tab_dxbj(dwbh,dxsj,ljsj) (select dwbh,wssj,0 as ljsj from tab_dwzl where wssj<%ld and dwbh not in(%s))",m_btime,tempdwbh);
358 if(da.QueryWrite(sql) == 0)
359 {
360 da.Logout();
361 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite %s fail!!\n",sql);
362 return 0;
363 }
364 }
365 else
366 {
367 memset(sql,0,5100);
368 sprintf(sql,"insert into tab_dxbj(dwbh,dxsj,ljsj) (select dwbh,wssj,0 as ljsj from tab_dwzl where wssj<%ld)",m_btime);
369 if(da.QueryWrite(sql) == 0)
370 {
371 da.Logout();
372 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite %s fail!!\n",sql);
373 return 0;
374 }
375 }
376
377
378
379 ///在5分钟内有握手的断线编号全部置为连接
380 memset(sql,0,5100);
381 sprintf(sql,"select dwbh from tab_dwzl where wssj>=%ld",m_btime);
382 if(da.QueryRead(sql) == 0)
383 {
384 da.Logout(); //关闭连接
385 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead2 %s fail!!\n",sql);
386 return 0;
387 }
388 int j = da.GetRowNum();
389 if(j>0)
390 {
391 char tempCard[5000] = {0};
392 int k = 0;
393 for(int i=0;i<j;i++)
394 {
395 da.GetRow();
396 k+=sprintf(tempCard+k,"%s,", da.row[0]);
397 }
398 tempCard[k-1] ='\0';
399 da.FreeResult();
400 memset(sql,0,500);
401 sprintf(sql,"update tab_dxbj set ljsj = %ld where ljsj = 0 and dwbh in(%s)",m_ltime,tempCard);
402 if(da.QueryWrite(sql) == 0)
403 {
404 da.Logout();
405 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite2 %s fail!!\n",sql);
406 return 0;
407 }
408 }
409 da.Logout();
410 }
411
412 /*
413
414/////////////
415
416 while(1)
417 {
418 sleep(88); //等待
419// yCMysql da;
420 if(0 == da.Login("127.0.0.1","operator","operator","dbs_whbzx")) //连接数据库
421 {
422 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro login db fail!!\n");
423 return 0;
424
425 }
426// char sql[500];
427// memset(sql,0,5100);
428// time_t m_ltime; //现在的时间
429// time_t m_btime;
430 time(&m_ltime);
431 time_t m_bbtime;
432 m_btime = m_ltime - 90; //
433 m_bbtime = m_ltime - 180; //
434
435 memset(sql,0,5100);
436 sprintf(sql,"select dwbh from tab_dwzl where wssj>=%ld and wssj<%ld",m_bbtime,m_btime); //90至180新断线
437 if(da.QueryRead(sql) == 0)
438 {
439 da.Logout(); //关闭连接
440 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead1 %s fail!!\n",sql);
441 return 0;
442 }
443 if(da.GetRowNum() > 0)
444 {
445 memset(sql,0,5100);
446 sprintf(sql,"insert into tab_dxbj(dwbh,dxsj,ljsj) (select dwbh,wssj,0 as ljsj from tab_dwzl where wssj>=%ld and wssj<%ld)",m_bbtime,m_btime); //握手时间前90至180的插入报警表
447 if(da.QueryWrite(sql) == 0)
448 {
449 da.Logout();
450 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite1 %s fail!!\n",sql);
451 return 0;
452 }
453 }
454 da.FreeResult();
455
456 ///在90秒内有握手的断线编号全部置为连接
457 memset(sql,0,5100);
458 sprintf(sql,"select dwbh from tab_dwzl where wssj>=%ld",m_btime);
459 if(da.QueryRead(sql) == 0)
460 {
461 da.Logout(); //关闭连接
462 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead2 %s fail!!\n",sql);
463 return 0;
464 }
465 int j = da.GetRowNum();
466 if(j>0)
467 {
468 char tempCard[5000] = {0};
469 int k = 0;
470 for(int i=0;i<j;i++)
471 {
472 da.GetRow();
473 k+=sprintf(tempCard+k,"%s,", da.row[0]);
474 }
475 tempCard[k-1] ='\0';
476 da.FreeResult();
477 memset(sql,0,500);
478 sprintf(sql,"update tab_dxbj set ljsj = %ld where ljsj = 0 and dwbh in(%s)",m_ltime,tempCard);
479 if(da.QueryWrite(sql) == 0)
480 {
481 da.Logout();
482 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite2 %s fail!!\n",sql);
483 return 0;
484 }
485 }
486 da.Logout();
487 }
488 */
489}
490
491void * kktjpro(void *arg)
492{
493
494 char sql[500];
495 memset(sql,0,500);
496 time_t m_ltime; //现在的时间
497 long m_ttime; //当日0点的时间
498 struct tm *p;
499 time(&m_ltime);
500 p = localtime(&m_ltime);
501 m_ttime =m_ltime - ((p->tm_hour *60*60)+(p->tm_min *60)+(p->tm_sec));
502// DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"0点时间:%ld!!\n",m_ttime);
503
504 while(1)
505 {
506
507 yCMysql da;
508 if(0 == da.Login("127.0.0.1","","","")) //连接数据库
509 {
510 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro login db fail!!\n");
511 return 0;
512
513 }
514 sprintf(sql,"select dwbh,count(dwbh) as kks from tab_kk where sfyx = 1 and kksj>=%ld group by dwbh",m_ttime); //统计0点以后开卡网吧及开卡数
515 if(da.QueryRead(sql) == 0)
516 {
517 da.Logout(); //关闭连接
518 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryRead %s fail!!\n",sql);
519 return 0;
520 }
521 int k = da.GetRowNum();
522 if(k > 0)
523 {
524 char temp[1000][30]; //最高1000家
525 memset(temp,0,1000*30*sizeof(char));
526 for(int i=0;i<k;i++)
527 {
528 da.GetRow();
529 sprintf(temp[i],"%s,%s",da.row[0],da.row[1]);
530 }
531 da.FreeResult();
532 for (int j=0;j<k;j++)
533 {
534 // da.GetRow();
535 char m_dwbh[15] ={0};
536 char m_kks[15] ={0};
537 char *p = temp[j];
538 char *pt = temp[j];
539 while(1)
540 {
541
542 if(*p ==',')
543 {
544 *p = '\0';
545 sprintf(m_dwbh,"%s",pt);
546 p++;
547 pt = p;
548 }
549 else if(*p == '\0')
550 {
551 sprintf(m_kks,"%s",pt);
552 break;
553 }
554 else
555 p++;
556 }
557
558 if(atoi(m_kks) > 5) //网吧开卡数大于5张不进行统计,并删除原来当天的报警项
559 {
560 memset(sql,0,500);
561 sprintf(sql,"delete from tab_kkbj where tjsj >= %ld and dwbh = %s",m_ttime,m_dwbh);
562 if(da.QueryWrite(sql) == 0)
563 {
564 da.Logout(); //关闭连接
565 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryWrite %s fail!!\n",sql);
566 return 0;
567 }
568 continue;
569 }
570 memset(sql,0,500);
571 sprintf(sql,"select dwbh from tab_kkbj where tjsj >= %ld and dwbh = %s",m_ttime,m_dwbh); //查询开卡报警表中是否有今天网吧记录
572 if(da.QueryRead(sql) == 0)
573 {
574 da.Logout(); //关闭连接
575 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryRead %s fail!!\n",sql);
576 return 0;
577 }
578 if(da.GetRowNum() >0)
579 {
580 da.FreeResult();
581 memset(sql,0,500);
582 sprintf(sql,"update tab_kkbj set kks = %s,tjsj = %ld where tjsj >= %ld and dwbh = %s",m_kks,m_ltime,m_ttime,m_dwbh);
583 if(da.QueryWrite(sql) == 0)
584 {
585 da.Logout();
586 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryWrite %s fail!!\n",sql);
587 return 0;
588 }
589 }
590 else
591 {
592 memset(sql,0,500);
593 sprintf(sql,"insert into tab_kkbj(dwbh,kks,tjsj) values(%s,%s,%ld)",m_dwbh,m_kks,m_ltime);
594 if(da.QueryWrite(sql) == 0)
595 {
596 da.Logout();
597 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryWrite %s fail!!\n",sql);
598 return 0;
599 }
600 }
601
602 }
603 }
604 da.Logout();
605 sleep(7200);
606 }
607
608
609}
610
611void main()
612{
613 sleep(5); ///暂停五秒等待数据库
614 Init();
615 pthread_t id;
616 int ret = pthread_create(&id,NULL,dxtjpro,NULL); //创建统计断线线程
617 if(ret != 0)
618 {
619 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro create fail! errcode %d\n",ret);
620 exit(0) ;
621 }
622
623 pthread_t td;
624 int m_ret = pthread_create(&td,NULL,kktjpro,NULL); //创建统计开卡线程
625 if(m_ret != 0)
626 {
627 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro create fail! errcode %d\n",ret);
628 exit(0) ;
629 }
630
631 if(gethour() == 0) //防止程序在0点至1点重启时进行重复统计
632 {
633 sleep(7200); //暂停2个小时
634 }
635
636 while(1)
637 {
638 if(gethour() == 0) //0点统计
639 {
640 datapro();
641 sleep(7200); //暂停2个小时
642 }
643 else
644 {
645 sleep(60); //暂停1分钟
646 }
647 }
648
649}
650
651
2//#include "libyfun.h"
3#include "tjpro.h"
4#include <time.h>
5#include <stdio.h>
6#include <memory.h>
7#include <pthread.h>
8
9GBLPARAM gb;//公有变量
10
11void Init()
12{
13 //得到调试信息
14
15 gb.is_debug = 1;
16 gb.debug_level = 3;
17 strcpy (gb.log_file, "/ty/log/tjpro.log");
18
19}
20
21int gethour()
22{
23 time_t timep;
24 struct tm *p;
25 time(&timep);
26 p= localtime(&timep);
27 return p->tm_hour;
28
29}
30
31int datapro()
32{
33 yCMysql da;
34 if(0 == da.Login("127.0.0.1","","","")) //连接数据库
35 {
36 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro login db fail!!\n");
37 return 0;
38
39 }
40 char sql[500];
41 memset(sql,0,500);
42
43
44 time_t m_ltime;
45 time(&m_ltime);
46 time_t m_btime;
47 time_t m_mtime;
48 m_btime = m_ltime - 86400;
49 m_mtime=m_ltime-300; //显示0点前一天的日期
50 time_t m_tmtime;
51 m_tmtime = m_ltime - 5184000; //60天以前
52
53 //删除60天以前的记录
54 sprintf(sql,"delete from tab_sxxrz where sjsj < %ld",m_tmtime);
55 if(da.QueryWrite(sql) == 0)
56 {
57 da.Logout();
58 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_sxxrz QueryWrite %s fail!!\n",sql);
59 return 0;
60 }
61
62 memset(sql,0,500);
63 sprintf(sql,"delete from tab_dxbj where dxsj < %ld",m_tmtime);
64 if(da.QueryWrite(sql) == 0)
65 {
66 da.Logout();
67 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_dxbj QueryWrite %s fail!!\n",sql);
68 return 0;
69 }
70
71
72 memset(sql,0,500);
73 sprintf(sql,"delete from tab_gkrz where tjsj < %ld",m_tmtime);
74 if(da.QueryWrite(sql) == 0)
75 {
76 da.Logout();
77 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_gkrz QueryWrite %s fail!!\n",sql);
78 return 0;
79 }
80
81 memset(sql,0,500);
82 sprintf(sql,"delete from tab_kkbj where tjsj < %ld",m_tmtime);
83 if(da.QueryWrite(sql) == 0)
84 {
85 da.Logout();
86 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tab_kkbj QueryWrite %s fail!!\n",sql);
87 return 0;
88 }
89
90
91 ////统计开卡
92 memset(sql,0,500);
93 sprintf(sql,"select dwbh,count(dwbh) as kks from tab_kk where sfyx = 1 and kksj>=%ld and kksj<%ld group by dwbh",m_btime,m_ltime); //统计0点以前24小时
94 if(da.QueryRead(sql) == 0)
95 {
96 da.Logout(); //关闭连接
97 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead1 %s fail!!\n",sql);
98 return 0;
99 }
100 int k = da.GetRowNum();
101 if(k > 0)
102 {
103 char temp[1000][30]; //最高1000家网吧开卡
104 memset(temp,0,1000*30*sizeof(char));
105 for(int i=0;i<k;i++)
106 {
107 da.GetRow();
108 sprintf(temp[i],"%s,%s",da.row[0],da.row[1]);
109 }
110 da.FreeResult();
111 for (int j=0;j<k;j++)
112 {
113 // da.GetRow();
114 char m_dwbh[15] ={0};
115 char m_kks[15] ={0};
116 char *p = temp[j];
117 char *pt = temp[j];
118 while(1)
119 {
120 if(*p ==',')
121 {
122 *p = '\0';
123 sprintf(m_dwbh,"%s",pt);
124 p++;
125 pt = p;
126 }
127 else if(*p == '\0')
128 {
129 sprintf(m_kks,"%s",pt);
130 break;
131 }
132 else
133 p++;
134
135 }
136
137
138 if(atoi(m_kks) > 5) //网吧开卡数大于5张不进行统计,并删除原来当天的报警项
139 {
140 memset(sql,0,500);
141 sprintf(sql,"delete from tab_kkbj where tjsj >= %ld and dwbh = %s",m_btime,m_dwbh);
142 if(da.QueryWrite(sql) == 0)
143 {
144 da.Logout(); //关闭连接
145 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite %s fail!!\n",sql);
146 return 0;
147 }
148 continue;
149 }
150
151 memset(sql,0,500);
152 sprintf(sql,"select dwbh from tab_kkbj where tjsj >= %ld and dwbh = %s",m_btime,m_dwbh); //查询开卡报警表中是否有今天网吧记录
153 if(da.QueryRead(sql) == 0)
154 {
155 da.Logout(); //关闭连接
156 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead2 %s fail!!\n",sql);
157 return 0;
158 }
159 if(da.GetRowNum() >0)
160 {
161 memset(sql,0,500);
162 sprintf(sql,"update tab_kkbj set kks = %s,tjsj = %ld where tjsj >= %ld and dwbh = %s",m_kks,m_mtime,m_btime,m_dwbh);
163 if(da.QueryWrite(sql) == 0)
164 {
165 da.Logout();
166 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite1 %s fail!!\n",sql);
167 return 0;
168 }
169 }
170 else
171 {
172 memset(sql,0,500);
173 sprintf(sql,"insert into tab_kkbj(dwbh,kks,tjsj) values(%s,%s,%ld)",m_dwbh,m_kks,m_mtime);
174 if(da.QueryWrite(sql) == 0)
175 {
176 da.Logout();
177 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite2 %s fail!!\n",sql);
178 return 0;
179 }
180 }
181 da.FreeResult();
182 }
183 }
184
185
186 /*
187 sprintf(sql,"select dwbh from tab_kk where kksj>%ld",m_btime);
188 if(da.QueryRead(sql) == 0)
189 {
190 da.Logout(); //关闭连接
191 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead %s fail!!\n",sql);
192 return 0;
193 }
194
195 if(da.GetRowNum() > 0)
196 {
197 da.FreeResult();
198 memset(sql,0,500);
199 sprintf(sql,"insert into tab_kkbj(dwbh,kks,tjsj) select dwbh,count(dwbh) as kks,%ld as tjsj from tab_kk where kksj>%ld group by dwbh",m_ltime,m_btime);
200 if(da.QueryWrite(sql) == 0)
201 {
202 da.Logout();
203 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite %s fail!!\n",sql);
204 return 0;
205 }
206 }
207*/
208
209
210// 统计公卡日志
211 memset(sql,0,500);
212 sprintf(sql,"select sl from tab_tjpz where mc='gkcs'");
213 if(da.QueryRead(sql) == 0)
214 {
215 da.Logout(); //关闭连接
216 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead %s fail!!\n",sql);
217 return 0;
218 }
219 char gkcs[4] = {0};
220 da.GetRow();
221 sprintf(gkcs,"%s",da.row[0]);
222 da.FreeResult();
223
224
225
226 memset(sql,0,500);
227 sprintf(sql,"select kh,dwbh,count(kh) as sjcs from tab_sxxrz where sjsj>=%ld group by kh,dwbh",m_btime);
228 if(da.QueryRead(sql) == 0)
229 {
230 da.Logout(); //关闭连接
231 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryRead %s fail!!\n",sql);
232 return 0;
233 }
234 int gk = da.GetRowNum();
235 if(gk > 0)
236 {
237 char tempgk[200000][35]; //最高20万人次
238 memset(tempgk,0,200000*35*sizeof(char));
239 for(int i=0;i<gk;i++)
240 {
241 da.GetRow();
242 sprintf(tempgk[i],"%s,%s:%s",da.row[0],da.row[1],da.row[2]);
243 }
244 da.FreeResult();
245 for (int j=0;j<gk;j++)
246 {
247 // da.GetRow();
248 char m_kh[15] ={0};
249 char m_dwbh[15] ={0};
250 char m_sjcs[15] ={0};
251 char *p = tempgk[j];
252 char *pt = tempgk[j];
253 while(1)
254 {
255 if(*p ==',')
256 {
257 *p = '\0';
258 sprintf(m_kh,"%s",pt);
259 p++;
260 pt = p;
261 }
262 else if(*p == ':')
263 {
264 *p = '\0';
265 sprintf(m_dwbh,"%s",pt);
266 p++;
267 pt = p;
268
269 }
270 else if(*p == '\0')
271 {
272
273 sprintf(m_sjcs,"%s",pt);
274 break;
275 }
276 else
277 p++;
278
279 }
280
281 if(atoi(m_sjcs) < atoi(gkcs))
282 {
283 continue;
284 }
285 sprintf(sql,"insert into tab_gkrz(kh,dwbh,sjcs,tjsj) values('%s',%s,%s,%ld)",m_kh,m_dwbh,m_sjcs,m_mtime);
286 if(da.QueryWrite(sql) == 0)
287 {
288 da.Logout();
289 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite3 %s fail!!\n",sql);
290 return 0;
291 }
292
293
294 }
295 }
296 /*
297 sprintf(sql,"insert into tab_gkrz(kh,dwbh,sjcs,tjsj) select kh,dwbh,count(kh) as sjcs,%ld as tjsj from tab_sxxrz where sjsj>%ld group by kh,dwbh",m_mtime,m_btime);
298 if(da.QueryWrite(sql) == 0)
299 {
300 da.Logout();
301 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"tjpro QueryWrite3 %s fail!!\n",sql);
302 return 0;
303 }
304
305
306 */
307
308 da.Logout();
309 return 1;
310
311}
312
313void * dxtjpro(void *arg)
314{
315
316
317 while(1)
318 {
319 sleep(298); //等待5分钟
320 ////////////
321 yCMysql da;
322 if(0 == da.Login("127.0.0.1","","","")) //连接数据库
323 {
324 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro login db fail!!\n");
325 return 0;
326 }
327 char sql[5100];
328 memset(sql,0,5100);
329 time_t m_ltime; //现在的时间
330 time_t m_btime;
331 time(&m_ltime);
332 m_btime = m_ltime - 300; //
333
334
335 memset(sql,0,5100);
336
337 sprintf(sql,"select dwbh from tab_dxbj where ljsj = 0");
338 if(da.QueryRead(sql) == 0)
339 {
340 da.Logout(); //关闭连接
341 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead %s fail!!\n",sql);
342 return 0;
343 }
344 int m = da.GetRowNum();
345 if(m>0)
346 {
347 char tempdwbh[5000] = {0}; //最高500家9位编号
348 int k = 0;
349 for(int n=0;n<m;n++)
350 {
351 da.GetRow();
352 k+=sprintf(tempdwbh+k,"%s,", da.row[0]);
353 }
354 tempdwbh[k-1] ='\0';
355 da.FreeResult();
356 memset(sql,0,5100);
357 sprintf(sql,"insert into tab_dxbj(dwbh,dxsj,ljsj) (select dwbh,wssj,0 as ljsj from tab_dwzl where wssj<%ld and dwbh not in(%s))",m_btime,tempdwbh);
358 if(da.QueryWrite(sql) == 0)
359 {
360 da.Logout();
361 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite %s fail!!\n",sql);
362 return 0;
363 }
364 }
365 else
366 {
367 memset(sql,0,5100);
368 sprintf(sql,"insert into tab_dxbj(dwbh,dxsj,ljsj) (select dwbh,wssj,0 as ljsj from tab_dwzl where wssj<%ld)",m_btime);
369 if(da.QueryWrite(sql) == 0)
370 {
371 da.Logout();
372 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite %s fail!!\n",sql);
373 return 0;
374 }
375 }
376
377
378
379 ///在5分钟内有握手的断线编号全部置为连接
380 memset(sql,0,5100);
381 sprintf(sql,"select dwbh from tab_dwzl where wssj>=%ld",m_btime);
382 if(da.QueryRead(sql) == 0)
383 {
384 da.Logout(); //关闭连接
385 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead2 %s fail!!\n",sql);
386 return 0;
387 }
388 int j = da.GetRowNum();
389 if(j>0)
390 {
391 char tempCard[5000] = {0};
392 int k = 0;
393 for(int i=0;i<j;i++)
394 {
395 da.GetRow();
396 k+=sprintf(tempCard+k,"%s,", da.row[0]);
397 }
398 tempCard[k-1] ='\0';
399 da.FreeResult();
400 memset(sql,0,500);
401 sprintf(sql,"update tab_dxbj set ljsj = %ld where ljsj = 0 and dwbh in(%s)",m_ltime,tempCard);
402 if(da.QueryWrite(sql) == 0)
403 {
404 da.Logout();
405 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite2 %s fail!!\n",sql);
406 return 0;
407 }
408 }
409 da.Logout();
410 }
411
412 /*
413
414/////////////
415
416 while(1)
417 {
418 sleep(88); //等待
419// yCMysql da;
420 if(0 == da.Login("127.0.0.1","operator","operator","dbs_whbzx")) //连接数据库
421 {
422 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro login db fail!!\n");
423 return 0;
424
425 }
426// char sql[500];
427// memset(sql,0,5100);
428// time_t m_ltime; //现在的时间
429// time_t m_btime;
430 time(&m_ltime);
431 time_t m_bbtime;
432 m_btime = m_ltime - 90; //
433 m_bbtime = m_ltime - 180; //
434
435 memset(sql,0,5100);
436 sprintf(sql,"select dwbh from tab_dwzl where wssj>=%ld and wssj<%ld",m_bbtime,m_btime); //90至180新断线
437 if(da.QueryRead(sql) == 0)
438 {
439 da.Logout(); //关闭连接
440 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead1 %s fail!!\n",sql);
441 return 0;
442 }
443 if(da.GetRowNum() > 0)
444 {
445 memset(sql,0,5100);
446 sprintf(sql,"insert into tab_dxbj(dwbh,dxsj,ljsj) (select dwbh,wssj,0 as ljsj from tab_dwzl where wssj>=%ld and wssj<%ld)",m_bbtime,m_btime); //握手时间前90至180的插入报警表
447 if(da.QueryWrite(sql) == 0)
448 {
449 da.Logout();
450 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite1 %s fail!!\n",sql);
451 return 0;
452 }
453 }
454 da.FreeResult();
455
456 ///在90秒内有握手的断线编号全部置为连接
457 memset(sql,0,5100);
458 sprintf(sql,"select dwbh from tab_dwzl where wssj>=%ld",m_btime);
459 if(da.QueryRead(sql) == 0)
460 {
461 da.Logout(); //关闭连接
462 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryRead2 %s fail!!\n",sql);
463 return 0;
464 }
465 int j = da.GetRowNum();
466 if(j>0)
467 {
468 char tempCard[5000] = {0};
469 int k = 0;
470 for(int i=0;i<j;i++)
471 {
472 da.GetRow();
473 k+=sprintf(tempCard+k,"%s,", da.row[0]);
474 }
475 tempCard[k-1] ='\0';
476 da.FreeResult();
477 memset(sql,0,500);
478 sprintf(sql,"update tab_dxbj set ljsj = %ld where ljsj = 0 and dwbh in(%s)",m_ltime,tempCard);
479 if(da.QueryWrite(sql) == 0)
480 {
481 da.Logout();
482 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro QueryWrite2 %s fail!!\n",sql);
483 return 0;
484 }
485 }
486 da.Logout();
487 }
488 */
489}
490
491void * kktjpro(void *arg)
492{
493
494 char sql[500];
495 memset(sql,0,500);
496 time_t m_ltime; //现在的时间
497 long m_ttime; //当日0点的时间
498 struct tm *p;
499 time(&m_ltime);
500 p = localtime(&m_ltime);
501 m_ttime =m_ltime - ((p->tm_hour *60*60)+(p->tm_min *60)+(p->tm_sec));
502// DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"0点时间:%ld!!\n",m_ttime);
503
504 while(1)
505 {
506
507 yCMysql da;
508 if(0 == da.Login("127.0.0.1","","","")) //连接数据库
509 {
510 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro login db fail!!\n");
511 return 0;
512
513 }
514 sprintf(sql,"select dwbh,count(dwbh) as kks from tab_kk where sfyx = 1 and kksj>=%ld group by dwbh",m_ttime); //统计0点以后开卡网吧及开卡数
515 if(da.QueryRead(sql) == 0)
516 {
517 da.Logout(); //关闭连接
518 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryRead %s fail!!\n",sql);
519 return 0;
520 }
521 int k = da.GetRowNum();
522 if(k > 0)
523 {
524 char temp[1000][30]; //最高1000家
525 memset(temp,0,1000*30*sizeof(char));
526 for(int i=0;i<k;i++)
527 {
528 da.GetRow();
529 sprintf(temp[i],"%s,%s",da.row[0],da.row[1]);
530 }
531 da.FreeResult();
532 for (int j=0;j<k;j++)
533 {
534 // da.GetRow();
535 char m_dwbh[15] ={0};
536 char m_kks[15] ={0};
537 char *p = temp[j];
538 char *pt = temp[j];
539 while(1)
540 {
541
542 if(*p ==',')
543 {
544 *p = '\0';
545 sprintf(m_dwbh,"%s",pt);
546 p++;
547 pt = p;
548 }
549 else if(*p == '\0')
550 {
551 sprintf(m_kks,"%s",pt);
552 break;
553 }
554 else
555 p++;
556 }
557
558 if(atoi(m_kks) > 5) //网吧开卡数大于5张不进行统计,并删除原来当天的报警项
559 {
560 memset(sql,0,500);
561 sprintf(sql,"delete from tab_kkbj where tjsj >= %ld and dwbh = %s",m_ttime,m_dwbh);
562 if(da.QueryWrite(sql) == 0)
563 {
564 da.Logout(); //关闭连接
565 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryWrite %s fail!!\n",sql);
566 return 0;
567 }
568 continue;
569 }
570 memset(sql,0,500);
571 sprintf(sql,"select dwbh from tab_kkbj where tjsj >= %ld and dwbh = %s",m_ttime,m_dwbh); //查询开卡报警表中是否有今天网吧记录
572 if(da.QueryRead(sql) == 0)
573 {
574 da.Logout(); //关闭连接
575 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryRead %s fail!!\n",sql);
576 return 0;
577 }
578 if(da.GetRowNum() >0)
579 {
580 da.FreeResult();
581 memset(sql,0,500);
582 sprintf(sql,"update tab_kkbj set kks = %s,tjsj = %ld where tjsj >= %ld and dwbh = %s",m_kks,m_ltime,m_ttime,m_dwbh);
583 if(da.QueryWrite(sql) == 0)
584 {
585 da.Logout();
586 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryWrite %s fail!!\n",sql);
587 return 0;
588 }
589 }
590 else
591 {
592 memset(sql,0,500);
593 sprintf(sql,"insert into tab_kkbj(dwbh,kks,tjsj) values(%s,%s,%ld)",m_dwbh,m_kks,m_ltime);
594 if(da.QueryWrite(sql) == 0)
595 {
596 da.Logout();
597 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro QueryWrite %s fail!!\n",sql);
598 return 0;
599 }
600 }
601
602 }
603 }
604 da.Logout();
605 sleep(7200);
606 }
607
608
609}
610
611void main()
612{
613 sleep(5); ///暂停五秒等待数据库
614 Init();
615 pthread_t id;
616 int ret = pthread_create(&id,NULL,dxtjpro,NULL); //创建统计断线线程
617 if(ret != 0)
618 {
619 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"dxtjpro create fail! errcode %d\n",ret);
620 exit(0) ;
621 }
622
623 pthread_t td;
624 int m_ret = pthread_create(&td,NULL,kktjpro,NULL); //创建统计开卡线程
625 if(m_ret != 0)
626 {
627 DEBUG(gb.is_debug,gb.debug_level,gb.log_file,"kktjpro create fail! errcode %d\n",ret);
628 exit(0) ;
629 }
630
631 if(gethour() == 0) //防止程序在0点至1点重启时进行重复统计
632 {
633 sleep(7200); //暂停2个小时
634 }
635
636 while(1)
637 {
638 if(gethour() == 0) //0点统计
639 {
640 datapro();
641 sleep(7200); //暂停2个小时
642 }
643 else
644 {
645 sleep(60); //暂停1分钟
646 }
647 }
648
649}
650
651