Beispiel #1
0
 public static function updateCar($request, $id)
 {
     if ($id && $request) {
         DB::transaction(function () use($request, $id) {
             $Car = Car::find($id);
             $Car->price = $request->input('price');
             $Car->save();
             $CarModel = CarModel::find($Car->model_id);
             $CarModel->name = $request->input('model.name');
             $CarModel->save();
             $CarMake = CarMake::find($Car->make_id);
             $CarMake->name = $request->input('make.name');
             $CarMake->save();
         });
         return TRUE;
     } else {
         return FALSE;
     }
 }
 public function delete(Request $request)
 {
     //locations to be deleted
     $data = [];
     //check for single delete
     if (isset($request->id)) {
         $data[] = $request->id;
     }
     //check for mass delete if no single delete
     if (empty($data)) {
         $data = $request->input('car_brand_id');
     }
     //delete
     if (!empty($data)) {
         CarBrand::destroy($data);
         CarModel::whereIn('car_brand_id', $data)->delete();
         //clear cache, set message, redirect to list
         Cache::flush();
         session()->flash('message', trans('admin_common.Car Brand deleted'));
         return redirect(url('admin/carbrand'));
     }
     //nothing for deletion set message and redirect
     session()->flash('message', trans('admin_common.Nothing for deletion'));
     return redirect(url('admin/carbrand'));
 }
 public function edit(Request $request)
 {
     //get ad id
     $ad_id = $request->id;
     //get ad info
     $ad_detail = $this->ad->getAdDetail($ad_id, 0);
     $ad_detail->ad_price_type_1 = $ad_detail->ad_price_type_2 = $ad_detail->ad_price_type_3 = $ad_detail->ad_price;
     $ad_detail->condition_id_type_1 = $ad_detail->condition_id_type_3 = $ad_detail->condition_id;
     $ad_detail->ad_description = Util::br2nl($ad_detail->ad_description);
     //get ad pics
     $ad_pic = AdPic::where('ad_id', $ad_id)->get();
     $car_model_id = array();
     if (old('car_brand_id')) {
         if (is_numeric(old('car_brand_id')) && old('car_brand_id') > 0) {
             $car_models = CarModel::where('car_brand_id', old('car_brand_id'))->orderBy('car_model_name', 'asc')->get();
             if (!$car_models->isEmpty()) {
                 $car_model_id = array(0 => 'Select Car Model');
                 foreach ($car_models as $k => $v) {
                     $car_model_id[$v->car_model_id] = $v->car_model_name;
                 }
             }
         }
     }
     $ad_detail->ad_category_info = $this->category->getParentsByIdFlat($ad_detail->category_id);
     $ad_detail->pics = AdPic::where('ad_id', $ad_detail->ad_id)->get();
     return view('admin.ad.ad_edit', ['ad_detail' => $ad_detail, 'ad_pic' => $ad_pic, 'c' => $this->category->getAllHierarhy(), 'l' => $this->location->getAllHierarhy(), 'at' => AdType::all(), 'ac' => AdCondition::all(), 'estate_construction_type' => EstateConstructionType::all(), 'estate_furnishing_type' => EstateFurnishingType::all(), 'estate_heating_type' => EstateHeatingType::all(), 'estate_type' => EstateType::all(), 'car_brand_id' => CarBrand::all(), 'car_model_id' => $car_model_id, 'car_engine_id' => CarEngine::all(), 'car_transmission_id' => CarTransmission::all(), 'car_condition_id' => CarCondition::all(), 'car_modification_id' => CarModification::all()]);
 }
 public function import(Request $request)
 {
     $car_brand_id = CarBrand::all();
     /**
      * form is submitted check values and save if needed
      */
     if ($request->isMethod('post')) {
         /**
          * validate data
          */
         $rules = ['car_brand_id' => 'required|integer|not_in:0', 'csv_file' => 'required'];
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             $this->throwValidationException($request, $validator);
         }
         /**
          * save data if validated
          */
         if ($request->file('csv_file')->isValid()) {
             //rename and move uploaded file
             $csv_file = Input::file('csv_file');
             $tmp_import_name = time() . '_car_model_import_.' . $csv_file->getClientOriginalExtension();
             $csv_file->move(storage_path() . '/app', $tmp_import_name);
             //read csv
             $csv_data = [];
             if (($handle = fopen(storage_path() . '/app/' . $tmp_import_name, "r")) !== FALSE) {
                 while (($data = fgetcsv($handle, 1000, ",", '"')) !== FALSE) {
                     $csv_data[] = $data;
                 }
                 fclose($handle);
             }
             if (!empty($csv_data)) {
                 $car_brand_id = $request->input('car_brand_id');
                 //import erros holder
                 $import_error_array = [];
                 foreach ($csv_data as $k => $v) {
                     if (is_array($v)) {
                         $data_to_save = [];
                         //set fields to be imported
                         if (isset($v[0]) && !empty($v[0])) {
                             $data_to_save['car_model_name'] = trim($v[0]);
                         }
                         if (isset($v[1]) && !empty($v[1])) {
                             $data_to_save['car_model_active'] = trim($v[1]);
                         }
                         $data_to_save['car_brand_id'] = $car_brand_id;
                         //check if all fields are here
                         if (count($data_to_save) == 3) {
                             try {
                                 CarModel::create($data_to_save);
                             } catch (\Exception $e) {
                                 $import_error_array[] = trans('admin_common.Error on line') . ': ' . join(',', $v) . ' <br />' . trans('admin_common.Error Message') . ': ' . $e->getMessage();
                             }
                         } else {
                             $import_error_array[] = trans('admin_common.Missing data line') . ': ' . join(',', $v);
                         }
                     }
                 }
             } else {
                 session()->flash('message', trans('admin_common.Cant read the csv file.'));
                 return redirect(url('admin/carmodel'));
             }
         }
         /**
          * delete temp file, clear cache, set message, redirect to list
          */
         @unlink(storage_path() . '/app/' . $tmp_import_name);
         Cache::flush();
         if (!empty($import_error_array)) {
             session()->flash('message', trans('admin_common.Car Models imported with the following errors') . ': <br />' . join('<br />', $import_error_array));
         } else {
             session()->flash('message', trans('admin_common.Car Models imported'));
         }
         return redirect(url('admin/carmodel'));
     }
     return view('admin.car_model.car_model_import', ['car_brand_id' => $car_brand_id]);
 }
Beispiel #5
0
 /**
  * @param $name
  * @param $carmake
  * @return static
  */
 private function makeCarmodel($name, $carmake)
 {
     $carmodel = CarModel::firstOrNew(['carmodel' => $name]);
     $carmodel->carmake()->associate($carmake);
     $carmodel->save();
     return $carmodel;
 }
Beispiel #6
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>';
     }
 }
Beispiel #7
0
 public function getModelCar($id)
 {
     $make = CarModel::where('make_id', '=', $id)->get();
     return response()->json($make);
 }
Beispiel #8
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]]);
 }