2 files added
	
		
		3 files modified
	
	
 
	
	
	
	
	
	
	
	
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public function getUserDataList(){ | 
|---|
|  |  |  | $rs = array('code' => 0, 'msg' => '', 'info' => array()); | 
|---|
|  |  |  | $data_service = new Domain_Data(); | 
|---|
|  |  |  | $data_list = $data_service->getUserDataList($this->user_id, $this->start_time, $this->end_time); | 
|---|
|  |  |  | if($data_list){ | 
|---|
|  |  |  | $rs['msg'] = '数据获取成功'; | 
|---|
|  |  |  | $rs['info'] = $data_list; | 
|---|
|  |  |  | $data_daily_service = new Domain_DataDaily(); | 
|---|
|  |  |  | if($data_daily_service->checkTime($this->start_time, $this->end_time)){ | 
|---|
|  |  |  | $result = $data_daily_service->getUserDataList($this->user_id, $this->start_time, $this->end_time); | 
|---|
|  |  |  | if($data_list){ | 
|---|
|  |  |  | $rs['msg'] = '数据获取成功'; | 
|---|
|  |  |  | $rs['info'] = $data_list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else{ | 
|---|
|  |  |  | $rs['code'] = 1; | 
|---|
|  |  |  | $rs['msg'] = '数据获取失败'; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else{print_r($data_lsit); | 
|---|
|  |  |  | else{ | 
|---|
|  |  |  | $rs['code'] = 1; | 
|---|
|  |  |  | $rs['msg'] = '数据获取失败'; | 
|---|
|  |  |  | $rs['msg'] = '开始时间必须小于结束时间或两者时间间隔必须大于等于7天'; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return $rs; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //public function updateTime(){ | 
|---|
|  |  |  | //    $data_service = new Domain_Data(); | 
|---|
|  |  |  | //     $datas = $data_service->getList(); | 
|---|
|  |  |  | //     foreach ($datas as $value) { | 
|---|
|  |  |  | //        $data['_id'] = $value['_id']; | 
|---|
|  |  |  | //        $data['date'] = date('Y-m-d', strtotime($value['create_time'])); | 
|---|
|  |  |  | //        $data_service->updateTime($data); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //  } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | return $this->model->getList(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取用户某个时间段内的数据 | 
|---|
|  |  |  | * @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){ | 
|---|
|  |  |  | $result = $this->model->aggregate( | 
|---|
|  |  |  | array( | 
|---|
|  |  |  | array( | 
|---|
|  |  |  | '$match' => array('create_time' => array('$gte' => $start_time.' 00:00:00', '$lte' => $end_time.' 23:59:59'), 'user_id' => $user_id) | 
|---|
|  |  |  | ), | 
|---|
|  |  |  | array( | 
|---|
|  |  |  | '$group' => array('_id' => '$date', 'day' => array('$avg' => '$value')) | 
|---|
|  |  |  | ), | 
|---|
|  |  |  | array( | 
|---|
|  |  |  | '$project' => array('_id' => 0, 'date'=> '$_id', 'day' => 1) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | print_r($result); | 
|---|
|  |  |  | die(); | 
|---|
|  |  |  | return $this->model->getList(array('user_id' => $user_id, 'create_time' => array('$gte' => $start_time, '$lte' => $end_time))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public function updateTime($data){ | 
|---|
|  |  |  | $id = $data['_id']; | 
|---|
|  |  |  | unset($data['_id']); | 
|---|
|  |  |  | return $this->model->update($data, array('_id' => $id)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //public function updateTime($data){ | 
|---|
|  |  |  | //    $id = $data['_id']; | 
|---|
|  |  |  | //     unset($data['_id']); | 
|---|
|  |  |  | //     return $this->model->update($data, array('_id' => $id)); | 
|---|
|  |  |  | //} | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
| New file | 
|  |  |  | 
|---|
|  |  |  | <?php | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 用户业务类 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | class Domain_DataDaily { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public function __construct(){ | 
|---|
|  |  |  | //DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers')); | 
|---|
|  |  |  | $this->model = new Model_DataDaily(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取用户某个时间段内的数据 | 
|---|
|  |  |  | * @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); | 
|---|
|  |  |  | $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 < 7*$step ; $i+=$step){ | 
|---|
|  |  |  | $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))); | 
|---|
|  |  |  | return $result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 验证时间 | 
|---|
|  |  |  | * @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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | return $this->db->insert($this->table, $data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * [add description] | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public function save($data){ | 
|---|
|  |  |  | return $this->db->insert($this->table, $data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public function update($data, $query){ | 
|---|
|  |  |  | return $this->db->update($this->table, $data, $query); | 
|---|
|  |  |  | } | 
|---|
| New file | 
|  |  |  | 
|---|
|  |  |  | <?php | 
|---|
|  |  |  |  | 
|---|
|  |  |  | class Model_DataDaily extends Model_Base{ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public $table = 'data_daily'; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|