// +---------------------------------------------------------------------- namespace Admin\Model; use Think\Model; /** * 设备记录模型 * @author 鹿文学 */ class DeviceRecordModel extends Model{ /* 自动验证规则 */ protected $_validate = array( ); /* 自动完成规则 */ protected $_auto = array( ); /** * 构造函数 * @param string $name 模型名称 * @param string $tablePrefix 表前缀 * @param mixed $connection 数据库连接信息 */ public function __construct($name = '', $tablePrefix = '', $connection = '') { /* 设置默认的表前缀 */ $this->tablePrefix ='tab_'; /* 执行构造方法 */ parent::__construct($name, $tablePrefix, $connection); } /** * 累计设备 * @author 鹿文学 */ public function all_device() { $list = $this->field('min(create_time) as create_time,unique_code')->group('unique_code')->select(); return count($list); } /** * 统计新增设备 * @author 鹿文学 */ public function total_device($map=array(),$flag=0) { if($flag) { $list = $this->active($map); } else { $list = $this->news($map); } return count($list); } /** * 新增设备 * @author 鹿文学 */ public function news($map=array()) { $sql = $this->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false); return $this->table('(' . $sql . ') as a')->where($map)->order('create_time')->select(); } /* * 新增设备 * @param array $map 条件数组 * @param string $field 字段别名 * @param string $group 分组字段名 * @author 鹿文学 */ public function news_on_time($map=array(),$field='news',$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 = $this->field('min(create_time) as create_time,version,unique_code,id')->group('unique_code')->select(false); $news = $this->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(); return $news; } /** * 活跃设备 * @author 鹿文学 */ public function active($map=array()) { $sql = $this->field('create_time,model,unique_code')->where($map)->select(false); return $this->table('(' . $sql . ') as a')->group('unique_code')->order('create_time')->select(); } /* * 活跃设备 * @param array $map 条件数组 * @param string $field 字段别名 * @param string $group 分组字段名 * @author 鹿文学 */ 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 = $this->field('create_time,version,unique_code,id')->where($map)->select(false); $mid = $this->table('(' . $sql . ') as m')->group('unique_code')->select(false); $active = $this->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(); return $active; } /** * 启动机型 * @author 鹿文学 */ public function model($map=array()) { $sql = $this->field('create_time,model,version,unique_code')->where($map)->select(false); $mid = $this->table('(' .$sql. ') as m')->group('unique_code')->select(false); return $this->table('(' .$mid. ') as a')->field('a.model,version,count(a.unique_code) as count')->group('model,version')->order('count desc')->select(); } /** * 单设备累计时长 * @author 鹿文学 */ public function single_duration($map=array()) { $duration = $this->duration($map); $total = $this->total_device($map); return $duration/$total; } /** * 累计时长 * @author 鹿文学 */ public function duration($map=array()) { return $this->where($map)->sum('duration'); } /** * 创建时间不写则取当前时间 * @return int 时间戳 * @author huajie */ protected function getCreateTime(){ $create_time = I('post.create_time'); return $create_time?strtotime($create_time):NOW_TIME; } /** * 新增或更新一个游戏 * @param array $data 手动传入的数据 * @return boolean fasle 失败 , int 成功 返回完整的数据 * @author 王贺 */ public function update($data = null){ /* 获取数据对象 */ $data = $this->token(false)->create($data); if(empty($data)){ return false; } /* 添加或新增基础内容 */ if(empty($data['id'])){ //新增数据 $id = $this->add($data); //添加基础内容 if(!$id){ $this->error = '新增基础内容出错!'; return false; } } else { //更新数据 $status = $this->save(); //更新基础内容 if(false === $status){ $this->error = '更新基础内容出错!'; return false; } } return $data; } }