Files
liche/api/controllers/wxapp/licheb/Customers.php
T
2021-07-26 10:41:48 +08:00

390 lines
15 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/06/23
* Time: 14:08
*/
require_once APPPATH.'controllers/wxapp/Wxapp.php';
class Customers extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->login_white = array();//登录白名单
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg =array();//授权微信信息
$this->load->model('receiver/receiver_customers_model','customers_model');
$this->load->model('receiver/receiver_customer_oplogs_model','customer_oplogs_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_brand_model');
}
protected function get(){
$id = $this->input_param('id');
if($id){
$where = [
'id' => $id,
];
$row = $this->customers_model->get($where);
if(!$row){
throw new Exception('数据不存在', ERR_PARAMS_ERROR);
}
$admin = $this->app_user_model->get(['id'=>$row['admin_id']],'id,uname');
$brand = $this->auto_brand_model->get(['id'=>$row['brand_id']],'name');
$series = $this->auto_series_model->get(['id'=>$row['s_id']],'name');
$car_json = json_decode($row['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color']['title'] : '';
$version = isset($car_json['version']) ? $car_json['version']['title'] : '';
$tags = [$row['level'].'级用户'];
$status_name = $this->customers_model->get_status();
$tip = $status_name[$row['status']] ? $status_name[$row['status']] : '';
$other_data = [
'品牌车型' => $brand['name'].$series['name'],
'颜色型号' => $color.'-'.$version,
'建卡时间' => date('Y-m-d',$row['c_time']),
//'上次联系' => date('Y-m-d'),
'客户来源' => $row['cf_title'],
'销售顾问' => isset($admin) ? $admin['uname'] : '',
];
$data = [
'id' => $row['id'],
'name' => $row['name'],
'mobile' => '****'.substr($row['mobile'],-4),
'tip' => $tip,
'is_top' => $row['is_top'],
'status' => $row['status'],
'other_data' => $other_data,
'tags' => $tags
];
return $data;
}else{
return $this->lists();
}
}
//获取客户其它信息
protected function get_data(){
$id = $this->input_param('id');
$where = [
'id' => $id
];
$row = $this->customers_model->get($where);
if(!$row){
throw new Exception('数据不存在', ERR_PARAMS_ERROR);
}
$data['baseinfo'] = [
'客户姓名' => $row['name'],
'客户电话' => mobile_asterisk($row['mobile']),
];
return $data;
}
//创建客户
protected function post(){
$biz_id = $this->session['biz_id'];
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_attr_model');
$name = $this->input_param('name');
$mobile = $this->input_param('mobile');
$car_id = $this->input_param('car_id'); //品牌车型id
$v_id = $this->input_param('v_id'); //车型id
$color_id = $this->input_param('color_id'); //颜色id
$back_s_id = $this->input_param('b_s_id'); //备选车型
$buy_time = $this->input_param('buy_time'); //预计购车时间
if(!mobile_valid($mobile)) throw new Exception('请输入正确的手机号码', ERR_PARAMS_ERROR);
if(!$name || !$car_id || !$v_id || !$color_id){
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if($this->customers_model->count(['biz_id'=>$biz_id,'mobile'=>$mobile])){
throw new Exception('客户已存在', API_CODE_FAIL);
}
$row = $this->auto_series_model->get(['id'=>$car_id]);
$where = [
"id in ($v_id,$color_id)" => null
];
$attr_row = $this->auto_attr_model->map('id','',$where);
if($attr_row[$color_id]){
$color_row = $attr_row[$color_id][0];
$color_row['jsondata'] = json_decode($color_row['jsondata'],true);
}
if($attr_row[$v_id]){
$version_row = $attr_row[$v_id][0];
$version_row['jsondata'] = json_decode($version_row['jsondata'],true);
}
$car_json = [
'c_id' => $color_id,
'v_id' => $v_id,
'back_s_id' => $back_s_id,
'color' => isset($color_row) ? $color_row : '',
'version' => isset($version_row) ? $version_row : ''
];
$this->load->library('receiver/customers_entity');
$level = $this->customers_entity->cal_level(strtotime($buy_time),time());
$add_data = [
'name' => $name,
'mobile' => $mobile,
'biz_id' => $biz_id,
'brand_id' => $row['brand_id'],
's_id' => $row['id'],
'v_id' => $v_id,
'admin_id' => $this->session['uid'],
'level' => $level,
'cf_title' => '自行到店',
'car_json' => json_encode($car_json,JSON_UNESCAPED_UNICODE),
'c_time' => time()
];
$buy_time && $add_data['buy_time'] = date('Y-m-d H:i:s',strtotime($buy_time));
$result = $this->customers_model->add($add_data);
if($result){
$uname = $this->session['uname'];
$this->load->library('receiver/customers_entity');
$this->customers_entity->add_log($result,$this->session['uid'],$uname,"{$uname}】创建客户");
throw new Exception('创建成功', API_CODE_SUCCESS);
}else{
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
}
//修改客户
protected function put(){
$uid = $this->session['uid'];
$id = $this->input_param('id');
$status = $this->input_param('status');
$t_num = $this->input_param('t_num');
$a_num = $this->input_param('a_num');
$is_top = $this->input_param('is_top');
$row = $this->customers_model->get(['id'=>$id]);
if(!$row){
throw new Exception('数据不存在', ERR_PARAMS_ERROR);
}
$up_data = [];
//变成到店
if(!$row['admin_id'] && $status==1){
$res = $this->customers_model->update(['admin_id'=>$uid],['id'=>$id]);
if($res){
$row['admin_id'] = $uid;
//更新线索跟进人
$this->load->model('receiver/receiver_clues_model','clues_model');
$this->clues_model->update(['admin_id'=>$uid,'status'=>2],['id'=>$row['rid']]);
}
}
if($row['admin_id']!=$uid){
throw new Exception('无法操作该客户', ERR_PARAMS_ERROR);
}
strlen($status) && $up_data['status'] = $status;
strlen($is_top) && $up_data['is_top'] = $is_top;
$t_num && $up_data['t_num = t_num+1'] = null;
$a_num && $up_data['a_num = a_num+1'] = null;
$result = true;
if($up_data){
$result = $this->customers_model->update($up_data,['id'=>$id]);
if($result){ //添加日志
$this->load->library('receiver/customers_entity');
$log = '';
if(strlen($status)){ //变更状态
$status_name = $this->customers_model->get_status();
$log .= '状态变更为'.$status_name[$status];
}
if($t_num){
$msg = '试驾+1';
$log = $log ? $log.','.$msg : $msg;
}
if($a_num){
$msg = '到店+1';
$log = $log ? $log.','.$msg : $msg;
}
$this->customers_entity->add_log($id,$uid,$this->session['uname'],$log);
}
}
if($result){
throw new Exception('修改成功', API_CODE_SUCCESS);
}else{
throw new Exception('修改失败', ERR_PARAMS_ERROR);
}
}
//订单列表头部
protected function get_tabs(){
$rows = $this->customers_model->get_status();
$lists = [];
if($rows){
foreach($rows as $key=>$val){
if($key!=-1){
$lists[] = [
'key' => $key,
'name' => $val
];
}
}
}
return $lists;
}
//获取筛选条件
protected function get_filter(){
$this->load->library('receiver/customers_entity');
$level = $this->customers_entity->get_level();
$cfrom = ['自行到店','平台分配'];
$data = [
'level' => $level,
'cfrom' => $cfrom
];
return $data;
}
//获取客户列表
private function lists(){
$uid = $this->session['uid'];
$group_id = $this->session['group_id'];
$biz_id = $this->session['biz_id'];
$s_time = $this->input_param('s_time');
$e_time = $this->input_param('e_time');
$if_driver = $this->input_param('if_driver');
$level = $this->input_param('level');
$s_id = $this->input_param('s_id'); //车系id
$v_id = $this->input_param('v_id'); //车型级别id
$cfrom = $this->input_param('cfrom'); //客户来源id
$status = $this->input_param('status'); //状态
$o_type = $this->input_param('o_type'); //排序
$page = $this->input_param('page');
$size = $this->input_param('size');
$istop = $this->input_param('istop');
$iscall = $this->input_param('iscall');
$unuse = $this->input_param('unuse'); //未派客户
!$page && $page = 1;
!$size && $size = 10;
if($o_type==1){ //创建时间排序
$orderby = 'c_time desc';
}elseif($o_type==2){//最近联系
$orderby = 'cont_time desc';
}else{ //特别关注
$orderby = 'is_top desc,id desc';
}
$where = [
'biz_id' => $biz_id
];
if($group_id==1){ //销售
$where ["admin_id=$uid or admin_id=0"] = null;
}
if($s_time && $e_time){
$where['c_time >='] = strtotime($s_time);
$where['c_time <='] = strtotime(date('Y-m-d 23:59:59',strtotime($e_time)));
}
if(strlen($iscall)){
if($iscall){
$where['cont_time!='] = '0000-00-00 00:00:00';
}else{
$where['cont_time'] = '0000-00-00 00:00:00';
}
}
$unuse && $where['admin_id'] = 0;
strlen($istop) && $where['is_top'] = $istop;
strlen($if_driver) && $where['if_driver'] = 1;
strlen($status) && $where['status'] = $status;
$level && $where['level'] = $level;
$cfrom && $where['cf_title'] = $cfrom;
$count = $this->customers_model->count($where);
$lists = [];
if($count){
$fileds = 'id,name,admin_id,mobile,level,car_json,is_top,cf_title,brand_id,s_id,c_time';
$rows = $this->customers_model->select($where,$orderby,$page,$size,$fileds);
//获取管理员
$admin_arr = array_unique(array_column($rows,'admin_id'));
$admin_ids = implode(',',$admin_arr);
$admins = [];
if($admin_ids){
$where = [
"id in ({$admin_ids})" => null
];
$admins = $this->app_user_model->map('id','',$where,'','','','id,uname');
}
//品牌车型
$brand_arr = array_unique(array_column($rows,'brand_id'));
$brands = $this->auto_brand_model->get_map_by_ids($brand_arr,'id,name');
//车系车型
$series_arr = array_unique(array_column($rows,'s_id'));
$series = $this->auto_series_model->get_map_by_ids($series_arr,'id,name');
foreach($rows as $key => $val){
$car_json = json_decode($val['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color']['title'] : '';
$version = isset($car_json['version']) ? $car_json['version']['title'] : '';
$brand_name = isset($brands[$val['brand_id']]) ? $brands[$val['brand_id']][0]['name'] : '';
$serie_name = isset($series[$val['s_id']]) ? $series[$val['s_id']][0]['name'] : '';
$other_data = [
'品牌车型' => $brand_name.$serie_name,
'颜色型号' => $color.'-'.$version,
'建卡时间' => date('Y-m-d',$val['c_time']),
//'上次联系' => date('Y-m-d'),
'客户来源' => $val['cf_title'],
'销售顾问' => isset($admins[$val['admin_id']]) ? $admins[$val['admin_id']][0]['uname'] : '',
];
$tags = [$val['level'].'级用户'];
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => mobile_asterisk($val['mobile']),
'is_top' => $val['is_top'],
'other_data' => $other_data,
'tags' => $tags
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
//派单给店员
protected function put_admins(){
$uname = $this->session['uname'];
$uid = $this->session['uid'];
$id_arr = $this->input_param('ids');
$admin_id = $this->input_param('admin_id');
$admin = $this->app_user_model->get(['id'=>$admin_id,'status'=>1]);
if (!$id_arr || !$admin) {
throw new Hd_exception('参数错误', API_CODE_INVILD_PARAM);
}
$ids = implode(',', $id_arr);
$ret = $this->customers_model->update(['admin_id' => $admin_id], ["id in ({$ids})" => null]);
if (is_bool($ret)) {
debug_log("[error]# " . $this->customers_model->db->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('分派失败', API_CODE_FAIL);
}
//写日志
$this->load->model('receiver/receiver_clues_model','clues_model');
$this->load->library('receiver/customers_entity');
$customers = $this->customers_model->get_map_by_ids ($id_arr,'id,rid');
foreach($id_arr as $val){
$rid = $customers[$val][0]['rid']; //线索id
if($rid){ //更新线索跟进人
$this->clues_model->update(['admin_id'=>$admin_id,'status'=>2],['id'=>$rid]);
}
$log = "{$uname}】分配客户";
$this->customers_entity->add_log($val,$uid,$uname,$log);
}
throw new Exception('分配成功', API_CODE_SUCCESS);
}
}