InfoController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $res = Excel::import(new CompanyInfoImport, request()->file('company_info'));
  101. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  102. $this->showMessage('导入成功', $url);
  103. }
  104. /**
  105. * 保存修改
  106. */
  107. private function _createSave()
  108. {
  109. $data = (array)request('data');
  110. $id = $this->repository->create($data);
  111. if ($id) {
  112. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  113. $url[] = array('url' => U('Company/Info/create'), 'title' => '继续添加');
  114. $this->showMessage('添加成功', $url);
  115. } else {
  116. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  117. return $this->showWarning('添加失败', $url);
  118. }
  119. }
  120. /**
  121. * 修改
  122. */
  123. public function update(Request $request)
  124. {
  125. if ($request->method() == 'POST') {
  126. return $this->_updateSave();
  127. }
  128. $data = $this->repository->find($request->get('id'));
  129. return view('admin.company.info.edit', compact('data'));
  130. }
  131. /**
  132. * 保存修改
  133. */
  134. private function _updateSave()
  135. {
  136. $data = (array)request('data');
  137. $ok = $this->repository->update(request('id'), $data);
  138. if ($ok) {
  139. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  140. return $this->showMessage('操作成功', urldecode(request('_referer')));
  141. } else {
  142. $url[] = array('url' => U('Company/Info/index'), 'title' => '返回列表');
  143. return $this->showWarning('操作失败', $url);
  144. }
  145. }
  146. public function view(Request $request)
  147. {
  148. $data = $this->repository->find(request('id'));
  149. $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
  150. return view('admin.company.info.view', compact('data', 'contacts'));
  151. }
  152. /**
  153. * 删除
  154. */
  155. public function destroy(Request $request)
  156. {
  157. $bool = $this->repository->destroy($request->get('id'));
  158. if ($bool) {
  159. return $this->showMessage('操作成功');
  160. } else {
  161. return $this->showWarning("操作失败");
  162. }
  163. }
  164. }