InfoController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\CompanyContactsModel;
  13. use App\Models\CompanyInfoModel;
  14. use App\Models\UserCompanyCollectionModel;
  15. use Illuminate\Http\Request;
  16. use App\Repositories\Company\InfoRepository;
  17. use Maatwebsite\Excel\Facades\Excel;
  18. class InfoController extends Controller
  19. {
  20. private $repository;
  21. public function __construct(InfoRepository $repository)
  22. {
  23. if (!$this->repository) $this->repository = $repository;
  24. }
  25. /*
  26. * 搜索页视图
  27. * */
  28. public function search()
  29. {
  30. $fields = (new CompanyInfoModel())->filterFields;
  31. return view('admin.company.info.search', compact('fields'));
  32. }
  33. /**
  34. * 列表页
  35. */
  36. function index(Request $request)
  37. {
  38. $search = $request->all();
  39. $order = array();
  40. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  41. $order[$request['sort_field']] = $request['sort_field_by'];
  42. } else {
  43. $order['id'] = 'DESC';
  44. }
  45. $list = $this->repository->searchCompany($search, $order);
  46. if ($request->ajax()) {
  47. $view = view('admin.company.info.data', compact('list'))->render();
  48. return response()->json(['html' => $view]);
  49. }
  50. $fields = (new CompanyInfoModel())->filterFields;
  51. $user_id = \Auth::guard('admin')->user()->id;
  52. $hasCollection = UserCompanyCollectionModel::where('user_id', $user_id)->count();
  53. return view('admin.company.info.index', compact('list', 'fields', 'hasCollection'));
  54. }
  55. /***
  56. * 导入企业信息
  57. * @param Request $request
  58. */
  59. public function import(Request $request)
  60. {
  61. $tmp_file = $_FILES['company_info']['name'];
  62. $file_types = explode(".", $tmp_file);
  63. $file_type = $file_types [count($file_types) - 1];
  64. if (strtolower($file_type) == "xls" || strtolower($file_type) == "xlsx") {
  65. $file = $request->file('company_info');
  66. $path = 'upload/excel_import';
  67. $filename = $tmp_file;
  68. $file->move($path, $filename);
  69. $filePath = $path . '/' . $filename;
  70. $objRead = new \PHPExcel_Reader_Excel2007(); //建立reader对象
  71. if(!$objRead->canRead($filePath)){
  72. $objRead = new \PHPExcel_Reader_Excel5();
  73. if(!$objRead->canRead($filePath)){
  74. die('No Excel!');
  75. }
  76. }
  77. $objPHPExcel = $objRead->load($filePath,$encode='utf-8');//获取excel文件
  78. $sheet = $objPHPExcel->getSheet(0); //激活当前的表
  79. $highestRow = $sheet->getHighestRow(); // 取得总行数
  80. $highestColumn = $sheet->getHighestColumn(); // 取得总列数
  81. if($highestColumn == 'C'){
  82. $a=0;
  83. //将表格里面的数据循环到数组中
  84. for($i=2;$i<=$highestRow;$i++)
  85. {
  86. if($i % 1000 ==0 ){
  87. ob_flush();
  88. flush();
  89. }
  90. //*为什么$i=2? (因为Excel表格第一行应该是表头)
  91. $data['companyName'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
  92. $res = CompanyInfoModel::create($data);
  93. $company_id = $res->id;
  94. CompanyContactsModel::create([
  95. 'company_id'=> $company_id,
  96. 'linkman'=> $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(),
  97. 'phone'=> $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(),
  98. ]);
  99. $a++;
  100. }
  101. }else if($highestColumn == 'L') {
  102. $a=0;
  103. //将表格里面的数据循环到数组中
  104. for($i=2;$i<=$highestRow;$i++)
  105. {
  106. //*为什么$i=2? (因为Excel表格第一行应该是姓名,年龄,班级,从第二行开始,才是我们要的数据。)
  107. $isExisted = CompanyInfoModel::where('companyName',$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue())->first();
  108. if($isExisted){
  109. $company_id = $isExisted->id;
  110. }else{
  111. $comp_data[$a]['companyName'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();//姓名
  112. $comp_data[$a]['legalPerson'] = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();//姓名
  113. $comp_data[$a]['startDate'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();//姓名
  114. $comp_data[$a]['regCapital'] = $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();//姓名
  115. $comp_data[$a]['regAddr'] = $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();//姓名
  116. $res = CompanyInfoModel::create($comp_data);
  117. $company_id = $res->id;
  118. }
  119. CompanyContactsModel::create([
  120. 'company_id'=> $company_id,
  121. 'linkman'=> $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(),
  122. 'phone'=> $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue()?$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue():$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(),
  123. 'email'=> $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue(),
  124. 'qq'=> $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue()
  125. ]);
  126. $a++;
  127. }
  128. }else{
  129. }
  130. return $this->showMessage('导入成功');
  131. }else{
  132. return $this->showWarning("请上传正确的Excel文件");
  133. }
  134. // Excel::import(new CompanyInfoImport, request()->file('company_info'));
  135. }
  136. /***
  137. * 企业详情页
  138. * @param Request $request
  139. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  140. */
  141. public function view(Request $request)
  142. {
  143. $data = $this->repository->find(request('id'));
  144. $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
  145. return view('admin.company.info.view', compact('data', 'contacts'));
  146. }
  147. /**
  148. * 删除
  149. */
  150. public function destroy(Request $request)
  151. {
  152. $bool = $this->repository->destroy($request->get('id'));
  153. if ($bool) {
  154. return $this->showMessage('操作成功');
  155. } else {
  156. return $this->showWarning("操作失败");
  157. }
  158. }
  159. /***
  160. * 添加联系人备注
  161. * @param Request $request
  162. * @return int
  163. */
  164. public function addremarks(Request $request)
  165. {
  166. $contact = CompanyContactsModel::find($request->get('id'));
  167. $contact->remark = $request->get('remark');
  168. $contact->save();
  169. return 200;
  170. }
  171. }