Files
liche/common/libraries/receiver/Stats_entity.php
T
2023-02-14 14:06:31 +08:00

368 lines
17 KiB
PHP

<?php
class Stats_entity{
private $ci;
public function __construct(){
$this->ci = & get_instance();
$this->ci->load->model('receiver/receiver_customers_model','customers_model');
$this->ci->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders');
$this->ci->load->model('biz/biz_model');
$this->ci->load->model('auto/auto_brand_model');
}
/**
* @param $s_time
* @param $e_time
* @param $city_id
* @param $type 0全部 1到店
* @return array
*/
public function customers($s_time,$e_time,$city_id,$type=0){
!$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)))
];
$pie1_level = $pie2_level = $pie1_offline = $bizs = $biz_customer = [];
$bottom3 = $bottom2 = $bottom1 = '';
$sdata_level = $this->ci->customers_model->get_sdata('level');
foreach ($sdata_level as $v) {
if($type){
$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.level' => $v,
"b.biz_id in (select id from lc_biz where city_id={$city_id} and status=1)" => null
];
$value = $this->ci->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();
}else{
$where = [
'status>=' => 0,
'cs_biz_id!=' => -1,
'level' => $v,
'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
];
$value = $this->ci->customers_model->count($where);
}
if($value){
$pie1_level[] = ['name' => $v, 'value' => $value];
$bottom1 += $value;
}
}
$offlineSources = $this->ci->customers_model->offlineSources();
foreach ($offlineSources as $k => $v) {
if($type){
$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,
'b.of_id' => $k
];
$value = $this->ci->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();
}else{
$where = [
'status>=' => 0,
'cs_biz_id!=' => -1,
'of_id' => $k,
'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
];
$value = $this->ci->customers_model->count($where);
}
$value && $pie1_offline[] = ['name' => $v['name'], 'value' => $value];
}
$biz_type = $this->ci->biz_model->type_ary();
foreach ($biz_type as $k => $v) {
if($type){
$where = [
'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,
'c.c_time>=' => $c_time['s_time'],
'c.c_time<=' => $c_time['e_time'],
'a.type' => $k,
'c.type' => 4,
'c.log' => '客户到店',
];
$value = $this->ci->customers_model->db->select('b.id')
->from('lc_biz as a')
->join('lc_receiver_customers as b', "a.id=b.biz_id", 'left')
->join('lc_receiver_customer_oplogs as c', "c.customer_id=b.id", 'left')
->where($where)
->count_all_results();
}else{
$where = [
'b.status>=' => 0,
'cs_biz_id!=' => -1,
'b.c_time>=' => $c_time['s_time'],
'b.c_time<=' => $c_time['e_time'],
"a.city_id" => $city_id,
'a.type' => $k
];
$value = $this->ci->customers_model->db->select('b.id')
->from('lc_biz as a')
->join('lc_receiver_customers as b', "a.id=b.biz_id", 'left')
->where($where)
->count_all_results();
}
$pie2_level[] = ['name' => $v, 'value' => $value];
$bottom2 += $value;
}
$biz_rows = $this->ci->biz_model->select(['status' => 1,'city_id' => $city_id], 'id desc', 0, 0, 'id,biz_name');
foreach ($biz_rows as $k => $v) {
$bizs[] = $v['biz_name'];
if($type){
$where = [
'b.status>=' => 0,
'c.c_time>=' => $c_time['s_time'],
'c.c_time<=' => $c_time['e_time'],
'b.biz_id' => $v['id'],
'c.type' => 4,
'c.log' => '客户到店',
];
$value = $this->ci->customers_model->db->select('b.id')
->from('lc_receiver_customers as b')
->join('lc_receiver_customer_oplogs as c', "c.customer_id=b.id", 'left')
->where($where)
->count_all_results();
}else{
$where = [
'status>=' => 0,
'biz_id' => $v['id'],
'c_time>=' => $c_time['s_time'],
'c_time<=' => $c_time['e_time'],
];
$value = $this->ci->customers_model->count($where);
}
$biz_customer[] = $value;
$bottom3 += $value;
}
$data['pie1'] = ['title' => '客户来源', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline,'bottom' => "合计:{$bottom1}"];
$data['pie2'] = ['title' => '门店类型', 'series_data_1' => $pie2_level,'bottom' => "合计:{$bottom2}"];
$data['bar'] = ['title' => '门店列表', 'xAxis_data' => $bizs, 'series_data' => $biz_customer,'bottom' => "合计:{$bottom3}"];
$data['title'] = $type ? '到店客户' : '全部客户';
return $data;
}
/**
* @param $s_time
* @param $e_time
* @param $city_id
* @param $type 0全部 1开票
* @return array
*/
public function orders($s_time,$e_time,$city_id,$type=0){
!$s_time && $s_time = date('Y-m-d');
!$e_time && $e_time = date('Y-m-d');
$o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $e_time . ' 23:59:59'];
$pie1_level = $pie1_offline = $pie2_level = $pie23level = $bizs = $biz_customer = [];
$bottom4 = $bottom3 = $bottom2 = $bottom1 = 0;
$sdata_level = $this->ci->customers_model->get_sdata('level');
foreach ($sdata_level as $v) {
if($type){
$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 level='{$v}' and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$value = $this->ci->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();
}else{
$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 level='{$v}' and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$value = $this->ci->mdOrders->count($where);
}
if($value){
$pie1_level[] = ['name' => $v, 'value' => $value];
$bottom1 += $value;
}
}
$offlineSources = $this->ci->customers_model->offlineSources();
foreach ($offlineSources as $k => $v) {
if($type){
$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 of_id=$k and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$value = $this->ci->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();
}else{
$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 of_id=$k and
biz_id in (select id from lc_biz where city_id={$city_id} and status=1)
)" => null
];
$value = $this->ci->mdOrders->count($where);
}
$value && $pie1_offline[] = ['name' => $v['name'], 'value' => $value];
}
$biz_type = $this->ci->biz_model->type_ary();
foreach ($biz_type as $k => $v) {
if($type){
$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 and type={$k})
)" => null
];
$value = $this->ci->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();
}else{
$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 and type={$k})
)" => null
];
$value = $this->ci->mdOrders->count($where);
}
$pie2_level[] = ['name' => $v, 'value' => $value];
$value && $bottom2 += $value;
}
if($type){
$sql = "select *,count(id) as t from (SELECT a.id,a.brand_id, count(b.status) as total
FROM lc_receiver_orders_v2 as a
LEFT JOIN lc_receiver_order_status as b ON b.o_id=a.id
WHERE a.status >= 0
AND a.status <> 2
AND a.brand_id > 0
AND a.order_time>= '{$o_time['s_time']}'
AND a.order_time<= '{$o_time['e_time']}'
AND a.customer_id in (select id from lc_receiver_customers where status>=0 and biz_id in (select id from lc_biz where city_id={$city_id} and status=1))
AND b.pid_status = 3
AND b.status in (1,2)
GROUP BY b.o_id
HAVING total = 1) u group by brand_id";
$brand_lists = $this->ci->db->query($sql)->result_array();
}else{
$where = [
'status>=' => 0,
'brand_id>' => 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
];
$query = $this->ci->db->select('id,brand_id,count(id) as t')->from('lc_receiver_orders_v2')
->where($where)->group_by('brand_id')->get();
$brand_lists = $query->result_array();
}
if($brand_lists){
$brand_rows = [];
$brand_ids = implode(',',array_column($brand_lists,'brand_id'));
$brand_ids && $brand_rows = $this->ci->auto_brand_model->map('id','name',"id in ($brand_ids)",'','','','id,name');
foreach ($brand_lists as $val) {
if($brand_rows[$val['brand_id']]){
$pie23level[] = ['name' => $brand_rows[$val['brand_id']], 'value' => $val['t']];
$bottom3 += $val['t'];
}
}
}
$biz_rows = $this->ci->biz_model->select(['status' => 1,'city_id' => $city_id], 'id desc', 0, 0, 'id,biz_name');
foreach ($biz_rows as $k => $v) {
$bizs[] = $v['biz_name'];
if($type){
$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.biz_id' => $v['id'],
];
$value = $this->ci->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();
}else{
$where = [
'status>=' => 0,
'biz_id' => $v['id'],
'order_time>=' => $o_time['s_time'],
'order_time<=' => $o_time['e_time'],
];
$value = $this->ci->mdOrders->count($where);
}
$biz_customer[] = $value;
$bottom4 += $value;
}
$data['pie1'] = ['title' => '来源占比', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline,'bottom'=>"合计:{$bottom1}"];
$data['pie2'] = ['title' => '门店占比', 'series_data_1' => $pie2_level,'bottom'=>"合计:{$bottom2}"];
$data['pie3'] = ['title' => '品牌占比', 'series_data_1' => $pie23level,'bottom'=>"合计:{$bottom3}"];
$data['bar'] = ['title' => '具体分布', 'xAxis_data' => $bizs, 'series_data' => $biz_customer,'bottom'=>"合计:{$bottom4}"];
$data['title'] = $type ? '开票数' : '订单数';
return $data;
}
}