Files
2025-01-20 08:47:45 +08:00

1137 lines
43 KiB
PHP

<?php
defined('WXAPP_APP') or exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/09/16
* Time: 10:30
*/
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
class Statistics extends Wxapp
{
function __construct($inputs, $app_key)
{
parent::__construct($inputs, $app_key);
$this->login_white = array();//登录白名单
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg = array();//授权微信信息
$this->load->model("biz/biz_model");
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->load->model('receiver/order/receiver_orders_model', 'orders_model');
$this->load->model('receiver/receiver_customer_sign_model', 'customer_sign_model');
}
protected function get()
{
}
//统计客户
protected function get_cust()
{
$season = $this->input_param('season');
$city_id = $this->input_param('city_id');
!strlen($season) && $season = date('n') - 1;//当前月份
$month = $season + 1;
$season_data = $this->months();
$series = [];
$bizs = [];
$months_arr = [];
$months_arr[] = [
'cn' => $season_data[$season],
's_time' => date("Y-{$month}-01 00:00:00"),
'e_time' => date("Y-{$month}-t 23:59:59"),
];
$fileds = 'id,biz_name';
$biz_id_arr = explode(',', $this->session['biz_id']);
if ($this->session['biz_id'] && $biz_id_arr) {
$city_id && $o_where = ['city_id' => $city_id];
$bizs_lists = $this->biz_model->get_by_id_arr($biz_id_arr, $o_where, $fileds);
} else {
$bizs_lists = $this->biz_model->select(['status' => 1, 'city_id' => $city_id], 'id desc', '', '', $fileds);
}
if ($bizs_lists) {
$bizs = array_column($bizs_lists, 'biz_name');
foreach ($bizs_lists as $key => $val) {
$count_data = [];
foreach ($months_arr as $v2) {
$where = [
'biz_id' => $val['id'],
'status>=' => 0,
'c_time>=' => strtotime($v2['s_time']),
'c_time<=' => strtotime($v2['e_time']),
'brand_id!=' => 3,
];
$count_data[] = $this->customers_model->count($where);
}
$series[] = [
'name' => $val['biz_name'],
'type' => 'bar',
'data' => $count_data
];
}
}
$stat_data = [
'tooltip' => [
'trigger' => 'axis',
'axisPointer' => ['type' => 'shadow']
],
'legend' => [
'top' => 'bottom',
'data' => $bizs
],
'grid' => [
'top' => '8%',
'left' => '2%',
'right' => '5%',
'bottom' => '12%',
'containLabel' => true
],
'xAxis' => [
'type' => 'value',
'boundaryGap' => [0, 0.01]
],
'yAxis' => [
'type' => 'category',
'data' => array_column($months_arr, 'cn')
],
'series' => $series
];
$data = [
'stat_data' => $stat_data,
'season_data' => $season_data,
'season' => $season
];
return $data;
}
//统计订单
protected function get_orders()
{
$season = $this->input_param('season');
$city_id = $this->input_param('city_id');
!strlen($season) && $season = ceil((date('n')) / 3) - 1;//当月是第几季度
$season_data = $this->season_data($season + 1);
$days = getMonth($season + 1);
$series = [];
$bizs = [];
$months_arr = [];
foreach ($days as $val) {
$months_arr[] = [
'cn' => date('n月', strtotime($val)),
's_time' => date('Y-m-01 00:00:00', strtotime($val)),
'e_time' => date('Y-m-t 23:59:59', strtotime($val)),
];
}
$fileds = 'id,biz_name';
$biz_id_arr = explode(',', $this->session['biz_id']);
if ($this->session['biz_id'] && $biz_id_arr) {
$city_id && $o_where = ['city_id' => $city_id];
$bizs_lists = $this->biz_model->get_by_id_arr($biz_id_arr, $o_where, $fileds);
} else {
$bizs_lists = $this->biz_model->select(['status' => 1, 'city_id' => $city_id], 'id desc', '', '', $fileds);
}
if ($bizs_lists) {
$bizs = array_column($bizs_lists, 'biz_name');
foreach ($bizs_lists as $key => $val) {
$count_data = [];
foreach ($months_arr as $v2) {
$where = [
'biz_id' => $val['id'],
'status>=' => 0,
'c_time>=' => strtotime($v2['s_time']),
'c_time<=' => strtotime($v2['e_time']),
'brand_id!=' => 3,
];
$count_data[] = $this->orders_model->count($where);
}
$series[] = [
'name' => $val['biz_name'],
'type' => 'line',
'stack' => '总量',
'data' => $count_data,
'smooth' => true
];
}
}
$stat_data = [
'tooltip' => ['trigger' => 'axis'],
'legend' => ['top' => 'bottom', 'data' => $bizs],
'grid' => [
'top' => '10%',
'left' => '2%',
'right' => '5%',
'bottom' => '12%',
'containLabel' => true
],
'xAxis' => [
'type' => 'category',
'boundaryGap' => false,
'data' => array_column($months_arr, 'cn')
],
'yAxis' => ['type' => 'value'],
'series' => $series,
];
$data = [
'stat_data' => $stat_data,
'season_data' => $season_data,
'season' => $season
];
return $data;
}
//数据分析页面客户数据
protected function get_scust()
{
$day = $this->input_param('day');
$month = $this->input_param('month');
$biz_id = $this->input_param('biz_id');
$admin_id = $this->input_param('admin_id');
if (!$biz_id) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if (!$day && $month) {
$day = date("Y-{$month}-01");
}
!$day && $day = date('Y-m-d');//获取当天
$status_list = [
[
'title' => '订单客户',
'icon' => 'icon-statistics-custom-1',
'status' => 2
],
[
'title' => '到店客户',
'icon' => 'icon-statistics-custom-2',
'status' => 1
],
[
'title' => '未见客户',
'icon' => 'icon-statistics-custom-3',
'status' => 0
],
[
'title' => '未联潜客',
'icon' => 'icon-statistics-custom-4',
'status' => ''
],
[
'title' => '战败客户',
'icon' => 'icon-statistics-custom-5',
'status' => 3
],
];
foreach ($status_list as $key => $val) {
$setValue = [
'title' => $val['title'],
'icon' => $val['icon'],
];
$where = [
'biz_id' => $biz_id,
'brand_id!=' => 3
];
$admin_id && $where['admin_id'] = $admin_id;
if ($day) {
$s_time = date('Y-m-01 00:00:00', strtotime($day));
$e_time = date('Y-m-t 23:59:59', strtotime($day));
$where['c_time>='] = strtotime($s_time);
$where['c_time<='] = strtotime($e_time);
}
if (strlen($val['status'])) {
$where['status'] = $val['status'];
$setValue['value'] = $this->customers_model->count($where);
} else {
$where["cont_time = '0000-00-00 00:00:00'"] = null;
$setValue['value'] = $this->customers_model->count($where);
}
$custom[] = $setValue;
}
$months_arr = [];
for ($i = 2; $i >= 0; $i--) {
$_time = date("Y-m-01", mktime(0, 0, 0, date("m", strtotime($day)) - $i, 1, date("Y", strtotime($day))));
$months_arr [] = [
'cn' => date('n月', strtotime($_time)),
's_time' => date('Y-m-01 00:00:00', strtotime($_time)),
'e_time' => date('Y-m-t 23:59:59', strtotime($_time))
];
}
foreach ($months_arr as $val) {
$where = [
'biz_id' => $biz_id,
'status>=' => 0,
'c_time>=' => strtotime($val['s_time']),
'c_time<=' => strtotime($val['e_time']),
'brand_id!=' => 3,
];
$count_data[] = $this->customers_model->count($where);
}
//图标数据
$stat_data = [
'tooltip' => [
'trigger' => 'axis',
'axisPointer' => [
'type' => 'shadow'
]
],
'grid' => [
'top' => '10%',
'left' => '2%',
'right' => '5%',
'bottom' => '5%',
'containLabel' => true
],
'xAxis' => [
'type' => 'value',
'boundaryGap' => [0, 0.01]
],
'yAxis' => [
'type' => 'category',
'data' => array_column($months_arr, 'cn'),
],
'series' => [
[
'type' => 'bar',
'itemStyle' => [
'color' => '#2e3246',
'borderRadius' => [0, 20, 20, 0],
],
'barWidth' => '15',
'data' => $count_data,
'label' => [
'show' => true,
'position' => 'right',
'formatter' => '{@[n]}',
'valueAnimation' => true
]
]
]
];
$data = [
'custom' => $custom,
'stat_data' => $stat_data,
];
return $data;
}
//数据分析页面订单数据
protected function get_ocust()
{
$day = $this->input_param('day');
$biz_id = $this->input_param('biz_id');
$month = $this->input_param('month');
$admin_id = $this->input_param('admin_id');
if (!$biz_id) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
if (!$day && $month) {
$day = date("Y-{$month}-01");
}
!$day && $day = date('Y-m-d');//获取当天
$status_list = [
[
'title' => '合同签订',
'icon' => 'icon-statistics-order-1',
'status' => 0
],
[
'title' => '分期办理',
'icon' => 'icon-statistics-order-2',
'status' => 1
],
[
'title' => '车辆确认',
'icon' => 'icon-statistics-order-3',
'status' => 2
],
[
'title' => '申请开票',
'icon' => 'icon-statistics-order-4',
'status' => 3
],
[
'title' => '代办服务',
'icon' => 'icon-statistics-order-5',
'status' => 4
],
[
'title' => '交付确认',
'icon' => 'icon-statistics-order-6',
'status' => 5
],
[
'title' => '完成交付',
'icon' => 'icon-statistics-order-7',
'status' => 6
],
];
foreach ($status_list as $key => $val) {
$setValue = [
'title' => $val['title'],
'icon' => $val['icon'],
];
$where = [
'status' => $val['status'],
'biz_id' => $biz_id,
'brand_id!=' => 3,
];
if ($day) {
$s_time = date('Y-m-01 00:00:00', strtotime($day));
$e_time = date('Y-m-t 23:59:59', strtotime($day));
$where['c_time>='] = strtotime($s_time);
$where['c_time<='] = strtotime($e_time);
}
$admin_id && $where['admin_id'] = $admin_id;
$setValue['value'] = $this->orders_model->count($where);
$custom[] = $setValue;
}
$months_arr = [];
for ($i = 2; $i >= 0; $i--) {
$_time = date("Y-m-01", mktime(0, 0, 0, date("m", strtotime($day)) - $i, 1, date("Y", strtotime($day))));
$months_arr [] = [
'cn' => date('n月', strtotime($_time)),
's_time' => date('Y-m-01 00:00:00', strtotime($_time)),
'e_time' => date('Y-m-t 23:59:59', strtotime($_time))
];
}
foreach ($months_arr as $val) {
$where = [
'biz_id' => $biz_id,
'status>=' => 0,
'c_time>=' => strtotime($val['s_time']),
'c_time<=' => strtotime($val['e_time']),
'brand_id!=' => 3,
];
$count_data[] = $this->orders_model->count($where);
}
$stat_data = [
'grid' => [
'top' => '15%',
'left' => '2%',
'right' => '5%',
'bottom' => '5%',
'containLabel' => true
],
'xAxis' => [
'type' => 'category',
'data' => array_column($months_arr, 'cn'),
],
'yAxis' => [
'type' => 'value'
],
'series' => [
[
'data' => $count_data,
'type' => 'line',
'smooth' => true
]
]
];
$data = [
'custom' => $custom,
'stat_data' => $stat_data,
];
return $data;
}
//首页饼状图客户统计
protected function get_hcust()
{
$biz_id = $this->input_param('biz_id');
$group_id = $this->session['group_id'];
$uid = $this->session['uid'];
!$biz_id && $biz_id = intval($this->session['biz_id']);
if (!$biz_id) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$status_list = $this->customers_model->get_status();
unset($status_list[-1]);
$count_data = [];
foreach ($status_list as $key => $val) {
$where = [
'biz_id' => $biz_id,
'status' => $key
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //客户成功经理过滤
$count = $this->customers_model->count($where);
$count_data[] = [
'value' => $count,
'name' => $val
];
}
//所有客户数据
$where = [
'status>=' => 0,
'biz_id' => $biz_id
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //客户成功经理过滤
$count = $this->customers_model->count($where);
$where = [
'status>=' => 0,
'biz_id' => $biz_id,
'is_top' => 1
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //客户成功经理过滤
$top_count = $this->customers_model->count($where); //关注
$where = [
'status>=' => 0,
'biz_id' => $biz_id,
"cont_time = '0000-00-00 00:00:00'" => null
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //客户成功经理过滤
$un_count = $this->customers_model->count($where);//未联系
$stat_data = [
'tooltip' => [
'trigger' => 'axis',
],
'legend' => [
'top' => 'bottom',
'orient' => 'horizontal',
'textStyle' => [
'fontSize' => 9,
]
],
'series' => [
'type' => 'pie',
'top' => '0',
'radius' => ['30%', '48%'],
'data' => $count_data,
'label' => [
'formatter' => '\n{b|{b}}\n{c|{c}}\n{per|{d}%} ',
'rich' => [
'b' => [
'color' => '#4C5058',
'fontSize' => 10,
'lineHeight' => 15,
'align' => 'left',
],
'b' => [
'color' => '#4C5058',
'fontSize' => 10,
'lineHeight' => 15,
'align' => 'left',
],
'per' => [
'color' => '#4C5058',
'fontSize' => 10,
'lineHeight' => 15,
'align' => 'left',
],
]
],
]
];
$data = [
'stat_data' => $stat_data,
'total' => $count,
'cont_total' => $un_count,
'top_total' => $top_count
];
return $data;
}
//首页柱状图订单统计
protected function get_horder()
{
$biz_id = $this->input_param('biz_id');
$group_id = $this->session['group_id'];
$uid = $this->session['uid'];
!$biz_id && $biz_id = intval($this->session['biz_id']);
if (!$biz_id) {
throw new Exception('参数错误', ERR_PARAMS_ERROR);
}
$status_list = $this->orders_model->get_status();
unset($status_list[7]);
$count_data = [];
$total = 0;
foreach ($status_list as $key => $val) {
$where = [
'biz_id' => $biz_id,
'status' => $key
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //客户成功经理过滤
$count = $this->orders_model->count($where);
$total += $count;
$count_data[] = [
'value' => $count,
'name' => $val
];
}
$stat_data = [
'tooltip' => [
'trigger' => 'axis',
'axisPointer' => [
'type' => 'shadow'
]
],
'grid' => [
'top' => '8%',
'left' => '2%',
'right' => '5%',
'bottom' => '2%',
'containLabel' => true
],
'xAxis' => [
'type' => 'value',
'boundaryGap' => [0, 0.01]
],
'yAxis' => [
'type' => 'category',
'data' => array_column($count_data, 'name'),
],
'series' => [
'type' => 'bar',
'itemStyle' => [
'color' => '#2e3246',
'borderRadius' => [0, 20, 20, 0],
],
'barWidth' => '15',
'data' => array_column($count_data, 'value'),
'label' => [
'show' => true,
'position' => 'right',
'formatter' => '{@[n]}',
'valueAnimation' => true
]
]
];
$data = [
'stat_data' => $stat_data,
'total' => $total
];
return $data;
}
/**
* 获取月份
*/
private function months()
{
$months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
return $months;
}
/**
* 获取季度
* @param int $season 当前季度
*/
private function season_data($season)
{
$chiNum = ['一', '二', '三', '四'];
if ($season > 4) {
return [];
}
for ($i = 0; $i < $season; $i++) {
$season_data[] = "{$chiNum[$i]}季度";
}
return $season_data;
}
protected function get_stats()
{
$s_time = $this->input_param('s_time');
$e_time = $this->input_param('e_time');
$province_id = intval($this->input_param('province_id'));
$city_id = intval($this->input_param('city_id'));
!$s_time && $s_time = date('Y-m-d');
!$e_time && $e_time = date('Y-m-d');
$c_time = ['s_time' => strtotime($s_time), 'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($e_time)))];
$o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $e_time . ' 23:59:59'];
if ($city_id) {
// $biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$bizs = array_column($bizs, 'id');
$this->session['biz_id'] && $bizs = array_intersect($bizs, explode(',', $this->session['biz_id']));
$biz_id_arr = implode(',', array_unique($bizs));
} else {
if ($this->session['biz_id']) {
$biz_id_arr = $this->session['biz_id'];
} else {
// $biz_where = array('province_id' => $province_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$biz_id_arr = implode(',', array_unique(array_column($bizs, 'id')));
}
}
!$biz_id_arr && $biz_id_arr = 0;
//客户
$c_where = [
'status>=' => 0,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null,
];
$customers = $this->customers_model->count($c_where);
//进店
// $s_where = [
// "c_time>=" => $c_time['s_time'],
// "c_time<=" => $c_time['e_time'],
// "status" => 1,
// "biz_id in ({$biz_id_arr})" => null,
// ];
// $sign = $this->customer_sign_model->count($s_where);
$s_where = [
'status>=' => 0,
'dt_time>=' => $o_time['s_time'],
'dt_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null,
];
$sign = $this->customers_model->count($s_where);
//个微
$g_where = [
'status>=' => 0,
'wxgr' => 1,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null,
];
$wxgr = $this->customers_model->count($g_where);
//成交
$o_where = [
'status>=' => 0,
'order_time>=' => $o_time['s_time'],
'order_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null,
];
$orders = $this->orders_model->count($o_where);
$data['tabs'] = [
['title' => '所有客户', 'value_1' => $customers, 'url' => '/pages/dataAnalysis/detail/index?type=0'],
['title' => '到店客户', 'value_1' => $sign, 'url' => '/pages/dataAnalysis/detail/index?type=1'],
['title' => '添加微信', 'value_1' => $wxgr, 'url' => '/pages/dataAnalysis/detail/index?type=2'],
['title' => '所有订单', 'value_1' => $orders, 'url' => '/pages/dataAnalysis/detail/index?type=3'],
];
$orders_per = $customers ? $orders / $customers : 0;
$intos_per = $customers ? $sign / $customers : 0;
$orders_per = number_format_com($orders_per * 100, 1, '');
$intos_per = number_format_com($intos_per * 100, 1, '');
$data['funnel'] = [
'title' => '转化漏斗',
'expected_data' => [
['name' => "订单({$orders_per}%)", 'value' => 33.3],
['name' => "到店({$intos_per}%)", 'value' => 66.7],
['name' => '客户(100%)', 'value' => 100]
],
'actual_data' => [
['name' => '订单', 'value' => $orders],
['name' => '到店', 'value' => $sign],
['name' => '客户', 'value' => $customers]
]
];
$data['title'] = '数据看板';
return $data;
}
protected function get_stats_days()
{
$days = intval($this->input_param('days'));
$province_id = intval($this->input_param('province_id'));
$city_id = intval($this->input_param('city_id'));
!$days && $days = 7;
$customers = $orders = $xAxis = [];
for ($i = ($days - 1); $i >= 0; $i--) {
$s_time = date('Y-m-d', strtotime("-{$i} day"));
$c_time = ['s_time' => strtotime($s_time), 'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($s_time)))];
$o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $s_time . ' 23:59:59'];
$xAxis[] = $days > 7 ? date('d', strtotime($s_time)) : date('n-d', strtotime($s_time));
$c_where = [
'status>=' => 0,
'c_time>=' => $c_time['s_time'],
'c_time<=' => $c_time['e_time'],
];
if ($city_id) {
// $biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$bizs = array_column($bizs, 'id');
$this->session['biz_id'] && $bizs = array_intersect($bizs, explode(',', $this->session['biz_id']));
$biz_id_arr = implode(',', array_unique($bizs));
} else {
if ($this->session['biz_id']) {
$biz_id_arr = $this->session['biz_id'];
} else {
// $biz_where = array('province_id' => $province_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$biz_id_arr = implode(',', array_unique(array_column($bizs, 'id')));
}
}
!$biz_id_arr && $biz_id_arr = 0;
$c_where["biz_id in ({$biz_id_arr})"] = null;
$customers[] = $this->customers_model->count($c_where);
$o_where = [
'status>=' => 0,
'order_time>=' => $o_time['s_time'],
'order_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null,
];
$orders[] = $this->orders_model->count($o_where);
}
$title = $days == 7 ? "近一周" : "{$days}";
$data = [
'title' => "{$title}走势", 'legend_data' => ["客户", "订单"],
'xAxis' => $xAxis,
'series' => [
['name' => "客户", 'type' => 'line', 'smooth' => true, 'data' => $customers],
['name' => "订单", 'type' => 'line', 'smooth' => true, 'data' => $orders]
]
];
return $data;
}
protected function get_stats_customer()
{
$s_time = $this->input_param('s_time');
$e_time = $this->input_param('e_time');
$province_id = intval($this->input_param('province_id'));
$city_id = intval($this->input_param('city_id'));
$type = intval($this->input_param('type'));
!$s_time && $s_time = date('Y-m-d');
!$e_time && $e_time = date('Y-m-d');
$c_time = [
's_time' => strtotime($s_time),
'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($e_time)))
];
$o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $e_time . ' 23:59:59'];
$pie1_level = $pie1_offline = $biz_customer = [];
$bottom2 = $bottom1 = '';
if ($city_id) {
// $biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$bizs = array_column($bizs, 'id');
$this->session['biz_id'] && $bizs = array_intersect($bizs, explode(',', $this->session['biz_id']));
$biz_id_arr = implode(',', array_unique($bizs));
} else {
if ($this->session['biz_id']) {
$biz_id_arr = $this->session['biz_id'];
} else {
// $biz_where = array('province_id' => $province_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$biz_id_arr = implode(',', array_unique(array_column($bizs, 'id')));
}
}
if ($biz_id_arr) {
// $biz_where = array('status' => 1, 'type' => 0, "id in ({$biz_id_arr})" => null);
$biz_where = array('status' => 1, "id in ({$biz_id_arr})" => null);
$bizs = $this->biz_model->select($biz_where);
$sdata_level = $this->customers_model->get_sdata('level');
foreach ($sdata_level as $v) {
if ($type == 1) {
$title = '到店客户';
// $where = [
// "a.c_time>=" => $c_time['s_time'],
// "a.c_time<=" => $c_time['e_time'],
// "a.status" => 1,
// "b.level" => $v,
// "b.biz_id in ({$biz_id_arr})" => null,
// ];
// $value = $this->customer_sign_model->db->select('a.id')
// ->from('lc_receiver_customer_sign as a')
// ->join('lc_receiver_customers as b', "a.customer_id=b.id", 'left')
// ->where($where)
// ->count_all_results();
$where = [
'status>=' => 0,
'dt_time>=' => $o_time['s_time'],
'dt_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null
];
$value = $this->customers_model->count($where);
} elseif ($type == 2) {
$title = '添加微信';
$c_where = [
'status>=' => 0,
'level' => $v,
'wxgr' => 1,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null
];
$value = $this->customers_model->count($c_where);
} elseif ($type == 3) {
$title = '所有订单';
$where = [
'a.order_time>=' => $o_time['s_time'],
'a.order_time<=' => $o_time['e_time'],
"a.status >=" => 0,
"b.level" => $v,
"b.biz_id in ({$biz_id_arr})" => null,
];
$value = $this->orders_model->db->select('a.id')
->from('lc_receiver_orders as a')
->join('lc_receiver_customers as b', "a.customer_id=b.id", 'left')
->where($where)
->count_all_results();
} else {
$title = '所有客户';
$c_where = [
'status>=' => 0,
'level' => $v,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null
];
$value = $this->customers_model->count($c_where);
}
if ($value) {
$pie1_level[] = ['name' => $v, 'value' => $value];
$bottom1 += $value;
}
}
$offlineSources = $this->customers_model->offlineSources();
foreach ($offlineSources as $k => $v) {
$id = count($v["list"]) > 0 ? $k : 0;
if ($type == 1) {
//到店
// $where = [
// "a.c_time>=" => $c_time['s_time'],
// "a.c_time<=" => $c_time['e_time'],
// "a.status" => 1,
// "b.of_id" => $k,
// "b.biz_id in ({$biz_id_arr})" => null,
// ];
// $value = $this->customer_sign_model->db->select('a.id')
// ->from('lc_receiver_customer_sign as a')
// ->join('lc_receiver_customers as b', "a.customer_id=b.id", 'left')
// ->where($where)
// ->count_all_results();
$where = [
'status>=' => 0,
'of_id' => $k,
'dt_time>=' => $o_time['s_time'],
'dt_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null
];
$value = $this->customers_model->count($where);
} elseif ($type == 2) {
//个微
$c_where = [
'status>=' => 0,
'of_id' => $k,
'wxgr' => 1,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null
];
$value = $this->customers_model->count($c_where);
} elseif ($type == 3) {
//订单
$where = [
'a.order_time>=' => $o_time['s_time'],
'a.order_time<=' => $o_time['e_time'],
"a.status >=" => 0,
"b.of_id" => $k,
"b.biz_id in ({$biz_id_arr})" => null,
];
$value = $this->orders_model->db->select('a.id')
->from('lc_receiver_orders as a')
->join('lc_receiver_customers as b', "a.customer_id=b.id", 'left')
->where($where)
->count_all_results();
} else {
//所有
$c_where = [
'status>=' => 0,
'of_id' => $k,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null
];
$value = $this->customers_model->count($c_where);
}
$value && $pie1_offline[] = ['id' => $id, 'name' => $v['name'], 'value' => $value];
}
//门店归属
foreach ($bizs as $k => $v) {
$biz_arr[] = str_replace('车卖场', '', $v['biz_name']);
if ($type == 1) {
//进店
// $s_where = [
// "c_time>=" => $c_time['s_time'],
// "c_time<=" => $c_time['e_time'],
// "status" => 1,
// "biz_id" => $v['id'],
// ];
// $value = $this->customer_sign_model->count($s_where);
$where = [
'status>=' => 0,
'biz_id' => $v['id'],
'dt_time>=' => $o_time['s_time'],
'dt_time<=' => $o_time['e_time'],
];
$value = $this->customers_model->count($where);
} elseif ($type == 2) {
$where = [
'status>=' => 0,
'biz_id' => $v['id'],
'wxgr' => 1,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
];
$value = $this->customers_model->count($where);
} elseif ($type == 3) {
$where = [
'status>=' => 0,
'biz_id' => $v['id'],
'order_time>=' => $o_time['s_time'],
'order_time<=' => $o_time['e_time'],
];
$value = $this->orders_model->count($where);
} else {
$where = [
'status>=' => 0,
'biz_id' => $v['id'],
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
];
$value = $this->customers_model->count($where);
}
$biz_customer[] = $value;
$bottom2 += $value;
}
}
$data['pie1'] = ['title' => '客户来源', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline, 'bottom' => "合计: {$bottom1}"];
$data['bar'] = ['title' => '归属门店', 'xAxis_data' => $biz_arr, 'series_data' => $biz_customer, 'bottom' => "合计: {$bottom2}"];
$data['title'] = $title;
return $data;
}
protected function get_stats_customer_pid()
{
$id = $this->input_param('id');
$type = $this->input_param('type');
$s_time = $this->input_param('s_time');
$e_time = $this->input_param('e_time');
$province_id = intval($this->input_param('province_id'));
$city_id = intval($this->input_param('city_id'));
!$s_time && $s_time = date('Y-m-d');
!$e_time && $e_time = date('Y-m-d');
$c_time = ['s_time' => strtotime($s_time), 'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($e_time)))];
$pie_offline = [];
$o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $e_time . ' 23:59:59'];
if ($city_id) {
// $biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'city_id' => $city_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$bizs = array_column($bizs, 'id');
$this->session['biz_id'] && $bizs = array_intersect($bizs, explode(',', $this->session['biz_id']));
$biz_id_arr = implode(',', array_unique($bizs));
} else {
if ($this->session['biz_id']) {
$biz_id_arr = $this->session['biz_id'];
} else {
// $biz_where = array('province_id' => $province_id, 'status' => 1, 'type' => 0);
$biz_where = array('province_id' => $province_id, 'status' => 1);
$bizs = $this->biz_model->select($biz_where);
$biz_id_arr = implode(',', array_unique(array_column($bizs, 'id')));
}
}
if ($biz_id_arr) {
$offlineSources = $this->customers_model->offlineSources($id, 0);
foreach ($offlineSources["list"] as $k => $v) {
$title = $offlineSources['name'];
if ($type == 1) {
$where = [
"a.c_time>=" => $c_time['s_time'],
"a.c_time<=" => $c_time['e_time'],
"a.status" => 1,
'b.of_id' => $id,
'b.of2_id' => $k,
"b.biz_id in ({$biz_id_arr})" => null,
];
$value_1 = $this->customer_sign_model->db->select('a.id')
->from('lc_receiver_customer_sign as a')
->join('lc_receiver_customers as b', "a.customer_id=b.id", 'left')
->where($where)
->count_all_results();
$value_1 && $pie_offline[] = ['id' => $id, 'name' => $v, 'value' => $value_1];
} else if ($type == 2) {
$where = [
'status>=' => 0,
"biz_id in ({$biz_id_arr})" => null,
'wxgr' => 1,
'of_id' => $id,
'of2_id' => $k,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
];
$value_1 = $this->customers_model->count($where);
$value_1 && $pie_offline[] = ['name' => $v, 'value' => $value_1];
} else if ($type == 3) {
$where = [
'a.order_time>=' => $o_time['s_time'],
'a.order_time<=' => $o_time['e_time'],
"a.status >=" => 0,
'b.of_id' => $id,
'b.of2_id' => $k,
"b.biz_id in ({$biz_id_arr})" => null,
];
$value_1 = $this->orders_model->db->select('a.id')
->from('lc_receiver_orders as a')
->join('lc_receiver_customers as b', "a.customer_id=b.id", 'left')
->where($where)
->count_all_results();
$value_1 && $pie_offline[] = ['name' => $v, 'value' => $value_1];
} else {
$where = [
'status>=' => 0,
'p_time>=' => $o_time['s_time'],
'p_time<=' => $o_time['e_time'],
"biz_id in ({$biz_id_arr})" => null,
];
$value_1 = $this->customers_model->count(array_merge($where, ['of_id' => $id, 'of2_id' => $k]));
$value_1 && $pie_offline[] = ['name' => $v, 'value' => $value_1];
}
$bottom += $value_1;
}
}
return ['title' => $title, 'series_data' => $pie_offline, 'bottom' => "合计: {$bottom}"];
}
}