0
0

ArticleWorker.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class ArticleWorker extends Auth
  6. {
  7. public function __construct(App $app = null) {
  8. parent::__construct($app);
  9. }
  10. public function index(){
  11. if(request()->isAjax()){
  12. //分页参数
  13. $length = input('rows',10,'intval'); //每页条数
  14. $page = input('page',1,'intval'); //第几页
  15. $start = ($page - 1) * $length; //分页开始位置
  16. $map1 = [];
  17. $map = [];
  18. $b = input('begin','','trim');
  19. $e = input('end','','trim');
  20. $title = input('title','','trim');//护工
  21. if($title) {
  22. $map1[] = ['title', 'like', '%' . $title . '%'];
  23. }
  24. if($b && $e){
  25. if($b <= $e){
  26. $b = date('Ymd',strtotime($b));
  27. $e = date('Ymd',strtotime($e));
  28. $map[] = ['create_yyyymmdd','>=',$b];
  29. $map[] = ['create_yyyymmdd','<=',$e];
  30. }
  31. }
  32. if($b && $e==''){
  33. $b = date('Ymd',strtotime($b));
  34. $map[] = ['create_yyyymmdd','>=',$b];
  35. }
  36. if($b=='' && $e){
  37. $e = date('Ymd',strtotime($e));
  38. $map[] = ['create_yyyymmdd','<=',$e];
  39. }
  40. $cateId = input('roles_id','','trim');
  41. if($cateId){
  42. $map1['roles_id'] = $cateId;
  43. }
  44. //数据返回
  45. $res = $this->show_all_page_search_worker($this->orgId,$map,$map1,$start,$length);
  46. $totalPage = $res['total'];
  47. $result['page'] = $page;
  48. $result['total'] = $totalPage;
  49. $result['records'] = $res['count'];
  50. $result['rows'] = $res['data'];
  51. return json($result);
  52. }else{
  53. $this->assign('m_name','员工学习数据');
  54. $cate = Db::name('roles')
  55. ->where('enable',1)
  56. ->where('del',0)
  57. ->where('del',0)
  58. ->where('org_id',$this->orgId)
  59. ->where('parent_id','>',0)
  60. ->select();
  61. $this->assign('cate',$cate);
  62. return $this->fetch();
  63. }
  64. }
  65. // 获取员工学习数据
  66. public function show_all_page_search_worker($org, $map,$map1,$start,$length)
  67. {
  68. $pageSize = $length;
  69. $total = 0;
  70. $roles_ids = array();
  71. if(isset($map1['roles_id'])){
  72. $roles_ids[] = $map1['roles_id'];
  73. }else{
  74. $roles_ids = $this->get_worker_roles([],$org);
  75. }
  76. if($roles_ids){
  77. $query = Db::name('user')
  78. ->alias('a')
  79. ->where('b.roles_id','in',$roles_ids);
  80. }else{
  81. $query = Db::name('user')
  82. ->alias('a')
  83. ->where('b.roles_id','=',-1);
  84. }
  85. $query->where('c.org_id',$org);
  86. $query->where('a.del',0);
  87. $query->join('user_roles b','user.id = b.user_id');
  88. $query->join('user_org c','user.id = c.user_id');
  89. $db = clone $query;
  90. $count = $query->count();
  91. $db1 = $db;
  92. $total = ceil($count / $pageSize);
  93. //查看当前的
  94. $pageNumber = empty($pageNumber) ? 0 : $pageNumber;
  95. //防止访问超过实际页数
  96. $pageNumber = max(0, min($pageNumber, $total - 1));
  97. $offset = $pageNumber * $pageSize;
  98. $db1->limit($start, $length);
  99. $db1->field('a.id as user_id,a.real_name,b.roles_id');
  100. $list = $db1->select();
  101. foreach ($list as $k=>$v){
  102. $list[$k]['roles_name'] = Db::name('roles')
  103. ->where('id',$v['roles_id'])
  104. ->value('name');
  105. $res = Db::name('record')
  106. ->where($map)
  107. ->where('user_id',$v['user_id'])
  108. ->field('SUM(view) as views,SUM(time) as times')
  109. ->group('user_id')
  110. ->find();
  111. $list[$k]['views'] = 0;
  112. $list[$k]['times'] = 0;
  113. if($res){
  114. $list[$k]['views'] = $res['views'];
  115. $list[$k]['times'] = $res['times'];
  116. }
  117. }
  118. $ret = array('data' => $list, 'total' => $total, 'count' => $count);
  119. return $ret;
  120. }
  121. public function get_worker_roles($roles,$org){
  122. $roles_ids = array();
  123. if (!$roles) {
  124. $roles_ids = Db::name('roles')
  125. ->where('enable',1)
  126. ->where('del',0)
  127. ->where('del',0)
  128. ->where('org_id',$org)
  129. ->where('parent_id','>',0)
  130. ->column('id');
  131. }else{
  132. $roles_ids = $roles;
  133. }
  134. return $roles_ids;
  135. }
  136. public function workLog($id){
  137. if(request()->isAjax()){
  138. //分页参数
  139. $length = input('rows',10,'intval'); //每页条数
  140. $page = input('page',1,'intval'); //第几页
  141. $start = ($page - 1) * $length; //分页开始位置
  142. $res = $this->show_all_page_search_worker_record($this->orgId,$id,$start,$length);
  143. $totalPage = $res['total'];
  144. $result['page'] = $page;
  145. $result['total'] = $totalPage;
  146. $result['records'] = $res['count'];
  147. $result['rows'] = $res['data'];
  148. return json($result);
  149. }else{
  150. $user = Db::name('user')
  151. ->where('id',$id)
  152. ->value('real_name');
  153. $this->assign('m_name','['.$user.']学习记录');
  154. $this->assign('id',$id);
  155. return $this->fetch();
  156. }
  157. }
  158. // 员工学习数据 -- 查看
  159. public function show_all_page_search_worker_record($org,$uid,$start,$length){
  160. $pageSize = $length;
  161. $total = 0;
  162. $query = Db::name('record')
  163. ->alias('a')
  164. ->join('article b','b.id = a.article_id')
  165. ->where('a.user_id',$uid)
  166. ->where('a.org_id',$org)
  167. ->group('a.article_id');
  168. $db = clone $query;
  169. $count = $query->count();
  170. $this->db = $db;
  171. $total = ceil($count / $pageSize);
  172. //查看当前的
  173. $pageNumber = empty($pageNumber) ? 0 : $pageNumber;
  174. //防止访问超过实际页数
  175. $pageNumber = max(0, min($pageNumber, $total - 1));
  176. $offset = $pageNumber * $pageSize;
  177. $this->db->field('b.title,b.limit,a.user_id,a.article_id,a.id');
  178. $this->db->limit($start, $length);
  179. $this->db->order('a.id', 'DESC');
  180. $list = $this->db->select();
  181. foreach ($list as $k=>$v){
  182. $res = Db::name('record')
  183. ->where('user_id',$v['user_id'])
  184. ->where('article_id',$v['article_id'])
  185. ->field('SUM(time) as times')->find();
  186. $list[$k]['times'] = 0;
  187. if($res){
  188. $list[$k]['times'] = $res['times'];
  189. }
  190. if($list[$k]['times'] >= $list[$k]['limit']){
  191. $list[$k]['status'] = '达标';
  192. }else{
  193. $list[$k]['status'] = '未达标';
  194. }
  195. }
  196. $ret = array('data' => $list, 'total' => $total, 'count' => $count);
  197. return $ret;
  198. }
  199. }