-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatisticAction.class.php
More file actions
2207 lines (1863 loc) · 71.9 KB
/
statisticAction.class.php
File metadata and controls
2207 lines (1863 loc) · 71.9 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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class statisticAction extends Action
{
/*增加预测维度
* params: 维度名 可选值 默认值
*/
public function addPredictVector()
{
$vectorname = $_REQUEST['vectorname'];
$defvalue = $_REQUEST['defvalue'];
$predictTable = new Model('statis_predict');
$self = $predictTable->where("vectorname='".$vectorname."'")->find();
if(isset($selft['id'])){
$this->error('已有同样的向量名');
}
$data['vectorname'] = $vectorname;
$data['defvalue'] = $defvalue;
$rt = $predictTable->add($data);
$this->success($rt);
}
public function addVectorOptional()
{
$vectorid = $_REQUEST['vectorid'];
$optional = $_REQUEST['optional'];
$data['vectorid'] = $vectorid;
$data['optional'] = $optional;
$labelTable = new Model('statis_label');
$rt = $labelTable->add($data);
$this->success($rt);
}
public function loadPredictLabel()
{
$id=$_REQUEST['id'];
$predictTable = new Model('statis_label');
$rt = $predictTable->where('vectorid='.$id)->select();
$this->success($rt);
}
public function loadPredictLabelByVectorName()
{
$name = $_REQUEST['vectorName'];
$predictTable = new Model('statis_predict');
$labelTable = new Model('statis_label');
$self = $predictTable ->where("vectorname='$name'")->find();
if(!isset($self['id'])){
$this->error('没有符合的向量名');
}
$rt = $labelTable->where('vectorid='.$self['id'])->select();
$this->success($rt);
}
public function removeVectorOptional()
{
$id = $_REQUEST['id'];
$labelTable = new Model('statis_label');
$labelTable->where('id='.$id)->delete();
$this->success(true);
}
public function modifyVectorOptional()
{
$id = $_REQUEST['id'];
$optional = $_REQUEST['optional'];
$labelTable = new Model('statis_label');
$data['id']=$id;
$data['optional']=$optional;
$labelTable->save($data);
$this->success(true);
}
/*
* 修改维度
*/
public function modifyVector()
{
$id = $_REQUEST['id'];
$key = $_REQUEST['key'];
$value = $_REQUEST['value'];
$predictTable=new Model('statis_predict');
//trans begin
$predictTable->startTrans();
$predictTable->execute(" update statis_predict set $key='$value' where id=".$id);
//trans commit
$predictTable->commit();
$this->success(true);
}
/*
* 删除维度:维度名
*/
public function removePredictVector()
{
$id = $_REQUEST['id'];
$predictTable = new Model('statis_predict');
$predictTable->where('id='.$id)->delete();
$this->success(true);
}
/*
* 加载维度信息
*/
public function loadPredictVector()
{
$predictTable = new Model('statis_predict');
$result = $predictTable->select();
$this->success($result);
}
/*
* 根据日期列出Epgcolumn的情况,加入导入情况
*/
public function loadStatisEpgcolumn()
{
$startDate=$_REQUEST['startDate'];
$endDate=$_REQUEST['endDate'];
$channelID=$_REQUEST['channelID'];
$epgversionTable = new Model("offline_epgversion");
$statisepgTable = new Model("statis_epgversion");
$result = $epgversionTable->query(" select a.ID,a.name,a.type,a.broadcastdate,a.subversion,b.alias from offline_epgversion a,user b ".
" where broadcastdate>='$startDate' and broadcastdate<='$endDate' and channelID=$channelID and a.userid=b.id order by a.broadcastdate asc, a.ID asc");
foreach($result as $key=>$value)
{
$statisepg = $statisepgTable->where('epgversionid='.$value['ID'])->find();
if(!isset($statisepg['ID']))
{
$result[$key]['correctRate']='尚未导入';
$result[$key]['lastEditDate']='尚未导入';
$result[$key]['lastCalculateDate']='尚未导入';
}else{
$result[$key]['correctRate']=$statisepg['correctRate'];
$result[$key]['lastEditDate']=$statisepg['lastEditDate'];
$result[$key]['lastCalculateDate']=$statisepg['lastCalculateDate'];
}
}
$this->success($result);
}
private function __insertStatisNode($nodeID, &$nodeMap, $epgversionid, &$statisepgcolumnTable, $parentID = 0, $position = 0, $level = 0)
{
$node = &$nodeMap[$nodeID];
// 定义插入数据
$insertData = null;
// 单独处理根节点
if(!$node['columnData'])
{
$insertData = array(
"columnID" => $nodeID,
"epgversionid" => $epgversionid,
"level" => $level,
"name" => "root",
"beginTime" => 0,
"endTime" => 0,
"IDMaterial" => 0,
"type" => "root",
"parentID" => 0,
"position" => 0,
"fixed" => 0
);
}
else
{
$columnData = &$node['columnData'];
$insertData = array(
"columnID" => $nodeID,
"epgversionid" => $epgversionid,
"level" => $level,
"name" => $columnData["name"],
"beginTime" => $columnData["beginTime"],
"endTime" => $columnData["endTime"],
"IDMaterial" => $columnData["IDMaterial"],
"type" => $columnData["type"],
"parentID" => $parentID,
"position" => $position,
"fixed" => $columnData['fixed'] ? 1:0
);
}
$insetID = $statisepgcolumnTable->add($insertData);
if(!isset($insetID)||($insetID==false))
{
$epgcolumnTable->rollback();
$this->error('导入有故障');
}
// 循环处理子节点
if(!isset($node['children']))
{
return;
}
// 处理子层次
$level++;
$children = $node['children'];
for($i = 0, $l = count($children); $i < $l; $i++)
{
$childID = $children[$i];
$this->__insertStatisNode($childID, $nodeMap, $epgversionid, $statisepgcolumnTable, $nodeID, $i, $level);
}
}
/*
* 从离线表展开内容到统计表,入参:offline_epgcolumn表id字段
*/
public function createStatisByEpgId()
{
$id=$_REQUEST['ID'];
$subversion = $_REQUEST['subversion'];
$this->_createStatisByEpgId($id,$subversion);
$this->success(true);
}
private function _createStatisByEpgId($epgversionid,$subversion)
{
$id = $epgversionid;
$epgversionTable = new Model('offline_epgversion');
$epgcolumnTable = new Model('offline_epgcolumn');
$statisepgcolumnTable=new Model('statis_epgcolumn');
$statismaterialTable= new Model('statis_material');
$predictTable = new Model('statis_predict');
$statisepgTable = new Model("statis_epgversion");
$statisresultTable = new Model("statis_result");
$epgcolumnTable->startTrans();
$epgcolumnResult=$epgcolumnTable->where('epgversionid='.$id." and subversion=".$subversion)->find();
$epgversionResult = $epgversionTable->where('ID='.$epgcolumnResult['epgversionid'])->find();
$content = $epgcolumnResult['content'];
$realContent = json_decode($content,true);
$nodeMap = $realContent['nodeMap'];
$this->__insertStatisNode(0, $nodeMap, $id, $statisepgcolumnTable);
$statisepgversiondata['epgversionid']=$epgversionResult['ID'];
$statisepgversiondata['subversion']=$epgversionResult['subversion'];
$statisepgversiondata['name']=$epgversionResult['name'];
$statisepgversiondata['type']=$epgversionResult['type'];
$statisepgversiondata['broadcastdate'] = $epgversionResult['broadcastdate'];
$tmprt = $statisepgTable->add($statisepgversiondata);
if(!isset($tmprt)||($tmprt==false))
{
$epgcolumnTable->rollback();
$this->error('加入统计表版本表时发生错误');
}
/*加入统计数据表数据*/
$predictvectors = $predictTable->select();
$result = $statisepgcolumnTable->where('epgversionid='.$id)->select();
$predictArray = array();
foreach($predictvectors as $predictkey=>$predictvalue)
{
$predictResult = $this->predictEpgcolumn($id,$predictvalue['id']);
foreach($result as $columnkey=>$columnvalue)
{
if((int)$columnvalue['level']==3){
//$result[$columnkey]['predict'][$predictvalue['vectorname']]=isset($predictResult[$columnvalue['ID']])?$predictResult[$columnvalue['ID']]:$predictResult['defvalue'];
$predictArray[]=array('key'=>$columnvalue['ID'],'vectorname'=>$predictvalue['vectorname'],'optional'=>isset($predictResult[$columnvalue['ID']])?$predictResult[$columnvalue['ID']]:$predictResult['defvalue']);
}
}
}
$predictjson = json_encode($predictArray);
$resultdata['epgversionid']=$epgversionResult['ID'];
$resultdata['total']=count($predictArray);
$resultdata['changed']=0;
$resultdata['original']=$predictjson;
$resultdata['confirm']=$predictjson;
$resultdata['canstudy']=0;
$tmprt = $statisresultTable->add($resultdata);
if(!isset($tmprt)||($tmprt==false))
{
$epgcolumnTable->rollback();
$this->error('加入统计表结果表时发生错误');
}
$rt=$epgcolumnTable->commit();
return true;
}
public function autoCreateStatisEpg()
{
//获取上个月的第一天和最后一天
$time=strtotime(date("Y-m-d",time()));
$firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
$epgversionTable = new Model('offline_epgversion');
$statisepgversionTable = new Model('statis_epgversion');
$epgresult = $epgversionTable->where("broadcastdate>='$firstday' and broadcastdate<='$lastday'")->select();
if(isset($epgresult[0])&&(count($epgresult)>0)){
foreach ($epgresult as $epgvalue)
{
$statisResult = $statisepgversionTable->where('epgversionid='.$epgvalue['ID'])->find();
if(isset($statisResult['ID'])) continue;
if(true!=$this->_createStatisByEpgId($epgvalue['ID'],$epgvalue['subversion']))
{
Log::write($epgvalue['name'].'导入统计表失败','WARN');
}
}
}else{
$this->error('未找到可以导入的数据');
}
Log::write("$firstday ~ $lastday ".'导入统计表成功','INFO');
$this->success(true);
}
/*
* 删除统计表单
*/
public function removeStatisByEpgId()
{
$ID=$_REQUEST['EPGVersionID'];
$statisepgcolumnTable=new Model('statis_epgcolumn');
$statisepgversionTable = new Model('statis_epgversion');
$statisresultTable = new Model('statis_result');
$statisepgversionresult = $statisepgversionTable->where('epgversionid='.$ID)->find();
if(!isset($statisepgversionresult['ID'])){
$this->error('未找到删除版本');
}
if($statisepgversionresult['checkstatus']!=0){
$this->error('该统计表在使用中不可以删除');
}
$statisepgcolumnTable->startTrans();
$statisepgcolumnTable->where('epgversionid='.$ID)->delete();
$statisepgversionTable->where('epgversionid='.$ID)->delete();
$statisresultTable->where('epgversionid='.$ID)->delete();
$statisepgcolumnTable->commit();
$this->success(true);
}
public function loadStatisEpgcolumnByID()
{
$id=$_REQUEST['ID'];
$statisepgcolumnTable = new Model('statis_epgcolumn');
$statisresultTable = new Model('statis_result');
$statisepgversionTable = new Model('statis_epgversion');
$predictTable = new Model('statis_predict');
$predictvectors = $predictTable->select();
$result = $statisepgcolumnTable->where('epgversionid='.$id)->select();
$statisresult= $statisresultTable->where('epgversionid='.$id)->find();
if(!isset($statisresult['confirm']))
{
$this->error('预测向量缺失');
}
$confirmjson = $statisresult['confirm'];
$confirmArray = json_decode($confirmjson,true);
/*
foreach($predictvectors as $predictkey=>$predictvalue)
{
$predictResult = $this->predictEpgcolumn($id,$predictvalue['id']);
foreach($result as $columnkey=>$columnvalue)
{
if((int)$columnvalue['level']==3){
$result[$columnkey]['predict'][$predictvalue['vectorname']]=isset($predictResult[$columnvalue['ID']])?$predictResult[$columnvalue['ID']]:$predictResult['defvalue'];
}
//$result[$columnkey][$predictvalue['vectorname']]=isset($predictResult[$columnvalue['ID']])?$predictResult[$columnvalue['ID']]:$predictResult['defvalue'];
}
}
*/
foreach($result as $columnkey=>$columnvalue)
{
foreach($confirmArray as $confirmvalue)
{
if($confirmvalue['key']==$columnvalue['ID'])
{
$result[$columnkey]['predict'][$confirmvalue['vectorname']]=$confirmvalue['optional'];
}
}
}
$predictlist = $predictTable->query(" select * from statis_predict");
foreach($predictlist as $prekey=>$list)
{
$prelabel = $predictTable->query(" select * from statis_label where vectorid=".$list['id']);
foreach($prelabel as $label)
{
$predictlist[$prekey]['options'][]=$label['optional'];
}
}
$epgversionResult = $statisepgversionTable->where('epgversionid='.$id)->find();
$statisResult['correctRate'] = $epgversionResult['correctRate'];
$statisResult['lastEditDate'] = $epgversionResult['lastEditDate'];
$statisResult['lastCalculateDate'] = $epgversionResult['lastCalculateDate'];
$this->success(array('epgcolumn'=>$result,'predict'=>$predictlist,'statisresult'=>$statisResult));
}
private function predictEpgcolumn($epgid,$predictid)
{
if(!isset($predictid))
{
return;
}
$epgcolumnTable=new Model('statis_epgcolumn');
$predictTable = new Model('statis_predict');
$labelTable=new Model('statis_label');
$statisEpgResult=$epgcolumnTable->query(" select ID,columnID,name,IDMaterial,type,parentID,epgversionid,beginTime,endTime,level from statis_epgcolumn where level=3 and epgversionid=".$epgid);
$statisSubColumnResult=$epgcolumnTable->query(" select ID,columnID,name,IDMaterial,type,parentID,epgversionid,beginTime,endTime,level from statis_epgcolumn where level=2 and epgversionid=".$epgid);
$samples=$this->createSamples($statisEpgResult,$statisSubColumnResult);
$result=array();
foreach($samples as $samplekey=>$samplevalue)
{
$value=$this->__predict($samplevalue,$predictid);
if($value==0){
$predictresult = $predictTable->where('id='.$predictid)->find();
$result[$statisEpgResult[$samplekey]['ID']]=$predictresult['defvalue'];
}else{
$labelmapresult = $labelTable->where('id='.$value)->find();
$result[$statisEpgResult[$samplekey]['ID']]=$labelmapresult['optional'];
}
}
return $result;
}
/*
* 对指定epgversionid的编播单进行预测统计类别
* 入参:ID
* 出参:预测键值对
*/
public function predictByEpgId()
{
$epgid=$_REQUEST['ID'];
$epgcolumnTable=new Model('statis_epgcolumn');
$predictTable=new Model('statis_predict');
$labelmapTable=new Model('statis_label');
$epgcolumnTable->startTrans();
$statisEpgResult=$epgcolumnTable->query(" select ID,columnID,name,IDMaterial,type,parentID,epgversionid,beginTime,endTime,level from statis_epgcolumn where level=3 and epgversionid=".$epgid);
$statisSubColumnResult=$epgcolumnTable->query(" select ID,columnID,name,IDMaterial,type,parentID,epgversionid,beginTime,endTime,level from statis_epgcolumn where level=2 and epgversionid=".$epgid);
$samples=$this->createSamples($statisEpgResult,$statisSubColumnResult);
$predictResult = $predictTable->select();
$result=array();
foreach($predictResult as $prevalue)
{
foreach($samples as $samplekey=>$samplevalue)
{
$value=$this->__predict($samplevalue,$prevalue['id']);
if($value==0)
{
$result[]=array($statisEpgResult[$samplekey]['ID'],$prevalue['defvalue']);
}else{
$labelmapresult = $labelmapTable->where('id='.$value)->find();
$result[]=array($statisEpgResult[$samplekey]['ID'],$labelmapresult['optional']);
}
}
$totalresult[]=$result;
}
$this->success($totalresult);
}
/*
* 统计表的人工修改
*/
public function updateStatisItems()
{
$epgversionid = $_REQUEST['epgversionid'];
$confirmjson = $_REQUEST['confirmjson'];
$Items=json_decode($confirmjson,true);
$statisresultTable =new Model('statis_result');
$statisepgTable =new Model('statis_epgcolumn');
$self = $statisresultTable->where('epgversionid='.$epgversionid)->find();
$originaljson =$self['original'];
$oldconfirm = $self['confirm'];
$oldconfirmArray = json_decode($oldconfirm,true);
foreach($Items as $Itemkey=>$Item)
{
$columnID=$Item['id'];
$epgresult = $statisepgTable->where("epgversionid=$epgversionid and columnID=$columnID")->find();
if(isset($epgresult['ID'])){
$Items[$Itemkey]['id']=$epgresult['ID'];
}else{
$this->error('未找到识别的修改');
}
}
foreach($oldconfirmArray as $oldkey=>$oldconfirmvalue)
{
foreach($Items as $Item)
{
if(($Item['id']==$oldconfirmvalue['key'])&&($Item['vectorname']==$oldconfirmvalue['vectorname']))
{
$oldconfirmArray[$oldkey]['key']=$Item['id'];
$oldconfirmArray[$oldkey]['vectorname']=$Item['vectorname'];
$oldconfirmArray[$oldkey]['optional']=$Item['optional'];
}
}
}
/*将修改的项汇合到confirm中去*/
$original=json_decode($originaljson,true);
$changed=0;
foreach($original as $originalvalue)
{
foreach($oldconfirmArray as $Item)
{
if(($Item['key']==$originalvalue['key'])&&($Item['vectorname']==$originalvalue['vectorname'])
&&($Item['optional']!=$originalvalue['optional']))
{
$changed+=1;
}
}
}
$data['ID']=$self['ID'];
$data['changed']=$changed;
$data['confirm']=json_encode($oldconfirmArray);
$data['canstudy']=1;
$statisresultTable->save($data);
//修改最后更改时间
$correctRate = (int)(($self['total']-$self['changed'])*100/($self['total']));
$statisresultTable->execute(" update statis_epgversion set lastEditDate=CURRENT_DATE,correctRate=$correctRate".
" where epgversionid=".$self['epgversionid']);
$this->success(true);
}
/*
* 回归测试结果
*/
private function _recursivePredictByID($id)
{
$statisepgTable = new Model('statis_epgcolumn');
$statisresultTable = new Model('statis_result');
$predictTable = new Model('statis_predict');
$predictvectors = $predictTable->select();
$statisResult = $statisresultTable->where('epgversionid='.$id)->find();
$statisepgresult = $statisepgTable->where('epgversionid='.$id)->select();
$predictArray = array();
foreach($predictvectors as $predictkey=>$predictvalue)
{
$predictResult = $this->predictEpgcolumn($statisResult['epgversionid'],$predictvalue['id']);
foreach($statisepgresult as $columnkey=>$columnvalue)
{
if((int)$columnvalue['level']==3)
{
$predictArray[]=array('key'=>$columnvalue['ID'],'vectorname'=>$predictvalue['vectorname'],'optional'=>isset($predictResult[$columnvalue['ID']])?$predictResult[$columnvalue['ID']]:$predictResult['defvalue']);
}
}
}
$statisresultTable->startTrans();
$data['ID'] = $statisResult['ID'];
$statisresultTable->execute("update statis_result set recursivity=null where ID=".$data['ID']);
$data['recursivity']=json_encode($predictArray);
/*没有列入学习的表使用预测值作为当前值*/
if((int)$statisResult['canstudy']!=1){
$statisresultTable->execute("update statis_result set confirm=null where ID=".$data['ID']);
$data['confirm']=json_encode($predictArray);
}
$statisresultTable->save($data);
//更新最后预测时间
$statisresultTable->execute(" update statis_epgversion set lastCalculateDate=CURRENT_DATE".
" where epgversionid=".$statisResult['epgversionid']);
$statisresultTable->commit();
return true;
}
/*根据ids预测*/
public function recursivePredictByIDs()
{
$predictRequest = json_decode($_REQUEST['predictRequest'],true);
if(false==$predictRequest){
$this->error('入参不是合法的Json');
}
foreach($predictRequest as $epgversionid)
{
if(true!=$this->_recursivePredictByID($epgversionid))
{
$this->error('统计单'.$epgversionid."预测失败");
}
}
$this->success(true);
}
public function autoRecursivePredictByID()
{
$statisresultTable = new Model('statis_result');
$allstatisResult = $statisresultTable->where('canstudy=1')->select();
$statisresultTable->startTrans();
foreach($allstatisResult as $statisResult)
{
if(true!=$this->_recursivePredictByID($statisResult['ID']))
{
$statisresultTable->rollback();
$this->error('自动执行回归预测失败');
}
}
$statisresultTable->commit();
$this->success(true);
}
/*
* 统计结果表
* 字段有:单号 统计总条数、人工修改条数
*/
public function addstatisResult()
{
$epgid=$_REQUEST['id'];
$predictjson = $_REQUEST['predictjson'];
$statisresultTable = new Model('statis_result');
$predictArray = json_decode($predictjson);
$data['epgversionid']=$epgid;
$data['total']=count($predictArray);
$data['changed']=0;
$data['original']=$predictjson;
$data['confirm']=$predictjson;
$data['recursivity']=$predictjson;
$rt = $statisresultTable->add($data);
$this->success($rt);
}
//列出已经进入统计表的编播
public function listAllStaticEpg()
{
$epgversionTable=new Model('offline_epgversion');
$epgversionResult=$epgversionTable->query(" select * from offline_epgversion where ID in (select distinct(epgversionid) from statisepgcolumn) ");
$this->success($epgversionResult);
}
/*查询出已经经过智能识别实现类型填充的统计内容
* IDs:需要统计的epgid组成的字符串
*/
public function queryStatisByEpgId()
{
$epgids=$_REQUEST['IDs'];
$statisepgcolumnTable = new Model('statis_epgcolumn');
$statismaterialTable = new Model('statis_material');
$idarray=explode(',', $epgids);
$result=array();
foreach($idarray as $epgidkey=>$epgidvalue)
{
$statisepgcolumnResult=$statisepgcolumnTable->where('epgversionid='.$epgidvalue)->select();
foreach($statisepgcolumnResult as $columnkey=>$columnvalue)
{
$statismaterialResult=$statismaterialTable->where(" materialid=".$columnvalue['IDMaterial'])->find();
if(isset($statismaterialResult)&&(count($statismaterialResult)>1))
{
if(isset($result['statistictype'][$statismaterialResult['statistictype']]))
{
$result['statistictype'][$statismaterialResult['statistictype']] +=$statismaterialResult['duration'];
}else{
$result['statistictype'][$statismaterialResult['statistictype']] = $statismaterialResult['duration'];
}
if(isset($result['specialAD'][$statismaterialResult['specialAD']]))
{
$result['specialAD'][$statismaterialResult['specialAD']] +=$statismaterialResult['duration'];
}else{
$result['specialAD'][$statismaterialResult['specialAD']] = $statismaterialResult['duration'];
}
if(isset($result['broadcasttype'][$statismaterialResult['broadcasttype']]))
{
$result['broadcasttype'][$statismaterialResult['broadcasttype']] +=$statismaterialResult['duration'];
}else{
$result['broadcasttype'][$statismaterialResult['broadcasttype']] = $statismaterialResult['duration'];
}
if(isset($result['firstbroadcast'][$statismaterialResult['firstbroadcast']]))
{
$result['firstbroadcast'][$statismaterialResult['firstbroadcast']] +=$statismaterialResult['duration'];
}else{
$result['firstbroadcast'][$statismaterialResult['firstbroadcast']] = $statismaterialResult['duration'];
}
}
}
}
$this->success($result);
}
//根据统计类型和epgid查询统计结果 返回素材总时长
//type值有statistictype specialAD broadcasttype firstbroadcast 4种
public function queryStatisByTypeAndEpgId()
{
$epgid=$_REQUEST['ID'];
$statisepgcolumnTable = new Model('statis_epgcolumn');
$statismaterialTable = new Model('statis_material');
$statisepgcolumnResult=$statisepgcolumnTable->where(" epgversionid=".$epgid)->select();
$result = array();
foreach($statisepgcolumnResult as $epgkey=>$epgvalue)
{
$statismaterialResult=$statismaterialTable->query(" select * from statis_material where materialid =".$epgvalue['IDMaterial']);
if(isset($statismaterialResult)&&(count($statismaterialResult)>0))
{
$result[$statismaterialResult[0][$type]] += $statismaterialResult[0]['duration'];
}else{
$result[$statismaterialResult[0][$type]] = $statismaterialResult[0]['duration'];
}
}
$this->success($result);
}
//根据播出日期查询某种类型素材总时长
public function queryStatisByTypeAndDateRange()
{
$beginDate=$_REQUEST['beginDate'];
$endDate=$_REQUEST['endDate'];
$type=$_REQUEST['type'];
$epgversionTable=new Model('offline_epgversion');
$statisepgcolumnTable = new Model('statis_epgcolumn');
$statismaterialTable = new Model('statis_material');
$epgversionResult=$epgversionTable->where("broadcastdate>='".$beginDate."' and broadcastdate<='".$endDate."'")->select();
$result = array();
foreach($epgversionResult as $versionkey=>$versionvalue)
{
$statisepgcolumnResult=$statisepgcolumnTable->where(" epgversionid=".$versionvalue['ID'])->select();
foreach($statisepgcolumnResult as $epgkey=>$epgvalue)
{
$statismaterialResult=$statismaterialTable->query(" select * from statis_material where materialid =".$epgvalue['IDMaterial']);
if(isset($statismaterialResult)&&(count($statismaterialResult)>0))
{
$result[$statismaterialResult[0][$type]] += $statismaterialResult[0]['duration'];
}else{
$result[$statismaterialResult[0][$type]] = $statismaterialResult[0]['duration'];
}
}
}
$this->success($result);
}
//生成统计表的统计类型,加入人工智能填充统计字段
public function queryDateRangeStatics()
{
$beginDate=$_REQUEST['beginDate'];
$endDate=$_REQUEST['endDate'];
$epgversionTable=new Model('epgversion');
$statisepgcolumnTable = new Model('statis_epgcolumn');
$statismaterialTable = new Model('statis_material');
//epgversion表需要加入代表进入统计的字段
$epgversionResult=$epgversionTable->where("broadcastdate>='".$beginDate."' and broadcastdate<='".$endDate."'")->select();
$this->success($epgversionResult);
}
/* SVM示例
<?php
$data = array(
array(-1, 1 => 0.43, 3 => 0.12, 9284 => 0.2),
array(1, 1 => 0.22, 5 => 0.01, 94 => 0.11),
);
$svm = new SVM();
$model = $svm->train($data);
$data = array(1 => 0.43, 3 => 0.12, 9284 => 0.2);
$result = $model->predict($data);
var_dump($result);
$model->save('model.svm');
?>
*
*/
//增加类型列
public function addMaterialTypes()
{
$ID=$_REQUEST['ID'];
$type=$_REQUEST['type'];
$statisEpgTable=new Model('statis_epgcolumn');
$statisMaterialTable = new Model('statis_material');
$statisEpgresult=$statisEpgTable->where('columnID='.$ID)->find();
$statisMaterialTable->execute(' update statismaterial set type= '.$type.' where materialid='.$ID);
$this->success(true);
}
//热词分析
private function __parseHotWords($name,&$sample,$offset)
{
$hotwords=array("预告", "正片", "公益", "广告", "变换", "高清", "标清", "尾", "集", "片花", "宣传片", "复播", "首播", "精华", "新闻", "避让", "接下来", "模块", "设备检修", "江苏", "下三", "第", "极速递", "前", "秒", "VCR", "版", "即将", "播出", "片头", "栏目", "信息", "MV", "日", "上载" );
foreach($hotwords as $key=>$value)
{
if(strstr($name,$value) != false)
{
$sample[$key+$offset]=1;
}
}
}
//获取样例维度 时间用time_to_sec从数据库获取
private function __getSampleScale($materialInfo)
{
$vector=1;
$sample=array();
$sample[0]=$materialInfo['label'];
// 维度1、播出时刻,有效范围为7:00 至 第二天早上7:00(系统表达为31:00),要进行规范化处理,如7:00:00,我们希望最终得到(-1,1)之间的数值播出时刻维度值 = 化为秒数(时间点) / 24小时秒数 - 1
$sample[$vector++]=($materialInfo['beginTime']-25200)/43200 -1.0;
// 维度2,3、子栏目时长, 相对起始时间点
$sample[$vector++]=($materialInfo['subduration']-$materialInfo['minsubduration'])*2/($materialInfo['maxsubduration']-$materialInfo['minsubduration'])-1.0;
$sample[$vector++]=($materialInfo['relativeTime']-$materialInfo['minrelativeTime'])*2/($materialInfo['maxrelativeTime']-$materialInfo['minrelativeTime'])-1.0;
// 维度4、节目时长
$sample[$vector++]=($materialInfo['duration']-$materialInfo['minduration'])*2/($materialInfo['maxduration']-$materialInfo['minduration'])-1.0;
// 维度5、相对时长 = 节目时长 / 栏目时长 * 2 - 1.0;
if($materialInfo['subduration']!=0){
$relativeduration=$materialInfo['duration']/$materialInfo['subduration'];
}else{
$relativeduration=999;
}
$sample[$vector++]=$relativeduration*2-1.0;
$this->__parseHotWords($materialInfo['name'], $sample, $vector);
return $sample;
}
/*生成各种维度的预测训练文件*/
private function __createTrainingfile($samples,$vectorid)
{
if(!isset($vectorid))
{
$vectorid=1;
}
$fp=fopen((string)$vectorid."training.txt","w");
$label=array();
$labelmapTable = new Model('statis_label');
foreach($samples as $materialinfo)
{
$labelresult = $labelmapTable->where("vectorid=$vectorid and optional='".$materialinfo[0]."'")->find();
$strline=$labelresult['id'];
foreach($materialinfo as $mkey=>$mvalue)
{
if($mkey!=0)
{
$tmpvalue=number_format($mvalue,2);
$strline = $strline." $mkey:$tmpvalue";
}
}
$strline = $strline."\r\n";
fputs($fp,$strline);
}
fclose($fp);
}
/*执行后生成model文件*/
private function __createModelfile($vectorid)
{
$svm=new svm();
try{
$options=array();
$filename='svmparams.txt';
if(!file_exists($filename))
{
$options[206]=128.0;
$options[201]=0.0078125;
}else{
$fp=fopen($filename,"r");
$contents = fread($fp, filesize ($filename));
$params=explode(';', $contents);
$parammap=array();
foreach($params as $paramvalue)
{
$parammap=explode('=', $paramvalue);
switch ($parammap[0])
{
case 'c':
$options[206]=doubleval($parammap[1]);
break;
case 'g':
$options[201]=doubleval($parammap[1]);
break;
default:
break;
}
}
fclose($fp);
}
$svm->setOptions($options);
}catch(SVMException $e){
$this->error('set options error');
}
if(file_exists((string)$vectorid.'training.txt'))
{
$model=$svm->train((string)$vectorid.'training.txt');
}else{
$this->error("training file not exists");
}
try{
$model->save((string)$vectorid.'training.model');
}catch(SvmException $e){
$this->error('save model error,check write rights');
}
}
//识别类型
//@materialinfo 输入的未识别文件
private function __predict($data,$predictid)
{
if(!file_exists((string)$predictid.'training.model'))
{
return 0;
}else{
$model = new svmmodel();
$model->load((string)$predictid.'training.model');
$result=$model->predict($data);
return $result;
}