Files
liche/admin/controllers/receiver/Clues.php
T
2021-07-22 10:01:41 +08:00

322 lines
12 KiB
PHP

<?php
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_customers_model','customers_model');
$this->load->model('app/licheb/app_licheb_users_model');
$this->load->model("biz/biz_model");
}
//首页信息
public function index(){
$this->lists();
}
public function lists($where = array()){
$status_arr = $this->clues_model->get_status();
$params = $this->input->get();
$page = $params['page'] = $params['page'] ? intval($params['page']) : 1;
$size = $params['size'] = $params['size'] ? intval($params['size']) : 20;
if ($params['title']) {
$where["{$params['search_tp']} like '%{$params['title']}%'"] = null;
}
//创建时间
if ($params['c_time']) {
$c_time = explode(' ~ ', $params['c_time']);
if ($c_time[0]) {
$where["c_time >="] = strtotime($c_time[0] . ' 00:00:00');
$where_yx["c.c_time >="] = strtotime($c_time[0] . ' 00:00:00');
}
if ($c_time[1]) {
$where["c_time <="] = strtotime($c_time[1] . ' 23:59:59');
$where_yx["c.c_time <="] = strtotime($c_time[1] . ' 23:59:59');
}
}
strlen($params['status']) && $where["status"] = $params['status'];
$count = $this->clues_model->count($where);
$lists = [];
if($count){
$fileds = 'id,name,mobile,cf_id,status_id,c_time,admin_id,status';
$rows = $this->clues_model->select($where,'id desc',$page,$size,$fileds);
//获取来源
$cf_id_arr = array_unique(array_column($rows,'cf_id'));
$cf_rows = $this->clues_cfrom_model->get_map_by_ids($cf_id_arr,'id,title');
//获取销售员
$admin_id_arr = array_unique(array_column($rows,'admin_id'));
$admin_rows = $this->app_licheb_users_model->get_map_by_ids($admin_id_arr,'id,uname');
foreach($rows as $key => $val){
$val['cf_title'] = isset($cf_rows[$val['cf_id']]) ? $cf_rows[$val['cf_id']][0]['title'] : '';
$val['status_name'] = $status_arr[$val['status']];
$val['admin_name'] = isset($admin_rows[$val['admin_id']]) ? $admin_rows[$val['admin_id']][0]['uname'] : '';
$lists[] = $val;
}
}
$this->data['lists'] = $lists;
$this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
$this->data['searchTpAry'] = $this->searchTpAry;
$this->data['params'] = $params;
$this->data['status_arr'] = $status_arr;
$this->data['_title'] = '线索池列表';
return $this->show_view('receiver/clues/lists', true);
}
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}");
}
//编辑单条数据
public function edit(){
}
//删除单条数据
public function del(){
}
//批量操作(默认修改状态)
public function batch(){
}
//导出数据列表
public function export(){
}
//分配
public function get_adviser()
{
$ids = $this->input->get('ids');
$where = [
'status' => 1,
];
$bizList = $this->biz_model->select($where,'','','','id,biz_name');
$this->data['bizList'] = $bizList;
$this->data['ids'] = $ids;
return $this->show_view('receiver/clues/get_adviser');
}
public function edit_adviser()
{
$biz_id = $this->input->post('biz_id');
$ids = $this->input->post('ids');
$ids_arr = explode(',',$ids);
$biz = $this->biz_model->get(['id'=>$biz_id]);
if (!$biz) {
return $this->show_json(SYS_CODE_FAIL, '请选择分配门店!');
}
if (!is_array($ids_arr)) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$where = [
"id in ($ids)" => null
];
$clues_rows = $this->clues_model->map('id','',$where);
foreach($ids_arr as $val){
$clues_row = $clues_rows[$val][0];
//判断是否已分配
$cus_row = $this->customers_model->get(['rid'=>$val,'status != 3'=>null]);
if($clues_row && !$cus_row){
$jsondata = json_decode($clues_row['jsondata'],true);
$add_data = [
'rid' => $val,
'name' => $clues_row['name'],
'mobile' => $clues_row['mobile'],
'biz_id' => $biz_id,
'brand_id' => $clues_row['brand_id'],
's_id' => $clues_row['s_id'],
'if_driver' => $clues_row['if_driver'],
'cf_title' => '后台分配',
'p_time' => date('Y-m-d H:i:s'),
'c_time' => time()
];
if($jsondata['car']){
isset($jsondata['car']['version']) && $add_data['v_id'] = $jsondata['car']['version']['id'];
$add_data['car_json'] = json_encode($jsondata['car'],JSON_UNESCAPED_UNICODE);
unset($jsondata['car']);
}
$jsondata && $add_data['jsondata'] = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
$customers_id = $this->customers_model->add($add_data);
if($customers_id){
$this->clues_model->update(['status'=>1],['id'=>$val]);
$this->load->library('receiver/clues_entity');
$this->clues_entity->add_log($val,$this->uid,$this->username,"分配给【{$biz['biz_name']}】商家");
$this->load->library('receiver/customers_entity');
$this->customers_entity->add_log($customers_id,$this->uid,$this->username,"后台分配",0,'admin');
}
}
}
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;
}
}