InfoController.php 8.0 KB

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