Esempio n. 1
0
 public function getAutoHomeDetail()
 {
     set_time_limit(0);
     $snoopy = new Snoopy();
     $datas = CarModel::whereBetween('id', [6500, 24000])->where('maintain_id', 0)->orderBy('id', 'asc')->get();
     $hasMaintains = array();
     //适配车型数组
     foreach ($datas as $data) {
         echo '------data_id------' . $data->id . '-START-';
         if (!in_array($data->id, $hasMaintains)) {
             $url = $data->url;
             $snoopy->fetch($url);
             $snoopy->results = mb_convert_encoding($snoopy->results, 'UTF-8', 'GBK');
             //首保
             preg_match('/首保:(\\d+?)公里\\/(\\d+?)个月\\s二保:(\\d+?)公里\\/(\\d+?)个月\\s+?间隔:(\\d+?)公里\\/(\\d+?)个月/', $snoopy->results, $match);
             $maintain = new Detail();
             if (isset($match[1])) {
                 $maintain->first_maintain_kilometers = $match[1];
             }
             if (isset($match[2])) {
                 $maintain->first_maintain_month = $match[2];
             }
             if (isset($match[3])) {
                 $maintain->second_maintain_kilometers = $match[3];
             }
             if (isset($match[4])) {
                 $maintain->second_maintain_month = $match[4];
             }
             if (isset($match[5])) {
                 $maintain->maintain_interval_kilometers = $match[5];
             }
             if (isset($match[6])) {
                 $maintain->maintain_interval_month = $match[6];
             }
             if ($maintain->save()) {
                 preg_match('/<tr><td><strong>保养项目\\/里程[\\s\\S]+(元)<\\/td>/', $snoopy->results, $match);
                 if (!isset($match[0])) {
                     continue;
                 } else {
                     $maintain_results = $match[0];
                 }
                 //project
                 preg_match_all('/<tr><td>(.*?)<\\/td>/', $maintain_results, $match);
                 $projects = $match[1];
                 //除去“保养项目/里程,前制动器,后制动器,总计”
                 array_shift($projects);
                 array_pop($projects);
                 array_pop($projects);
                 array_pop($projects);
                 $project_row_map = array_flip($projects);
                 //项目,价格信息入库
                 foreach ($projects as $project) {
                     $pro = Project::firstOrCreate(['name' => $project]);
                     preg_match('/<tr><td>' . $project . '<\\/td>[\\s]+<td>(\\d*)<\\/td>/', $maintain_results, $match);
                     if (isset($match[1]) && $match[1]) {
                         DetailProjectPrice::firstOrCreate(['detail_id' => $maintain->id, 'project_id' => $pro->id, 'price' => $match[1]]);
                     }
                     $row_project_id_map[$project_row_map[$project]] = $pro['id'];
                 }
                 //列数
                 preg_match('/保养项目\\/里程[\\s\\S]+?<tr><td>/', $maintain_results, $match);
                 preg_match_all('/(class="t_H3_item_bg")/', $match[0], $match);
                 $column = count($match[0]);
                 //行数
                 preg_match_all('/(<tr><td>)/', $maintain_results, $match);
                 $row = count($match[0]) - 4;
                 //section矩阵
                 for ($c = 0; $c < $column; $c++) {
                     $section = array();
                     for ($r = 0; $r < $row; $r++) {
                         $id = "c{$c}r{$r}";
                         preg_match('/' . $id . '"[\\s]+?>(.*?)<\\/td>/', $maintain_results, $match);
                         if (isset($match[1]) && !empty($match[1])) {
                             $section[] = $row_project_id_map[$r];
                         }
                     }
                     if (!empty($section)) {
                         $project_ids = implode(',', $section);
                         DetailSection::firstOrCreate(['detail_id' => $maintain->id, 'section' => $c, 'project_ids' => $project_ids]);
                     }
                 }
                 //保存
                 $data->maintain_id = $maintain->id;
                 $data->save();
                 preg_match('/id="specBox"[\\s\\S]+?<div/', $snoopy->results, $match);
                 if (isset($match[0]) && !empty($match[0])) {
                     $adaptMatch = $match[0];
                     preg_match_all('/<dd>(.+?)<\\/dd>/', $adaptMatch, $match);
                     if (isset($match[1]) && !empty($match[1])) {
                         //所有适用的车型
                         $model = CarModel::where('type_id', $data->type_id)->whereIn('name', $match[1]);
                         echo 1;
                         $model->update(['maintain_id' => $maintain->id]);
                         $ids = $model->lists('id');
                         //                            print_r($ids);die;
                         if (isset($ids) && !empty($ids)) {
                             $hasMaintains = empty($hasMaintains) ? $ids : array_merge($hasMaintains, $ids);
                             //                                print_r($hasMaintains);die;
                         }
                     }
                 }
                 echo 'new maintain_id-----' . $maintain->id . '-';
             }
         }
         echo '-------<br>';
     }
 }
Esempio n. 2
0
 /**
  * 姹借溅涔嬪淇濆吇鏁版嵁
  * @param Request $request
  */
 public function index(Request $request)
 {
     $validator = Validator::make($request->all(), ['mycar_id' => 'required|integer']);
     if ($validator->fails()) {
         echo json_encode(['status' => 'false', 'msg' => $validator->errors(), 'data' => (object) null]);
         die;
     }
     $mycar_id = Input::get('mycar_id');
     //获取保养ID
     $myCarData = $this->connection->table('my_car')->where('id', $mycar_id)->first();
     if (is_null($myCarData)) {
         echo json_encode(['status' => 'false', 'msg' => 'mycar_id没有被发现', 'data' => (object) null]);
         die;
     }
     $model_slug = $myCarData->model_detail;
     $kilometers = intval($myCarData->mileage * 10000);
     $detail_id = $this->connection->table('foreign_category_autohome')->where('detail_model_slug', $model_slug)->pluck('dmodel_id');
     $maintain_id = CarModel::where('id', $detail_id)->pluck('maintain_id');
     if (is_null($maintain_id)) {
         $year = $myCarData->year;
         $brand_name = $this->connection->table('open_category')->where('slug', $myCarData->brand)->pluck('name');
         $type_name = $this->connection->table('open_category')->where('slug', $myCarData->model)->pluck('name');
         $typeNames[] = $type_name;
         if (strpos($type_name, $brand_name) !== false) {
             $length = strlen($brand_name);
             $typeNames[] = substr($type_name, $length);
         }
         $typeId = $this->connection->table('autohome_types')->whereIn('name', $typeNames)->pluck('id');
         $nearModels = CarModel::select('maintain_id', 'year')->where('type_id', $typeId)->groupBy('year')->get();
         if (count($nearModels) > 0) {
             foreach ($nearModels as $nearModel) {
                 //取最相近同款年份的保养ID
                 if (!isset($minYearAbs) || abs($nearModel->year - $year) < $minYearAbs) {
                     $minYearAbs = $nearModel->year;
                     $maintain_id = $nearModel->maintain_id;
                 }
             }
         }
     }
     //通配
     if (is_null($maintain_id) && $myCarData->eval_price < 50) {
         $maintain_id = $this->commonMaintainId;
     }
     if (is_null($maintain_id)) {
         echo json_encode(['status' => "false", 'msg' => '匹配不到保养信息', 'data' => (object) null]);
         die;
     }
     //保养信息
     $detailInfo = Detail::find($maintain_id);
     $projectInfo = Project::all();
     foreach ($projectInfo as $project) {
         $projectMaps[$project->id] = $project->name;
     }
     //价格
     $priceInfo = DetailProjectPrice::where('detail_id', $maintain_id)->get();
     //价格通配
     if (count($priceInfo) == 0) {
         $priceInfo = DetailProjectPrice::where('detail_id', $this->commonMaintainId)->get();
     }
     foreach ($priceInfo as $pro_price) {
         $priceMaps[$pro_price->project_id] = $pro_price->price;
     }
     //我的车保养记录
     $maintainCarInfo = $this->connection->table('maintain_car_record')->where('mycar_id', $mycar_id)->orderBy('created_at', 'desc')->first();
     $fixKilometers = 0;
     //根据之前的保养里程,修正保养里程
     $current_not_maintain_flag = false;
     //不提醒当次保养过的数据
     if (!is_null($maintainCarInfo) && $maintainCarInfo->kilometers <= $kilometers) {
         $maintainKilometers = $maintainCarInfo->kilometers;
         $fixKilometers = $this->getFixKilometersByLastMaintainData($maintainKilometers, $detailInfo);
         if ($kilometers <= $maintainKilometers + $this->deviationKilometers) {
             $current_not_maintain_flag = true;
         }
     }
     //算出保养区间
     if ($kilometers <= $detailInfo->first_maintain_kilometers + $fixKilometers + $this->deviationKilometers) {
         $section = 0;
     } elseif ($kilometers <= $detailInfo->second_maintain_kilometers + $fixKilometers + $this->deviationKilometers) {
         $section = 1;
     } else {
         $section = ceil(($kilometers - ($detailInfo->second_maintain_kilometers + $fixKilometers + $this->deviationKilometers)) / $detailInfo->maintain_interval_kilometers) + 1;
     }
     if ($current_not_maintain_flag) {
         $section++;
     }
     //距离下次保养里程
     if ($section == 0) {
         $next_kilometers = $detailInfo->first_maintain_kilometers;
         $next_next_kilometers = $detailInfo->second_maintain_kilometers;
     } else {
         $next_kilometers = $detailInfo->second_maintain_kilometers + $detailInfo->maintain_interval_kilometers * ($section - 1);
         $next_next_kilometers = $detailInfo->second_maintain_kilometers + $detailInfo->maintain_interval_kilometers * $section;
     }
     $next['kilometers'] = $next_kilometers + $fixKilometers - $kilometers;
     $next['day'] = max(0, floor($next['kilometers'] / ($this->kilometersPerSection / 180)));
     if (abs($next['kilometers']) > $this->deviationKilometers) {
         $next['need_maintain_immediately'] = false;
     } else {
         $next['need_maintain_immediately'] = true;
     }
     $next['maintain_interval_kilometers'] = $detailInfo->maintain_interval_kilometers;
     //保养数据支持的最高里程数 10000为保留值
     $allSections = DetailSection::where('detail_id', $maintain_id)->get();
     $next['max_kilometers'] = (count($allSections) - 2) * $detailInfo->maintain_interval_kilometers + $detailInfo->second_maintain_kilometers - 10000;
     $sectionInfo = DetailSection::where('detail_id', $maintain_id)->where('section', $section)->first();
     if (!is_null($sectionInfo)) {
         //需要保养项目
         $price = 0;
         $projects_ids = explode(',', $sectionInfo->project_ids);
         foreach ($projects_ids as $projectId) {
             $arr['id'] = $projectId;
             $arr['name'] = $projectMaps[$projectId];
             if (isset($priceMaps[$projectId])) {
                 $price += $priceMaps[$projectId];
             }
             $projects[] = $arr;
         }
         $title = '推荐保养计划: ' . $next_kilometers . '公里保养';
         $maintainData[0] = ['title' => $title, 'price' => $price, 'projects' => $projects];
         //下次需要保养项目
         $another_sectionInfo = DetailSection::where('detail_id', $maintain_id)->where('section', $section + 1)->first();
         if (!is_null($another_sectionInfo)) {
             $another_price = 0;
             $another_projects_ids = explode(',', $another_sectionInfo->project_ids);
             if (!empty($another_projects_ids)) {
                 foreach ($another_projects_ids as $projectId) {
                     $arr['id'] = $projectId;
                     $arr['name'] = $projectMaps[$projectId];
                     if (isset($priceMaps[$projectId])) {
                         $another_price += $priceMaps[$projectId];
                     }
                     $another_projects[] = $arr;
                 }
             }
             $another_title = '保养计划: ' . $next_next_kilometers . '公里保养';
             $maintainData[1] = ['title' => $another_title, 'price' => $another_price, 'projects' => $another_projects];
         }
     } else {
         echo json_encode(['status' => "false", 'msg' => '无法得到保养项目信息', 'data' => (object) null]);
         die;
     }
     echo json_encode(['status' => "success", 'msg' => '返回成功', 'data' => ['next' => $next, 'maintain' => $maintainData]]);
 }