Files
liche/api/controllers/wxapp/licheb/Customers.php
T
2022-06-10 15:04:39 +08:00

1104 lines
46 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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
{
private $biz_id;
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('receiver/receiver_customers_visit_data_model', 'mdCustomerVisitData');
$this->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders');
$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");
$this->biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
}
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');
$tags = [$row['level'] . '级用户'];
$status_name = $this->customers_model->get_status();
$tip = $status_name[$row['status']] ? $status_name[$row['status']] : '';
$other_data = [
'客户等级' => $row['level'],
'客户来源' => $this->get_cfTitle($row),
'建卡时间' => date('Y-m-d', $row['c_time']),
];
$row['cont_time'] != '0000-00-00 00:00:00' && $other_data['最近联系'] = date('Y-m-d', strtotime($row['cont_time']));
if ($this->session['group_id'] == 1) {
$where_visit = ['c_id' => $id, 'biz_id' => $this->biz_id, 'sales_id' => $this->session['uid']];
} else {
$where_visit = ['c_id' => $id, 'biz_id' => $row['biz_id'], 'sales_id' => $row['admin_id']];
}
$visit_time = $this->get_visit_time($where_visit);
$visit_time && $other_data['计划回访时间'] = $visit_time;
$other_data['销售顾问'] = isset($admin) ? $admin['uname'] : '';
$data = [
'id' => $row['id'],
'name' => $row['name'],
'mobile' => $this->get_mobile(['mobile' => $row['mobile'], 'of_id' => $row['of_id'], 'of2_id' => $row['of2_id']]),
'complete_mobile' => $row['mobile'],
'tip' => $tip,
'is_top' => $row['is_top'],
'status' => $row['status'],
'other_data' => $other_data,
'is_weChat' => $row['unionid'] ? true : false,
'generate_order' => $this->myuid == $row['admin_id'] ? true : false,
'level' => $row['level'],
'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);
}
$of_title = $row['of_id'] ? $this->get_cfTitle($row) : '';
$data['baseinfo'] = [
'name' => ['value' => $row['name'], 'cn' => '客户姓名'],
'mobile' => ['value' => $this->get_mobile(['mobile' => $row['mobile'], 'of_id' => $row['of_id'], 'of2_id' => $row['of2_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, 'show<>' => 1], '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->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->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()
{
$biz_id = $this->biz_id;
$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');//申请战败内容
$level = $this->input_param('level');//客户等级
$content = trim($this->input_param('content'));//小记内容
$content_imgs = $this->input_param('imgs');//小记图片
$follow_channel = $this->input_param('follow_channel');//跟进渠道
$visit_time = $this->input_param('visit_time');//计划回访时间
$row = $this->customers_model->get(['id' => $id]);
if (!$row) {
throw new Exception('数据不存在!', ERR_PARAMS_ERROR);
}
$check_admin_id = true;//检查销售
if ($this->session['group_id'] == 2 || $this->session['group_id'] == 3) {//店长、老板可以操作其他人的客户
$check_admin_id = false;
}
if ($check_admin_id && $row['admin_id'] != $uid) {
throw new Exception('无权限操作该客户!', ERR_PARAMS_ERROR);
}
$up_data = [];
//变成到店
if (!$row['admin_id'] && $status == 1) {
$up_data['admin_id'] = $uid;
}
$log_0 = $log_4 = $log_9 = '';
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_0 = '申请战败:' . $defeat_reason;
} else {
if (strlen($status) && $status != $row['status']) { //变更状态
$up_data['status'] = $status;
$status_name = $this->customers_model->get_status();
$log_9 = '状态变更为' . $status_name[$status];
}
}
strlen($is_top) && $up_data['is_top'] = $is_top;
if ($status == 1 && $status != $row['status']) {//到店客户和数据库不同到店加1
$a_num = 1;
}
$day = date('Y-m-d');
if ($a_num || $t_num) {
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;
$visit_time && $up_data['visit_time'] = $visit_time;
if ($level && $level != $row['level']) {
$up_data['level'] = $level;//更改客户等级
}
$log_4 = '';//到店
if ($a_num) {
$log_4 = $row['status'] == 1 ? '客户再次到店' : '客户到店';
$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 = true;
$this->load->library('receiver/customers_entity');
if ($up_data) {
$result = $this->customers_model->update($up_data, ['id' => $id]);
if ($result) { //添加日志
if ($level && $level != $row['level']) {
$this->customers_entity->add_log($id, $uid, $uname, '更改客户等级', 9);
}
if ($log_9) {//系统变更
$this->customers_entity->add_log($id, $uid, $uname, $log_9, 9);
}
if ($log_4) {//到店
$this->customers_entity->add_log($id, $uid, $uname, $log_4, 4);
}
if ($t_num) {//试驾
$this->customers_entity->add_log($id, $uid, $uname, '客户试驾', 5);
}
if ($log_0) {//小记
$this->customers_entity->add_log($id, $uid, $uname, $log_0, 0);
}
// if ($status == 2) {//变成订单客户 更新客户已回访
// $this->customers_entity->add_log_visit($id, $uid, $uname, '', 0, 1);
// }
}
}
if ($content) {//加小记
$follow_channel && $content = "跟进渠道($follow_channel)" . $content;
if ($visit_time) {
$content .= '。计划回访时间:' . $visit_time;
$sales_id = $row['admin_id'] ? $row['admin_id'] : $uid;//有销售id,回访归属销售
$where_vis = ['c_id' => $id, 'biz_id' => $biz_id, 'sales_id' => $sales_id, 't_day' => $visit_time];
$re_vis = $this->mdCustomerVisitData->get($where_vis);
if (!$re_vis) {
$where_vis['level'] = $level;
$where_vis['c_time'] = time();
$this->mdCustomerVisitData->add($where_vis);
}
}
$result = $this->customers_entity->add_log_visit($id, $uid, $uname, $content, 0, 1, $content_imgs);
}
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();
$follow_channel = $this->customers_model->get_sdata('follow_channel');
$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,
'follow_channel' => $follow_channel
];
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->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');
$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'));//线下来源二级
$s_visit_time = $this->input_param('s_visit_time');//回访开始时间
$e_visit_time = $this->input_param('e_visit_time');//回访结束时间
$status_tp = intval($this->input_param('status_tp')); //状态类型
!$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 ($s_visit_time && $e_visit_time) {
$where['visit_time >='] = $s_visit_time;
$where['visit_time <='] = $e_visit_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';
$status_tp = 1;
}
}
if ($status_tp == 1) {
$where['status in(0,1)'] = null;
}
$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;
$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,status,biz_id';
$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');
}
$allot = $this->get_allot();
$status_name = $this->customers_model->get_status();
foreach ($rows as $key => $val) {
$other_data = [
'客户来源' => $this->get_cfTitle($val),
'建卡时间' => date('Y-m-d', $val['c_time']),
];
$val['cont_time'] != '0000-00-00 00:00:00' && $other_data['最近联系'] = date('Y-m-d', strtotime($val['cont_time']));
if ($group_id == 1) {
$where_visit = ['c_id' => $val['id'], 'biz_id' => $this->biz_id, 'sales_id' => $this->session['uid']];
} else {
$where_visit = ['c_id' => $val['id'], 'biz_id' => $val['biz_id'], 'sales_id' => $val['admin_id']];
}
$visit_time = $this->get_visit_time($where_visit);
$visit_time && $other_data['计划回访时间'] = $visit_time;
$other_data['销售顾问'] = isset($admins[$val['admin_id']]) ? $admins[$val['admin_id']][0]['uname'] : '';
$tags = [$val['level'] . '级用户'];
$defeat = $orders_pay = '';
if ($val['if_defeat'] == 1) {
$defeat = '战败申请中';
} else if ($val['if_defeat'] == 2) {
$defeat = '再战';
}
//查找订单未支付
if ($status == 0 || $status == 1) {
$orders_count = $this->mdOrders->count(['status' => 0, 'order_time' => '0000-00-00 00:00:00', 'customer_id' => $val['id']]);
$orders_count && $orders_pay = '订单未支付';
}
$tip = $status_name[$val['status']] ? $status_name[$val['status']] : '';
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $this->get_mobile(['mobile' => $val['mobile'], 'of_id' => $val['of_id'], 'of2_id' => $val['of2_id']]),
'complete_mobile' => $val['mobile'],
'is_top' => $val['is_top'],
'other_data' => $other_data,
'tags' => $tags,
'defeat' => $defeat,
'orders_pay' => $orders_pay,
'group_id' => $group_id,
'is_weChat' => $val['unionid'] ? true : false,
'allot' => $allot,
'tip' => $tip,
'level' => $val['level'],
];
}
}
$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');
$visit_time = $this->input_param('visit_time');
$allot = $this->get_allot();
if ($allot == 0) {
throw new Hd_exception('无权限分配', API_CODE_INVILD_PARAM);
}
if (!$visit_time) {
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, 'visit_time' => $visit_time];
!$re['sales_id'] && $upDate['sales_id'] = $admin_id;//初始销售id
$ret = $this->customers_model->update($upDate, ["id" => $id]);
if ($ret) {
//写日志
$this->customers_entity->add_log($id, $uid, $uname, "分配客户");
$biz_id = $re['biz_id'];
$level = $re['level'];
$where_vis = ['c_id' => $id, 'biz_id' => $biz_id, 'sales_id' => $admin_id, 't_day' => $visit_time];
$re_vis = $this->mdCustomerVisitData->get($where_vis);
if (!$re_vis) {
//删除未回访计划
$this->mdCustomerVisitData->delete(['c_id' => $id, 'biz_id' => $biz_id, 'status<>' => 2, 't_day >=' => $visit_time]);
$where_vis['level'] = $level;
$where_vis['c_time'] = time();
$this->mdCustomerVisitData->add($where_vis);
$this->customers_entity->add_log_visit($id, $uid, $uname, '计划回访时间:' . $visit_time, 0, 1);
}
//发送短信
$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:客户待回访tab
* Created on: 2022/6/2 10:20
* Created by: dengbw
* @return array
*/
protected function get_visit_tabs()
{
$status = $this->input_param('status');
$tabs = [];
if ($status == 1) {//跟进客户
$tabs = [['name' => '本日新增跟进', 'id' => 1], ['name' => '逾期未跟进', 'id' => 3]];
}
return ['list' => $tabs];
}
/**
* Notes:客户回访记录列表
* Created on: 2021/10/21 14:27
* Created by: dengbw
* @param array $params
* @return array
*/
private function visit_lists($params = [])
{
$group_id = $this->session['group_id'];
$page = intval($params['page']);
$size = intval($params['size']);
$status = intval($params['status']);
$tab_id = intval($params['visit_tab_id']);
!$page && $page = 1;
!$size && $size = 10;
$t_day = date('Y-m-d');
$where = array('a.biz_id' => $this->biz_id, 'a.status in(0,1)' => null, 'b.t_day' => $t_day);
$params['level'] && $where['a.level'] = $params['level'];//等级
if ($status == 2) {//已跟进
$where['b.status'] = 2;
} else {
if ($tab_id) {
$where['b.status'] = $tab_id;
} else {
$where['b.status<>'] = 2;
}
}
$group_id == 1 && $where['a.admin_id'] = $this->session['uid'];
$count = $this->mdCustomerVisitData->count_visit($where);
$lists = [];
if ($count) {
$fileds = 'a.id,a.name,a.admin_id,a.mobile,a.level,a.is_top,a.cont_time,a.c_time,a.if_defeat,a.cf_title
,a.of_id,a.of2_id,a.status,a.biz_id';
$rows = $this->mdCustomerVisitData->select_visit($where, 'a.id desc', $page, $size, $fileds);
//获取管理员
$admin_arr = array_unique(array_column($rows, 'admin_id'));
$admins = [];
if ($admin_arr) {
$admin_ids = implode(',', $admin_arr);
$admins = $this->app_user_model->map('id', '', ["id in ({$admin_ids})" => null], '', '', '', 'id,uname');
}
$status_name = $this->customers_model->get_status();
foreach ($rows as $key => $val) {
$other_data = [
'客户来源' => $this->get_cfTitle($val),
'建卡时间' => date('Y-m-d', $val['c_time']),
];
$val['cont_time'] != '0000-00-00 00:00:00' && $other_data['上次联系'] = date('Y-m-d', strtotime($val['cont_time']));
if ($group_id == 1) {
$where_visit = ['c_id' => $val['id'], 'biz_id' => $this->biz_id, 'sales_id' => $this->session['uid']];
} else {
$where_visit = ['c_id' => $val['id'], 'biz_id' => $val['biz_id'], 'sales_id' => $val['admin_id']];
}
$visit_time = $this->get_visit_time($where_visit);
$visit_time && $other_data['计划回访时间'] = $visit_time;
$other_data['销售顾问'] = isset($admins[$val['admin_id']]) ? $admins[$val['admin_id']][0]['uname'] : '';
$tags = [$val['level'] . '级用户'];
$defeat = '';
if ($val['if_defeat'] == 1) {
$defeat = '战败申请中';
} else if ($val['if_defeat'] == 2) {
$defeat = '再战';
}
$tip = $status_name[$val['status']] ? $status_name[$val['status']] : '';
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $this->get_mobile(['mobile' => $val['mobile'], 'of_id' => $val['of_id'], 'of2_id' => $val['of2_id']]),
'complete_mobile' => $val['mobile'],
'is_top' => $val['is_top'],
'other_data' => $other_data,
'tags' => $tags,
'defeat' => $defeat,
'tip' => $tip,
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
/**
* Notes:待回访客户列表(已废除)
* Created on: 2021/10/21 14:27
* Created by: dengbw
* @param array $params
* @return array
*/
private function visit_lists_temp($params = array())
{
$biz_id = $this->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, 'a.status<>' => 2, '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,a.of_id,a.of2_id';
$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');
}
foreach ($rows as $key => $val) {
$other_data = [
'建卡时间' => 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'], 'of_id' => $val['of_id'], 'of2_id' => $val['of2_id']]),
'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->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,of_id,of2_id';
$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'], 'of_id' => $val['of_id'], 'of2_id' => $val['of2_id']]),
'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, '拒绝战败申请', 9);
} 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);
// }
if ($params['of2_id'] == 37 && $params['of_id'] == 3) { //客户来源:网络推广-狸车分配 隐藏手机号
$mobile = '****' . substr($mobile, -4);
}
return $mobile;
}
/**
* Notes:判断分配客户权限
* Created on: 2022/3/18 10:02
* Created by: dengbw
* @return int
*/
private function get_allot()
{
$allot = 1;
$group_id = $this->session['group_id'];
if ($group_id == 1) {//销售不可分配用户
$allot = 0;
}
return $allot;
}
/**
* Notes:计划回访时间
* Created on: 2022/6/6 14:13
* Created by: dengbw
* @param $params
* @return string
*/
private function get_visit_time($params)
{
$today = date('Y-m-d');//今天
$re = $this->mdCustomerVisitData->get(['c_id' => $params['c_id'], 'biz_id' => $params['biz_id']
, 'sales_id' => $params['sales_id'], 't_day' => $today]);
$visit_time = '';
if ($re) {
if ($re['pid']) {//有逾期id 找最初回访日期并统计次数
$re_pid = $this->mdCustomerVisitData->get(['id' => $re['pid']]);
if ($re_pid) {
$visit_time = $re_pid['t_day'];
$count = $this->mdCustomerVisitData->count(['pid' => $re['pid']]);
$count && $visit_time = $visit_time . "(逾期{$count}次)";
}
} else {
$visit_time = $re['t_day'];
}
} else {
$re = $this->mdCustomerVisitData->get(['c_id' => $params['c_id'], 'biz_id' => $params['biz_id'],
'sales_id' => $params['sales_id'], 't_day>' => $today]);
$re && $visit_time = $re['t_day'];
}
return $visit_time;
}
}