colly_wyx
2018-04-28 7f02c34d386f16edb9cbf71976651d6f9495c9f7
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
<?php
 
class Service_Task extends System_Service_Base{
 
    public function __construct(){
        $this->data_hourly_model = new DataHourlyModel();
        $this->data_three_hourly_model = new DataThreeHourlyModel();
        $this->task_model = new TaskModel();
    }
 
    public function summary(){
        $hourly_model = new DataHourlyModel();
        $task_lists = $this->task_model->getList();
 
        foreach ($task_lists as $task) {
            $day = $task['date'];
            $user_id = $task['user_id'];
            $time_slot_arr = array();
            for($time_slot = 1; $time_slot<=8 ; $time_slot++){
                
                $hour = ($time_slot - 1)*3;            
                $start_hour = $hour.':00:00';
                $end_hour = ($hour + 3).':00:00';
                if($hour < 10 ){
                    $start_hour = '0'.$start_hour;
                }
                if($hour + 2 < 10){
                    $end_hour = '0'.$end_hour;
                }
                echo '日期:'.$day.' 时间段:'.$start_hour.'---'.$end_hour."======".$user_id."<br />";
                $hourly_data = $hourly_model->aggregate(
                    array(
                        array(
                            '$match' => array('create_time' => array('$gte' => $day.' '.$start_hour, '$lt' => $day.' '.$end_hour), 'user_id' => $user_id)
                        ),
                        array(
                            '$group' => array('_id' => '$user_id', 'avg' => array('$avg' => '$value'))
                        ),
                        array(
                            '$project' => array('_id' => 0, 'user_id' => '$_id', 'avg' => 1)
                        )
                    )    
                );
                
                if($hourly_data){
                    $time_slot_arr[$time_slot] = $hourly_data[0]['avg'];
                }
            }
 
            $is_creat_data = false;
            $data_three_hourly = $this->data_three_hourly_model->get(array('user_id' => $user_id, 'date' => $day));
            $update_id = "";
            if(!$data_three_hourly){
                $is_creat_data = true;
            }
            else{
                $update_id = $data_three_hourly['_id'];
            }
 
            foreach ($time_slot_arr as $slot => $val) {
                $param = array();
                if($is_creat_data){                    
                    $param['user_id'] = $user_id;
                    $param['date'] = $day;
                    for($i = 1; $i <= 8; $i++){
                        if($i == $slot){
                            $param['time_slot_'.$i] = floatval(substr($val, 0, 5));
                        }
                        else{
                            $param['time_slot_'.$i] = 0;
                        }
                    }
                    $param['create_time'] = date('Y-m-d H:i:s');
                    $this->data_three_hourly_model->add($param);
                    $update_id = $param['_id'];
                    $is_creat_data = false;
                }
                else{
                    $param['time_slot_'.$slot] = floatval(substr($val, 0, 5));
                    $this->data_three_hourly_model->update($param, array('_id' => $update_id));
                }
            }
            if($day != date('Y-m-d')){
                $this->task_model->delete(array('_id' => $task['_id']));
            }
 
        }
 
    }
 
 
}