Dashboard.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\User;
  5. use app\common\controller\Backend;
  6. use app\common\model\Attachment;
  7. use fast\Date;
  8. use think\Db;
  9. /**
  10. * 控制台
  11. *
  12. * @icon fa fa-dashboard
  13. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  14. */
  15. class Dashboard extends Backend
  16. {
  17. /**
  18. * 查看
  19. */
  20. public function index()
  21. {
  22. try {
  23. \think\Db::execute("SET @@sql_mode='';");
  24. } catch (\Exception $e) {
  25. }
  26. $column = [];
  27. $starttime = Date::unixtime('day', -6);
  28. $endtime = Date::unixtime('day', 0, 'end');
  29. $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
  30. ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
  31. ->group('join_date')
  32. ->select();
  33. for ($time = $starttime; $time <= $endtime;) {
  34. $column[] = date("Y-m-d", $time);
  35. $time += 86400;
  36. }
  37. $userlist = array_fill_keys($column, 0);
  38. foreach ($joinlist as $k => $v) {
  39. $userlist[$v['join_date']] = $v['nums'];
  40. }
  41. $dbTableList = Db::query("SHOW TABLE STATUS");
  42. $addonList = get_addon_list();
  43. $totalworkingaddon = 0;
  44. $totaladdon = count($addonList);
  45. foreach ($addonList as $index => $item) {
  46. if ($item['state']) {
  47. $totalworkingaddon += 1;
  48. }
  49. }
  50. $this->view->assign([
  51. 'totaluser' => User::count(),
  52. 'totaladdon' => $totaladdon,
  53. 'totaladmin' => Admin::count(),
  54. 'totalcategory' => \app\common\model\Category::count(),
  55. 'todayusersignup' => User::whereTime('jointime', 'today')->count(),
  56. 'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
  57. 'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
  58. 'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
  59. 'threednu' => User::whereTime('jointime', '-3 days')->count(),
  60. 'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
  61. 'dbtablenums' => count($dbTableList),
  62. 'dbsize' => array_sum(array_map(function ($item) {
  63. return $item['Data_length'] + $item['Index_length'];
  64. }, $dbTableList)),
  65. 'totalworkingaddon' => $totalworkingaddon,
  66. 'attachmentnums' => Attachment::count(),
  67. 'attachmentsize' => Attachment::sum('filesize'),
  68. 'picturenums' => Attachment::where('mimetype', 'like', 'image/%')->count(),
  69. 'picturesize' => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
  70. ]);
  71. $this->assignconfig('column', array_keys($userlist));
  72. $this->assignconfig('userdata', array_values($userlist));
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 报表统计
  77. */
  78. public function tongji(){
  79. $starttime = strtotime(date('Y-m-d',strtotime("-7 day"))); //默认7天前
  80. $endtime = strtotime(date('Y-m-d')) - 1;
  81. //接收
  82. $choicedatetime = input('choicedatetime','');
  83. if(!empty($choicedatetime)){
  84. $choicedatetime = explode(' - ',$choicedatetime);
  85. $starttime = strtotime($choicedatetime[0]);
  86. $endtime = strtotime($choicedatetime[1]) + 86399;
  87. }
  88. $this->view->assign('defaultdatetime',date('Y-m-d',$starttime).' - '.date('Y-m-d',$endtime));
  89. //dump($starttime);
  90. //dump($endtime);
  91. //所有应该参加考试的人
  92. $exam_paper = Db::name('exam_paper')->where('start_time','BETWEEN',[$starttime,$endtime])->where('deletetime',NULL)->column('id,user_ids');
  93. // dump($exam_paper);
  94. $new_exam_paper = [];
  95. foreach($exam_paper as $key => $val){
  96. $new_exam_paper = array_merge($new_exam_paper,explode(',',$val));
  97. }
  98. // dump($new_exam_paper);
  99. $new_exam_paper = array_count_values($new_exam_paper);
  100. // dump($new_exam_paper);
  101. //未通过考试的人
  102. $nopass = Db::name('exam_grade')->where('paper_id','IN',array_keys($exam_paper))->where('is_pass',0)->column('user_id');
  103. // dump($nopass);
  104. $nopass = array_count_values($nopass);
  105. // dump($nopass);
  106. //参加考试的人
  107. $exam_grade = Db::name('exam_grade')->where('paper_id','IN',array_keys($exam_paper))->column('user_id');
  108. // dump($exam_grade);
  109. $exam_grade = array_count_values($exam_grade);
  110. // dump($exam_grade);
  111. //应该参加培训的人
  112. $train_active = Db::name('train_active')->where('sign_endtime','BETWEEN',[$starttime,$endtime])->where('deletetime',NULL)->column('id,user_ids');
  113. // dump($train_active);
  114. $new_train_active = [];
  115. foreach($train_active as $key => $val){
  116. $new_train_active = array_merge($new_train_active,explode(',',$val));
  117. }
  118. // dump($new_train_active);
  119. $new_train_active = array_count_values($new_train_active);
  120. // dump($new_train_active);
  121. //参加签到的人
  122. $user_train = Db::name('user_train')->where('train_id','IN',array_keys($train_active))->column('user_id');
  123. // dump($user_train);
  124. $user_train = array_count_values($user_train);
  125. // dump($user_train);
  126. //所有用户
  127. $userlist = Db::name('user')->order('idcard_status desc')->select();
  128. foreach($userlist as $key => $user){
  129. $user['realname'] = '';
  130. if($user['idcard_status'] == 1){
  131. $user['realname'] = $user['nickname'];
  132. }
  133. //应参加考试次数
  134. $user['paper_count'] = isset($new_exam_paper[$user['id']]) ? $new_exam_paper[$user['id']] : 0;
  135. //不及格次数
  136. $user['no_pass'] = isset($nopass[$user['id']]) ? $nopass[$user['id']] : 0;
  137. //考试次数
  138. $user['grade_count'] = isset($exam_grade[$user['id']]) ? $exam_grade[$user['id']] : 0;
  139. //缺考次数
  140. $user['no_grade'] = $user['paper_count'] - $user['grade_count'];
  141. if($user['no_grade'] < 0){ $user['no_grade'] = 0;}
  142. //应该签到的次数
  143. $user['train_count'] = isset($new_train_active[$user['id']]) ? $new_train_active[$user['id']] : 0;
  144. //签到的次数
  145. $user['sign_count'] = isset($user_train[$user['id']]) ? $user_train[$user['id']] : 0;
  146. //未到的次数
  147. $user['no_sign'] = $user['train_count'] - $user['sign_count'];
  148. if($user['no_sign'] < 0){ $user['no_sign'] = 0;}
  149. //匿名用户
  150. if($user['idcard_status'] != 1){
  151. $user['no_pass'] = 0;
  152. $user['grade_count'] = 0;
  153. $user['no_grade'] = 0;
  154. $user['sign_count'] = 0;
  155. $user['no_sign'] = 0;
  156. }
  157. //
  158. $userlist[$key] = $user;
  159. }
  160. $this->assign('datalist',$userlist);
  161. $this->view->engine->layout(false);
  162. return $this->view->fetch();
  163. }
  164. }