diff --git a/admin/controllers/auto/Business.php b/admin/controllers/auto/Business.php index e123e628..86f252ce 100644 --- a/admin/controllers/auto/Business.php +++ b/admin/controllers/auto/Business.php @@ -274,6 +274,181 @@ class Business extends HD_Controller{ return $this->show_json(SYS_CODE_SUCCESS, '添加成功'); } + function add_excel() + { + require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php'; + $res = $this->upload(); + if (!$res['code']) { + return $this->show_json(0, $res['message']); + } + $file = $res['path']; + if ($res['file_ext'] == '.xls') { + $reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档 + } elseif ($res['file_ext'] == '.xlsx') { + $reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档 + } else { + return $this->show_json(SYS_CODE_FAIL, '文件无法识别'); + } + $PHPExcel = $reader->load($file); // 文档名称 + $objWorksheet = $PHPExcel->getActiveSheet(); + $rowCnt = $objWorksheet->getHighestRow(); //获取总行数 + if ($rowCnt > 800) { + @unlink($file); + return $this->show_json(0, '数据大于800请拆分多个表格导入'); + } + $data = array(); + $yearary = $this->auto_business_model->year(); + $monthary = $this->auto_business_model->month(); + $c_time = time(); + $count = $rowCnt - 1; + $count_ignore = 0; + $msg_debug = "file: {$file};count: {$count};"; + for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容 + $brand_name = $objWorksheet->getCell('A' . $_row)->getValue(); + $res = $this->auto_brand_model->get(array('status'=>1, 'name'=>$brand_name), 'id'); + if (!$res){ + $count_ignore += 1; + $msg_debug .= 'no brand_id;'; + continue; + } + $brand_id = $res['id']; + + $s_name = $objWorksheet->getCell('B' . $_row)->getValue(); + $res = $this->auto_series_model->get(array('status'=>1, 'brand_id'=> $brand_id, 'name'=>$s_name), 'id'); + if (!$res){ + $count_ignore += 1; + $msg_debug .= 'no s_id;'; + continue; + } + $s_id = $res['id']; + + $v_name = $objWorksheet->getCell('C' . $_row)->getValue(); + $res = $this->auto_attr_model->get(array('status'=>1, 's_id'=> $s_id, 'title'=>$v_name), 'id'); + if (!$res){ + $count_ignore += 1; + $msg_debug .= 'no v_id;'; + continue; + } + $v_id = $res['id']; + + $city_name = $objWorksheet->getCell('D' . $_row)->getValue(); + if (!$city_name){ + $count_ignore += 1; + $msg_debug .= 'no city_name;'; + continue; + } + $res = $this->sys_city_model->get(array('status'=>1, "name like '%{$city_name}%'"=>null), 'city_id'); + if (!$res){ + $count_ignore += 1; + $sql = $this->sys_city_model->db->last_query(); + $msg_debug .= "no city_id: {$sql};"; + continue; + } + $city_id = $res['city_id']; + + $s_effect_date = $objWorksheet->getCell('E' . $_row)->getValue(); # 此处得到是距离1900-01-01天数 + $s_effect_date && $s_effect_date = gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP($s_effect_date)); + $s_effect_time = 0; + $if_effect = 0; + if ($s_effect_date) { + $s_effect_time = strtotime($s_effect_date); + if($c_time>$s_effect_time){ + $if_effect = 1; + } + } + $msg_debug .= "s_effect_date:{$s_effect_date};s_effect_time:{$s_effect_time};if_effect:{$if_effect};"; + + $year = $objWorksheet->getCell('F' . $_row)->getValue(); + $month = $objWorksheet->getCell('G' . $_row)->getValue(); + $year = intval($year); + $month = intval($month); + if (!in_array($year, $yearary) || !in_array($month, $monthary)){ + $count_ignore += 1; + $msg_debug .= 'year or month exception;'; + continue; + } + + $where = [ + 'brand_id' => $brand_id, + 's_id' => $s_id, + 'v_id' => $v_id, + 'city_id' => $city_id, + 'year' => $year, + 'month' => $month, + 'status' => 1, + ]; + if($this->auto_business_model->get($where)){ + $count_ignore += 1; + $sql = $this->auto_business_model->db->last_query(); + $msg_debug .= "exists business: {$sql};"; + continue; + } + + $price_car = $objWorksheet->getCell('H' . $_row)->getValue(); + $price_floor = $objWorksheet->getCell('I' . $_row)->getValue(); + $delivery_day = $objWorksheet->getCell('J' . $_row)->getValue(); + $delivery_day = intval($delivery_day); + $delivery_day = $delivery_day < 0 ? 0 : $delivery_day; + $price_color = $objWorksheet->getCell('K' . $_row)->getValue(); + $proxy_name = $objWorksheet->getCell('L' . $_row)->getValue(); + $proxy_type = $proxy_name == '票折' ? 1 : 0; + + $proxy_profix_car = $objWorksheet->getCell('M' . $_row)->getValue(); + $profix_car = $objWorksheet->getCell('N' . $_row)->getValue(); + $profix_cardno = $objWorksheet->getCell('O' . $_row)->getValue(); + $profix_insure = $objWorksheet->getCell('P' . $_row)->getValue(); + $profix_loan = $objWorksheet->getCell('Q' . $_row)->getValue(); + + $info = [ + 'brand_id' => $brand_id, + 's_id' => $s_id, + 'v_id' => $v_id, + 'city_id' => $city_id, + 'year' => $year, + 'month' => $month, + 'status' => 1, + 'c_time' => $c_time, + ]; + + $info['price_car'] = $price_car; + $info['price_floor'] = $price_floor; + $info['price_color'] = $price_color; + $info['proxy_type'] = $proxy_type; + $info['proxy_profix_car'] = $proxy_profix_car; + $info['profix_car'] = $profix_car; + $info['profix_carno'] = $profix_cardno; + $info['profix_insure'] = $profix_insure; + $info['profix_loan'] = $profix_loan; + $info['s_effect_time'] = $s_effect_time; + $info['if_effect'] = $if_effect; + $info['delivery_day'] = $delivery_day; + $data[] = $info; + } + $msg_debug .= "count_ignore:{$count_ignore}"; + $done = 0; + $data && $done = $this->auto_business_model->add_batch($data); + @unlink($file); + $this->data['load_num'] = count($data); + $this->data['done'] = $done; + #$this->data['msg_debug'] = $msg_debug; + return $this->show_json(SYS_CODE_SUCCESS, "共{$count}条,成功导入{$done}条"); + } + + private function upload() + { + $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/'; + $config['allowed_types'] = 'xls|xlsx'; + $config['max_size'] = 5120; + $config['file_name'] = 'receiver_clues' . time() . rand(1, 99999); + $this->load->library('upload', $config); + if (!$this->upload->do_upload('file')) { + return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', '')); + } else { + $data = $this->upload->data(); + return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']); + } + } + public function edit() { $info = $this->input->post('info'); diff --git a/admin/views/auto/business/get.php b/admin/views/auto/business/get.php index f6749702..6a21607c 100644 --- a/admin/views/auto/business/get.php +++ b/admin/views/auto/business/get.php @@ -72,7 +72,7 @@ -
+
+ + + diff --git a/www/admin/temp/business.xlsx b/www/admin/temp/business.xlsx new file mode 100644 index 00000000..010444d5 Binary files /dev/null and b/www/admin/temp/business.xlsx differ