Files
liche/admin/controllers/app/licheb/Userslog.php
T
2022-07-12 16:24:24 +08:00

440 lines
21 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:狸车宝顾问_日志
* Created on: 2022/4/15 16:43
* Created by: dengbw
*/
class Userslog extends HD_Controller
{
private $tabAry = [1 => '客户统计', 2 => '回访统计'];
public function __construct()
{
parent::__construct();
$this->load->model('app/licheb/app_licheb_users_model', 'mdUsers');
$this->load->model('app/licheb/app_licheb_users_log_model', 'mdUsersLog');
$this->load->model('app/licheb/app_licheb_bizs_log_model', 'mdBizsLog');
$this->load->model('receiver/receiver_customers_visit_sales_model', 'mdCustomerVisitSales');
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$this->load->model("biz/biz_model", 'mdBiz');
$this->load->model('area_model', 'mdArea');
}
public function index()
{
return $this->lists();
}
public function lists()
{
$params = $this->input->get();
$params['page'] = $params['page'] ? intval($params['page']) : 1;
$params['size'] = $params['size'] ? intval($params['size']) : 20;
$data = $this->dataBizSelect($params);
$data['typeAry'] = $this->mdBiz->type_ary();
$this->data = $data;
$this->show_view('app/licheb/userslog/lists', true);
}
public function lists_users()
{
$params = $this->input->get();
$params['page'] = $params['page'] ? intval($params['page']) : 1;
$params['size'] = $params['size'] ? intval($params['size']) : 20;
$params['tab'] = $params['tab'] ? intval($params['tab']) : 1;
if ($params['tab'] == 2) {
$view = 'app/licheb/userslog/lists_visit';
$data = $this->visitSelect($params);
} else {
$view = 'app/licheb/userslog/lists_users';
$data = $this->dataUsersSelect($params);
}
$data['tabAry'] = $this->tabAry;
$this->data = $data;
$this->show_view($view, true);
}
private function dataBizSelect($params)
{
$lists = $where = [];
if ($params['biz_id']) {
$where['biz_id'] = $params['biz_id'];
} else if ($params['city_id']) {
$where_biz['status'] = 1;
$params['city_id'] && $where_biz['city_id'] = $params['city_id'];
$params['county_id'] && $where_biz['county_id'] = $params['county_id'];
$res_biz = $this->mdBiz->select($where_biz, 'id desc', 0, 0, 'id');
if ($res_biz) {
$str_ids = implode(',', array_column($res_biz, 'id'));
$where["biz_id in ({$str_ids})"] = null;
} else {
$where['biz_id'] = -2;
}
} else {
$params['city_id'] = '';
$params['county_id'] = '';
$params['biz_id'] = '';
}
if ($params['type']) {
$where["type"] = $params['type'];
} else {
$params['type'] = '';
}
if ($params['time']) {
$time = explode(' ~ ', $params['time']);
$time[0] && $where["log_date>="] = $time[0];
$time[1] && $where["log_date<="] = $time[1];
}
$total = $this->mdBizsLog->count($where);
if ($total) {
$res = $this->mdBizsLog->select($where, "id desc", $params['page'], $params['size']);
if ($res) {
$str_ids = implode(',', array_unique(array_column($res, 'biz_id')));
$map_biz = $this->mdBiz->map('id', 'biz_name', ["id in ({$str_ids})" => null]);
$offlineSources = $this->mdCustomers->offlineSources();
foreach ($res as $v) {
$biz_name = $biz_type = $customer_info = $c_num_info = $order_info = '';
$customers = $c_num = $orders = 0;
$map_biz[$v['biz_id']] && $biz_name = $params['size'] == 10000 ? $map_biz[$v['biz_id']] : "<a href=\"javascript:void(0);\"
data-open=\"/app/licheb/userslog/lists_users?tab=1&biz_id={$v['biz_id']}&time={$v['log_date']} ~ {$v['log_date']}\">
{$map_biz[$v['biz_id']]}</a>";
$temp = ['biz_name' => $biz_name, 'defeats' => $v['defeats']
, 't_num' => $v['t_num'], 'orders' => $v['orders']
, 'bills' => $v['bills'], 'reassigns' => $v['reassigns'], 'receives' => $v['receives']
, 'qy_adds' => $v['qy_adds'], 'log_date' => $v['log_date']];
$customer_json = $v['customer_json'] ? json_decode($v['customer_json'], true) : [];
$c_num_json = $v['customer_json'] ? json_decode($v['c_num_json'], true) : [];
$order_json = $v['customer_json'] ? json_decode($v['order_json'], true) : [];
foreach ($offlineSources as $key1 => $value1) {
$nums = intval($v['customer_' . $key1]);
$nums_c_num = intval($v['c_num_' . $key1]);
$nums_orders = intval($v['order_' . $key1]);
if ($nums) {
$customers += $nums;
if ($params['size'] != 10000) {
if ($customer_info) {
$customer_info = $customer_info . '+' . $value1['name'] . '(' . $nums . ')';
} else {
$customer_info = $value1['name'] . '(' . $nums . ')';
}
}
}
if ($nums_c_num) {
$c_num += $nums_c_num;
if ($params['size'] != 10000) {
if ($c_num_info) {
$c_num_info = $c_num_info . '+' . $value1['name'] . '(' . $nums_c_num . ')';
} else {
$c_num_info = $value1['name'] . '(' . $nums_c_num . ')';
}
}
}
if ($nums_orders) {
$orders += $nums_orders;
if ($params['size'] != 10000) {
if ($order_info) {
$order_info = $order_info . '+' . $value1['name'] . '(' . $nums_orders . ')';
} else {
$order_info = $value1['name'] . '(' . $nums_orders . ')';
}
}
}
if ($params['size'] == 10000) {//导出使用
$temp['customer_' . $key1] = $nums;
$temp['c_num_' . $key1] = $nums_c_num;
$temp['order_' . $key1] = $nums_orders;
foreach ($value1['list'] as $key2 => $value2) {
$temp['customer_' . $key2] = intval($customer_json[$key1]['list'][$key2]['num']);
$temp['c_num_' . $key2] = intval($c_num_json[$key1]['list'][$key2]['num']);
$temp['order_' . $key2] = intval($order_json[$key1]['list'][$key2]['num']);
}
}
}
$customer_info && $customers = $customers . '=' . $customer_info;
$c_num_info && $c_num = $c_num . '=' . $c_num_info;
$order_info && $orders = $orders . '=' . $order_info;
$temp['customers'] = $customers;
$temp['c_num'] = $c_num;
$temp['orders'] = $orders;
$lists[] = $temp;
}
}
}
if ($params['size'] != 10000) {
$sum = $this->mdUsersLog->sum('customers', $where);
$menus[] = ['title' => "客户数", 'value' => intval($sum['customers']), 'tag' => '人'];
$sum_c = $this->mdUsersLog->sum('c_num', $where);
$menus[] = ['title' => "进店人数", 'value' => intval($sum_c['c_num']), 'tag' => '人'];
$sum = $this->mdUsersLog->sum('orders', $where);
$menus[] = ['title' => "订单数", 'value' => intval($sum['orders']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('t_num', $where);
$menus[] = ['title' => "试驾数", 'value' => intval($sum['t_num']), 'tag' => '次'];
$sum = $this->mdUsersLog->sum('defeats', $where);
$menus[] = ['title' => "战败数", 'value' => intval($sum['defeats']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('bills', $where);
$menus[] = ['title' => "开票数", 'value' => intval($sum['bills']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('qy_adds', $where);
$menus[] = ['title' => "企微数", 'value' => intval($sum['qy_adds']), 'tag' => '人'];
$sum = $this->mdUsersLog->sum('reassigns', $where);
$menus[] = ['title' => "改派数", 'value' => intval($sum['reassigns']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('receives', $where);
$menus[] = ['title' => "接收数", 'value' => intval($sum['receives']), 'tag' => '个'];
$data['menus'] = $menus;
}
$data['pager'] = array('count' => ceil($total / $params['size']), 'curr' => $params['page'], 'totle' => $total);
$data['lists'] = $lists;
$data['params'] = $params;
$data['_title'] = "门店日志";
return $data;
}
private function dataUsersSelect($params)
{
$_title = '顾问日志';
$lists = $where = [];
if ($params['uname']) {
$where["sale_id in(select id from lc_app_licheb_users where uname like '%{$params['uname']}%')"] = null;
}
if ($params['biz_id']) {
$where['biz_id'] = $params['biz_id'];
$re_biz = $this->mdBiz->get(['id' => $params['biz_id']]);
$re_biz && $_title = $re_biz['biz_name'] . '_顾问日志';
}
if ($params['time']) {
$time = explode(' ~ ', $params['time']);
$time[0] && $where["log_date>="] = $time[0];
$time[1] && $where["log_date<="] = $time[1];
}
$total = $this->mdUsersLog->count($where);
if ($total) {
$res = $this->mdUsersLog->select($where, "log_date desc,id desc", $params['page'], $params['size']);
$sale_ids = [];
foreach ($res as $v) {
$v['sale_id'] && !in_array($v['sale_id'], $sale_ids) && $sale_ids[] = $v['sale_id'];
}
$map_sale = [];
if ($sale_ids) {
$str_ids = implode(',', $sale_ids);
$map_sale = $this->mdUsers->map('id', 'uname', ["id in ({$str_ids})" => null]);
}
$offlineSources = $this->mdCustomers->offlineSources();
foreach ($res as $v) {
$uname = $map_sale[$v['sale_id']] ? $map_sale[$v['sale_id']] : '';
$temp = ['uname' => $uname, 't_num' => $v['t_num'], 'defeats' => $v['defeats'], 'bills' => $v['bills']
, 'qy_adds' => $v['qy_adds'], 'qy_dels' => $v['qy_dels'], 'log_date' => $v['log_date']];
$customer_json = $v['customer_json'] ? json_decode($v['customer_json'], true) : [];
$c_num_json = $v['customer_json'] ? json_decode($v['c_num_json'], true) : [];
$order_json = $v['customer_json'] ? json_decode($v['order_json'], true) : [];
$customers = $c_num = $orders = 0;
foreach ($offlineSources as $key1 => $value1) {
$nums = intval($v['customer_' . $key1]);
$nums_c_num = intval($v['c_num_' . $key1]);
$nums_orders = intval($v['order_' . $key1]);
$nums && $customers += $nums;
$nums_c_num && $c_num += $nums_c_num;
$nums_orders && $orders += $nums_orders;
if ($params['size'] == 10000) {//导出使用
$temp['customer_' . $key1] = $nums;
$temp['c_num_' . $key1] = $nums_c_num;
$temp['order_' . $key1] = $nums_orders;
foreach ($value1['list'] as $key2 => $value2) {
$temp['customer_' . $key2] = intval($customer_json[$key1]['list'][$key2]['num']);
$temp['c_num_' . $key2] = intval($c_num_json[$key1]['list'][$key2]['num']);
$temp['order_' . $key2] = intval($order_json[$key1]['list'][$key2]['num']);
}
}
}
$temp['customers'] = $customers;
$temp['c_num'] = $c_num;
$temp['orders'] = $orders;
$lists[] = $temp;
}
}
if ($params['size'] != 10000) {
$sum = $this->mdUsersLog->sum('customers', $where);
$menus[] = ['title' => "客户数", 'value' => intval($sum['customers']), 'tag' => '人'];
$sum_c = $this->mdUsersLog->sum('c_num', $where);
$menus[] = ['title' => "进店人数", 'value' => intval($sum_c['c_num']), 'tag' => '人'];
$sum = $this->mdUsersLog->sum('orders', $where);
$menus[] = ['title' => "订单数", 'value' => intval($sum['orders']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('t_num', $where);
$menus[] = ['title' => "试驾数", 'value' => intval($sum['t_num']), 'tag' => '次'];
$sum = $this->mdUsersLog->sum('defeats', $where);
$menus[] = ['title' => "战败数", 'value' => intval($sum['defeats']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('bills', $where);
$menus[] = ['title' => "开票数", 'value' => intval($sum['bills']), 'tag' => '个'];
$sum = $this->mdUsersLog->sum('qy_adds', $where);
$menus[] = ['title' => "新增企微", 'value' => intval($sum['qy_adds']), 'tag' => '人'];
$sum = $this->mdUsersLog->sum('qy_dels', $where);
$menus[] = ['title' => "删除企微", 'value' => intval($sum['qy_dels']), 'tag' => '人'];
$data['menus'] = $menus;
}
$data['pager'] = array('count' => ceil($total / $params['size']), 'curr' => $params['page'], 'totle' => $total);
$data['lists'] = $lists;
$data['params'] = $params;
$data['_title'] = $_title;
return $data;
}
public function visitSelect($params)
{
$_title = '顾问日志';
$lists = $where = [];
if ($params['uname']) {
$where["sales_id in(select id from lc_app_licheb_users where uname like '%{$params['uname']}%')"] = null;
}
if ($params['biz_id']) {
$where['biz_id'] = $params['biz_id'];
$re_biz = $this->mdBiz->get(['id' => $params['biz_id']]);
$re_biz && $_title = $re_biz['biz_name'] . '_顾问日志';
}
if ($params['time']) {
$time = explode(' ~ ', $params['time']);
$time[0] && $where["t_day>="] = $time[0];
$time[1] && $where["t_day<="] = $time[1];
}
$total = $this->mdCustomerVisitSales->count($where);
if ($total) {
$res = $this->mdCustomerVisitSales->select($where, "t_day desc,id desc", $params['page'], $params['size']);
$sales_ids = [];
foreach ($res as $v) {
$v['sales_id'] && !in_array($v['sales_id'], $sales_ids) && $sales_ids[] = $v['sales_id'];
}
$map_sale = [];
if ($sales_ids) {
$str_ids = implode(',', $sales_ids);
$map_sale = $this->mdUsers->map('id', 'uname', ["id in ({$str_ids})" => null]);
}
foreach ($res as $v) {
$uname = '';
$map_sale[$v['sales_id']] && $uname = $map_sale[$v['sales_id']];
$follows = $v['t_follows'] + $v['h_follows'] + $v['w_follows'];
$temp = ['uname' => $uname, 'follows' => $follows, 't_follows' => $v['t_follows']
, 'h_follows' => $v['h_follows'], 'w_follows' => $v['w_follows'], 't_day' => $v['t_day']];
$lists[] = $temp;
}
}
if ($params['size'] != 10000) {
$sum_h = $this->mdCustomerVisitSales->sum('h_follows', $where);
$sum_t = $this->mdCustomerVisitSales->sum('t_follows', $where);
$sum_w = $this->mdCustomerVisitSales->sum('w_follows', $where);
$sum = $sum_h['h_follows'] + $sum_t['t_follows'] + $sum_w['w_follows'];
$menus[] = ['title' => "计划跟进数", 'value' => intval($sum), 'tag' => '个'];
$menus[] = ['title' => "已跟进数", 'value' => intval($sum_h['h_follows']), 'tag' => '个'];
$menus[] = ['title' => "新增未跟进数", 'value' => intval($sum_t['t_follows']), 'tag' => '个'];
$menus[] = ['title' => "逾期未跟进数", 'value' => intval($sum_w['w_follows']), 'tag' => '个'];
$data['menus'] = $menus;
}
$data['pager'] = array('count' => ceil($total / $params['size']), 'curr' => $params['page'], 'totle' => $total);
$data['lists'] = $lists;
$data['params'] = $params;
$data['_title'] = $_title;
return $data;
}
public function get()
{
}
public function add()
{
}
public function edit()
{
}
public function del()
{
}
public function batch()
{
}
public function export()
{
$params = $this->input->get();
$params['page'] = 1;
$params['size'] = 10000;
$data = $indexs = [];
if ($params['tab'] == 1) {//客户统计
$res = $this->dataUsersSelect($params);
$offlineSources = $this->mdCustomers->offlineSources();
$indexs = ['uname' => '顾问', 'customers' => '客户数︾'];
foreach ($offlineSources as $key1 => $value1) {
$indexs['customer_' . $key1] = '【' . $value1['name'] . '】';
foreach ($value1['list'] as $key2 => $value2) {
$indexs['customer_' . $key2] = $value2;
}
}
$indexs['c_num'] = '进店人数︾';
foreach ($offlineSources as $key1 => $value1) {
$indexs['c_num_' . $key1] = '【' . $value1['name'] . '】';
foreach ($value1['list'] as $key2 => $value2) {
$indexs['c_num_' . $key2] = $value2;
}
}
$indexs['orders'] = '订单数︾';
foreach ($offlineSources as $key1 => $value1) {
$indexs['order_' . $key1] = '【' . $value1['name'] . '】';
foreach ($value1['list'] as $key2 => $value2) {
$indexs['order_' . $key2] = $value2;
}
}
$indexs['t_num'] = '试驾数';
$indexs['defeats'] = '战败数';
$indexs['bills'] = '开票数';
$indexs['qy_adds'] = '新增企微';
$indexs['qy_dels'] = '删除企微';
$indexs['log_date'] = '日期';
$fileName = $res['_title'] . '_' . $this->tabAry[$params['tab']];
} else if ($params['tab'] == 2) {//回访统计
$res = $this->visitSelect($params);
$indexs = ['biz_name' => '门店', 'uname' => '顾问', 'follows' => '计划跟进数', 'h_follows' => '已跟进数'
, 't_follows' => '今日新增未跟进数', 'w_follows' => '逾期未跟进数', 't_day' => '日期'];
$fileName = $res['_title'] . '_' . $this->tabAry[$params['tab']];
} else {
$res = $this->dataBizSelect($params);
$offlineSources = $this->mdCustomers->offlineSources();
$indexs = ['biz_name' => '门店', 'customers' => '客户数︾'];
foreach ($offlineSources as $key1 => $value1) {
$indexs['customer_' . $key1] = '【' . $value1['name'] . '】';
foreach ($value1['list'] as $key2 => $value2) {
$indexs['customer_' . $key2] = $value2;
}
}
$indexs['c_num'] = '进店人数︾';
foreach ($offlineSources as $key1 => $value1) {
$indexs['c_num_' . $key1] = '【' . $value1['name'] . '】';
foreach ($value1['list'] as $key2 => $value2) {
$indexs['c_num_' . $key2] = $value2;
}
}
$indexs['orders'] = '订单数︾';
foreach ($offlineSources as $key1 => $value1) {
$indexs['order_' . $key1] = '【' . $value1['name'] . '】';
foreach ($value1['list'] as $key2 => $value2) {
$indexs['order_' . $key2] = $value2;
}
}
$indexs['t_num'] = '试驾数';
$indexs['defeats'] = '战败数';
$indexs['bills'] = '开票数';
$indexs['qy_adds'] = '企微数';
$indexs['reassigns'] = '改派数';
$indexs['receives'] = '接收数';
$indexs['log_date'] = '日期';
$fileName = $res['_title'];
}
foreach ($res['lists'] as $key => $value) {
$data[] = $value;
}
array_unshift($data, $indexs);
$this->load->library('excel');
$this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis'));
}
}