Files
spacestation/api/controllers/wxapp/licheb/Customers.php
T
2024-09-26 17:55:06 +08:00

1445 lines
64 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_clues_cfrom_model', 'clues_cfrom_model');
$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('area_model', 'mdArea');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_brand_model');
$this->load->model("biz/biz_model");
$this->load->library('receiver/customers_entity');
$this->biz_id = $this->get_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']];
if ($row['county_id']) {
$re_area = $this->mdArea->get(['county_id' => $row['county_id']], 'city_name,county_name');
$re_area && $other_data['所在地区'] = "{$re_area['city_name']}-{$re_area['county_name']}";
}
if ($this->get_biz('type') != 5) {
$other_data['客户来源'] = $this->get_cfTitle($row);
}
$other_data['建档时间'] = date('Y-m-d', strtotime($row['p_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'] : '';
$jsondata = $row['jsondata'] ? json_decode($row['jsondata'], true) : array();
$data = [
'id' => $row['id'],
'cid' => $row['cid'],
'name' => $row['name'],
'mobile' => $this->get_mobile(['mobile' => $row['mobile'], 'rid' => $row['rid']]),
'complete_mobile' => $row['mobile'],
'tip' => $tip,
'is_top' => $row['is_top'],
'status' => $row['status'],
'defeat' => $jsondata['defeat'],
'other_data' => $other_data,
'is_weChat' => $row['wxqy'] == 1 ? true : false,
'generate_order' => $this->myuid == $row['admin_id'] ? true : false,
'level' => $row['level'],
'tags' => $tags,
'wxgr' => $row['wxgr'],
'wxgrimg' => $row['wxgrimg'],
'wxgrimg_url' => $row['wxgrimg'] ? build_qiniu_image_url($row['wxgrimg']) : '',
];
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);
}
$edit_status = false; //是否可编辑来源
if ($row['of_id']) {
$cfrom_rows = $this->clues_cfrom_model->get(array('id' => $row['of_id']));
$cfrom_rows['type'] == 1 && $edit_status = true;
}
$brand = $this->auto_brand_model->get(['id' => $row['brand_id']]);
$of_title = $row['of_id'] ? $this->get_cfTitle($row) : '';
$data['baseinfo'] = [
'name' => ['value' => $row['name'], 'cn' => '客户姓名'],
'mobile' => ['value' => $this->get_mobile(['mobile' => $row['mobile'], 'rid' => $row['rid']]), 'cn' => '客户电话'],
// 'c_brand' => $row['c_brand'],
'of_id' => ['value' => $of_title, 'of_id' => intval($row['of_id']), 'of2_id' => intval($row['of2_id']), 'cn' => '客户来源', 'edit_status' => $edit_status],
'buy_time' => ['value' => $row['buy_time'], 'cn' => '预计购车时间'],
'wxgr' => $row['wxgr'],
'wxgrimg' => $row['wxgrimg'],
'wxgrimg_url' => $row['wxgrimg'] ? build_qiniu_image_url($row['wxgrimg']) : '',
'brand' => ['id' => $row['brand_id'], 'name' => $brand['name']],
'series_id' => $row['series_id'],
];
return $data;
}
# tag_type=2意向标签的类型:1到店意向 2 购买意向
private function get_tag_type2_pid($tag_type2 = null)
{
$arr = array(1 => 33, 2 => 34);
if (!$tag_type2) {
return $arr;
}
if (!in_array($tag_type2, array_keys($arr))) {
$tag_type2 = 1;
}
return $arr[$tag_type2];
}
protected function get_tag()
{
$id = intval($this->input_param('id'));
$type = $this->input_param('type');
$tag_type = $this->input_param('tag_type');
!strlen($tag_type) && $tag_type = 0;
$tag_type2 = $this->input_param('tag_type2');
#$tag_type == 1 && $tag_type = 2; # 手工测试tag_type=2用
$tag_type == 2 && !$tag_type2 && $tag_type2 = 1;
$tag_type2_pid = $tag_type == 2 ? $this->get_tag_type2_pid($tag_type2) : 0;
$tag_id = 0; # 230312, $tag_type=1时首个select中tag的id
$tags = $res_td = $re_cus = [];
$re_biz = $this->get_biz();
$city_id = intval($re_biz['city_id']);
$county_id = intval($re_biz['county_id']);
$province_id = intval($re_biz['province_id']);
$c_brand = intval($re_biz['car_brand_id']);
if ($id) {
$re_cus = $this->customers_model->get(['id' => $id]);
$re_cus['province_id'] && $province_id = $re_cus['province_id'];
$re_cus['city_id'] && $city_id = $re_cus['city_id'];
$re_cus['county_id'] && $county_id = $re_cus['county_id'];
$re_cus['c_brand'] && $c_brand = $re_cus['c_brand'];
}
$where = ['status' => 1, 'pid' => 0, 'show<>' => 1, 'tag_type' => $tag_type];
$tag_type2_pid && $where['id'] = $tag_type2_pid;
$res = $this->mdCustomerTag->select($where, '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')) : '';
$tag_checked = array();
$tag_checked_id = 0;
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];
$tag_type == 1 && $checked && $tag_checked[] = $val2['id'];
$tag_type == 1 && $checked && !$tag_checked_id && $tag_checked_id = $val['id'];
}
$tags[] = ['id' => $val['id'], 'name' => $val['name'], 'type' => $val['type'], 'list' => $list];
}
$tag_type == 1 && count($tag_checked) == 1 && $tag_id = $tag_checked_id;
}
$res = ['tags' => $tags, 'province_id' => $province_id, 'city_id' => $city_id, 'county_id' => $county_id, 'c_brand' => $c_brand];
$tag_type == 1 && $res['tag_id'] = $tag_id;
return $res;
}
//修改基本信息
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');
$c_brand = $this->input_param('c_brand');
$of2_id = $this->input_param('of2_id'); //线下来源一级
if ($of2_id) {
$cf_row = $this->clues_cfrom_model->get(array('id' => $of2_id));
$of_id = $cf_row['pid'];
}
$buy_time = $this->input_param('buy_time'); //预计购车时间
$tag = $this->input_param('tag'); //客户标签
$province_id = $this->input_param('province_id'); //省份id
$city_id = $this->input_param('city_id'); //城市id
$county_id = $this->input_param('county_id'); //区id
$wxgr = $this->input_param('wxgr'); //是否加个微
$wxgrimg = $this->input_param('wxgrimg'); //个微截图
$brand_id = (int)$this->input_param('brand_id');
$series_id = (int)$this->input_param('series_id');
$row = $this->customers_model->get(['id' => $id]);
if (!$row) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if ($wxgr && !$wxgrimg && !$row['wxgrimg']) {
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;
}
}
$name && $update['name'] = $name;
isset($c_brand) && $update['c_brand'] = $c_brand;
$of_id && $update['of_id'] = $of_id;
$of2_id && $update['of2_id'] = $of2_id;
$province_id && $update['province_id'] = $province_id;
$city_id && $update['city_id'] = $city_id;
$county_id && $update['county_id'] = $county_id;
$wxgr && $update['wxgr'] = intval($wxgr) ? 1 : 0;
$wxgrimg && $wxgrimg != $row['wxgrimg'] && $update['wxgrimg'] = $wxgrimg;
$brand_id && $update['brand_id'] = $brand_id ?: 0;
$series_id && $update['series_id'] = $series_id ?: 0;
if ($buy_time) {
$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->customers_entity->add_log($id, $this->session['uid'], $uname, "修改用户基本信息");
if ($wxgr) {
$uid = $this->session['uid'];
$res = $this->customer_oplogs_model->get(['customer_id' => $id, 'uid' => $uid, 'type' => 10]);
if ($res) {
if ($wxgrimg && $wxgrimg != $row['wxgrimg'] || $wxgrimg && !$res['imgs']) {
$imgs = $res['imgs'] ? json_decode($res['imgs'], true) : [];
$imgs[] = $wxgrimg;
$this->customer_oplogs_model->update(array('imgs' => json_encode($imgs, JSON_UNESCAPED_UNICODE)), array('id' => $res['id']));
}
} else {
$imgs = [];
$wxgrimg && $imgs[] = $wxgrimg;
$this->customers_entity->add_log($id, $uid, $uname, '加个微', 10, 'wxapp', $imgs);
}
}
throw new Exception('保存成功', API_CODE_SUCCESS);
}
//创建客户
protected function post()
{
$name = $this->input_param('name');
$mobile = $this->input_param('mobile');
$buy_time = $this->input_param('buy_time'); //预计购车时间
$c_brand = $this->input_param('c_brand'); //线下来源一级
$of2_id = $this->input_param('of2_id'); //线下来源一级
$cf_row = $this->clues_cfrom_model->get(array('id' => $of2_id));
$of_id = $cf_row['pid'];
$status = 0; //状态
$tag = $this->input_param('tag'); //客户标签
$province_id = $this->input_param('province_id');//省份id
$city_id = $this->input_param('city_id'); //城市id
$county_id = $this->input_param('county_id'); //区id
$wxgr = $this->input_param('wxgr'); //是否加个微
$wxgrimg = $this->input_param('wxgrimg'); //个微截图
$brand_id = $this->input_param('brand_id');
$series_id = $this->input_param('series_id');
$car_id = $this->input_param('car_id');
if (!mobile_valid($mobile)) throw new Exception('请输入正确的手机号码', ERR_PARAMS_ERROR);
if (!$city_id || !$county_id) {
throw new Exception('请选择城市与行政区', API_CODE_FAIL);
}
if (!$cf_row) {
throw new Exception('请选择客户来源', API_CODE_FAIL);
}
if ($wxgr && !$wxgrimg) {
throw new Exception('请上传‘添加个微’截图!', ERR_PARAMS_ERROR);
}
$is_exit = $this->customers_model->get(['biz_id' => $this->biz_id, 'mobile' => $mobile], 'id,sales_id');
if ($is_exit) {
if ($is_exit['sales_id'] != $this->session['uid']) {
$user = $this->app_user_model->get(['id' => $is_exit['sales_id']], 'uname');
$msg = "该客户归属于{$user['uname']}";
$owner = 0;
} else {
$msg = '客户已存在';
$owner = 1;
}
return ['code' => API_CODE_FAIL, 'msg' => $msg, 'data' => ['id' => $is_exit['id'], 'owner' => $owner]];
}
if($cf_row['id']==10){ //自然到店直接改成到店客户
$status = 1;
}
$this->load->helper("order");
$biz_row = $this->biz_model->get(['id' => $this->biz_id]);
$level = $this->customers_entity->cal_level($buy_time);
$time = date('Y-m-d H:i:s');
$add_data = [
'cid' => create_customer_no($biz_row['county_id']),
'name' => $name,
'mobile' => $mobile,
'biz_id' => $this->biz_id,
'admin_id' => $this->session['uid'],
'sales_id' => $this->session['uid'],
'level' => $level,
'cf_title' => '自有资源',
'cont_time' => $time,
'p_time' => $time,
'status' => $status,
'province_id' => $province_id,
'city_id' => $city_id,
'county_id' => $county_id,
'wxgr' => intval($wxgr) ? 1 : 0,
'wxgrimg' => $wxgrimg ? $wxgrimg : '',
'c_time' => time()
];
if (!$add_data['city_id'] && $biz_row['city_id']) {
$add_data['city_id'] = $biz_row['city_id'];
}
if (!$add_data['county_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;
$brand_id && $add_data['brand_id'] = $brand_id;
$series_id && $add_data['series_id'] = $series_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->customers_entity->add_log($id, $uid, $uname, "创建客户档案", 3);
if ($wxgr) {
$imgs = [];
$wxgrimg && $imgs[] = $wxgrimg;
$this->customers_entity->add_log($id, $uid, $uname, '加个微', 10, 'wxapp', $imgs);
}
throw new Exception('创建成功', API_CODE_SUCCESS);
} else {
throw new Exception('创建失败', ERR_PARAMS_ERROR);
}
}
/**
* Notes:用户评论
* Created on: 2022/6/20 14:05
* Created by: dengbw
* @throws Exception
*/
protected function post_comments()
{
$pid = $this->input_param('id');
$content = $this->input_param('content');
if (!$pid) {
throw new Exception('参数错误', API_CODE_FAIL);
}
if (!$content) {
throw new Exception('请输入评论内容', API_CODE_FAIL);
}
$this->load->model('receiver/receiver_comments_model', 'mdComments');
$uid = $this->session['uid'];
$uname = $this->session['uname'];
$add_data = ['pid' => $pid, 'content' => $content, 'uid' => $uid, 'uname' => $uname, 'c_time' => time()];
$id = $this->mdComments->add($add_data);
if ($id) {
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');
$wxgr = $this->input_param('wxgr');
$wxgrimg = $this->input_param('wxgrimg');
$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');//计划回访时间
$invalid_tag = $this->input_param('invalid_tag'); //战败标签
$daodian_tag = $this->input_param('daodian_tag'); //意向标签-到店
$goumai_tag = $this->input_param('goumai_tag'); //意向标签-购买
if ($follow_channel == '微信' && !$content_imgs) {
throw new Exception('请上传跟进截图!', ERR_PARAMS_ERROR);
}
$row = $this->customers_model->get(['id' => $id]);
if (!$row) {
throw new Exception('数据不存在!', ERR_PARAMS_ERROR);
}
if ($wxgr && !$wxgrimg && !$row['wxgrimg']) {
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;
}
$daodian_tag_str = '';
if ($daodian_tag) {
$tag_arr_all = array();
$tag_checked = 0;
foreach ($daodian_tag as $key => $val) {
$tag_arr = [];
foreach ($val['list'] as $kt => $vt) {
$vt['checked'] && $tag_arr[] = $vt['name'];
$vt['checked'] && $tag_checked = 1;
}
$tag_arr_all = array_merge($tag_arr_all, $tag_arr);
$tag_arr && $daodian_tag_str .= $val['name'] . ': ' . implode(', ', $tag_arr) . '; ';
}
if (!$tag_checked) {
throw new Exception("到店意向不能为空!", ERR_PARAMS_ERROR);
}
}
$goumai_tag_str = '';
if ($goumai_tag) {
$tag_arr_all = array();
$tag_checked = 0;
foreach ($goumai_tag as $key => $val) {
$tag_arr = [];
foreach ($val['list'] as $kt => $vt) {
$vt['checked'] && $tag_arr[] = $vt['name'];
$vt['checked'] && $tag_checked = 1;
}
$tag_arr_all = array_merge($tag_arr_all, $tag_arr);
$tag_arr && $goumai_tag_str .= $val['name'] . ': ' . implode(', ', $tag_arr) . '; ';
}
if (!$tag_checked) {
throw new Exception("购买意向不能为空!", ERR_PARAMS_ERROR);
}
}
$log_0 = $log_4 = $log_9 = '';
if ($status == 3) {
if (!$defeat_reason) {
throw new Exception('请输入战败理由!', ERR_PARAMS_ERROR);
}
$tag_str = '';
$tag_arr_all = array();
$tag_checked = 0; # 20230312 只要有一个选中即可
foreach ($invalid_tag as $key => $val) {
$tag_arr = [];
foreach ($val['list'] as $kt => $vt) {
$vt['checked'] && $tag_arr[] = $vt['name'];
$vt['checked'] && $tag_checked = 1;
}
/*# 20230101 提示没选中的标签
if (!$tag_arr){
throw new Exception("请选择标签[{$val['name']}]", ERR_PARAMS_ERROR);
}*/
$tag_arr_all = array_merge($tag_arr_all, $tag_arr);
$tag_arr && $tag_str .= $val['name'] . ': ' . implode(', ', $tag_arr) . '; ';
}
if (!$tag_checked) {
throw new Exception("请选择至少一个标签!", ERR_PARAMS_ERROR);
}
#$tag_str = implode(', ', $tag_arr_all);
$jsondata = $row['jsondata'] ? json_decode($row['jsondata'], true) : array();
$jsondata['defeat']['time'] = date("Y-m-d H:i:s");
$jsondata['defeat']['reason'] = $defeat_reason . "({$tag_str})";
$up_data['if_defeat'] = 1;
$up_data['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
$log_0 = '申请战败:' . $defeat_reason . "({$tag_str})";
} 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;
$wxgr && $up_data['wxgr'] = 1;
$wxgrimg && $wxgrimg != $row['wxgrimg'] && $up_data['wxgrimg'] = $wxgrimg;
$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 ($wxgr) {
$res = $this->customer_oplogs_model->get(['customer_id' => $id, 'uid' => $uid, 'type' => 10]);
if ($res) {
if ($wxgrimg && $wxgrimg != $row['wxgrimg'] || $wxgrimg && !$res['imgs']) {
$imgs = $res['imgs'] ? json_decode($res['imgs'], true) : [];
$imgs[] = $wxgrimg;
$this->customer_oplogs_model->update(array('imgs' => json_encode($imgs, JSON_UNESCAPED_UNICODE)), array('id' => $res['id']));
}
} else {
$imgs = [];
$wxgrimg && $imgs[] = $wxgrimg;
$this->customers_entity->add_log($id, $uid, $uname, '加个微', 10, 'wxapp', $imgs);
}
}
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;
$daodian_tag_str && $content .= '。' . $daodian_tag_str;
$goumai_tag_str && $content .= '。' . $goumai_tag_str;
$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) {
//删除大于等今天的回访记录
$this->mdCustomerVisitData->delete(['c_id' => $id, 'biz_id' => $biz_id, 't_day>=' => date('Y-m-d')]);
$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) {
//战败标签
//意向标签 合并到 战败标签 统一处理
!$invalid_tag && $invalid_tag = array();
$daodian_tag && $invalid_tag = array_merge($invalid_tag, $daodian_tag);
$goumai_tag && $invalid_tag = array_merge($invalid_tag, $goumai_tag);
if ($invalid_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 ($invalid_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);
}
}
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 = $tags = [];
foreach ($buy_time as $key => $val) {
$show_btime[] = ['id' => $key, 'name' => $val];
}
$res_tag = $this->mdCustomerTag->select(['status' => 1, 'pid' => 0, 'show<>' => 1, 'tag_type' => 0], 'sort desc,id desc', 0, 0, 'id,name,type');
if ($res_tag) {
foreach ($res_tag 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) {
$list[] = ['id' => $val2['id'], 'name' => $val2['name'], 'checked' => false];
}
$tags[] = ['id' => $val['id'], 'name' => $val['name'], 'type' => $val['type'], 'list' => $list];
}
}
$data = [
'tags' => $tags,
'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()
{
$map_cfrom = $this->clues_cfrom_model->get(array('status' => 1, 'pid' => 0, 'type' => 1));
$map_cfrom2 = $this->clues_cfrom_model->select(array('status' => 1, 'pid' => $map_cfrom['id']));
$lists = [];
foreach ($map_cfrom2 as $key => $item) {
$lists[] = [
'id' => $item['id'],
'name' => $item['title'],
];
}
return $lists;
}
//获取客户列表
private function lists()
{
$visit = $this->input_param('visit');
if ($visit) {
return $this->visit_lists($this->input_param(), $visit);
}
$uid = $this->session['uid'];
$group_id = $this->session['group_id'];
$biz_type = $this->get_biz('type');
$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');
$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')); //状态类型
$admin_ids = $this->input_param('admin_ids');//多选销售人员
$tag_ids = $this->input_param('tag_ids');//多选客户画像
$city_id = $this->input_param('city_id');
$county_id = $this->input_param('county_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' => $this->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 && $this->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;
if ($admin_ids) {
$where["admin_id in ({$admin_ids})"] = null;
}
$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;
$city_id && $where['city_id'] = $city_id;
$county_id && $where['county_id'] = $county_id;
if ($tag_ids) {
$pidAry = [];
$res_tag = $this->mdCustomerTag->select(["id in ({$tag_ids})" => null], 'id desc', 0, 0, 'pid,id');
foreach ($res_tag as $v) {
$v['pid'] && $pidAry[$v['pid']][] = $v['id'];
}
$str_c_ids = $res_tag_data = '';
foreach ($pidAry as $v) {
if ($v) {
$where_tag = [];
$str_ids = implode(',', $v);
$where_tag["t_id in({$str_ids})"] = null;
if ($str_c_ids) {
$where_tag["c_id in({$str_c_ids})"] = null;
}
$res_tag_data = $this->mdCustomerTagdata->select_groupby('c_id', $where_tag, "id desc", 0, 0, "c_id");
if (!$res_tag_data) {
break;
}
if ($res_tag_data) {
$str_c_ids = implode(',', array_column($res_tag_data, 'c_id'));
}
}
}
if ($res_tag_data) {
$str_cids = implode(',', array_column($res_tag_data, 'c_id'));
$where["id in({$str_cids})"] = null;
} else {
$where["id"] = -1;
}
}
$count = $this->customers_model->count($where);
$lists = [];
if ($count) {
$fileds = 'id,rid,name,admin_id,mobile,level,car_json,is_top,cf_title,brand_id,cont_time,c_time,p_time,if_defeat
,of_id,of2_id,wxqy,status,biz_id,county_id,cid';
$rows = $this->customers_model->select($where, $orderby, $page, $size, $fileds);
$lists = $this->listCustomerField(['rows' => $rows, 'biz_type' => $biz_type, 'group_id' => $group_id]);
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
/**
* Notes:客户列表字段
* Created on: 2022/10/14 16:03
* Created by: dengbw
* @param array $param
* @return array
*/
private function listCustomerField($param = [])
{
$lists = $admins = $map_county = [];
//所在地区
$str_county_ids = implode(',', array_unique(array_column($param['rows'], 'county_id')));
if ($str_county_ids) {
$map_county = $this->mdArea->map('county_id', 'city_name,county_name', ["county_id in({$str_county_ids})" => null]);
}
//获取管理员
$admin_ids = implode(',', array_unique(array_column($param['rows'], 'admin_id')));
if ($admin_ids) {
$admins = $this->app_user_model->map('id', '', ["id in ({$admin_ids})" => null], '', '', '', 'id,uname');
}
$allot = $this->get_allot();
$status_name = $this->customers_model->get_status();
foreach ($param['rows'] as $key => $val) {
if ($map_county[$val['county_id']]) {
$other_data['所在地区'] = "{$map_county[$val['county_id']]['city_name']}-{$map_county[$val['county_id']]['county_name']}";
}
if ($param['biz_type'] != 5) {
$other_data['客户来源'] = $this->get_cfTitle($val);
}
$other_data['建档时间'] = date('Y-m-d', strtotime($val['p_time']));
$val['cont_time'] != '0000-00-00 00:00:00' && $other_data['最近联系'] = date('Y-m-d', strtotime($val['cont_time']));
if ($param['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 = '再战';
}
$tip = $status_name[$val['status']] ? $status_name[$val['status']] : '';
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => $this->get_mobile(['mobile' => $val['mobile'], 'rid' => $val['rid']]),
'complete_mobile' => $val['mobile'],
'is_top' => $val['is_top'],
'other_data' => $other_data,
'tags' => $tags,
'defeat' => $defeat,
'orders_pay' => '',
'group_id' => $param['group_id'],
'is_weChat' => $val['wxqy'] == 1 ? true : false,
'allot' => $allot,
'tip' => $tip,
'level' => $val['level'],
'reassign' => $val['cs_biz_id'] > 0 ? '他店改派' : '',
'cid' => $val['cid'],
];
}
return $lists;
}
//派单给店员
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');
$biz_id = intval($this->input_param('biz_id'));
$allot = $this->get_allot();
if ($allot == 0) {
throw new Hd_exception('无权限分配', API_CODE_INVILD_PARAM);
}
if (!$id_arr) {
throw new Hd_exception('参数错误', API_CODE_INVILD_PARAM);
}
$this->load->library('receiver/customers_entity');
if ($biz_id) {//改派
$nums = count($id_arr);
foreach ($id_arr as $val) {
$id = $val;
$re = $this->customers_model->get(['id' => $id]);
if ($re['biz_id'] == $biz_id) {//同个门店跳出循环
if ($nums == 1) {
throw new Hd_exception('不能分配给相同门店', API_CODE_INVILD_PARAM);
}
continue;
}
$re2 = $this->customers_model->get(['biz_id' => $biz_id, 'mobile' => $re['mobile']]);
if ($re2) {//客户已存在门店里
if ($nums == 1) {
throw new Hd_exception('客户已在此门店', API_CODE_INVILD_PARAM);
}
continue;
}
$addData = ['rid' => $re['rid'], 'name' => $re['name'], 'mobile' => $re['mobile'], 'biz_id' => $biz_id
, 'cs_biz_id' => $re['biz_id'], 'level' => $re['level'], 'cf_title' => $re['cf_title'], 'cf_id' => $re['cf_id']
, 'of_id' => $re['of_id'], 'of2_id' => $re['of2_id'], 'status' => $re['status'], 'c_time' => time(), 'city_id' => $re['city_id']
, 'county_id' => $re['county_id']];
$re['jsondata'] && $addData['jsondata'] = $re['jsondata'];
$customer_id = $this->customers_model->add($addData);
if ($customer_id) {
//初始门店更改-1,理车宝过滤
$this->customers_model->update(['cs_biz_id' => -1], ["id" => $id]);
//删除未回访计划
$this->mdCustomerVisitData->delete(['c_id' => $id, 'biz_id' => $re['biz_id'], 'status<>' => 2, 't_day >=' => date('Y-m-d')]);
//同步客户标签
$res_tag = $this->mdCustomerTagdata->select(['c_id' => $id], 'id asc', 0, 0, 't_id,c_time');
if ($res_tag) {
$add_tag = [];
foreach ($res_tag as $val2) {
$val2['c_id'] = $customer_id;
$add_tag[] = $val2;
}
$this->mdCustomerTagdata->add_batch($add_tag);
}
//同步跟进记录
$res_oplogs = $this->customer_oplogs_model->select(['customer_id' => $id], 'id asc', 0, 0
, 'uid,uname,type,log,imgs,cf_platform,c_time');
if ($res_oplogs) {
$add_oplogs = [];
foreach ($res_oplogs as $val3) {
$val3['customer_id'] = $customer_id;
$add_oplogs[] = $val3;
}
$this->customer_oplogs_model->add_batch($add_oplogs);
}
$this->customers_entity->add_log($id, $uid, $uname, "客户改派");//改派的客户日志
$this->customers_entity->add_log($customer_id, $uid, $uname, "客户改派");//改派后的客户日志
}
}
throw new Exception('分配成功', API_CODE_SUCCESS);
}
if (!$visit_time) {
throw new Hd_exception('请选择回访时间', API_CODE_INVILD_PARAM);
}
$admin = $this->app_user_model->get(['id' => $admin_id, 'status' => 1]);
if (!$admin) {
throw new Hd_exception('参数错误', API_CODE_INVILD_PARAM);
}
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 >=' => date('Y-m-d')]);
$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;
b2m_send_sms($admin['mobile'], '【理车宝】您有一个新的客户需要跟进,手机尾号为' . $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]];
} else if ($status == 3) {
$tabs = [['name' => '今日', 'id' => 1], ['name' => '本月客户', 'id' => 2]];
} else if ($status == 4) {
$tabs = [['name' => '今日', 'id' => 1], ['name' => '本月企v', 'id' => 2]];
} else if ($status == 5) {
$tabs = [['name' => '今日', 'id' => 1], ['name' => '本月到店', 'id' => 2]];
} else if ($status == 6) {
$tabs = [['name' => '今日', 'id' => 1], ['name' => '本月订单', 'id' => 2]];
} else if ($status == 7) {
$tabs = [['name' => '今日', 'id' => 1], ['name' => '本月战败', 'id' => 2]];
} else if ($status == 8) {
$tabs = [['name' => '今日', 'id' => 1], ['name' => '本月退订', 'id' => 2]];
}
return ['list' => $tabs];
}
/**
* Notes:客户回访记录列表
* Created on: 2022/10/14 14:40
* Created by: dengbw
* @param array $params
* @param int $visit
* @return array
*/
private function visit_lists($params = [], $visit = 1)
{
$group_id = $this->session['group_id'];
$biz_type = $this->get_biz('type');
$page = intval($params['page']);
$size = intval($params['size']);
$tab_id = intval($params['visit_tab_id']);
!$page && $page = 1;
!$size && $size = 10;
$count = 0;
$lists = $where = $rows = [];
if ($visit == 1) {//客户回访记录
$status = intval($params['status']);
$t_day = date('Y-m-d');
$where = array('a.biz_id' => $this->biz_id, 'a.cs_biz_id<>' => -1, 'a.status in(0,1)' => null, 'b.t_day' => $t_day);
$params['level'] && $where['a.level'] = $params['level'];//等级
$params['admin_id'] && $where['a.admin_id'] = $params['admin_id'];//等级
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->myuid;
$count = $this->mdCustomerVisitData->count_visit($where);
} else {//数据看板 数据列表
if ($tab_id == 2) {//今日
$s_c_time = date('Y-m-01', strtotime(date("Y-m-d"))) . ' 00:00:00';
$e_c_time = date('Y-m-d', strtotime("$s_c_time +1 month -1 day")) . ' 23:59:59';
} else {//本月
$s_c_time = date('Y-m-d') . ' 00:00:00';
$e_c_time = date('Y-m-d') . ' 23:59:59';
}
if ($visit == 3) {//今日/本月线索
$where = ['biz_id' => $this->biz_id, 'status>=' => 0, 'p_time>=' => $s_c_time
, 'p_time<=' => $e_c_time];
$group_id == 1 && $where['admin_id'] = $this->myuid;
$count = $this->customers_model->count($where);
} else if ($visit == 5 || $visit == 7) {//今日/本月到店/战败
$oplogs_type = $visit == 7 ? 7 : 4;//4到店7战败
$s_c_time = strtotime($s_c_time);
$e_c_time = strtotime($e_c_time);
$where = ['biz_id' => $this->biz_id, 'status>=' => 0];
$str_uids = '';
if ($group_id == 1) {
$str_uids = $this->myuid;
} else {
$res_user = $this->app_user_model->select(['biz_id' => $this->biz_id, 'group_id <' => 4, 'status>=' => 0], 'id asc', 0, 0, 'id,userid');
foreach ($res_user as $k => $v) {
$str_uids = $str_uids ? $str_uids . ',' . $v['id'] : $v['id'];
}
}
!$str_uids && $str_uids = '-1';
$where["id in(select customer_id from lc_receiver_customer_oplogs where type={$oplogs_type} and uid in({$str_uids}) and c_time>={$s_c_time} and c_time<={$e_c_time})"] = null;
$count = $this->customers_model->count($where);
}
}
if ($count) {
$fileds = 'id,name,admin_id,mobile,level,car_json,is_top,cf_title,brand_id,sales_id,cont_time,c_time,p_time,if_defeat
,of_id,of2_id,wxqy,status,biz_id,cs_biz_id,county_id,cid';
if ($visit == 1) {
$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,a.county_id,cid';
$rows = $this->mdCustomerVisitData->select_visit($where, 'a.id desc', $page, $size, $fileds);
$lists = $this->listCustomerField(['rows' => $rows, 'biz_type' => $biz_type, 'group_id' => $group_id]);
} else if ($visit == 4) {
$fileds = 'distinct(external_userid) as external_userid,userid,c_time';
$rows = $this->mdWechatLog->select($where, 'c_time desc', $page, $size, $fileds);
foreach ($rows as $k => $v) {
$mobile = $nickname = $avatar = '';
$re_item = [];
if ($v['external_userid']) {//查找企微用户
$where_wec = ['external_userid' => $v['external_userid']];
$v['userid'] && $where_wec['userid'] = $v['userid'];
$res_wec = $this->mdWechat->get($where_wec);
if ($res_wec) {
$mobile = $res_wec['mobile'];
$nickname = $res_wec['name'];
$avatar = $res_wec['avatar'];
}
}
if ($mobile) {//查找关连客户
$where = ['mobile' => $mobile, 'status>=' => 0];
$this->biz_id && $where['biz_id'] = $this->biz_id;
$res_cus = $this->customers_model->get($where);
if ($res_cus) {
if ($res_cus['wxqy'] == 0) {//更新为企微好友
$res_cus['wxqy'] = 1;
$this->customers_model->update(['wxqy' => $res_cus['wxqy']], ['id' => $res_cus['id']]);
}
$rows = [$res_cus];
$re = $this->listCustomerField(['rows' => $rows, 'biz_type' => $biz_type, 'group_id' => $group_id]);
count($re) && $re_item = $re[0];
}
}
if (!$re_item) {//未关连到客户
$other_data['通过时间'] = date('Y-m-d', $v['c_time']);
if ($v['userid']) {//查找销售顾问
$res_user = $this->app_user_model->get(['userid' => $v['userid']]);
$res_user && $other_data['车管家'] = $res_user['uname'] ? $res_user['uname'] : $res_user['nickname'];
}
$re_item = ['nickname' => $nickname, 'avatar' => $avatar, 'other_data' => $other_data];
}
$lists[] = $re_item;
}
} else {
$rows = $this->customers_model->select($where, 'c_time desc', $page, $size, $fileds);
$lists = $this->listCustomerField(['rows' => $rows, 'biz_type' => $biz_type, 'group_id' => $group_id]);
}
}
$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;
$where = array('biz_id' => $this->biz_id, 'cs_biz_id<>' => 1, '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,rid';
$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'], 'rid' => $val['rid']]),
'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)
{
$map_cfrom2 = $this->clues_cfrom_model->get(array('id' => $params['of2_id']));
$map_cfrom = $this->clues_cfrom_model->get(array('id' => $map_cfrom2['pid']));
$title = $map_cfrom['title'] . '-' . $map_cfrom2['title'];
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 '';
} elseif ($params['rid']) {
return mobile_asterisk($mobile);
} else {
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;
}
/**
* Notes:获取门店信息
* Created on: 2022/6/30 15:09
* Created by: dengbw
* @param string $params
* @return string
*/
private function get_biz($params = '')
{
if ($params) {
$re = $this->biz_model->get(['id' => $this->biz_id, 'status' => 1], $params);
$re = $re ? $re[$params] : '';
} else {
$re = $this->biz_model->get(['id' => $this->biz_id, 'status' => 1]);
}
return $re;
}
}