HomeController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Article;
  4. use App\Articletype;
  5. use App\Exam;
  6. use App\Examarg;
  7. use App\Http\Controllers\Controller;
  8. use App\Question;
  9. use App\School;
  10. use App\Sclass;
  11. use App\User;
  12. use Encore\Admin\Widgets\InfoBox;
  13. use DB;
  14. use Illuminate\Http\Request;
  15. use function EasyWeChat\Kernel\data_to_array;
  16. use Encore\Admin\Controllers\Dashboard;
  17. use Encore\Admin\Facades\Admin;
  18. use Encore\Admin\Layout\Column;
  19. use Encore\Admin\Layout\Content;
  20. use Encore\Admin\Layout\Row;
  21. use Illuminate\Support\Facades\Storage;
  22. use Maatwebsite\Excel\Facades\Excel;
  23. class HomeController extends Controller
  24. {
  25. public function index(Request $request)
  26. {
  27. return Admin::content(function (Content $content) use ($request) {
  28. $content->header('统计');
  29. $content->description('微考试信息');
  30. $start_time = $request->get('start_time');
  31. $stop_time = $request->get('stop_time');
  32. $content->row(function ($row) use ($request) {
  33. $school_num = School::count();
  34. $sclass_num = Sclass::count();
  35. $student_num = User::count();
  36. $question_num = Question::count();
  37. $art_count = Article::count();
  38. $exam_count = Examarg::count();
  39. //$row->column(2, new InfoBox('总项目数', 'users', 'aqua', '/demo/users', Project::filter($request)->count('*')));
  40. $row->column(2, new InfoBox('学校数量', 'bank', 'green', '/admin/school', $school_num));
  41. $row->column(2, new InfoBox('班级数量', 'asl-interpreting', 'red', '/admin/sclass', $sclass_num));
  42. $row->column(2, new InfoBox('学生数量', 'graduation-cap', 'yellow', '/admin/users', $student_num));
  43. $row->column(2, new InfoBox('试卷数', 'twitch', 'purple', '/admin/examarg', $exam_count));
  44. $row->column(2, new InfoBox('题目数量', 'file-text', 'purple', '/admin/question', $question_num));
  45. $row->column(2, new InfoBox('文章总数', 'bars', 'purple', '/admin/article', $art_count));
  46. });
  47. $bills = (User::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month')]));
  48. $content->row(function ($row) use ($bills) {
  49. $studentCount = collect($bills)->groupBy('ym')->map(function ($v, $k) {
  50. // Salary::where('time', 'like', "{$k}%")->where('ac', 'F')->sum('money')
  51. return (collect($v)->count() + 0);
  52. })->toArray();
  53. $keys = array_keys(array_flip(array_keys($studentCount)));
  54. $row->column(12, view('admin.charts.line', [
  55. 'title' => '每月学生增加数',
  56. 'come' => ($this->getValue($studentCount, $keys)),
  57. 'keys' => $keys
  58. ]));
  59. });
  60. $school = (School::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month'), DB::raw('schools.*')]));
  61. $sclass = (Sclass::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month')]));
  62. $article = (Article::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month'), DB::raw('articles.*')]));
  63. $content->row(function ($row) use ($school, $sclass, $article) {
  64. $scloo_num = collect($school)->groupBy('ym')->map(function ($v, $k) {
  65. return (collect($v)->count() + 0);
  66. })->toArray();
  67. $sclass_num = collect($sclass)->groupBy('ym')->map(function ($v, $k) {
  68. // Salary::where('time', 'like', "{$k}%")->where('ac', 'F')->sum('money')
  69. return (collect($v)->count() + 0);
  70. })->toArray();
  71. $article_num = collect($article)->groupBy('ym')->map(function ($v, $k) {
  72. // Salary::where('time', 'like', "{$k}%")->where('ac', 'F')->sum('money')
  73. return (collect($v)->count() + 0);
  74. })->toArray();
  75. $keys = array_keys(array_flip(array_keys($article_num)));
  76. $row->column(12, view('admin.charts.sline', [
  77. 'title' => '每月学校班级等增加数',
  78. 'school' => ($this->getValue($scloo_num, $keys)),
  79. "sclass" => ($this->getValue($sclass_num, $keys)),
  80. "article" => ($this->getValue($article_num, $keys)),
  81. 'keys' => $keys,
  82. ]));
  83. });
  84. });
  85. }
  86. public function getValue($arr, $keys)
  87. {
  88. return collect($keys)->map(function ($v) use ($arr) {
  89. return isset($arr[$v]) ? $arr[$v] : 0;
  90. })->values();
  91. }
  92. public function getSchoolApi(Request $request)
  93. {
  94. $q = $request->get('q');
  95. return School::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
  96. }
  97. public function getClassApi(Request $request)
  98. {
  99. $q = $request->get('q');
  100. return Sclass::where('school_id', $q)->select('id', 'name as text')->get();
  101. }
  102. public function getArticleTypeApi(Request $request)
  103. {
  104. return Articletype::select('id', 'name as text')->get();
  105. }
  106. public function getExams()
  107. {
  108. return Exam::select('id', 'name as text')->get();
  109. }
  110. public function getSchoolSclass()
  111. {
  112. $allsclass = Sclass::get();
  113. $selectdata = [];
  114. foreach ($allsclass as $key => $value) {
  115. $selectdata[] = array(
  116. 'id' => $value['id'],
  117. "text" => $value['school']['name'] . '-' . $value['name']
  118. );
  119. }
  120. return $selectdata;
  121. }
  122. protected function upload(Request $request)
  123. {
  124. //图片存储方法。如果为图片存起来。如果不是返回''
  125. $fileCharater = $request->file('file');
  126. if (!$fileCharater) {
  127. $msg = '请选择上传文件';
  128. return view('admin.tools.excelskip', compact('msg'));
  129. }
  130. if ($fileCharater->isValid()) { //括号里面的是必须加的哦
  131. //如果括号里面的不加上的话,下面的方法也无法调用的
  132. $originalName = $fileCharater->getClientOriginalName(); // 文件原名
  133. $ext = $fileCharater->getClientOriginalExtension(); // 扩展名
  134. $realPath = $fileCharater->getRealPath(); //临时文件的绝对路径
  135. $type = $fileCharater->getClientMimeType(); // image/jpeg
  136. if (strpos($type, 'vnd.openxmlformats-officedocument.spreadsheetml.sheet') === false) {
  137. return '非法格式';
  138. }
  139. //// // 上传文件
  140. $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . 'xlsx';
  141. // 使用我们新建的uploads本地存储空间(目录)
  142. //这里的uploads是配置文件的名称
  143. $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
  144. if ($bool) {
  145. $filePath = 'storage/app/public/uploads/' . $filename;
  146. $data = [];
  147. Excel::load($filePath, function ($reader) use (&$data) {
  148. $reader = $reader->getSheet(0);
  149. $data = $reader->toArray();
  150. });
  151. array_splice($data, 0, 2);
  152. $questions = [];
  153. foreach ($data as $key => $row) {
  154. $question['name'] = $row[1];
  155. $question['category'] = array_search($row[2], Question::$categoryMap);
  156. $question['type'] = $row[3];
  157. $question['answers'] = [];
  158. for ($i = 0; $i < strlen($row[4]); $i++) {
  159. $question['answers'][] = strtoupper(substr($row[4], $i, 1));
  160. }
  161. $question['options'] = [];
  162. if ($row[5] !== null) $question['options'][] = ['name' => 'A', 'val' => $row[5]];
  163. if ($row[6] !== null) $question['options'][] = ['name' => 'B', 'val' => $row[6]];
  164. if ($row[7] !== null) $question['options'][] = ['name' => 'C', 'val' => $row[7]];
  165. if ($row[8] !== null) $question['options'][] = ['name' => 'D', 'val' => $row[8]];
  166. if ($row[9] !== null) $question['options'][] = ['name' => 'E', 'val' => $row[9]];
  167. if ($row[10] !== null) $question['options'][] = ['name' => 'F', 'val' => $row[10]];
  168. if ($row[11] !== null) $question['options'][] = ['name' => 'G', 'val' => $row[11]];
  169. if ($question['name'] && $question['type'] && count($question['answers']) && count($question['options'])) {
  170. $model = new Question($question);
  171. $model->save();
  172. }
  173. }
  174. $msg = "上传成功";
  175. return view('admin.tools.excelskip', compact('msg'));
  176. }
  177. return "文件错误只能使用模板上传";
  178. }
  179. }
  180. // protected function upload(Request $request)
  181. // {
  182. // //图片存储方法。如果为图片存起来。如果不是返回''
  183. // $fileCharater = $request->file('file');
  184. //
  185. // if ($fileCharater->isValid()) { //括号里面的是必须加的哦
  186. // //如果括号里面的不加上的话,下面的方法也无法调用的
  187. //
  188. // $originalName = $fileCharater->getClientOriginalName(); // 文件原名
  189. // $ext = $fileCharater->getClientOriginalExtension(); // 扩展名
  190. // $realPath = $fileCharater->getRealPath(); //临时文件的绝对路径
  191. // $type = $fileCharater->getClientMimeType(); // image/jpeg
  192. //
  193. //// if (strpos($type, 'octet-stream') === false) {
  194. //// return '非法格式';
  195. //// }
  196. //
  197. // $filePath = 'storage/app/public/uploads/2019-02-18-09-57-23-5c6a8183ebc40.xlsx';
  198. //// $filePath = 'storage/exports/'.iconv('UTF-8', 'GBK', '学生成绩').'.xls';
  199. //
  200. // Excel::load($filePath, function($reader) {
  201. // $data = $reader->get();
  202. // return $data->toArray();
  203. //// foreach($qdatas as $qdata => $value){
  204. ////
  205. //// }
  206. //
  207. // });
  208. //
  209. //// // 上传文件
  210. //// $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . 'xls';
  211. //// // 使用我们新建的uploads本地存储空间(目录)
  212. //// //这里的uploads是配置文件的名称
  213. //// $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
  214. //// if ($bool) {
  215. //// return '/storage/uploads/' . $filename;
  216. //// }
  217. // }
  218. // return 'error';
  219. // }
  220. }