diff --git a/admin/controllers/receiver/Clues.php b/admin/controllers/receiver/Clues.php index 78803713..3309c028 100644 --- a/admin/controllers/receiver/Clues.php +++ b/admin/controllers/receiver/Clues.php @@ -4,8 +4,13 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class Clues extends HD_Controller{ private $searchTpAry = array('mobile' => '客户手机号', 'name' => '客户姓名'); + protected $log_dir; + public function __construct(){ parent::__construct(); + + $this->log_dir = 'receiver_clues'; + $this->load->model('receiver/receiver_clues_model','clues_model'); $this->load->model('receiver/receiver_clues_cfrom_model','clues_cfrom_model'); $this->load->model('receiver/receiver_status_model','status_model'); @@ -70,10 +75,107 @@ class Clues extends HD_Controller{ } public function get(){ + $id = $this->input->get('id'); + + if($id){ + $row = $this->clues_model->get(array('id' => $id)); + if(!$row){ + return $this->show_json(SYS_CODE_FAIL, '记录不存在'); + } + + $info = array( + 'name' => $row['name'], + 'mobile' => $row['mobile'], + 'cf_id' => $row['cf_id'], + ); + $title = "编辑线索"; + $action = "edit"; + } else { + $info = array( + 'name' => '', + 'mobile' => '', + 'cf_id' => '', + ); + $title = "新增线索"; + $action = "add"; + } + + $where = array('status' => 1); + $select = 'id, title'; + $map_cfrom = $this->clues_cfrom_model->map('id', 'title', $where, '', 0, 0, $select); + + $this->data['info'] = $info; + $this->data['cfrom_ary'] = $map_cfrom; + $this->data['action'] = $action; + + $this->data['_title'] = $title; + return $this->show_view('receiver/clues/get'); } //添加单条数据 public function add(){ + $info = $this->input->post('info'); + + if(!$info['name']){ + return $this->show_json(SYS_CODE_FAIL, '请填写姓名'); + } + + if(! mobile_valid($info['mobile'])){ + return $this->show_json(SYS_CODE_FAIL, '手机号码不准确'); + } + + $add = array( + 'name' => $info['name'], + 'mobile' => $info['mobile'], + 'cf_id' => $info['cf_id'] ? $info['cf_id'] : 0, + 'c_time' => time(), + ); + $ret = $this->clues_model->add($add); + if(!$ret){ + debug_log("[error]# add fail; " . $this->clues_model->db->last_query(), __FUNCTION__, $this->log_dir); + return $this->show_json(SYS_CODE_FAIL, '添加失败'); + } + + 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(); + for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容 + if ($objWorksheet->getCell('A' . $_row)->getValue()) { + $data[] = array( + 'name' => $objWorksheet->getCell('A' . $_row)->getValue(), + 'mobile' => $objWorksheet->getCell('B' . $_row)->getValue(), + 'cfrom' => $objWorksheet->getCell('C' . $_row)->getValue(), + ); + } + } + $data && $data = array_unique($data); //去除重复数据 + $done = $this->add_batch($data); + @unlink($file); + $this->data['load_num'] = count($data); + $this->data['done'] = $done; + return $this->show_json(SYS_CODE_SUCCESS, "成功导入{$done}条"); } //编辑单条数据 @@ -160,4 +262,59 @@ class Clues extends HD_Controller{ return $this->show_json(SYS_CODE_SUCCESS, '分配成功!'); } + 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']); + } + } + + /** + * 批量新增 + * @param $lists + * @return int + */ + private function add_batch($lists){ + $done = 0; + $adds = array(); + + $where = array('status' => 1); + $select = 'id, title'; + $map_cfrom = $this->clues_cfrom_model->map('title', 'id', $where, '', 0, 0, $select); + foreach($lists as $v){ + if(!$v['name']){ + continue; + } + if(!mobile_valid($v['mobile'])){ + continue; + } + $cf_id = $map_cfrom[$v['cfrom']]; + $adds[] = array( + 'name' => $v['name'], + 'mobile' => $v['mobile'], + 'cf_id' => $cf_id ? $cf_id : 0, + 'c_time' => time(), + ); + $done++; + } + + if($adds){ + $ret = $this->clues_model->add_batch($adds); + if(!$ret){ + debug_log("[error] add_batch fail; " . $this->clues_model->db->last_query(), __FUNCTION__, $this->log_dir); + $done = 0; + } + } + + return $done; + } + } diff --git a/admin/views/receiver/clues/get.php b/admin/views/receiver/clues/get.php new file mode 100644 index 00000000..a1034cb1 --- /dev/null +++ b/admin/views/receiver/clues/get.php @@ -0,0 +1,88 @@ +
+ diff --git a/admin/views/receiver/clues/lists.php b/admin/views/receiver/clues/lists.php index b9056ff6..c749c965 100644 --- a/admin/views/receiver/clues/lists.php +++ b/admin/views/receiver/clues/lists.php @@ -59,14 +59,12 @@