Files
liche/api/controllers/wxapp/licheb/Statistics.php
T
2021-10-14 16:42:15 +08:00

624 lines
19 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');
}
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->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->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>3){
return [];
}
for($i=0;$i<$season;$i++){
$season_data[] = "{$chiNum[$i]}季度";
}
return $season_data;
}
}