123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- /**
- * 专业信息
- * @author system
- * @version 1.0
- * @date 2018-06-06 01:44:59
- *
- */
- namespace App\Http\Controllers\Admin\Major;
- use App\Http\Controllers\Admin\Controller;
- use App\Models\MajorInfoModel;
- use Illuminate\Http\Request;
- use App\Repositories\Base\Criteria\OrderBy;
- use App\Repositories\Major\Criteria\MultiWhere;
- use App\Repositories\Major\InfoRepository;
- use Maatwebsite\Excel\Facades\Excel;
- class InfoController extends Controller
- {
- private $repository;
- public function __construct(InfoRepository $repository)
- {
- if (!$this->repository) $this->repository = $repository;
- }
- function index(Request $request)
- {
- $search['keyword'] = $request->input('keyword');
- $query = $this->repository->pushCriteria(new MultiWhere($search));
- if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $query = $query->pushCriteria(new OrderBy($request['sort_field'], $request['sort_field_by']));
- } else {
- $query = $query->pushCriteria(new OrderBy('id', 'DESC'));
- }
- $list = $query->paginate(10);
- return view('admin.major.info.index', compact('list'));
- }
- function check(Request $request)
- {
- $request = $request->all();
- $search['keyword'] = $request->input('keyword');
- $orderby = array();
- if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $orderby[$request['sort_field']] = $request['sort_field_by'];
- }
- $list = $this->repository->search($search, $orderby);
- return view('admin.major.info.check', compact('list'));
- }
- /**
- * 添加
- *
- */
- public function create(Request $request)
- {
- if ($request->method() == 'POST') {
- return $this->_createSave();
- }
- return view('admin.major.info.edit');
- }
- /**
- * 保存修改
- */
- private function _createSave()
- {
- $data = (array)request('data');
- $id = $this->repository->create($data);
- if ($id) {
- $url[] = array('url' => U('Major/Info/index'), 'title' => '返回列表');
- $url[] = array('url' => U('Major/Info/create'), 'title' => '继续添加');
- $this->showMessage('添加成功', $url);
- } else {
- $url[] = array('url' => U('Major/Info/index'), 'title' => '返回列表');
- return $this->showWarning('添加失败', $url);
- }
- }
- /**
- *
- * 修改
- *
- *
- */
- public function update(Request $request)
- {
- if ($request->method() == 'POST') {
- return $this->_updateSave();
- }
- $data = $this->repository->find($request->get('id'));
- return view('admin.major.info.edit', compact('data'));
- }
- /**
- * 保存修改
- */
- private function _updateSave()
- {
- $data = (array)request('data');
- $ok = $this->repository->update(request('id'), $data);
- if ($ok) {
- $url[] = array('url' => U('Major/Info/index'), 'title' => '返回列表');
- return $this->showMessage('操作成功', urldecode(request('_referer')));
- } else {
- $url[] = array('url' => U('Major/Info/index'), 'title' => '返回列表');
- return $this->showWarning('操作失败', $url);
- }
- }
- public function view(Request $request)
- {
- $data = $this->repository->find(request('id'));
- return view('admin.major.info.view', compact('data'));
- }
- /**
- *
- * 状态改变
- *
- */
- public function status(Request $request)
- {
- $ok = $this->repository->updateStatus(request('id'), request('status'));
- if ($ok) {
- return $this->showMessage('操作成功');
- } else {
- return $this->showWarning('操作失败');
- }
- }
- /**
- * 删除
- */
- public function destroy(Request $request)
- {
- $bool = $this->repository->destroy($request->get('id'));
- if ($bool) {
- return $this->showMessage('操作成功');
- } else {
- return $this->showWarning("操作失败");
- }
- }
- /***
- * 导入excel表格
- * @param Request $request
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
- */
- public function import(Request $request)
- {
- if ($request->method() == 'GET') {
- return view('kid_import');
- } else {
- $tmp_file = $_FILES['test'] ['name'];
- $file_types = explode(".", $tmp_file);
- $file_type = $file_types [count($file_types) - 1];
- if (strtolower($file_type) == "xls" || strtolower($file_type) == "xlsx") {
- $file = $request->file('test');
- $path = 'upload/excel_import';
- $filename = $tmp_file;
- $file->move($path, $filename);
- $filePath = $path . '/' . $filename;
- Excel::load($filePath, function ($reader) {
- $data = $reader->toArray();
- $a = [];
- if (empty($data)) {
- die('<script>alert("信息填写不完整,请检查后导入数据");history.back();</script>');
- } else {
- foreach ($data as $k1 => $v1) {
- foreach ($v1 as $k2 => $v2) {
- $a[$k1]['year'] = isset($v2['年度'])?$v2['年度']:'';
- $a[$k1]['class'] = isset($v2['科类'])?$v2['科类']:'';
- $a[$k1]['batch'] = isset($v2['批次'])?$v2['批次']:'';
- $a[$k1]['level'] = isset($v2['层次'])?$v2['层次']:'';
- $a[$k1]['type'] = isset($v2['类别'])?$v2['类别']:'';
- $a[$k1]['college'] = isset($v2['院校'])?$v2['院校']:'';
- $a[$k1]['province'] = isset($v2['所在省份'])?$v2['所在省份']:'
- ';
- $a[$k1]['city'] = isset($v2['所在地市'])?$v2['所在地市']:'';
- $a[$k1]['major'] = isset($v2['专业'])?$v2['专业']:'';
- $a[$k1]['comment'] = isset($v2['专业备注'])?$v2['专业备注']:'';
- $a[$k1]['min_grade'] = isset($v2['最低分'])?$v2['最低分']:'';
- $a[$k1]['avg_grade'] = isset($v2['平均分'])?$v2['平均分']:'';
- $a[$k1]['max_grade'] = isset($v2['最高分'])?$v2['最高分']:'';
- $a[$k1]['batch_grade'] = isset($v2['批次线'])?$v2['批次线']:'';
- $a[$k1]['miss_grade'] = isset($v2['线差'])?$v2['线差']:'';
- $a[$k1]['min_rank'] = isset($v2['最低位次'])?$v2['最低位次']:'';
- $a[$k1]['max_rank'] = isset($v2['最高位次'])?$v2['最高位次']:'';
- $a[$k1]['avg_rank'] = isset($v2['平均位次'])?$v2['平均位次']:'';
- $a[$k1]['major_grade'] = $a[$k1]['batch_grade'] + $a[$k1]['miss_grade'];
- $a[$k1]['created_at'] = date('Y-m-d H:i:s', time());
- $a[$k1]['updated_at'] = date('Y-m-d H:i:s', time());
- $res = $this->repository->create($a[$k1]);
- }
- }
- }
- if (!$res) {
- die('<script>alert("导入专业信息失败");history.back();</script>');
- }
- });
- // 读取.xls文件后删除文件
- unlink($filePath);
- return back()->with('success', '导入专业信息成功');
- } else {
- return back()->with('error', '不是Excel .xls或者.xlsx文件,请重新上传');
- }
- }
- }
- }
|