<?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']));
|
}
|
|
}
|
|
}
|
|
|
}
|