Inspection.php
37.2 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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
<?php
namespace app\api\controller\v7\screen;
use app\common\controller\Api;
use fast\Random;
use think\Db;
/**
* 巡检管理
* Class Inspection
* @package app\api\controller\v7\screen
*/
class Inspection extends Api
{
public $noNeedLogin = ['*'];
public $noNeedRight = ['*'];
protected $warningStatusList = [
'-1'=>'审核失败',
'0'=>'提交隐患',
'1'=>'审核通过',
'2'=>'处理完成'
];
protected $workorderStatusList = [
'0'=>'待处理',
'1'=>'待审核',
'2'=>'已处理',
'3'=>'审核未通过',
'4'=>'无法处理'
];
protected $projectStateList = [
0=>'进行中',
1=>'正常',
2=>'异常'
];
public function _initialize()
{
parent::_initialize();
}
//问题上传
public function problem(){
$list = Db::name('inspection_workorder')
->alias('w')
->field('w.id,w.message,w.images,war.title,w.status')
->join('inspection_warning war', 'war.id=w.warning_id', 'LEFT')
->order(['w.id' => 'desc'])
->page(1,30)
->select();
foreach ($list as &$item){
$item['status_text'] = $this->workorderStatusList[$item['status']];
if (empty($item['title'])){
$item['title'] = $item['message'];
}
}
$this->success('成功', $list);
}
//问题类型
public function problemStatics(){
$category = Db::name('inspection_warning_category')->field('id,name')->where(['level' => 1, 'show_switch' => 1, 'deletetime' => null])->select();
$total = Db::name('inspection_workorder')->count();
foreach ($category as &$item){
$item['num'] = Db::name('inspection_workorder')->where(['category_pid' => $item['id']])->count();
if ($total > 0){
$item['rate'] = round($item['num'] / $total * 100, 2) . '%';
}else{
$item['rate'] = '0.00%';
}
}
$this->success('成功', $category);
}
//问题趋势图
public function problemMap(){
$category = Db::name('inspection_warning_category')->field('id,name')->where(['level' => 1, 'show_switch' => 1, 'deletetime' => null])->select();
$workorderTableName = 'inspection_workorder';
// 按月统计
$currentMonthDate = date('Y-m'); // 获取当前年份和月份
$currentMonth = strtotime($currentMonthDate);
$previousMonth1 = strtotime("-1 months", $currentMonth); // 计算前一个月的时间戳
$previousMonthDate1 = date('Y-m', $previousMonth1); // 格式化为年份和月份字符串
$previousMonth2 = strtotime("-2 months", $currentMonth);
$previousMonthDate2 = date('Y-m', $previousMonth2);
$previousMonth3 = strtotime("-3 months", $currentMonth);
$previousMonthDate3 = date('Y-m', $previousMonth3);
$previousMonth4 = strtotime("-4 months", $currentMonth);
$previousMonthDate4 = date('Y-m', $previousMonth4);
$previousMonth5 = strtotime("-5 months", $currentMonth);
$previousMonthDate5 = date('Y-m', $previousMonth5);
$previousMonth6 = strtotime("-6 months", $currentMonth);
$previousMonthDate6 = date('Y-m', $previousMonth6);
$previousMonth7 = strtotime("-7 months", $currentMonth);
$previousMonthDate7 = date('Y-m', $previousMonth7);
$previousMonth8 = strtotime("-8 months", $currentMonth);
$previousMonthDate8 = date('Y-m', $previousMonth8);
$previousMonth9 = strtotime("-9 months", $currentMonth);
$previousMonthDate9 = date('Y-m', $previousMonth9);
$previousMonth10 = strtotime("-10 months", $currentMonth);
$previousMonthDate10 = date('Y-m', $previousMonth10);
$previousMonth11 = strtotime("-11 months", $currentMonth);
$previousMonthDate11 = date('Y-m', $previousMonth11);
//查询某分类某时间段工单数量
function getNum($pid,$timeStart,$timeEnd){
$num = Db::name('inspection_workorder')
->where(['category_pid' => $pid,'start_time' => ['between', [$timeStart, $timeEnd]]])->count();
return $num;
}
$result = [
'category' => $category,
'x' => [
$previousMonthDate11,
$previousMonthDate10,
$previousMonthDate9,
$previousMonthDate8,
$previousMonthDate7,
$previousMonthDate6,
$previousMonthDate5,
$previousMonthDate4,
$previousMonthDate3,
$previousMonthDate2,
$previousMonthDate1,
$currentMonthDate,
]
];
foreach ($category as $item){
$result[$item['id']] = [
getNum($item['id'],$previousMonth11,$previousMonth10 - 1),
getNum($item['id'],$previousMonth10,$previousMonth9 - 1),
getNum($item['id'],$previousMonth9,$previousMonth8 - 1),
getNum($item['id'],$previousMonth8,$previousMonth7 - 1),
getNum($item['id'],$previousMonth7,$previousMonth6 - 1),
getNum($item['id'],$previousMonth6,$previousMonth5 - 1),
getNum($item['id'],$previousMonth5,$previousMonth4 - 1),
getNum($item['id'],$previousMonth4,$previousMonth3 - 1),
getNum($item['id'],$previousMonth3,$previousMonth2 - 1),
getNum($item['id'],$previousMonth2,$previousMonth1 - 1),
getNum($item['id'],$previousMonth1,$currentMonth - 1),
getNum($item['id'],$currentMonth,time()),
];
}
$this->success('成功', $result);
}
//巡检数据统计
public function warningStatics(){
$total_num = Db::name('inspection_project')->count();
$finish_num = Db::name('inspection_project')->where(['status' => 1])->count();
$finish_rate = round($finish_num/$total_num*100, 2) . '%';
$list = Db::name('inspection_warning')
->alias('w')
->field('w.id,w.title,w.staff_id,staff.staff_name,w.createtime')
->join('inspection_staff staff','staff.id=w.staff_id','LEFT')
->order(['w.id' => 'desc'])
->page(1,30)
->select();
foreach ($list as &$item){
$item['createtime'] = date('Y-m-d H:i', $item['createtime']);
}
$result = [
'total_num' => $total_num,
'finish_num' => $finish_num,
'finish_rate' => $finish_rate,
'rows' => $list
];
$this->success('成功', $result);
}
//异常详情
public function warningDetail(){
$id = $this->request->param('id');
if (empty($id)){
$this->error('异常id必须');
}
$find = Db::name('inspection_warning')
->alias('w')
->field('w.*,staff.staff_name,area.area_name,cate.name as category_name,site.site_name,item.item_name')
->join('inspection_staff staff','staff.id=w.staff_id','LEFT')
->join('inspection_warning_category cate','cate.id=w.category_id','LEFT')
->join('inspection_area area','area.id=w.area_id','LEFT')
->join('inspection_area_site site','site.id=w.areasite_id','LEFT')
->join('inspection_area_item item','item.id=w.areaitem_id','LEFT')
->where('w.id',$id)
->find();
if (!$find){
$this->error('数据不存在');
}
$find['status_text'] = $this->warningStatusList[$find['status']];
$find['createtime'] = date('Y-m-d H:i', $find['createtime']);
//查询回复内容
$reply = Db::name('inspection_warning_reply')
->alias('wr')
->field('wr.*,staff.staff_name')
->join('inspection_staff staff','staff.id=wr.staffid','LEFT')
->where('wr.wid', $find['id'])
->order(['wr.id' => 'desc'])
->select();
if (!empty($reply)){
foreach ($reply as &$re){
$re['status_text'] = $this->warningStatusList[$re['status']];
$re['createtime'] = date('Y-m-d H:i', $re['createtime']);
}
}else{
$reply = [];
}
$find['reply_info'] = $reply;
$this->success('成功', $find);
}
//巡检趋势图
public function warningMap(){
$projectTableName = 'inspection_project';
// 按月统计
$currentMonthDate = date('Y-m'); // 获取当前年份和月份
$currentMonth = strtotime($currentMonthDate);
$previousMonth1 = strtotime("-1 months", $currentMonth); // 计算前一个月的时间戳
$previousMonthDate1 = date('Y-m', $previousMonth1); // 格式化为年份和月份字符串
$previousMonth2 = strtotime("-2 months", $currentMonth);
$previousMonthDate2 = date('Y-m', $previousMonth2);
$previousMonth3 = strtotime("-3 months", $currentMonth);
$previousMonthDate3 = date('Y-m', $previousMonth3);
$previousMonth4 = strtotime("-4 months", $currentMonth);
$previousMonthDate4 = date('Y-m', $previousMonth4);
$previousMonth5 = strtotime("-5 months", $currentMonth);
$previousMonthDate5 = date('Y-m', $previousMonth5);
$previousMonth6 = strtotime("-6 months", $currentMonth);
$previousMonthDate6 = date('Y-m', $previousMonth6);
$previousMonth7 = strtotime("-7 months", $currentMonth);
$previousMonthDate7 = date('Y-m', $previousMonth7);
$previousMonth8 = strtotime("-8 months", $currentMonth);
$previousMonthDate8 = date('Y-m', $previousMonth8);
$previousMonth9 = strtotime("-9 months", $currentMonth);
$previousMonthDate9 = date('Y-m', $previousMonth9);
$previousMonth10 = strtotime("-10 months", $currentMonth);
$previousMonthDate10 = date('Y-m', $previousMonth10);
$previousMonth11 = strtotime("-11 months", $currentMonth);
$previousMonthDate11 = date('Y-m', $previousMonth11);
$current_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$currentMonth, time()]]])->count();
$current_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$currentMonth, time()]]])->count();
$current_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$currentMonth, time()]]])->count();
$pre1_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth1, $currentMonth - 1]]])->count();
$pre1_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth1, $currentMonth - 1]]])->count();
$pre1_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth1, $currentMonth - 1]]])->count();
$pre2_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth2, $previousMonth1 - 1]]])->count();
$pre2_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth2, $previousMonth1 - 1]]])->count();
$pre2_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth2, $previousMonth1 - 1]]])->count();
$pre3_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth3, $previousMonth2 - 1]]])->count();
$pre3_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth3, $previousMonth2 - 1]]])->count();
$pre3_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth3, $previousMonth2 - 1]]])->count();
$pre4_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth4, $previousMonth3 - 1]]])->count();
$pre4_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth4, $previousMonth3 - 1]]])->count();
$pre4_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth4, $previousMonth3 - 1]]])->count();
$pre5_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth5, $previousMonth4 - 1]]])->count();
$pre5_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth5, $previousMonth4 - 1]]])->count();
$pre5_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth5, $previousMonth4 - 1]]])->count();
$pre6_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth6, $previousMonth5 - 1]]])->count();
$pre6_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth6, $previousMonth5 - 1]]])->count();
$pre6_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth6, $previousMonth5 - 1]]])->count();
$pre7_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth7, $previousMonth6 - 1]]])->count();
$pre7_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth7, $previousMonth6 - 1]]])->count();
$pre7_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth7, $previousMonth6 - 1]]])->count();
$pre8_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth8, $previousMonth7 - 1]]])->count();
$pre8_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth8, $previousMonth7 - 1]]])->count();
$pre8_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth8, $previousMonth7 - 1]]])->count();
$pre9_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth9, $previousMonth8 - 1]]])->count();
$pre9_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth9, $previousMonth8 - 1]]])->count();
$pre9_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth9, $previousMonth8 - 1]]])->count();
$pre10_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth10, $previousMonth9 - 1]]])->count();
$pre10_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth10, $previousMonth9 - 1]]])->count();
$pre10_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth10, $previousMonth9 - 1]]])->count();
$pre11_inspection_num = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$previousMonth11, $previousMonth10 - 1]]])->count();
$pre11_noinspection_num = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$previousMonth11, $previousMonth10 - 1]]])->count();
$pre11_problem_num = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$previousMonth11, $previousMonth10 - 1]]])->count();
//按周统计
$currentW = date('w') - 1;
$currentWeek = strtotime("-$currentW days", strtotime(date('Y-m-d')));
$preW1 = $currentW + 7;
$preWeek1 = strtotime("-$preW1 days", strtotime(date('Y-m-d')));
$preW2 = $preW1 + 7;
$preWeek2 = strtotime("-$preW2 days", strtotime(date('Y-m-d')));
$preW3 = $preW2 + 7;
$preWeek3 = strtotime("-$preW3 days", strtotime(date('Y-m-d')));
$preW4 = $preW3 + 7;
$preWeek4 = strtotime("-$preW4 days", strtotime(date('Y-m-d')));
$preW5 = $preW4 + 7;
$preWeek5 = strtotime("-$preW5 days", strtotime(date('Y-m-d')));
$preW6 = $preW5 + 7;
$preWeek6 = strtotime("-$preW6 days", strtotime(date('Y-m-d')));
$preW7 = $preW6 + 7;
$preWeek7 = strtotime("-$preW7 days", strtotime(date('Y-m-d')));
$preW8 = $preW7 + 7;
$preWeek8 = strtotime("-$preW8 days", strtotime(date('Y-m-d')));
$preW9 = $preW8 + 7;
$preWeek9 = strtotime("-$preW9 days", strtotime(date('Y-m-d')));
$preW10 = $preW9 + 7;
$preWeek10 = strtotime("-$preW10 days", strtotime(date('Y-m-d')));
$preW11 = $preW10 + 7;
$preWeek11 = strtotime("-$preW11 days", strtotime(date('Y-m-d')));
$current_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$currentWeek, time()]]])->count();
$current_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$currentWeek, time()]]])->count();
$current_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$currentWeek, time()]]])->count();
$pre1_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek1, $currentWeek - 1]]])->count();
$pre1_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek1, $currentWeek - 1]]])->count();
$pre1_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek1, $currentWeek - 1]]])->count();
$pre2_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek2, $preWeek1 - 1]]])->count();
$pre2_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek2, $preWeek1 - 1]]])->count();
$pre2_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek2, $preWeek1 - 1]]])->count();
$pre3_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek3, $preWeek2 - 1]]])->count();
$pre3_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek3, $preWeek2 - 1]]])->count();
$pre3_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek3, $preWeek2 - 1]]])->count();
$pre4_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek4, $preWeek3 - 1]]])->count();
$pre4_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek4, $preWeek3 - 1]]])->count();
$pre4_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek4, $preWeek3 - 1]]])->count();
$pre5_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek5, $preWeek4 - 1]]])->count();
$pre5_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek5, $preWeek4 - 1]]])->count();
$pre5_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek5, $preWeek4 - 1]]])->count();
$pre6_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek6, $preWeek5 - 1]]])->count();
$pre6_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek6, $preWeek5 - 1]]])->count();
$pre6_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek6, $preWeek5 - 1]]])->count();
$pre7_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek7, $preWeek6 - 1]]])->count();
$pre7_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek7, $preWeek6 - 1]]])->count();
$pre7_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek7, $preWeek6 - 1]]])->count();
$pre8_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek8, $preWeek7 - 1]]])->count();
$pre8_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek8, $preWeek7 - 1]]])->count();
$pre8_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek8, $preWeek7 - 1]]])->count();
$pre9_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek9, $preWeek8 - 1]]])->count();
$pre9_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek9, $preWeek8 - 1]]])->count();
$pre9_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek9, $preWeek8 - 1]]])->count();
$pre10_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek10, $preWeek9 - 1]]])->count();
$pre10_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek10, $preWeek9 - 1]]])->count();
$pre10_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek10, $preWeek9 - 1]]])->count();
$pre11_inspection_num_w = Db::name($projectTableName)
->where(['status' => 1,'begintime' => ['between', [$preWeek11, $preWeek10 - 1]]])->count();
$pre11_noinspection_num_w = Db::name($projectTableName)
->where(['status' => -1,'begintime' => ['between', [$preWeek11, $preWeek10 - 1]]])->count();
$pre11_problem_num_w = Db::name($projectTableName)
->where(['state' => 2, 'begintime' => ['between', [$preWeek11, $preWeek10 - 1]]])->count();
//巡检数据
$list = Db::name($projectTableName)
->alias('p')
->field('p.id,site.site_name,staff.staff_name,p.begintime,state')
->where('status', 1)
->join('inspection_area_site site','site.id=p.area_site_id','LEFT')
->join('inspection_staff staff','staff.id=p.staff_id','LEFT')
->order(['begintime' => 'desc'])
->page(1,30)
->select();
foreach ($list as &$item){
$item['begintime'] = date('Y-m-d H:i', $item['begintime']);
$item['state_text'] = $this->projectStateList[$item['state']];
}
$result = [
'by_month' => [
'inspection_num' => [
$pre11_inspection_num,
$pre10_inspection_num,
$pre9_inspection_num,
$pre8_inspection_num,
$pre7_inspection_num,
$pre6_inspection_num,
$pre5_inspection_num,
$pre4_inspection_num,
$pre3_inspection_num,
$pre2_inspection_num,
$pre1_inspection_num,
$current_inspection_num,
],
'inspection_not' => [
$pre11_noinspection_num,
$pre10_noinspection_num,
$pre9_noinspection_num,
$pre8_noinspection_num,
$pre7_noinspection_num,
$pre6_noinspection_num,
$pre5_noinspection_num,
$pre4_noinspection_num,
$pre3_noinspection_num,
$pre2_noinspection_num,
$pre1_noinspection_num,
$current_noinspection_num,
],
'problem_num' => [
$pre11_problem_num,
$pre10_problem_num,
$pre9_problem_num,
$pre8_problem_num,
$pre7_problem_num,
$pre6_problem_num,
$pre5_problem_num,
$pre4_problem_num,
$pre3_problem_num,
$pre2_problem_num,
$pre1_problem_num,
$current_problem_num,
],
'x' => [
$previousMonthDate11,
$previousMonthDate10,
$previousMonthDate9,
$previousMonthDate8,
$previousMonthDate7,
$previousMonthDate6,
$previousMonthDate5,
$previousMonthDate4,
$previousMonthDate3,
$previousMonthDate2,
$previousMonthDate1,
$currentMonthDate,
],
],
'by_week' => [
'inspection_num' => [
$pre11_inspection_num_w,
$pre10_inspection_num_w,
$pre9_inspection_num_w,
$pre8_inspection_num_w,
$pre7_inspection_num_w,
$pre6_inspection_num_w,
$pre5_inspection_num_w,
$pre4_inspection_num_w,
$pre3_inspection_num_w,
$pre2_inspection_num_w,
$pre1_inspection_num_w,
$current_inspection_num_w,
],
'inspection_not' => [
$pre11_noinspection_num_w,
$pre10_noinspection_num_w,
$pre9_noinspection_num_w,
$pre8_noinspection_num_w,
$pre7_noinspection_num_w,
$pre6_noinspection_num_w,
$pre5_noinspection_num_w,
$pre4_noinspection_num_w,
$pre3_noinspection_num_w,
$pre2_noinspection_num_w,
$pre1_noinspection_num_w,
$current_noinspection_num_w,
],
'problem_num' => [
$pre11_problem_num_w,
$pre10_problem_num_w,
$pre9_problem_num_w,
$pre8_problem_num_w,
$pre7_problem_num_w,
$pre6_problem_num_w,
$pre5_problem_num_w,
$pre4_problem_num_w,
$pre3_problem_num_w,
$pre2_problem_num_w,
$pre1_problem_num_w,
$current_problem_num_w,
],
'x' => [
'-11周',
'-10周',
'-9周',
'-8周',
'-7周',
'-6周',
'-5周',
'-4周',
'-3周',
'-2周',
'-1周',
'本周',
],
],
'rows' => $list
];
$this->success('成功', $result);
}
/**
* 【更多】查询巡检数据或者下载巡检数据
*/
public function warningExport(){
$type = request()->param('type');
$startTime = request()->param('start_time');
$endTime = request()->param('end_time');
$routeId = request()->param('route_id');
$siteId = request()->param('site_id');
$page = request()->param('page');
$limit = request()->param('limit');
$page = empty($page) ? 1 : intval($page);
$limit = empty($limit) ? 50 : intval($limit);
if (!in_array($type, [1,2])){
$this->error('请求类型参数错误');
}
if ($type == 2){
if (empty($routeId)){
$this->error('请选择路线');
}
if (empty($siteId)){
$this->error('请选择巡检点');
}
if (empty($startTime) || empty($endTime)){
$this->error('请选择开始时间和结束时间');
}
}
$where = [
'p.status' => 1
];
if (!empty($routeId)){
$where['p.route_id'] = $routeId;
}
if (!empty($siteId)){
$where['p.area_site_id'] = $siteId;
}
if (!empty($startTime) && !empty($endTime)){
$where['p.begintime'] = ['between',[$startTime, $endTime]];
}elseif (!empty($startTime) && empty($endTime)){
$where['p.begintime'] = ['egt', $startTime];
}elseif (empty($startTime) && !empty($endTime)){
$where['p.begintime'] = ['elt', $endTime];
}
$order = [
'p.begintime' => 'desc'
];
$field = [
'p.id',
'site.site_name',
'staff.staff_name',
'p.begintime',
'p.state'
];
//导出数据列表返回50条
$tableNameData = 'inspection_project';
if ($type == 1){
$total = Db::name($tableNameData)
->alias('p')
->field($field)
->where($where)
->join('inspection_area_site site','site.id=p.area_site_id','LEFT')
->join('inspection_staff staff','staff.id=p.staff_id','LEFT')
->count();
$list = [];
if ($total > 0){
$list = Db::name($tableNameData)
->alias('p')
->field($field)
->where($where)
->join('inspection_area_site site','site.id=p.area_site_id','LEFT')
->join('inspection_staff staff','staff.id=p.staff_id','LEFT')
->order($order)
->page($page,$limit)
->select();
}
foreach ($list as &$item){
$item['begintime_text'] = date('Y-m-d H:i:s', $item['begintime']);
$item['state_text'] = $this->projectStateList[$item['state']];
}
$this->success('成功', ['total' => $total, 'rows' => $list]);
}else{
//下载所有符合条件的数据
$list = Db::name($tableNameData)
->alias('p')
->field($field)
->where($where)
->join('inspection_area_site site','site.id=p.area_site_id','LEFT')
->join('inspection_staff staff','staff.id=p.staff_id','LEFT')
->order($order)
->select();
foreach ($list as &$item){
$item['begintime_text'] = date('Y-m-d H:i:s', $item['begintime']);
$item['state_text'] = $this->projectStateList[$item['state']];
}
//排版excel
// 实例化excel
$phpExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$title = '巡检数据';
$phpExcel->getProperties()->setTitle($title);
$phpExcel->getProperties()->setSubject($title);
$columnArr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
$fieldNum = count($field);
for ($i = 0; $i < $fieldNum; $i++){
// 对单元格设置居中效果
$phpExcel->getActiveSheet()->getStyle($columnArr[$i])->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
//设置宽度
$phpExcel->getActiveSheet()->getColumnDimension($columnArr[$i])->setWidth(20);
}
$phpExcel->setActiveSheetIndex(0);
//字体加粗
$phpExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);
$phpExcel->getActiveSheet()->setCellValue('A1', '巡检记录');
$phpExcel->getActiveSheet()->setCellValue('B1', '负责人');
$phpExcel->getActiveSheet()->setCellValue('C1', '巡检日期');
$phpExcel->getActiveSheet()->setCellValue('D1', '状态');
$rowNum = 2;
foreach ($list as $item){
$phpExcel->getActiveSheet()->setCellValue('A'.$rowNum, $item['site_name']);
$phpExcel->getActiveSheet()->setCellValue('B'.$rowNum, $item['staff_name']);
$phpExcel->getActiveSheet()->setCellValue('C'.$rowNum, $item['begintime_text']);
$phpExcel->getActiveSheet()->setCellValue('D'.$rowNum, $item['state_text']);
$rowNum++;
}
// 重命名工作sheet
$phpExcel->getActiveSheet()->setTitle($title);
// 设置第一个sheet为工作的sheet
$phpExcel->setActiveSheetIndex(0);
ob_end_clean();
// 保存Excel 2007格式文件,保存路径为当前路径,名字为export.xlsx
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($phpExcel, 'Xlsx');
$file = date('Ymd').Random::alnum() . '.xlsx';
$objWriter->save($file);
header('Content-type:application/octet-stream');
$filename = basename($file);
header('Content-Disposition:attachment;filename = ' . $filename);
header('Accept-ranges:bytes');
header('Accept-length:' . filesize($file));
readfile($file);
unlink($file);
exit;
}
}
/**
* 【更多】路线列表(供选项使用)
*/
public function routeList(){
$list = Db::name('inspection_route')->field('id,route_name')->order(['createtime' => 'DESC'])->select();
$this->success('成功', $list);
}
/**
* 【更多】巡检点列表(供选项使用)
*/
public function siteList(){
$routeId = request()->param('route_id');
if (empty($routeId)){
$this->error('路线ID必须');
}
$list = Db::name('inspection_route_site')
->alias('rs')
->field('site.id,site.site_name')
->join('inspection_area_site site', 'site.id=rs.area_site_id', 'LEFT')
->where(['rs.route_id' => $routeId])
->order(['site.createtime' => 'DESC'])
->select();
$this->success('成功', $list);
}
//巡检任务详情
public function projectDetail(){
$id = $this->request->param('id');
if (empty($id)){
$this->error('任务id必须');
}
$find = Db::name('inspection_project')
->alias('p')
->field('p.*,site.site_name,staff.staff_name,state,plan.plan_name,area.area_name,route.route_name,staff.mobile')
->where('p.id', $id)
->join('inspection_area_site site','site.id=p.area_site_id','LEFT')
->join('inspection_staff staff','staff.id=p.staff_id','LEFT')
->join('inspection_plan plan','plan.id=p.plan_id','LEFT')
->join('inspection_area area','area.id=p.area_id','LEFT')
->join('inspection_route route','route.id=p.route_id','LEFT')
->find();
if (!$find){
$this->error('数据不存在');
}
$find['begintime'] = date('Y-m-d H:i', $find['begintime']);
$find['state_text'] = $this->projectStateList[$find['state']];
//查询巡检项结果
$pitem = Db::name('inspection_project_item')
->where('project_id', $find['id'])
->order(['id' => 'desc'])
->select();
if (!empty($pitem)){
foreach ($pitem as &$pi){
$pi['type_text'] = $pi['type'] == 1 ? '正常/异常' : '数据';
}
}else{
$pitem = [];
}
$find['item_info'] = $pitem;
//统计异常上报信息
$totalWarningNum = Db::name('inspection_warning')->where(['project_id' => $find['id']])->count();
$dealWarningNum = Db::name('inspection_warning')->where(['project_id' => $find['id'], 'status' => '2'])->count();
$noDealWarningNum = $totalWarningNum - $dealWarningNum;
$find['total_warning_num'] = $totalWarningNum;
$find['deal_warning_num'] = $dealWarningNum;
$find['nodeal_warning_num'] = $noDealWarningNum;
$this->success('成功', $find);
}
}