isAjax()) { //分页参数 $length = input('rows', 10, 'intval'); //每页条数 $page = input('page', 1, 'intval'); //第几页 $start = ($page - 1) * $length; //分页开始位置 $map1 = []; $map = []; $b = input('begin','','trim'); $e = input('end','','trim'); $title = input('title','','trim');//护工 if($title) { $map1[] = ['title', 'like', '%' . $title . '%']; } if($b && $e){ if($b <= $e){ $b = date('Ymd',strtotime($b)); $e = date('Ymd',strtotime($e)); $map[] = ['create_yyyymmdd','>=',$b]; $map[] = ['create_yyyymmdd','<=',$e]; } } if($b && $e==''){ $b = date('Ymd',strtotime($b)); $map[] = ['create_yyyymmdd','>=',$b]; } if($b=='' && $e){ $e = date('Ymd',strtotime($e)); $map[] = ['create_yyyymmdd','<=',$e]; } $cateId = input('cate_id','','trim'); if($cateId){ $map1[] = ['cate_id','=',$cateId]; } $map1[] = ['org_id','=',$this->orgId]; $map[] = ['org_id','=',$this->orgId]; $ret = $this->get_document_data($map,$map1,$start,$length); $totalCount = Db::name('article') ->where('del',0) ->where('enable',1) ->where($map1) ->count(); $totalPage = ceil($totalCount/$length); $result['page'] = $page; $result['total'] = $totalPage; $result['records'] = $totalCount; $result['rows'] = $ret; return json($result); }else{ $cate = (new \app\common\model\ArticleCate())->getList(); $this->assign('cate',$cate); return $this->fetch(); } } public function get_document_data($map,$map1,$start,$length){ $ret = Db::name('article') ->where('del',0) ->where('enable',1) ->where($map1) ->limit($start,$length) ->select(); $count = $this->get_workers_count(array(),$this->orgId); $read_user_num = 0; foreach ($ret as $k=>$v){ $ret[$k]['cate_name'] = db('article_cate') ->where('id',$v['cate_id']) ->value('title'); $query = Db::name('record') // ->where('org_id', $this->orgId) ->where($map) ->where('article_id',$v['id']); $db = clone $query; $read_total = $db ->field('SUM(view) as views') ->find(); $ret[$k]['read_total'] = $read_total['views']?$read_total['views']:0; $db1 = clone $query; $ret[$k]['read_user_num'] = $db1 ->group('user_id') // ->distinct(true) ->count(); $read_user_num += $ret[$k]['read_user_num']; $db2= clone $query; $time = $db2 ->field('SUM(time) as times') ->find(); $ret[$k]['read_time'] = $time['times']?$time['times']:0; if ($v['limit'] > 0) { $roles = empty($v['roles_ids'])?array():explode(',',$v['roles_ids']); $roles_ids = $this->get_worker_roles($roles,$this->orgId); if(!$roles_ids){ $ret[$k]['unread_users'] = 0; }else{ $db3 = clone $query; $db3->where('roles_id', 'in',$roles_ids); $finishuser = $db3 ->field('SUM(time) as times,user_id') ->group('user_id') ->having('times >= '.$v['limit']) ->select(); if($v['id'] == 3){ // $finishuser = $db3 // ->field('SUM(time) as times,user_id') // ->group('user_id') // ->having('times > '.$v['limit']) // ->fetchSql() // ->select(); // var_dump($finishuser); } if($roles){ $ucount = $this->get_workers_count($roles,$this->orgId); $ret[$k]['unread_users'] = $ucount - count($finishuser); }else{ $ret[$k]['unread_users'] = $count - count($finishuser); } } } else { $ret[$k]['unread_users'] = 0; } } return $ret; } public function get_worker_roles($roles,$org){ $roles_ids = array(); if (!$roles) { $roles_ids = Db::name('roles') ->where('enable',1) ->where('del',0) ->where('org_id',$org) ->where('parent_id','>',0) ->column('id'); }else{ $roles_ids = $roles; } return $roles_ids; } public function get_workers_count($roles,$org){ if (!$roles) { $roles_ids = Db::name('roles') ->where('enable',1) ->where('del',0) ->where('del',0) ->where('org_id',$org) ->where('parent_id','>',0) ->column('id'); }else{ $roles_ids = Db::name('roles') ->where('enable',1) ->where('del',0) ->where('org_id',$org) ->where('parent_id','in',$roles) ->column('id'); } if($roles_ids){ $query = Db::name('user') ->alias('a') ->where('b.roles_id','in',$roles_ids); }else{ $query = Db::name('user') ->alias('a') ->where('b.roles_id','=',-1); } $query->join('user_roles b','a.id = b.user_id'); $query->join('user_org c','a.id = c.user_id'); $query->where('c.org_id',$org); $query->where('a.type',0); $query->where('a.del',0); $count = $query->count('a.id'); return $count; } public function recordLog($id){ if(request()->isAjax()){ //分页参数 $length = input('rows',10,'intval'); //每页条数 $page = input('page',1,'intval'); //第几页 $start = ($page - 1) * $length; //分页开始位置 $res = $this->get_document_record($this->orgId,$id,$start,$length); $totalPage = $res['total']; $result['page'] = $page; $result['total'] = $totalPage; $result['records'] = $res['count']; $result['rows'] = $res['data']; return json($result); }else{ $article = Db::name('article') ->where('id',$id) ->find(); if($article['limit'] > 0){ $m_name = '文档名称:'.$article['title'].'| 最低时长限制:'.$article['limit'].'分钟'; }else{ $m_name = '文档名称:'.$article['title'].'| 最低时长限制:不限制'; } $this->assign('m_name',$m_name); $this->assign('id',$id); return $this->fetch(); } } public function get_document_record($org,$id,$start,$length){ $article = Db::name('article') ->where('id',$id) ->find(); $roles = $article['roles_ids']?explode(',',$article['roles_ids']):array(); $user_ids = $this->get_workers($roles,$org); //所有人的id if ($article['limit'] > 0) { $roles_ids = $this->get_worker_roles($roles,$org); $finishuser = Db::name('record') ->where('org_id', $org) ->where('article_id', $id) ->where('roles_id', 'in',$roles_ids) ->field('SUM(time) as times,user_id') ->group('user_id') // ->distinct(true) ->having('times >='.$article['limit']) ->select(); $finishuserids = array(); // 达标的人 if($finishuser){ foreach ($finishuser as $k=>$v){ $finishuserids[] = $v['user_id']; } } $user_ids = array_diff($user_ids, $finishuserids); } else { $user_ids = array(); } $ret = $this->get_user_lists($user_ids,$start,$length); return $ret; } public function get_workers($roles,$org){ $roles_ids = array(); if ($roles) { $roles_ids = Db::name('roles') ->where('enable',1) ->where('del',0) ->where('org_id',$org) ->where('parent_id','in',$roles) ->column('id'); }else{ // $roles_ids = Db::name('roles') // ->where('enable',1) // ->where('del',0) // ->where('org_id',$org) // ->where('parent_id','in',[3,4,5,6,7,8,9,10,11]) // ->column('id'); } $this->db = Db::name('user') ->alias('a'); if($roles){ if($roles_ids){ $this->db->where('b.roles_id','in',$roles_ids); }else{ $this->db->where('b.roles_id',-1); } } $this->db->where('c.org_id',$org); $this->db->where('a.del',0); $this->db->join('user_roles b','a.id = b.user_id'); $this->db->join('user_org c','a.id = c.user_id'); $userIds = $this->db->column('a.id'); return $userIds; } public function get_user_lists($user_ids,$start,$length, $type = 0) { $pageSize = 10; $total = 0; $list = array(); if ($user_ids) { $this->db = Db::name('user') ->alias('a'); $this->db->where('a.id','in', $user_ids); $this->db->where('a.type', $type); $this->db->where('a.del', 0); $this->db->join('user_roles b', 'b.user_id = a.id'); $db = clone $this->db; $this->db->field('count(*) as count'); $count = $this->db->find(); $this->db = $db; $this->db->limit($start, $length); $this->db->field('a.*,b.roles_id'); $list = $this->db->select(); $list = $list?$list:array(); foreach ($list as $k=>$v){ $list[$k]['roles_name'] = Db::name('roles') ->where('id',$v['roles_id'])->value('name'); } } $count = empty($count) ? 0 : $count['count']; return array('data' => $list, 'total' => $count, 'count' => $count); } }