CountController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * 人数统计
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-06-06 01:48:31
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Student;
  10. use App\Http\Controllers\Admin\Controller;
  11. use Illuminate\Http\Request;
  12. use App\Repositories\Base\Criteria\OrderBy;
  13. use App\Repositories\Student\Criteria\MultiWhere;
  14. use App\Repositories\Student\CountRepository;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. class CountController extends Controller
  17. {
  18. private $repository;
  19. public function __construct(CountRepository $repository) {
  20. if(!$this->repository) $this->repository = $repository;
  21. }
  22. function index(Request $request) {
  23. $search['keyword'] = $request->input('keyword');
  24. $query = $this->repository->pushCriteria(new MultiWhere($search));
  25. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  26. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  27. }else{
  28. $query = $query->pushCriteria(new OrderBy('total','asc'));
  29. }
  30. $list = $query->paginate(10);
  31. return view('admin.student.count.index',compact('list'));
  32. }
  33. function check(Request $request) {
  34. $request = $request->all();
  35. $search['keyword'] = $request->input('keyword');
  36. $orderby = array();
  37. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  38. $orderby[$request['sort_field']] = $request['sort_field_by'];
  39. }
  40. $list = $this->repository->search($search,$orderby);
  41. return view('admin.student.count.check',compact('list'));
  42. }
  43. /**
  44. * 添加
  45. *
  46. */
  47. public function create(Request $request)
  48. {
  49. if($request->method() == 'POST') {
  50. return $this->_createSave();
  51. }
  52. return view('admin.student.count.edit');
  53. }
  54. /**
  55. * 保存修改
  56. */
  57. private function _createSave(){
  58. $data = (array) request('data');
  59. $id = $this->repository->create($data);
  60. if($id) {
  61. $url[] = array('url'=>U( 'Student/Count/index'),'title'=>'返回列表');
  62. $url[] = array('url'=>U( 'Student/Count/create'),'title'=>'继续添加');
  63. $this->showMessage('添加成功',$url);
  64. }else{
  65. $url[] = array('url'=>U( 'Student/Count/index'),'title'=>'返回列表');
  66. return $this->showWarning('添加失败',$url);
  67. }
  68. }
  69. /**
  70. *
  71. * 修改
  72. *
  73. *
  74. */
  75. public function update(Request $request) {
  76. if($request->method() == 'POST') {
  77. return $this->_updateSave();
  78. }
  79. $data = $this->repository->find($request->get('id'));
  80. return view('admin.student.count.edit',compact('data'));
  81. }
  82. /**
  83. * 保存修改
  84. */
  85. private function _updateSave() {
  86. $data = (array) request('data');
  87. $ok = $this->repository->update(request('id'),$data);
  88. if($ok) {
  89. $url[] = array('url'=>U( 'Student/Count/index'),'title'=>'返回列表');
  90. return $this->showMessage('操作成功',urldecode(request('_referer')));
  91. }else{
  92. $url[] = array('url'=>U( 'Student/Count/index'),'title'=>'返回列表');
  93. return $this->showWarning('操作失败',$url);
  94. }
  95. }
  96. public function view(Request $request) {
  97. $data = $this->repository->find(request('id'));
  98. return view('admin.student.count.view',compact('data'));
  99. }
  100. /**
  101. *
  102. * 状态改变
  103. *
  104. */
  105. public function status(Request $request) {
  106. $ok = $this->repository->updateStatus(request('id'),request('status'));
  107. if($ok) {
  108. return $this->showMessage('操作成功');
  109. }else{
  110. return $this->showWarning('操作失败');
  111. }
  112. }
  113. /**
  114. * 删除
  115. */
  116. public function destroy(Request $request) {
  117. $bool = $this->repository->destroy($request->get('id'));
  118. if($bool) {
  119. return $this->showMessage('操作成功');
  120. }else{
  121. return $this->showWarning("操作失败");
  122. }
  123. }
  124. /***
  125. * 导入excel表格
  126. * @param Request $request
  127. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
  128. */
  129. public function import(Request $request)
  130. {
  131. if ($request->method() == 'GET') {
  132. return view('kid_import');
  133. } else {
  134. $tmp_file = $_FILES['test'] ['name'];
  135. $file_types = explode(".", $tmp_file);
  136. $file_type = $file_types [count($file_types) - 1];
  137. if (strtolower($file_type) == "xls" || strtolower($file_type) == "xlsx") {
  138. $file = $request->file('test');
  139. $path = 'upload/excel_import';
  140. $filename = $tmp_file;
  141. $file->move($path, $filename);
  142. $filePath = $path . '/' . $filename;
  143. Excel::load($filePath, function ($reader) {
  144. $data = $reader->toArray();
  145. $a = [];
  146. if (empty($data)) {
  147. die('<script>alert("信息填写不完整,请检查后导入数据");history.back();</script>');
  148. } else {
  149. foreach ($data as $k1 => $v1) {
  150. foreach ($v1 as $k2 => $v2) {
  151. $a[$k1]['year'] = $v2['年份']?$v2['年份']:'';
  152. $a[$k1]['class'] = $v2['科类']?$v2['科类']:'';
  153. $a[$k1]['grade'] = $v2['分数']?$v2['分数']:'';
  154. $a[$k1]['sum'] = $v2['人数']?$v2['人数']:'';
  155. $a[$k1]['total'] = $v2['累计']?$v2['累计']:'';
  156. $a[$k1]['created_at'] = date('Y-m-d H:i:s', time());
  157. $a[$k1]['updated_at'] = date('Y-m-d H:i:s', time());
  158. $res = $this->repository->create($a[$k1]);
  159. }
  160. }
  161. }
  162. if (!$res) {
  163. die('<script>alert("导入人数统计信息失败");history.back();</script>');
  164. }
  165. });
  166. // 读取.xls文件后删除文件
  167. unlink($filePath);
  168. return back()->with('success', '导入人数统计信息成功');
  169. } else {
  170. return back()->with('error', '不是Excel .xls或者.xlsx文件,请重新上传');
  171. }
  172. }
  173. }
  174. }