InfoController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * 挖掘线索
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-11-19 08:23:08
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Company;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Imports\CompanyInfoImport;
  12. use App\Models\CompanyInfoModel;
  13. use App\Models\UserCompanyCollectionModel;
  14. use Illuminate\Http\Request;
  15. use App\Repositories\Company\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. /*
  25. * 搜索页
  26. * */
  27. public function search()
  28. {
  29. $fields = (new CompanyInfoModel())->filterFields;
  30. return view('admin.company.info.search', compact('fields'));
  31. }
  32. /**
  33. * 列表页
  34. */
  35. function index(Request $request)
  36. {
  37. $search = $request->all();
  38. $order = array();
  39. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  40. $order[$request['sort_field']] = $request['sort_field_by'];
  41. } else {
  42. $order['id'] = 'DESC';
  43. }
  44. $list = $this->repository->searchCompany($search, $order);
  45. if ($request->ajax()) {
  46. $view = view('admin.company.info.data', compact('list'))->render();
  47. return response()->json(['html' => $view]);
  48. }
  49. $fields = (new CompanyInfoModel())->filterFields;
  50. return view('admin.company.info.index', compact('list', 'fields'));
  51. }
  52. /***
  53. * 创建公海
  54. * @param Request $request
  55. * @return int
  56. */
  57. public function add_collection(Request $request)
  58. {
  59. $filter_fields = $request->except('_token');
  60. $data['user_id'] = \Auth::guard('admin')->user()->id;
  61. $data['filter_fields'] = json_encode($filter_fields);
  62. $data['name'] = '我的公海';
  63. UserCompanyCollectionModel::create($data);
  64. return 200;
  65. }
  66. public function collection(Request $request)
  67. {
  68. if ($request->ajax()) {
  69. $search = $request->all();
  70. $order = array();
  71. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  72. $order[$request['sort_field']] = $request['sort_field_by'];
  73. } else {
  74. $order['id'] = 'DESC';
  75. }
  76. $list = $this->repository->searchCompany($search, $order);
  77. $view = view('admin.company.collection.data', compact('list'))->render();
  78. return response()->json(['html' => $view]);
  79. }
  80. $user_id = \Auth::guard('admin')->user()->id;
  81. $collection = UserCompanyCollectionModel::where('user_id', $user_id)->first();
  82. if (count($collection)) {
  83. $search = (array)json_decode($collection->filter_fields);
  84. $order = array();
  85. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  86. $order[$request['sort_field']] = $request['sort_field_by'];
  87. } else {
  88. $order['id'] = 'DESC';
  89. }
  90. $list = $this->repository->searchCompany($search, $order);
  91. $fields = (new CompanyInfoModel())->filterFields;
  92. return view('admin.company.collection.index', compact('list', 'fields'));
  93. }
  94. }
  95. /**
  96. * 添加
  97. */
  98. public function create(Request $request)
  99. {
  100. $tmp_file = $_FILES['company_info']['name'];
  101. $file_types = explode(".", $tmp_file);
  102. $file_type = $file_types [count($file_types) - 1];
  103. if (strtolower($file_type) == "xls" || strtolower($file_type) == "xlsx") {
  104. $file = $request->file('company_info');
  105. $path = 'upload/excel';
  106. $filename = $tmp_file;
  107. $file->move($path, $filename);
  108. $filePath = $path . '/' . $filename;
  109. Excel::import(new CompanyInfoImport, ''.$filePath.'','public_file');
  110. // Excel::load($filePath, function ($reader) {
  111. // $data = $reader->toArray();
  112. // $a = [];
  113. // if (empty($data)) {
  114. // die('<script>alert("信息填写不完整,请检查后导入数据");history.back();</script>');
  115. // } else {
  116. // foreach ($data as $k1 => $v1) {
  117. //
  118. // foreach ($v1 as $k2 => $v2) {
  119. // $a[$k1]['year'] = isset($v2['年度'])?$v2['年度']:'';
  120. // $a[$k1]['class'] = isset($v2['科类'])?$v2['科类']:'';
  121. // $a[$k1]['batch'] = isset($v2['批次'])?$v2['批次']:'';
  122. // $a[$k1]['level'] = isset($v2['层次'])?$v2['层次']:'';
  123. // $a[$k1]['type'] = isset($v2['类别'])?$v2['类别']:'';
  124. // $a[$k1]['college'] = isset($v2['院校'])?$v2['院校']:'';
  125. // $a[$k1]['province'] = isset($v2['所在省份'])?$v2['所在省份']:'
  126. // ';
  127. // $a[$k1]['city'] = isset($v2['所在地市'])?$v2['所在地市']:'';
  128. // $a[$k1]['major'] = isset($v2['专业'])?$v2['专业']:'';
  129. // $a[$k1]['comment'] = isset($v2['专业备注'])?$v2['专业备注']:'';
  130. // $a[$k1]['min_grade'] = isset($v2['最低分'])?$v2['最低分']:'';
  131. // $a[$k1]['avg_grade'] = isset($v2['平均分'])?$v2['平均分']:'';
  132. // $a[$k1]['max_grade'] = isset($v2['最高分'])?$v2['最高分']:'';
  133. // $a[$k1]['batch_grade'] = isset($v2['批次线'])?$v2['批次线']:'';
  134. // $a[$k1]['miss_grade'] = isset($v2['线差'])?$v2['线差']:'';
  135. // $a[$k1]['min_rank'] = isset($v2['最低位次'])?$v2['最低位次']:'';
  136. // $a[$k1]['max_rank'] = isset($v2['最高位次'])?$v2['最高位次']:'';
  137. // $a[$k1]['avg_rank'] = isset($v2['平均位次'])?$v2['平均位次']:'';
  138. // $a[$k1]['major_grade'] = $a[$k1]['batch_grade'] + $a[$k1]['miss_grade'];
  139. //
  140. // $a[$k1]['created_at'] = date('Y-m-d H:i:s', time());
  141. // $a[$k1]['updated_at'] = date('Y-m-d H:i:s', time());
  142. //
  143. // $res = $this->repository->create($a[$k1]);
  144. // }
  145. //
  146. // }
  147. // }
  148. //
  149. // if (!$res) {
  150. // die('<script>alert("导入专业信息失败");history.back();</script>');
  151. // }
  152. // });
  153. // 读取.xls文件后删除文件
  154. unlink($filePath);
  155. return back()->with('success', '导入专业信息成功');
  156. } else {
  157. return back()->with('error', '不是Excel .xls或者.xlsx文件,请重新上传');
  158. }
  159. }
  160. /**
  161. * 保存修改
  162. */
  163. private function _createSave()
  164. {
  165. $data = (array)request('data');
  166. $id = $this->repository->create($data);
  167. if ($id) {
  168. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  169. $url[] = array('url' => U('Company/Info/create'), 'title' => '继续添加');
  170. $this->showMessage('添加成功', $url);
  171. } else {
  172. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  173. return $this->showWarning('添加失败', $url);
  174. }
  175. }
  176. /**
  177. * 修改
  178. */
  179. public function update(Request $request)
  180. {
  181. if ($request->method() == 'POST') {
  182. return $this->_updateSave();
  183. }
  184. $data = $this->repository->find($request->get('id'));
  185. return view('admin.company.info.edit', compact('data'));
  186. }
  187. /**
  188. * 保存修改
  189. */
  190. private function _updateSave()
  191. {
  192. $data = (array)request('data');
  193. $ok = $this->repository->update(request('id'), $data);
  194. if ($ok) {
  195. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  196. return $this->showMessage('操作成功', urldecode(request('_referer')));
  197. } else {
  198. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  199. return $this->showWarning('操作失败', $url);
  200. }
  201. }
  202. public function view(Request $request)
  203. {
  204. $data = $this->repository->find(request('id'));
  205. $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
  206. return view('admin.company.info.view', compact('data', 'contacts'));
  207. }
  208. /**
  209. * 删除
  210. */
  211. public function destroy(Request $request)
  212. {
  213. $bool = $this->repository->destroy($request->get('id'));
  214. if ($bool) {
  215. return $this->showMessage('操作成功');
  216. } else {
  217. return $this->showWarning("操作失败");
  218. }
  219. }
  220. }