Files
liche/common/libraries/receiver/Stats_entity.php
T
2023-02-13 13:50:34 +08:00

332 lines
15 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 = [];
$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.city_id' => $city_id,
'b.status>=' => 0,
'b.level' => $v
];
$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,
'level' => $v,
'c_time>=' => $c_time['s_time'],
'c_time<=' => $c_time['e_time'],
'city_id' => $city_id
];
$value = $this->ci->customers_model->count($where);
}
$value && $pie1_level[] = ['name' => $v, 'value' => $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.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,
'of_id' => $k,
'c_time>=' => $c_time['s_time'],
'c_time<=' => $c_time['e_time'],
'city_id' => $city_id
];
$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.city_id' => $city_id,
'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,
'b.c_time>=' => $c_time['s_time'],
'b.c_time<=' => $c_time['e_time'],
'b.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];
}
$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,
'b.city_id' => $city_id,
'c.c_time>=' => $c_time['s_time'],
'c.c_time<=' => $c_time['e_time'],
'b.biz_id' => $k,
'c.type' => 4,
'c.log' => '客户到店',
];
$biz_customer[] = $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'],
'city_id' => $city_id
];
$biz_customer[] = $this->ci->customers_model->count($where);
}
}
$data['pie1'] = ['title' => '客户来源', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline];
$data['pie2'] = ['title' => '门店类型', 'series_data_1' => $pie2_level];
$data['bar'] = ['title' => '门店列表', 'xAxis_data' => $bizs, 'series_data' => $biz_customer];
$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 = [];
$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 city_id={$city_id} and level='{$v}')" => 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 city_id={$city_id} and level='{$v}')" => null
];
$value = $this->ci->mdOrders->count($where);
}
$value && $pie1_level[] = ['name' => $v, 'value' => $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 city_id={$city_id} and of_id=$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 city_id={$city_id} and of_id=$k)" => 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 city_id={$city_id} and of_id=$k and biz_id in
(select id from lc_biz where 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 city_id={$city_id} and of_id=$k and biz_id in
(select id from lc_biz where type={$k})
)" => null
];
$value = $this->ci->mdOrders->count($where);
}
$pie2_level[] = ['name' => $v, 'value' => $value];
}
if($type){
$sql = "select * from (SELECT a.id,a.brand_id, count(b.status) as t
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 city_id={$city_id})
AND b.pid_status = 3
AND b.status in (1,2)
GROUP BY b.o_id
HAVING t = 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 city_id={$city_id})" => 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) {
$brand_rows[$val['brand_id']] && $pie23level[] = ['name' => $brand_rows[$val['brand_id']], 'value' => $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'],
"a.customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})" => null
];
$biz_customer[] = $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'],
"customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})" => null
];
$biz_customer[] = $this->ci->mdOrders->count($where);
}
}
$data['pie1'] = ['title' => '客户来源', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline];
$data['pie2'] = ['title' => '门店类型', 'series_data_1' => $pie2_level];
$data['pie3'] = ['title' => '品牌分布', 'series_data_1' => $pie23level];
$data['bar'] = ['title' => '门店列表', 'xAxis_data' => $bizs, 'series_data' => $biz_customer];
$data['title'] = $type ? '开票数' : '订单数';
return $data;
}
}