Files
liche/api/controllers/wxapp/licheb/Customers.php
T
2022-04-19 16:01:10 +08:00

918 lines
38 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('receiver/receiver_customer_tag_model', 'mdCustomerTag');
$this->load->model('receiver/receiver_customer_tagdata_model', 'mdCustomerTagdata');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_attr_model');
$this->load->model("biz/biz_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']),
'客户来源' => $this->get_cfTitle($row),
'销售顾问' => isset($admin) ? $admin['uname'] : '',
];
$other_data['销售顾问'] = isset($admin) ? $admin['uname'] : '';
$row['cont_time'] != '0000-00-00 00:00:00' && $other_data['上次联系'] = date('Y-m-d', strtotime($row['cont_time']));
$data = [
'id' => $row['id'],
'name' => $row['name'],
'mobile' => $this->get_mobile(['mobile' => $row['mobile'], 'cf_title' => $row['cf_title']]),
'complete_mobile' => $row['mobile'],
'tip' => $tip,
'is_top' => $row['is_top'],
'status' => $row['status'],
'other_data' => $other_data,
'is_weChat' => $row['unionid'] ? true : false,
'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);
}
//$car_json = json_decode($row['car_json'], true);
$of_title = $row['of_id'] ? $this->get_cfTitle($row) : '';
$data['baseinfo'] = [
'name' => ['value' => $row['name'], 'cn' => '客户姓名'],
'mobile' => ['value' => $this->get_mobile(['mobile' => $row['mobile'], 'cf_title' => $row['cf_title']]), 'cn' => '客户电话'],
//'brand_id' => ['value' => intval($row['brand_id']), 'cn' => '车辆品牌'],
//'car_id' => ['value' => intval($row['s_id']), 'cn' => '车辆车系'],
//'v_id' => ['value' => intval($row['v_id']), 'cn' => '车型级别'],
//'color_id' => ['value' => intval($car_json['c_id']), 'cn' => '车型颜色'],
'of_id' => ['value' => $of_title, 'of_id' => intval($row['of_id']), 'of2_id' => intval($row['of2_id']), 'cn' => '线索来源'],
'buy_time' => ['value' => $row['buy_time'], 'cn' => '预计购车时间'],
];
return $data;
}
protected function get_tag()
{
$id = intval($this->input_param('id'));
$show = $res_td = [];
$res = $this->mdCustomerTag->select(['status' => 1, 'pid' => 0], 'sort desc,id desc', 0, 0, 'id,name,type');
if ($res) {
if ($id) {
$res_td = $this->mdCustomerTagdata->select(['c_id' => $id], 'id desc', 0, 0, 't_id');
}
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($res as $key => $val) {
$list = [];
$res2 = $this->mdCustomerTag->select(['status' => 1, 'pid' => $val['id']], 'sort desc,id desc', 0, 0, 'id,name');
foreach ($res2 as $key2 => $val2) {
//检查是否选中标签
$checked = $tag_data && in_array($val2['id'], $tag_data) ? true : false;
$list[] = ['id' => $val2['id'], 'name' => $val2['name'], 'checked' => $checked];
}
$show[] = ['id' => $val['id'], 'name' => $val['name'], 'type' => $val['type'], 'list' => $list];
}
}
return $show;
}
//修改基本信息
protected function put_data()
{
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$id = $this->input_param('cus_id');
$name = $this->input_param('name');
$mobile = $this->input_param('mobile');
$of_id = $this->input_param('of_id'); //线下来源一级
$of2_id = $this->input_param('of2_id'); //线下来源一级
$buy_time = $this->input_param('buy_time'); //预计购车时间
$tag = $this->input_param('tag'); //客户标签
$row = $this->customers_model->get(['id' => $id]);
if (!$row) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$update = [];
if ($row['cf_title'] != '平台分配') {
if ($mobile) {
if (!mobile_valid($mobile)) {
throw new Exception('手机号格式错误', ERR_PARAMS_ERROR);
}
if ($this->customers_model->count(['biz_id' => $biz_id, 'mobile' => $mobile])) {
throw new Exception('客户已存在', API_CODE_FAIL);
}
$update['mobile'] = $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
//$car_json = json_decode($row['car_json'], true);
//$s_row = $this->auto_series_model->get(['id' => $car_id]);
//$ids_arr = [];
//if ($v_id) {
//$car_json['v_id'] = $v_id;
//$ids_arr[] = $v_id;
//}
//if ($color_id) {
//$car_json['c_id'] = $color_id;
//$ids_arr[] = $color_id;
//}
//$attr_row = $this->auto_attr_model->get_map_by_ids($ids_arr);
//if ($attr_row[$color_id]) {
//$color_row = $attr_row[$color_id][0];
//$color_row['jsondata'] = json_decode($color_row['jsondata'], true);
//isset($color_row) && $car_json['color'] = $color_row;
//}
//if ($attr_row[$v_id]) {
//$version_row = $attr_row[$v_id][0];
//$version_row['jsondata'] = json_decode($version_row['jsondata'], true);
//isset($version_row) && $car_json['version'] = $version_row;
//}
//$update['car_json'] = json_encode($car_json, JSON_UNESCAPED_UNICODE);
// $v_id && $update['v_id'] = $v_id;
//if ($s_row) {
//$update['s_id'] = $s_row['id'];
//$update['brand_id'] = $s_row['brand_id'];
//}
$name && $update['name'] = $name;
$of_id && $update['of_id'] = $of_id;
$of2_id && $update['of2_id'] = $of2_id;
if ($buy_time) {
$this->load->library('receiver/customers_entity');
$update['level'] = $this->customers_entity->cal_level($buy_time);
$update['buy_time'] = $buy_time;
}
$update && $this->customers_model->update($update, ['id' => $id]);
//客户标签
if ($tag) {
$add_tag = [];
//查找已加入标签
$res_td = $this->mdCustomerTagdata->select(['c_id' => $id], 'id desc', 0, 0, 't_id');
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($tag as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val2['checked'] == true) {
if (!$tag_data || !in_array($val2['id'], $tag_data)) {//未加标签,新增
$add_tag[] = ['c_id' => $id, 't_id' => $val2['id'], 'c_time' => time()];
}
} else {
if ($tag_data && in_array($val2['id'], $tag_data)) {//删除标签
$this->mdCustomerTagdata->delete(['c_id' => $id, 't_id' => $val2['id']]);
}
}
}
}
if ($add_tag && count($add_tag)) {
$this->mdCustomerTagdata->add_batch($add_tag);
}
}
$uname = $this->session['uname'];
$this->load->library('receiver/customers_entity');
$this->customers_entity->add_log($id, $this->session['uid'], $uname, "修改用户基本信息");
throw new Exception('保存成功', API_CODE_SUCCESS);
}
//创建客户
protected function post()
{
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$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'); //预计购车时间
$of_id = $this->input_param('of_id'); //线下来源一级
$of2_id = $this->input_param('of2_id'); //线下来源一级
$status = intval($this->input_param('status')); //状态
$tag = $this->input_param('tag'); //客户标签
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);
}
$biz_row = $this->biz_model->get(['id' => $biz_id]);
//$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($buy_time);
$time = date('Y-m-d H:i:s');
$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),
'cont_time' => $time,
'status' => $status,
'c_time' => time()
];
if ($status == 1) {//到店客户 到店次数默认1
$add_data['a_num'] = 1;
$add_data['dt_time'] = $time;
}
$biz_row['city_id'] && $add_data['city_id'] = $biz_row['city_id'];
$biz_row['county_id'] && $add_data['county_id'] = $biz_row['county_id'];
$buy_time && $add_data['buy_time'] = $buy_time;
$of_id && $add_data['of_id'] = $of_id;
$of2_id && $add_data['of2_id'] = $of2_id;
$id = $this->customers_model->add($add_data);
if ($id) {
//客户标签
if ($tag) {
$add_tag = [];
//查找已加入标签
$res_td = $this->mdCustomerTagdata->select(['c_id' => $id], 'id desc', 0, 0, 't_id');
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($tag as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val2['checked'] == true) {
if (!$tag_data || !in_array($val2['id'], $tag_data)) {//未加标签,新增
$add_tag[] = ['c_id' => $id, 't_id' => $val2['id'], 'c_time' => time()];
}
}
}
}
if ($add_tag && count($add_tag)) {
$this->mdCustomerTagdata->add_batch($add_tag);
}
}
$uid = $this->session['uid'];
$uname = $this->session['uname'];
$this->load->library('receiver/customers_entity');
$this->customers_entity->add_log($id, $uid, $uname, "创建客户档案", 3);
if ($status == 1) {//到店客户加日志
$this->customers_entity->add_log($id, $uid, $uname, "客户到店", 4);
}
throw new Exception('创建成功', API_CODE_SUCCESS);
} else {
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
}
//修改客户
protected function put()
{
$uid = $this->session['uid'];
$uname = $this->session['uname'];
$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');
$defeat_reason = $this->input_param('defeat_reason');
$row = $this->customers_model->get(['id' => $id]);
if (!$row) {
throw new Exception('数据不存在', ERR_PARAMS_ERROR);
}
$up_data = [];
//变成到店
if (!$row['admin_id'] && $status == 1) {
$up_data['admin_id'] = $uid;
}
if ($row['admin_id'] != $uid) {
throw new Exception('无法操作该客户', ERR_PARAMS_ERROR);
}
$log = $a_log = '';
if ($status == 3) {
if (!$defeat_reason) {
throw new Exception('请输入战败理由', ERR_PARAMS_ERROR);
}
$jsondata = $row['jsondata'] ? json_decode($row['jsondata'], true) : array();
$jsondata['defeat']['time'] = date("Y-m-d H:i:s");
$jsondata['defeat']['reason'] = $defeat_reason;
$up_data['if_defeat'] = 1;
$up_data['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
$log = '申请战败:' . $defeat_reason;
} else {
if (strlen($status)) { //变更状态
$up_data['status'] = $status;
$status_name = $this->customers_model->get_status();
$log = '状态变更为' . $status_name[$status];
}
}
strlen($is_top) && $up_data['is_top'] = $is_top;
if ($a_num || $t_num) {
$day = date('Y-m-d');
if ($a_num) {//判断今日是否到店
$re = $this->customer_oplogs_model->get(['customer_id' => $id, 'type' => 4,
'c_time >=' => strtotime($day . ' 00:00:00'), 'c_time <=' => strtotime($day . ' 23:59:59')]);
$re && $a_num = 0;
}
if ($t_num) {//判断今日是否试驾
$re = $this->customer_oplogs_model->get(['customer_id' => $id, 'type' => 5,
'c_time >=' => strtotime($day . ' 00:00:00'), 'c_time <=' => strtotime($day . ' 23:59:59')]);
$re && $t_num = 0;
}
}
$a_num && $up_data['a_num = a_num+1'] = null;
$t_num && $up_data['t_num = t_num+1'] = null;
$result = true;
if ($up_data) {
$a_log = '';//到店
if ($status == 1 || $a_num) {
$a_log = '客户到店';
$up_data['cont_time'] = date('Y-m-d H:i:s'); //修改到店状态修改最后联系时间
if ($row['dt_time'] == '0000-00-00 00:00:00') {//首次到店时间
$up_data['dt_time'] = date('Y-m-d H:i:s');
}
}
$result = $this->customers_model->update($up_data, ['id' => $id]);
if ($result) { //添加日志
$this->load->library('receiver/customers_entity');
if ($a_log) {//到店
$this->customers_entity->add_log($id, $uid, $uname, $a_log, 4);
}
if ($t_num) {//试驾
$this->customers_entity->add_log($id, $uid, $uname, '客户试驾', 5);
}
if ($log) {//小记
$this->customers_entity->add_log($id, $uid, $uname, $log, 0);
}
if ($status == 2) {//变成订单客户 更新客户已回访
$this->customers_entity->add_log_visit($id, $uid, $uname, '', 0, 1);
}
}
}
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()
{
$level = $this->customers_model->get_sdata('level');
$cfrom = $this->customers_model->get_sdata();
$buy_time = $this->customers_model->get_sdata('btime');
$show_btime = [];
foreach ($buy_time as $key => $val) {
$show_btime[] = ['id' => $key, 'name' => $val];
}
$data = [
'level' => $level,
'cfrom' => $cfrom,
'buy_time' => $show_btime
];
return $data;
}
/**
* Notes:线下来源
* Created on: 2022/3/7 10:01
* Created by: dengbw
* @return mixed
*/
protected function get_offline_sources()
{
$status = $this->input_param('status');
$offline_sources = $this->customers_model->offlineSources();
if (strlen($status) && $status == 0) {//未见客户
unset($offline_sources[1]);
}
$show = [];
foreach ($offline_sources as $key => $val) {
$list = [];
foreach ($val['list'] as $key2 => $val2) {
$list[] = ['id' => $key2, 'name' => $val2];
}
$show[] = ['id' => $key, 'name' => $val['name'], 'list' => $list];
}
return $show;
}
//获取客户列表
private function lists()
{
$visit = $this->input_param('visit');
if ($visit) {//待回访客户
return $this->visit_lists($this->input_param());
}
$uid = $this->session['uid'];
$group_id = $this->session['group_id'];
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($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');
$brand_id = $this->input_param('brand_id');
//$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'); //未派客户
$ismy = $this->input_param('ismy'); //是否只显示自己
$name = $this->input_param('name');
$mobile = $this->input_param('mobile');
//$cf_clues = $this->input_param('cf_clues');//线索来源
$admin_id = $this->input_param('admin_id');
$id = $this->input_param('cus_id');
$a_id = intval($this->input_param('a_id'));//私域通活动id
$of_id = intval($this->input_param('of_id'));//线下来源一级
$of2_id = intval($this->input_param('of2_id'));//线下来源二级
!$page && $page = 1;
!$size && $size = 10;
if ($o_type == 1) { //创建时间排序
$orderby = 'c_time desc';
} elseif ($o_type == 2) {//最近联系
$orderby = 'cont_time desc';
} else { //特别关注
if ($group_id == 1) {
$orderby = 'is_top desc,c_time desc';
} else {
$orderby = 'c_time desc';
}
}
$where = [
'biz_id' => $biz_id,
'status>=' => 0
];
if ($of_id) {
$where["of_id"] = $of_id;
}
if ($of2_id) {
$where["of2_id"] = $of2_id;
}
if ($a_id) {
$where["cf_id"] = 35;
$where["t_id"] = $a_id;
}
if ($group_id == 1 || $ismy) {
$where["admin_id"] = $uid;
}
if ($group_id == 4 && $biz_id != 1) {
$where['brand_id!='] = 3; //渠道经理过滤
}
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;
$admin_id && $where['admin_id'] = $admin_id;
$id && $where['id'] = $id;
strlen($istop) && $where['is_top'] = $istop;
strlen($if_driver) && $where['if_driver'] = 1;
strlen($status) && $where['status'] = $status;
$brand_id && $where['brand_id'] = $brand_id;
$level && $where['level'] = $level;
$cfrom && $where['cf_title'] = $cfrom;
$name && $where["name like '%{$name}%'"] = null;
$mobile && $where["mobile like '%$mobile%'"] = null;
//$cf_clues && $where['cf_clues'] = $cf_clues;
$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,cont_time,c_time,if_defeat
,of_id,of2_id,unionid';
$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');
$allot = $this->get_allot();
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']),
'客户来源' => $this->get_cfTitle($val),
'销售顾问' => isset($admins[$val['admin_id']]) ? $admins[$val['admin_id']][0]['uname'] : '',
];
$val['cont_time'] != '0000-00-00 00:00:00' && $other_data['上次联系'] = date('Y-m-d', strtotime($val['cont_time']));
$tags = [$val['level'] . '级用户'];
$defeat = '';
if ($val['if_defeat'] == 1) {
$defeat = '战败申请中';
} else if ($val['if_defeat'] == 2) {
$defeat = '再战';
}
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $this->get_mobile(['mobile' => $val['mobile'], 'cf_title' => $val['cf_title']]),
'complete_mobile' => $val['mobile'],
'is_top' => $val['is_top'],
'other_data' => $other_data,
'tags' => $tags,
'defeat' => $defeat,
'group_id' => $group_id,
'is_weChat' => $val['unionid'] ? true : false,
'allot' => $allot
];
}
}
$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');
$allot = $this->get_allot();
if ($allot == 0) {
throw new Hd_exception('无权限分配', API_CODE_INVILD_PARAM);
}
$admin = $this->app_user_model->get(['id' => $admin_id, 'status' => 1]);
if (!$id_arr || !$admin) {
throw new Hd_exception('参数错误', API_CODE_INVILD_PARAM);
}
$this->load->library('receiver/customers_entity');
foreach ($id_arr as $val) {
$id = $val;
$re = $this->customers_model->get(['id' => $id]);
if ($re['admin_id'] == $admin_id) {//同个销售跳出循环
continue;
}
$upDate = ['admin_id' => $admin_id];
!$re['sales_id'] && $upDate['sales_id'] = $admin_id;//初始销售id
$ret = $this->customers_model->update($upDate, ["id" => $id]);
if ($ret) {
//写日志
$log = "分配客户";
$this->customers_entity->add_log($id, $uid, $uname, $log);
//发送短信
$num = $re['mobile'] ? substr($re['mobile'], -4) : 0;
send_alisms(array('mobile' => $admin['mobile'], 'template' => 'SMS_226945702', 'param' => ['num' => $num]));
}
}
throw new Exception('分配成功', API_CODE_SUCCESS);
}
/**
* Notes:待回访客户列表
* Created on: 2021/10/21 14:27
* Created by: dengbw
* @param array $params
* @return array
*/
private function visit_lists($params = array())
{
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$group_id = $this->session['group_id'];
$this->load->model('receiver/receiver_customers_visit_model', 'mdCustomersVisit');
$page = $params['page'];
$size = $params['size'];
!$page && $page = 1;
!$size && $size = 10;
$where = array('a.biz_id' => $biz_id, 'b.contact' => 1, 'b.status' => 1);
$group_id == 1 && $where['a.admin_id'] = $this->session['uid'];
$count = $this->mdCustomersVisit->count_visit($where);
$lists = [];
if ($count) {
$fileds = 'a.id,a.name,a.admin_id,a.mobile,a.level,a.car_json,a.is_top,a.cf_title,a.brand_id,a.s_id
,a.cont_time,a.c_time,a.if_defeat';
$rows = $this->mdCustomersVisit->select_visit($where, 'a.id desc', $page, $size, $fileds);
//获取管理员
$admin_arr = array_unique(array_column($rows, 'admin_id'));
$admin_ids = implode(',', $admin_arr);
$admins = [];
if ($admin_ids) {
$admins = $this->app_user_model->map('id', '', ["id in ({$admin_ids})" => null], '', '', '', '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']),
'客户来源' => $val['cf_title'],
'销售顾问' => isset($admins[$val['admin_id']]) ? $admins[$val['admin_id']][0]['uname'] : '',
];
$val['cont_time'] != '0000-00-00 00:00:00' && $other_data['上次联系'] = date('Y-m-d', strtotime($val['cont_time']));
$tags = [$val['level'] . '级用户'];
$defeat = '';
if ($val['if_defeat'] == 1) {
$defeat = '战败申请中';
} else if ($val['if_defeat'] == 2) {
$defeat = '再战';
}
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $this->get_mobile(['mobile' => $val['mobile'], 'cf_title' => $val['cf_title']]),
'complete_mobile' => $val['mobile'],
'is_top' => $val['is_top'],
'other_data' => $other_data,
'tags' => $tags,
'defeat' => $defeat
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
/**
* Notes:战败申请列表
* Created on: 2021/10/21 15:19
* Created by: dengbw
* @return array
* @throws Hd_exception
*/
protected function get_defeats()
{
$group_id = $this->session['group_id'];
if ($group_id == 1) {
return $data = ['list' => [], 'total' => 0];
}
$params = $this->input_param();
$page = $params['page'];
$size = $params['size'];
!$page && $page = 1;
!$size && $size = 10;
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$where = array('biz_id' => $biz_id, 'if_defeat' => 1, 'status>' => -1);
$count = $this->customers_model->count($where);
$lists = [];
if ($count) {
$fileds = 'id,name,mobile,jsondata,cf_title';
$rows = $this->customers_model->select($where, 'id desc', $page, $size, $fileds);
foreach ($rows as $key => $val) {
$jsondata = $val['jsondata'] ? json_decode($val['jsondata'], true) : array();
$reason = $jsondata['defeat']['reason'] ? '战败理由:' . $jsondata['defeat']['reason'] : '';
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $this->get_mobile(['mobile' => $val['mobile'], 'cf_title' => $val['cf_title']]),
'reason' => $reason,
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
/**
* Notes:通过战败申请
* Created on: 2021/10/21 16:19
* Created by: dengbw
* @throws Exception
*/
protected function put_defeats()
{
$group_id = $this->session['group_id'];
if ($group_id == 1) {
throw new Exception('无操作权限', ERR_PARAMS_ERROR);
}
$params = $this->input_param();
$id = intval($params['id']);
$type = intval($params['type']);
if (!$id) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$row = $this->customers_model->get(array('id' => $id));
if (!$row) {
throw new Exception('用户不存在', ERR_PARAMS_ERROR);
}
if (!$row['if_defeat']) {
throw new Exception('未申请战败', ERR_PARAMS_ERROR);
}
$jsondata = $row['jsondata'] ? json_decode($row['jsondata'], true) : array();
if ($type == 1) {
$update['if_defeat'] = 2;//再战
} else {
$def_time = date("Y-m-d H:i:s");
$jsondata['defeat']['pass_time'] = $def_time;
$update['status'] = 3;
$update['if_defeat'] = 0;
$update['def_time'] = $def_time;
}
$update['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
$ret = $this->customers_model->update($update, ['id' => $id]);
if ($ret) {
$this->load->library('receiver/customers_entity');
$uid = $this->session['uid'];
$uname = $this->session['uname'];
if ($type == 1) {
$this->customers_entity->add_log($id, $uid, $uname, '拒绝战败申请', 0);
} else {
//战败
$admin_id = $row['admin_id'] ? $row['admin_id'] : $uid;//战败归属顾问的uid
$this->customers_entity->add_log($id, $admin_id, $uname, '客户战败', 7);
//更新客户已回访
$this->customers_entity->add_log_visit($id, $uid, $uname, '', 0, 1);
}
throw new Exception('操作成功', API_CODE_SUCCESS);
}
throw new Exception('操作失败', ERR_PARAMS_ERROR);
}
/**
* Notes:来源title
* Created on: 2022/3/11 15:53
* Created by: dengbw
* @param $params
* @return string
*/
private function get_cfTitle($params)
{
$title = $params['cf_title'] ? $params['cf_title'] : '';
if ($title == '自有资源' && $params['of_id']) {//自有资源 取线下来源
$of_ary = $this->customers_model->offlineSources()[$params['of_id']];
$title = $of_ary['name'];
$params['of2_id'] && $title .= '-' . $of_ary['list'][$params['of2_id']];
}
return $title;
}
/**
* Notes:显示电话格式
* Created on: 2022/3/9 14:38
* Created by: dengbw
* @param $params
* @return string
*/
private function get_mobile($params)
{
$mobile = $params['mobile'];
if (!$mobile) {
return '';
}
$group_id = $this->session['group_id'];
if ($params['cf_title'] == '自有资源' && ($group_id == 2 || $group_id == 3)) {//2店长,3老板显示全部电话
return $mobile;
}
if ($params['type'] == 1) {
$mobile = '****' . substr($mobile, -4);
} else {
$mobile = mobile_asterisk($mobile);
}
return $mobile;
}
/**
* Notes:判断分配客户权限
* Created on: 2022/3/18 10:02
* Created by: dengbw
* @return int
*/
private function get_allot()
{
$allot = 0;
$group_id = $this->session['group_id'];
if ($group_id == 2 || $group_id == 3) {//店长和老板可再分配用户
$allot = 1;
}
return $allot;
}
}