123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- /**
- * 挖掘线索
- * @author system
- * @version 1.0
- * @date 2018-11-19 08:23:08
- *
- */
- namespace App\Http\Controllers\Admin\Company;
- use App\Http\Controllers\Admin\Controller;
- use App\Imports\CompanyInfoImport;
- use App\Models\CompanyContactsModel;
- use App\Models\CompanyInfoModel;
- use App\Models\UserCompanyCollectionModel;
- use Illuminate\Http\Request;
- use App\Repositories\Company\InfoRepository;
- use Maatwebsite\Excel\Facades\Excel;
- class InfoController extends Controller
- {
- private $repository;
- public function __construct(InfoRepository $repository)
- {
- if (!$this->repository) $this->repository = $repository;
- }
- /*
- * 搜索页视图
- * */
- public function search()
- {
- $fields = (new CompanyInfoModel())->filterFields;
- return view('admin.company.info.search', compact('fields'));
- }
- /**
- * 列表页
- */
- function index(Request $request)
- {
- $search = $request->all();
- $order = array();
- if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $order[$request['sort_field']] = $request['sort_field_by'];
- } else {
- $order['id'] = 'DESC';
- }
- $list = $this->repository->searchCompany($search, $order);
- if ($request->ajax()) {
- $view = view('admin.company.info.data', compact('list'))->render();
- return response()->json(['html' => $view]);
- }
- $fields = (new CompanyInfoModel())->filterFields;
- $user_id = \Auth::guard('admin')->user()->id;
- $hasCollection = UserCompanyCollectionModel::where('user_id', $user_id)->count();
- return view('admin.company.info.index', compact('list', 'fields', 'hasCollection'));
- }
- /***
- * 导入企业信息
- * @param Request $request
- */
- public function import(Request $request)
- {
- $tmp_file = $_FILES['company_info']['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('company_info');
- $path = 'upload/excel_import';
- $filename = $tmp_file;
- $file->move($path, $filename);
- $filePath = $path . '/' . $filename;
- $objRead = new \PHPExcel_Reader_Excel2007(); //建立reader对象
- if(!$objRead->canRead($filePath)){
- $objRead = new \PHPExcel_Reader_Excel5();
- if(!$objRead->canRead($filePath)){
- die('No Excel!');
- }
- }
- $objPHPExcel = $objRead->load($filePath,$encode='utf-8');//获取excel文件
- $sheet = $objPHPExcel->getSheet(0); //激活当前的表
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- $highestColumn = $sheet->getHighestColumn(); // 取得总列数
- if($highestColumn == 'C'){
- $a=0;
- //将表格里面的数据循环到数组中
- for($i=2;$i<=$highestRow;$i++)
- {
- if($i % 1000 ==0 ){
- ob_flush();
- flush();
- }
- //*为什么$i=2? (因为Excel表格第一行应该是表头)
- $data['companyName'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
- $res = CompanyInfoModel::create($data);
- $company_id = $res->id;
- CompanyContactsModel::create([
- 'company_id'=> $company_id,
- 'linkman'=> $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(),
- 'phone'=> $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(),
- ]);
- $a++;
- }
- }else if($highestColumn == 'L') {
- $a=0;
- //将表格里面的数据循环到数组中
- for($i=2;$i<=$highestRow;$i++)
- {
- //*为什么$i=2? (因为Excel表格第一行应该是姓名,年龄,班级,从第二行开始,才是我们要的数据。)
- $isExisted = CompanyInfoModel::where('companyName',$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue())->first();
- if($isExisted){
- $company_id = $isExisted->id;
- }else{
- $comp_data[$a]['companyName'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();//姓名
- $comp_data[$a]['legalPerson'] = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();//姓名
- $comp_data[$a]['startDate'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();//姓名
- $comp_data[$a]['regCapital'] = $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();//姓名
- $comp_data[$a]['regAddr'] = $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();//姓名
- $res = CompanyInfoModel::create($comp_data);
- $company_id = $res->id;
- }
- CompanyContactsModel::create([
- 'company_id'=> $company_id,
- 'linkman'=> $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(),
- 'phone'=> $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue()?$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue():$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(),
- 'email'=> $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue(),
- 'qq'=> $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue()
- ]);
- $a++;
- }
- }else{
- }
- return $this->showMessage('导入成功');
- }else{
- return $this->showWarning("请上传正确的Excel文件");
- }
- // Excel::import(new CompanyInfoImport, request()->file('company_info'));
- }
- /***
- * 企业详情页
- * @param Request $request
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
- */
- public function view(Request $request)
- {
- $data = $this->repository->find(request('id'));
- $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
- return view('admin.company.info.view', compact('data', 'contacts'));
- }
- /**
- * 删除
- */
- public function destroy(Request $request)
- {
- $bool = $this->repository->destroy($request->get('id'));
- if ($bool) {
- return $this->showMessage('操作成功');
- } else {
- return $this->showWarning("操作失败");
- }
- }
- /***
- * 添加联系人备注
- * @param Request $request
- * @return int
- */
- public function addremarks(Request $request)
- {
- $contact = CompanyContactsModel::find($request->get('id'));
- $contact->remark = $request->get('remark');
- $contact->save();
- return 200;
- }
- }
|