// +---------------------------------------------------------------------- namespace Admin\Controller; use User\Api\UserApi as UserApi; /** * 后台首页控制器 * @author 麦当苗儿 */ class TimingController extends AdminController { public function _initialize(){ } public function caculateDeviceInfo() { $start = strtotime(date("Ymd",time()))-86400; $end = $start+1; // if (I('time')) { // $start = strtotime(I('time')); // $end = $start+1; // } if (I('start')&&I('end')) { $start = strtotime(I('start')); $end = strtotime(I('end'))+1; } else if (I('start')&&!I('end')) { $start = strtotime(I('start')); $end = strtotime(I('start'))+1; } else if (!I('start')&&I('end')) { $start = strtotime(I('end')); $end = strtotime(I('end'))+1; } if ($start>$end) { echo '开始时间不能大于结束时间'; } ini_set('memory_limit','1024M'); // $device = D('DeviceRecord'); // echo 1; for($startTime = $start;$startTime<$end;$startTime=$startTime+86400) { echo date("Y-m-d",$startTime).' 安卓 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>1])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); echo $data['new_device'].' '; //当日活跃设备 $data['active_device'] = $this->active(['create_time'=>array('between',[$startTime,$startTime+86399]),'version'=>1]); echo $data['active_device'].' '; $data['time'] = $startTime; $data['version'] = 1; $data['today_new_device'] =json_encode($this->news_on_time(['create_time'=>['between',[$startTime,$startTime+86399]]],'news',5,'time,version','time',1)); // echo M('device_record','tab_')->_sql()."\n"; $data['today_active_device'] =json_encode($this->active_on_time(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1],'active',5,'time,version')); // echo M('device_record','tab_')->_sql()."\n"; $data['today_model_device'] = json_encode($this->model(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1])); $data['model'] = json_encode($this->modelDevice(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1])); $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])]); $newSqls = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false); $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSqls . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->save($data); } echo date("Y-m-d",$startTime).' 苹果 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>2])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); echo $data['new_device'].' '; //当日活跃设备 $data['active_device'] = $this->active(['create_time'=>array('between',[$startTime,$startTime+86399]),'version'=>2]); echo $data['active_device'].' '; $data['time'] = $startTime; $data['version'] = 2; $data['today_new_device'] =json_encode($this->news_on_time(['create_time'=>['between',[$startTime,$startTime+86399]]],'news',5,'time,version','time',2)); // echo M('device_record','tab_')->_sql();die(); $data['today_active_device'] =json_encode($this->active_on_time(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2],'active',5,'time,version')); $data['today_model_device'] = json_encode($this->model(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2])); $data['model'] = json_encode($this->modelDevice(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2])); // $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1]),'version'=>2]); // $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7; $data['duration7'] = 0; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->save($data); } } } public function caculateTodayDevice() { $start = strtotime(date("Ymd",time())); $end = $start+1; if (I('start')&&I('end')) { $start = strtotime(I('start')); $end = strtotime(I('end'))+1; } else if (I('start')&&!I('end')) { $start = strtotime(I('start')); $end = strtotime(I('start'))+1; } else if (!I('start')&&I('end')) { $start = strtotime(I('end')); $end = strtotime(I('end'))+1; } if ($start>$end) { echo '开始时间不能大于结束时间'; } ini_set('memory_limit','1024M'); // $device = D('DeviceRecord'); // echo 1; for($startTime = $start;$startTime<$end;$startTime=$startTime+86400) { echo date("Y-m-d",$startTime).' 安卓 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>1])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); $data['time'] = $startTime; echo $data['new_device'].' '; $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])]); $newSqls = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false); $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSqls . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7; $data['version'] = 1; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->save($data); } $data['duration7'] = 0; echo date("Y-m-d",$startTime).' 苹果 '; //当日新增设备 $newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>2])->group('unique_code')->select(false); $data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count(); echo $data['new_device'].' '; $data['version'] = 2; $getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->find(); echo "\n"; if (!$getStatistics) { M('device_statistics','tab_')->add($data); } else { M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->save($data); } } } public function duration($map=array()) { return M('device_record','tab_')->where($map)->sum('duration'); } public function active($map=array()) { $sql = M('device_record','tab_')->field('create_time,model,unique_code')->where($map)->select(false); $sql = M('device_record','tab_')->table('(' . $sql . ') as a')->group('unique_code')->order('create_time')->select(false); $sql = "select count(*) as count from ({$sql})t"; $count = M('device_record','tab_')->query($sql); return $count[0]['count']; } public function news_on_time($map=array(),$field='news',$flag=1,$group='time',$order='time',$version = 1) { switch($flag) { case 2:{$dateform = '%Y-%m';};break; case 3:{$dateform = '%Y-%u';};break; case 4:{$dateform = '%Y';};break; case 5:{$dateform = '%Y-%m-%d %H';};break; default:$dateform = '%Y-%m-%d'; } $sql = M('device_record','tab_')->field('min(create_time) as create_time,version,unique_code,id')->where(['version'=>$version])->group('unique_code')->select(false); $news = M('device_record','tab_')->table('(' . $sql . ') as a') ->field('FROM_UNIXTIME(a.create_time, "'.$dateform.'") as '.$group.',group_concat(a.id) as id,group_concat(a.unique_code) as unique_code ,COUNT(a.unique_code) AS '.$field) ->where($map)->group($group)->order($order)->select(); // echo M('device_record','tab_')->_sql();die(); return $news; } public function active_on_time($map=array(),$field='active',$flag=1,$group='time',$order='time') { switch($flag) { case 2:{$dateform = '%Y-%m';};break; case 3:{$dateform = '%Y-%u';};break; case 4:{$dateform = '%Y';};break; case 5:{$dateform = '%Y-%m-%d %H';};break; default:$dateform = '%Y-%m-%d'; } $sql = M('device_record','tab_')->field('create_time,version,unique_code,id')->where($map)->select(false); $mid = M('device_record','tab_')->table('(' . $sql . ') as m')->group('unique_code')->select(false); $active = M('device_record','tab_')->table('(' . $mid . ') as a') ->field('FROM_UNIXTIME(a.create_time, "'.$dateform.'") as '.$group.',group_concat(a.id) as id,group_concat(a.unique_code) as unique_code ,COUNT(a.unique_code) AS '.$field) ->group($group)->order($order)->select(); // echo $this->_sql();die(); return $active; } public function model($map=array()) { $sql = M('device_record','tab_')->field('create_time,model,version,unique_code')->where($map)->select(false); $mid = M('device_record','tab_')->table('(' .$sql. ') as m')->group('unique_code')->select(false); return M('device_record','tab_')->table('(' .$mid. ') as a')->field('a.model,version,count(a.unique_code) as count')->group('model,version')->order('count desc')->select(); } public function modelDevice($map=array()) { $sql = M('device_record','tab_')->field('model,version,unique_code')->where($map)->select(false); $mid = M('device_record','tab_')->table('(' .$sql. ') as m')->group('unique_code')->select(); return $mid; } }