colly_wyx
2018-04-28 4b9bdbdab8d33a9197e12cac65477018e8bdc20e
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
<?php
/**
 * 用户业务类
 */
class Domain_DataDaily {
 
    public function __construct(){
        //DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers'));
       $this->model = new Model_DataDaily();
       $this->data_three_hourly_model = new Model_DataThreeHourly();
    }
 
    /**
     * 获取用户某个时间段内的数据
     * @param  [type] $user_id    [description]
     * @param  [type] $start_time [description]
     * @param  [type] $end_time   [description]
     * @return [type]             [description]
     */
    public function getUserDataList($user_id, $start_time, $end_time){
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        if($end_time > $start_time){
            $diff_days = ($end_time - $start_time) / 86400;
            $total_days = $diff_days + 1;//时间段内总天数
            //$step = ($total_days - $total_days % 7) / 7;
            $days_arr=  array();
            for($i = 0; $i < $total_days ; $i+=1){
                $days_arr[] = date('Y-m-d', strtotime('+'.$i.' day', $start_time));
            }
            $result = $this->model->getList(array('user_id' => $user_id, 'date' => array('$in' => $days_arr)));
        }
        else{
            $result = $this->data_three_hourly_model->get(array('user_id' => $user_id, 'date' => date('Y-m-d', $start_time)));
            $_result = array();
            if($result){
                for($i = 1; $i <=8 ; $i++){
                    array_push($_result, array('hour_avg' => $result['time_slot_'.$i], 'time_level' => $i));
                }         
            }
            else{
                $_result = null;
            }
            return $_result;
        }
        
        return $result;
    }
 
    /**
     * 获取用户某个时间段内的平均值
     * @param  [type] $user_id    [description]
     * @param  [type] $start_time [description]
     * @param  [type] $end_time   [description]
     * @return [type]             [description]
     */
    public function getUserDataAvg($user_id, $start_time, $end_time){
       
         if($start_time == $end_time){
             $result = $this->data_three_hourly_model->get(array('date' => $start_time));
             if($result){
                $sum = 0;
                for($i = 1; $i <= 8; $i++){
                    $sum += $result['time_slot_'.$i];
                }
                
                $result[0]['day_avg'] = $sum/8;
             }
             else{
                $result[0]['day_avg'] = 0;
             }
             
         }
         else{
            $result = $this->model->aggregate(
                array(
                    array(
                         '$match' => array('date' => array('$gte' => $start_time, '$lte' => $end_time), 'user_id' => $user_id)
                    ),
                    array(
                        '$group' => array('_id' => '$user_id', 'day_avg' => array('$avg' => '$day_avg'))
                    ),
                    array(
                        '$project' => array('_id' => 0, 'day_avg' => 1)
                    )
                )   
            );
        }
 
        return isset($result[0]['day_avg'])?$result[0]['day_avg']:0;
    }
 
    /**
     * 验证时间
     * @param  [type] $start_time [description]
     * @param  [type] $end_time   [description]
     * @return [type]             [description]
     */
    public function checkTime($start_time, $end_time){
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        if($start_time > $end_time){
            return false;
        }
        $diff_days = ($end_time - $start_time) / 86400;
        $total_days = $diff_days + 1;//时间段内总天数
        if($total_days < 7){
            return false;
        }
        return true;
    }
 
    
 
}