Files
liche/api/controllers/wxapp/licheb/Statistics.php
T
2023-02-14 14:06:31 +08:00

773 lines
26 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->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/order/receiver_orders_v2_model', 'mdOrders');
$this->load->library('receiver/stats_entity');
}
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');
$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'];
//客户
$where = [
'status>=' => 0,
'cs_biz_id!=' => -1,
'c_time>=' => $c_time['s_time'],
'c_time<=' => $c_time['e_time'],
"biz_id in (select id from lc_biz where city_id={$city_id} and status=1)" => null
];
$customers = $this->customers_model->count($where);
//进店
$where = [
'a.type' => 4,
'a.log' => '客户到店',
'a.c_time>=' => $c_time['s_time'],
'a.c_time<=' => $c_time['e_time'],
'b.status>=' => 0,
'b.cs_biz_id!=' => -1,
"b.biz_id in (select id from lc_biz where city_id={$city_id} and status=1)" => null
];
$intos = $this->customers_model->db->select('a.id')
->from('lc_receiver_customer_oplogs as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where($where)
->count_all_results();
//成交
$where = [
'status>=' => 0,
'order_time>=' => $o_time['s_time'],
'order_time<=' => $o_time['e_time'],
"customer_id in (
select id from lc_receiver_customers where status>=0 and cs_biz_id!=-1 and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$orders = $this->mdOrders->count($where);
$where = [
'a.status>=' => 0,
'a.order_time>=' => $o_time['s_time'],
'a.order_time<=' => $o_time['e_time'],
'a.status<>' => 2,
'b.pid_status' => 3,
'b.status in (1,2)' => null,
"a.customer_id in (
select id from lc_receiver_customers where status>=0 and cs_biz_id!=-1 and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$bill_orders = $this->customers_model->db->select('a.id,count(b.status) as total')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_order_status as b', "b.o_id=a.id", 'left')
->where($where)->group_by('b.o_id')->having('total=1')
->count_all_results();
$data['tabs'] = [
['title' => '全部客户', 'value_1' => $customers, 'url' => '/pages/dataAnalysis/detail/index?type=0'],
['title' => '到店客户', 'value_1' => $intos, 'url' => '/pages/dataAnalysis/detail/index?type=1'],
['title' => '订单数', 'value_1' => $orders, 'url' => '/pages/dataAnalysis/detail/index?type=2'],
['title' => '开票数', 'value_1' => $bill_orders, 'url' => '/pages/dataAnalysis/detail/index?type=3'],
];
$orders_per = number_format_com($orders / $customers * 100, 1, '');
$intos_per = number_format_com($intos / $customers * 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' => $intos],
['name' => '客户量', 'value' => $customers]
]
];
$data['title'] = '数据看板';
return $data;
}
protected function get_stats_days()
{
$days = intval($this->input_param('days'));
$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));
$where = [
'status>=' => 0,
'cs_biz_id!=' => -1,
'c_time>=' => $c_time['s_time'],
'c_time<=' => $c_time['e_time'],
"biz_id in (select id from lc_biz where city_id={$city_id} and status=1)" => null
];
$customers[] = $this->customers_model->count($where);
$where = [
'status>=' => 0,
'order_time>=' => $o_time['s_time'],
'order_time<=' => $o_time['e_time'],
"customer_id in (
select id from lc_receiver_customers where status>=0 and cs_biz_id!=-1 and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$orders[] = $this->mdOrders->count($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');
$city_id = intval($this->input_param('city_id'));
$type = intval($this->input_param('type'));
switch ($type){
case 1: //到店客户
return $this->stats_entity->customers($s_time,$e_time,$city_id,$type);
case 2: //订单数
return $this->stats_entity->orders($s_time,$e_time,$city_id);;
case 3: //开票数
return $this->stats_entity->orders($s_time,$e_time,$city_id,$type);
default: //全部客户
return $this->stats_entity->customers($s_time,$e_time,$city_id);
}
}
}