680 lines
34 KiB
PHP
680 lines
34 KiB
PHP
<?php
|
||
|
||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||
|
||
class order extends HD_Controller
|
||
{
|
||
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders');
|
||
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
|
||
$this->load->model('app/licheb/app_licheb_users_log_model', 'mdUsersLog');
|
||
$this->load->model('biz/biz_model', 'mdBiz');
|
||
}
|
||
|
||
//首页信息
|
||
public function index()
|
||
{
|
||
return $this->get_dfdq();
|
||
}
|
||
|
||
/**
|
||
* Notes:东风
|
||
* Created on: 2022/8/22 15:04
|
||
* Created by: dengbw
|
||
* @return bool|void
|
||
*/
|
||
public function get_dfmc()
|
||
{
|
||
$params = $this->input->get();
|
||
$config['brand_id'] = 1;
|
||
$config['bizs'] = [
|
||
1 => ['biz_name' => '厦门品牌店', 'ids' => '20', 'rowspan' => 1],
|
||
2 => ['biz_name' => '泉州品牌店', 'ids' => '99'], 3 => ['biz_name' => '福州品牌店', 'ids' => '55'],
|
||
4 => ['biz_name' => '厦门合伙店', 'type' => 2, 'city_id' => '350200'],
|
||
5 => ['biz_name' => '泉州代理店', 'not_ids' => '99', 'city_id' => '350500'],
|
||
6 => ['biz_name' => '福州代理店', 'type' => 3, 'city_id' => '350100'],
|
||
7 => ['biz_name' => '龙岩代理店', 'type' => 3, 'city_id' => '350800'],
|
||
8 => ['biz_name' => '宁德代理店', 'city_id' => '350900'],
|
||
9 => ['biz_name' => '莆田代理店', 'type' => 3, 'city_id' => '350300'],
|
||
10 => ['biz_name' => '三明代理店', 'type' => 3, 'city_id' => '350400'],
|
||
11 => ['biz_name' => '漳州代理店', 'type' => 3, 'city_id' => '350600'],
|
||
12 => ['biz_name' => '南平代理店', 'type' => 3, 'city_id' => '350700'],
|
||
];
|
||
$res = $this->commonSelect($params, $config);
|
||
$this->data['info'] = $res['info'];
|
||
$this->data['params'] = $res['params'];
|
||
$this->data['url'] = '/stats/order/get_dfmc';
|
||
$this->data['_title'] = '东风';
|
||
return $this->show_view('stats/order/common', true);
|
||
}
|
||
|
||
/**
|
||
* Notes:零跑
|
||
* Created on: 2022/8/22 14:19
|
||
* Created by: dengbw
|
||
*/
|
||
public function get_leapmotor()
|
||
{
|
||
$params = $this->input->get();
|
||
$config['brand_id'] = 5;
|
||
$config['bizs'] = [
|
||
1 => ['biz_name' => '泉州零跑品牌店', 'ids' => '63', 'rowspan' => 1],
|
||
2 => ['biz_name' => '泉州渠道', 'not_ids' => '63', 'city_id' => '350500', 'show' => 'order'],
|
||
3 => ['biz_name' => '厦门渠道', 'city_id' => '350200', 'show' => 'order'],
|
||
4 => ['biz_name' => '福州渠道', 'city_id' => '350100', 'show' => 'order'],
|
||
];
|
||
$res = $this->commonSelect($params, $config);
|
||
$this->data['info'] = $res['info'];
|
||
$this->data['params'] = $res['params'];
|
||
$this->data['url'] = '/stats/order/get_leapmotor';
|
||
$this->data['_title'] = '零跑';
|
||
return $this->show_view('stats/order/common', true);
|
||
}
|
||
|
||
/**
|
||
* Notes:哪吒
|
||
* Created on: 2022/8/17 14:19
|
||
* Created by: dengbw
|
||
*/
|
||
public function get_hozonauto()
|
||
{
|
||
$params = $this->input->get();
|
||
$config['brand_id'] = 4;
|
||
$config['bizs'] = [
|
||
1 => ['biz_name' => '宁德哪吒品牌店', 'ids' => '70', 'rowspan' => 1],
|
||
2 => ['biz_name' => '宁德哪吒代理店', 'type' => 3, 'city_id' => '350900', 'show' => 'order'],
|
||
3 => ['biz_name' => '厦门哪吒订单中心', 'city_id' => '350200', 'show' => 'order'],
|
||
4 => ['biz_name' => '泉州哪吒订单中心', 'city_id' => '350500', 'show' => 'order'],
|
||
5 => ['biz_name' => '福州哪吒订单中心', 'city_id' => '350100', 'show' => 'order'],
|
||
6 => ['biz_name' => '漳州哪吒品牌店', 'type' => 1, 'city_id' => '350600', 'show' => 'order'],
|
||
7 => ['biz_name' => '漳州哪吒代理店', 'type' => 3, 'city_id' => '350600', 'show' => 'order'],
|
||
];
|
||
$res = $this->commonSelect($params, $config);
|
||
$this->data['info'] = $res['info'];
|
||
$this->data['params'] = $res['params'];
|
||
$this->data['url'] = '/stats/order/get_hozonauto';
|
||
$this->data['_title'] = '哪吒';
|
||
return $this->show_view('stats/order/common', true);
|
||
}
|
||
|
||
/**
|
||
* Notes:代理店
|
||
* Created on: 2022/8/23 14:17
|
||
* Created by: dengbw
|
||
* @return bool|void
|
||
*/
|
||
public function get_agent()
|
||
{
|
||
$params = $this->input->get();
|
||
$config['brands'] = [1 => '东风ev', 4 => '哪吒', 5 => '零跑'];
|
||
$config['bizs'] = [
|
||
1 => ['biz_name' => '泉州代理店', 'type' => 3, 'city_id' => '350500', 'rowspan' => 1],
|
||
2 => ['biz_name' => '福州代理店', 'type' => 3, 'city_id' => '350100'],
|
||
3 => ['biz_name' => '龙岩代理店', 'type' => 3, 'city_id' => '350800'],
|
||
4 => ['biz_name' => '宁德代理店', 'type' => 3, 'city_id' => '350900'],
|
||
5 => ['biz_name' => '莆田代理店', 'type' => 3, 'city_id' => '350300'],
|
||
6 => ['biz_name' => '三明代理店', 'type' => 3, 'city_id' => '350400'],
|
||
7 => ['biz_name' => '南平代理店', 'type' => 3, 'city_id' => '350700'],
|
||
8 => ['biz_name' => '漳州代理店', 'type' => 3, 'city_id' => '350600'],
|
||
9 => ['biz_name' => '厦门代理店', 'type' => 3, 'city_id' => '350200'],
|
||
];
|
||
$res = $this->common2Select($params, $config);
|
||
$this->data['info'] = $res['info'];
|
||
$this->data['params'] = $res['params'];
|
||
$this->data['url'] = '/stats/order/get_agent';
|
||
$this->data['type_name'] = '代理店';
|
||
$this->data['_title'] = '代理店群';
|
||
return $this->show_view('stats/order/common2', true);
|
||
}
|
||
|
||
/**
|
||
* Notes:合伙店
|
||
* Created on: 2022/8/23 14:17
|
||
* Created by: dengbw
|
||
* @return bool|void
|
||
*/
|
||
public function get_partner()
|
||
{
|
||
$params = $this->input->get();
|
||
$config['brands'] = [1 => '东风ev', 4 => '哪吒', 5 => '零跑'];
|
||
$config['bizs'] = [1 => ['biz_name' => '厦门合伙店', 'type' => 2, 'city_id' => '350200', 'rowspan' => 1]];
|
||
$res = $this->common2Select($params, $config);
|
||
$this->data['info'] = $res['info'];
|
||
$this->data['params'] = $res['params'];
|
||
$this->data['url'] = '/stats/order/get_partner';
|
||
$this->data['type_name'] = '合伙店';
|
||
$this->data['_title'] = '合伙店群';
|
||
return $this->show_view('stats/order/common2', true);
|
||
}
|
||
|
||
/**
|
||
* Notes:品牌店群
|
||
* Created on: 2022/8/17 14:17
|
||
* Created by: dengbw
|
||
* @return bool|void
|
||
*/
|
||
public function get_dfdq()
|
||
{
|
||
$params = $this->input->get();
|
||
$config['brands'] = [1 => '东风ev', 4 => '哪吒', 5 => '零跑'];
|
||
$config['bizs'] = [
|
||
1 => ['biz_name' => '厦门东风', 'ids' => '20', 'rowspan' => 1],
|
||
2 => ['biz_name' => '晋江东风', 'ids' => '99'],
|
||
3 => ['biz_name' => '福州东风', 'ids' => '55'],
|
||
4 => ['biz_name' => '厦门哪吒', 'ids' => '191'],
|
||
5 => ['biz_name' => '宁德哪吒', 'ids' => '70'],
|
||
6 => ['biz_name' => '漳州哪吒', 'ids' => '160'],
|
||
7 => ['biz_name' => '泉州零跑', 'ids' => '63'],
|
||
];
|
||
$res = $this->common2Select($params, $config);
|
||
$this->data['info'] = $res['info'];
|
||
$this->data['params'] = $res['params'];
|
||
$this->data['url'] = '/stats/order/';
|
||
$this->data['type_name'] = '品牌店';
|
||
$this->data['_title'] = '品牌店群';
|
||
return $this->show_view('stats/order/common2', true);
|
||
}
|
||
|
||
private function common2Select($params, $config)
|
||
{
|
||
!$params['type'] && $params['type'] = 1;
|
||
$start_date = date('Y-m', strtotime("-2 month"));//取最近3个月
|
||
$end_date = date('Y-m');
|
||
if ($params['time']) {
|
||
$time = explode(' ~ ', $params['time']);
|
||
$time[0] && $start_date = $time[0];
|
||
$time[1] && $end_date = $time[1];
|
||
} else {
|
||
$params['time'] = $start_date . ' ~ ' . $end_date;
|
||
}
|
||
$date_months = $this->dateMonths($start_date, $end_date);
|
||
rsort($date_months);
|
||
$days = [];
|
||
foreach ($date_months as $k => $v) {
|
||
$month = date('n', strtotime($v . '-01'));
|
||
$days[$v] = ['month' => $month, 'num' => 0];
|
||
}
|
||
$brands = $config['brands'];
|
||
$bizs = $config['bizs'];
|
||
foreach ($bizs as $k => $v) {
|
||
$str_ids = $v['ids'] ? $v['ids'] : '';
|
||
if (!$str_ids) {
|
||
$where_b = ['city_id' => $v['city_id']];
|
||
$v['type'] && $where_b['type'] = $v['type'];
|
||
$v['not_ids'] && $where_b["id not in({$v['not_ids']})"] = null;
|
||
$res = $this->mdBiz->select($where_b, 'id desc', 0, 0, 'id');
|
||
if ($res) {
|
||
$str_ids = implode(',', array_unique(array_column($res, 'id')));
|
||
$str_ids = $str_ids ? $str_ids : '-2';
|
||
}
|
||
}
|
||
$list = [];
|
||
foreach ($days as $k2 => $v2) {
|
||
$order_time = $this->getTime($k2);
|
||
$c_time = $this->getTime($k2, 1);
|
||
if ($params['type'] == 2) {
|
||
$num = $this->mdOrders->count(["biz_id in({$str_ids})" => null, 'status in(0,1)' => null, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
|
||
} else {
|
||
$num = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$str_ids})" => null, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
|
||
, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
|
||
->count_all_results();
|
||
}
|
||
$list[$k2] = $num;
|
||
$days[$k2]['num'] = $v2['num'] + $num;
|
||
}
|
||
$v['list'] = $list;
|
||
$v['ids'] = $str_ids;
|
||
$bizs[$k] = $v;
|
||
}
|
||
$brands_stats = $customers_stats = $process = [];
|
||
foreach ($bizs as $k => $v) {
|
||
$list = [];
|
||
foreach ($brands as $k2 => $v2) {
|
||
$stats_per = $stats = [];
|
||
foreach ($days as $k3 => $v3) {
|
||
$order_time = $this->getTime($k3);
|
||
$c_time = $this->getTime($k3, 1);
|
||
if ($params['type'] == 2) {
|
||
$num = $this->mdOrders->count(["biz_id in({$v['ids']})" => null, 'brand_id' => $k2, 'status in(0,1)' => null, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
|
||
} else {
|
||
$num = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.brand_id' => $k2, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
|
||
, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
|
||
->count_all_results();
|
||
}
|
||
$stats_per[] = $this->getPercentage(['num' => $num, 'sum' => $v['list'][$k3]]);
|
||
$stats[] = $num;
|
||
}
|
||
$list[] = ['brand_name' => $v2, 'stats_per' => $stats_per, 'stats' => $stats];
|
||
}
|
||
$brands_stats[] = ['biz_name' => $v['biz_name'], 'list' => $list];
|
||
}
|
||
$offlineSources = $this->mdCustomers->offlineSources();
|
||
$offlineSources_count = count($offlineSources);
|
||
//线索成交情况
|
||
foreach ($bizs as $k => $v) {
|
||
$list = $cus_sum = $ord_sum = $cus_per = [];
|
||
$cus_1 = $ord_1 = [];//自然到店
|
||
foreach ($offlineSources as $k2 => $v2) {
|
||
$stats_per_cus = $stats_cus = $stats_per_ord = $stats_ord = [];
|
||
foreach ($days as $k3 => $v3) {
|
||
$c_time = $this->getTime($k3, 1);
|
||
$order_time = $this->getTime($k3);
|
||
$num_cus = $this->mdCustomers->count(["biz_id in({$v['ids']})" => null, 'of_id' => $k2, 'status>=' => 0, 'c_time>=' => $c_time['s_time'], 'c_time<=' => $c_time['e_time']]);
|
||
if ($params['type'] == 2) {
|
||
$num_ord = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time']
|
||
, 'a.order_time<=' => $order_time['e_time'], 'b.of_id' => $k2])
|
||
->count_all_results();
|
||
} else {
|
||
$num_ord = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
|
||
, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
|
||
->count_all_results();
|
||
}
|
||
$cus_sum[$k3] += $num_cus;
|
||
$ord_sum[$k3] += $num_ord;
|
||
$stats_cus[] = $num_cus;
|
||
$stats_ord[$k3] = $num_ord;
|
||
$stats_per_cus[] = $this->getPercentage(['num' => $num_ord, 'sum' => $num_cus]);
|
||
$stats_per_ord[$k3] = '0.00%';
|
||
if ($k2 == 1) {//自然到店
|
||
$cus_1[$k3] = $num_cus;
|
||
$ord_1[$k3] = $num_ord;
|
||
}
|
||
}
|
||
$list[] = ['name' => "{$v2['name']}(转化率)", 'type' => 1, 'stats_per' => $stats_per_cus, 'stats' => $stats_cus];
|
||
$list[] = ['name' => "{$v2['name']}(订单占比)", 'type' => 2, 'stats_per' => $stats_per_ord, 'stats' => $stats_ord];
|
||
}
|
||
foreach ($days as $k3 => $v3) {
|
||
$ord = $ord_1[$k3] ? $ord_sum[$k3] - $ord_1[$k3] : $ord_sum[$k3];
|
||
$cus = $cus_1[$k3] ? $cus_sum[$k3] - $cus_1[$k3] : $cus_sum[$k3];
|
||
$cus_per[] = $this->getPercentage(['num' => $ord, 'sum' => $cus]);
|
||
}
|
||
$customers_stats[] = ['biz_name' => $v['biz_name'], 'cus_sum' => $cus_sum, 'ord_sum' => $ord_sum, 'cus_per' => $cus_per, 'list' => $list];
|
||
}
|
||
//过程数据
|
||
$process_cus = $process_into = $process_ord = [];
|
||
foreach ($bizs as $k => $v) {
|
||
$list_cus = $cus_sum = $list_into = $ord_into = $into_sum = $list_ord = $ord_sum = [];
|
||
foreach ($offlineSources as $k2 => $v2) {
|
||
$stats_per_cus = $stats_cus = $stats_per_into = $stats_into = $stats_per_ord = $stats_ord = [];
|
||
foreach ($days as $k3 => $v3) {
|
||
$order_time = $this->getTime($k3);
|
||
$c_time = $this->getTime($k3, 1);
|
||
//线索数
|
||
$num_cus = 0;
|
||
if ($k2 != 1) {//不包括自然到店
|
||
$num_cus = $this->mdCustomers->count(["biz_id in({$v['ids']})" => null, 'of_id' => $k2, 'status>=' => 0, 'c_time>=' => $c_time['s_time'], 'c_time<=' => $c_time['e_time']]);
|
||
$cus_sum[$k3] += $num_cus;
|
||
$stats_cus[] = $num_cus;
|
||
$stats_per_cus[$k3] = $num_cus;
|
||
}
|
||
//进店数
|
||
if ($params['type'] == 2) {
|
||
$num_into = $this->mdUsersLog->db->select('a.id')
|
||
->from('lc_receiver_customer_oplogs as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(['a.type' => 4, 'a.log' => '客户到店', 'a.c_time>=' => $c_time['s_time'], 'a.c_time<=' => $c_time['e_time'],
|
||
'b.status>=' => 0, "b.biz_id in({$v['ids']})" => null, 'b.of_id' => $k2])
|
||
->count_all_results();
|
||
} else {
|
||
$num_into = $this->mdUsersLog->db->select('a.id')
|
||
->from('lc_receiver_customer_oplogs as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(['a.type' => 4, 'a.log' => '客户到店', 'a.c_time>=' => $c_time['s_time'], 'a.c_time<=' => $c_time['e_time'],
|
||
'b.status>=' => 0, "b.biz_id in({$v['ids']})" => null, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
|
||
->count_all_results();
|
||
}
|
||
$into_sum[$k3] += $num_into;
|
||
$stats_into[] = $num_into;
|
||
$num_into_per = '';
|
||
if ($k2 != 1) {//转化率=进店数/客户数
|
||
$num_into_per = $this->getPercentage(['num' => $num_into, 'sum' => $num_cus]);
|
||
}
|
||
$stats_per_into[$k3] = $num_into_per;
|
||
//成交数
|
||
if ($params['type'] == 2) {
|
||
$num_ord = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time']
|
||
, 'a.order_time<=' => $order_time['e_time'], 'b.of_id' => $k2])
|
||
->count_all_results();
|
||
} else {
|
||
$num_ord = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
|
||
, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
|
||
->count_all_results();
|
||
}
|
||
$ord_sum[$k3] += $num_ord;
|
||
$stats_ord[] = $num_ord;
|
||
//转化率=订单数/进店数
|
||
$stats_per_ord[$k3] = $this->getPercentage(['num' => $num_ord, 'sum' => $num_into]);
|
||
}
|
||
if ($k2 != 1) {//不包括自然到店
|
||
$list_cus[] = ['name' => $v2['name'], 'stats_per' => $stats_per_cus, 'stats' => $stats_cus];
|
||
}
|
||
$list_into[] = ['name' => $v2['name'], 'stats_per' => $stats_per_into, 'stats' => $stats_into];
|
||
$list_ord[] = ['name' => $v2['name'], 'stats_per' => $stats_per_ord, 'stats' => $stats_ord];
|
||
}
|
||
$process_cus[] = ['biz_name' => $v['biz_name'], 'num_sum' => $cus_sum, 'list' => $list_cus];
|
||
$process_into[] = ['biz_name' => $v['biz_name'], 'num_sum' => $into_sum, 'list' => $list_into];
|
||
$process_ord[] = ['biz_name' => $v['biz_name'], 'num_sum' => $ord_sum, 'list' => $list_ord];
|
||
}
|
||
$process = ['customers' => $process_cus, 'into_shop' => $process_into, 'orders' => $process_ord];
|
||
$data['info'] = ['days' => $days, 'bizs' => $bizs, 'bizs_count' => count($bizs), 'days_count' => count($days)
|
||
, 'brands_count' => count($brands), 'offlineSources_count' => $offlineSources_count, 'brands_stats' => $brands_stats
|
||
, 'customers_stats' => $customers_stats, 'process' => $process, 'type_ary' => [1 => '月销售模型(当月线索)', 2 => '月销售模型']];
|
||
$data['params'] = $params;
|
||
return $data;
|
||
}
|
||
|
||
private function commonSelect($params, $config)
|
||
{
|
||
$brand_id = $config['brand_id'];//品牌id
|
||
!$params['type'] && $params['type'] = 1;
|
||
$start_date = date('Y-m', strtotime("-2 month"));//取最近3个月
|
||
$end_date = date('Y-m');
|
||
if ($params['time']) {
|
||
$time = explode(' ~ ', $params['time']);
|
||
$time[0] && $start_date = $time[0];
|
||
$time[1] && $end_date = $time[1];
|
||
} else {
|
||
$params['time'] = $start_date . ' ~ ' . $end_date;
|
||
}
|
||
$date_months = $this->dateMonths($start_date, $end_date);
|
||
rsort($date_months);
|
||
$days = [];
|
||
foreach ($date_months as $k => $v) {
|
||
$month = date('n', strtotime($v . '-01'));
|
||
$days[$v] = ['month' => $month, 'num' => 0];
|
||
}
|
||
$bizs = $config['bizs'];
|
||
foreach ($bizs as $k => $v) {
|
||
$str_ids = $v['ids'] ? $v['ids'] : '';
|
||
if (!$str_ids) {
|
||
$where_b = ['city_id' => $v['city_id']];
|
||
$v['type'] && $where_b['type'] = $v['type'];
|
||
$v['not_ids'] && $where_b["id not in({$v['not_ids']})"] = null;
|
||
$res = $this->mdBiz->select($where_b, 'id desc', 0, 0, 'id');
|
||
if ($res) {
|
||
$str_ids = implode(',', array_unique(array_column($res, 'id')));
|
||
$str_ids = $str_ids ? $str_ids : '-2';
|
||
}
|
||
}
|
||
$list = $customer_ids = [];
|
||
foreach ($days as $k2 => $v2) {
|
||
$order_time = $this->getTime($k2);
|
||
$c_time = $this->getTime($k2, 1);
|
||
if ($params['type'] == 2) {
|
||
$where_o = ["biz_id in({$str_ids})" => null, 'brand_id' => $brand_id,
|
||
'status in(0,1)' => null, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']];
|
||
$num = $this->mdOrders->count($where_o);
|
||
if ($num && $v['show'] == 'order') {//按订单数来匹配
|
||
$res_o = $this->mdOrders->select($where_o, 'id desc', 0, 0, 'customer_id');
|
||
$customer_ids[$k2] = implode(',', array_unique(array_column($res_o, 'customer_id')));
|
||
}
|
||
} else {
|
||
$where_o = ["a.biz_id in({$str_ids})" => null, 'a.brand_id' => $brand_id, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time']
|
||
, 'a.order_time<=' => $order_time['e_time'], 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']];
|
||
$num = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where($where_o)
|
||
->count_all_results();
|
||
if ($num && $v['show'] == 'order') {//按订单数来匹配
|
||
$res_o = $this->mdOrders->db->select('a.customer_id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where($where_o)
|
||
->get()->result_array();
|
||
$customer_ids[$k2] = implode(',', array_unique(array_column($res_o, 'customer_id')));
|
||
}
|
||
}
|
||
$list[$k2] = $num;
|
||
$days[$k2]['num'] = $v2['num'] + $num;
|
||
}
|
||
$v['list'] = $list;
|
||
$v['ids'] = $str_ids;
|
||
$v['customer_ids'] = $customer_ids;
|
||
$bizs[$k] = $v;
|
||
}
|
||
//echo json_encode($bizs,JSON_UNESCAPED_UNICODE);
|
||
$offlineSources = $this->mdCustomers->offlineSources();
|
||
$offlineSources_count = count($offlineSources);
|
||
//过程数据
|
||
$process_cus = $process_into = $process_ord = [];
|
||
foreach ($bizs as $k => $v) {
|
||
$list_cus = $cus_sum = $list_into = $ord_into = $into_sum = $list_ord = $ord_sum = [];
|
||
foreach ($offlineSources as $k2 => $v2) {
|
||
$stats_per_cus = $stats_cus = $stats_per_into = $stats_into = $stats_per_ord = $stats_ord = [];
|
||
foreach ($days as $k3 => $v3) {
|
||
$order_time = $this->getTime($k3);
|
||
$c_time = $this->getTime($k3, 1);
|
||
//成交数
|
||
if ($params['type'] == 2) {
|
||
$num_ord = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.brand_id' => $brand_id, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time']
|
||
, 'a.order_time<=' => $order_time['e_time'], 'b.of_id' => $k2])
|
||
->count_all_results();
|
||
} else {
|
||
$num_ord = $this->mdOrders->db->select('a.id')
|
||
->from('lc_receiver_orders_v2 as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(["a.biz_id in({$v['ids']})" => null, 'a.brand_id' => $brand_id, 'a.status in(0,1)' => null, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
|
||
, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
|
||
->count_all_results();
|
||
}
|
||
//线索数
|
||
$num_cus = 0;
|
||
if ($k2 != 1) {//不包括自然到店
|
||
if ($v['show'] == 'order') {
|
||
$num_cus = $num_ord;
|
||
} else {
|
||
$num_cus = $this->mdCustomers->count(["biz_id in({$v['ids']})" => null, 'of_id' => $k2, 'status>=' => 0, 'c_time>=' => $c_time['s_time'], 'c_time<=' => $c_time['e_time']]);
|
||
}
|
||
$cus_sum[$k3] += $num_cus;
|
||
$stats_cus[] = $num_cus;
|
||
$stats_per_cus[$k3] = $num_cus;
|
||
}
|
||
//进店数
|
||
$num_into = 0;
|
||
if ($params['type'] == 2) {
|
||
$where_into = ['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' => $k2];
|
||
if ($v['show'] == 'order') {
|
||
if ($v['customer_ids'][$k3]) {
|
||
$num_into = $this->mdUsersLog->db->select('a.id')
|
||
->from('lc_receiver_customer_oplogs as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(array_merge($where_into, ["b.id in({$v['customer_ids'][$k3]})" => null]))
|
||
->count_all_results();
|
||
}
|
||
} else {
|
||
$num_into = $this->mdUsersLog->db->select('a.id')
|
||
->from('lc_receiver_customer_oplogs as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(array_merge($where_into, ["b.biz_id in({$v['ids']})" => null]))
|
||
->count_all_results();
|
||
}
|
||
} else {
|
||
$where_into = ['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' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']];
|
||
if ($v['show'] == 'order') {
|
||
if ($v['customer_ids'][$k3]) {
|
||
$num_into = $this->mdUsersLog->db->select('a.id')
|
||
->from('lc_receiver_customer_oplogs as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(array_merge($where_into, ["b.id in({$v['customer_ids'][$k3]})" => null]))
|
||
->count_all_results();
|
||
}
|
||
} else {
|
||
$num_into = $this->mdUsersLog->db->select('a.id')
|
||
->from('lc_receiver_customer_oplogs as a')
|
||
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
|
||
->where(array_merge($where_into, ["b.biz_id in({$v['ids']})" => null]))
|
||
->count_all_results();
|
||
}
|
||
}
|
||
$into_sum[$k3] += $num_into;
|
||
$stats_into[] = $num_into;
|
||
$num_into_per = '';
|
||
if ($k2 != 1) {//转化率=进店数/客户数
|
||
$num_into_per = $this->getPercentage(['num' => $num_into, 'sum' => $num_cus]);
|
||
}
|
||
$stats_per_into[$k3] = $num_into_per;
|
||
//成交数
|
||
$ord_sum[$k3] += $num_ord;
|
||
$stats_ord[] = $num_ord;
|
||
//转化率=订单数/进店数
|
||
$stats_per_ord[$k3] = $this->getPercentage(['num' => $num_ord, 'sum' => $num_into]);
|
||
}
|
||
if ($k2 != 1) {//不包括自然到店
|
||
$list_cus[] = ['name' => $v2['name'], 'stats_per' => $stats_per_cus, 'stats' => $stats_cus];
|
||
}
|
||
$list_into[] = ['name' => $v2['name'], 'stats_per' => $stats_per_into, 'stats' => $stats_into];
|
||
$list_ord[] = ['name' => $v2['name'], 'stats_per' => $stats_per_ord, 'stats' => $stats_ord];
|
||
}
|
||
$process_cus[] = ['biz_name' => $v['biz_name'], 'num_sum' => $cus_sum, 'list' => $list_cus];
|
||
$process_into[] = ['biz_name' => $v['biz_name'], 'num_sum' => $into_sum, 'list' => $list_into];
|
||
$process_ord[] = ['biz_name' => $v['biz_name'], 'num_sum' => $ord_sum, 'list' => $list_ord];
|
||
}
|
||
$process = ['customers' => $process_cus, 'into_shop' => $process_into, 'orders' => $process_ord];
|
||
$data['info'] = ['days' => $days, 'bizs' => $bizs, 'bizs_count' => count($bizs), 'days_count' => count($days)
|
||
, 'brands_count' => 1, 'offlineSources_count' => $offlineSources_count, 'process' => $process,
|
||
'type_ary' => [1 => '月销售模型(当月线索)', 2 => '月销售模型']];
|
||
$data['params'] = $params;
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* Notes:计算出两个日期之间的月份
|
||
* Created on: 2022/8/9 14:38
|
||
* Created by: dengbw
|
||
* @param $start_date [开始日期,如2022-03]
|
||
* @param $end_date [结束日期,如2022-12]
|
||
* @param string $explode [年份和月份之间分隔符,此例为 - ]
|
||
* @param bool $addOne [算取完之后最后是否加一月,用于算取时间戳用]
|
||
* @return array [返回是两个月份之间所有月份字符串]
|
||
*/
|
||
private function dateMonths($start_date, $end_date, $explode = '-', $addOne = false)
|
||
{
|
||
//判断两个时间是不是需要调换顺序
|
||
$start_int = strtotime($start_date);
|
||
$end_int = strtotime($end_date);
|
||
if ($start_int > $end_int) {
|
||
$tmp = $start_date;
|
||
$start_date = $end_date;
|
||
$end_date = $tmp;
|
||
}
|
||
|
||
//结束时间月份+1,如果是13则为新年的一月份
|
||
$start_arr = explode($explode, $start_date);
|
||
$start_year = intval($start_arr[0]);
|
||
$start_month = intval($start_arr[1]);
|
||
|
||
$end_arr = explode($explode, $end_date);
|
||
$end_year = intval($end_arr[0]);
|
||
$end_month = intval($end_arr[1]);
|
||
|
||
$data = array();
|
||
$data[] = $start_date;
|
||
|
||
$tmp_month = $start_month;
|
||
$tmp_year = $start_year;
|
||
|
||
//如果起止不相等,一直循环
|
||
while (!(($tmp_month == $end_month) && ($tmp_year == $end_year))) {
|
||
$tmp_month++;
|
||
//超过十二月份,到新年的一月份
|
||
if ($tmp_month > 12) {
|
||
$tmp_month = 1;
|
||
$tmp_year++;
|
||
}
|
||
$data[] = $tmp_year . $explode . str_pad($tmp_month, 2, '0', STR_PAD_LEFT);
|
||
}
|
||
|
||
if ($addOne == true) {
|
||
$tmp_month++;
|
||
//超过十二月份,到新年的一月份
|
||
if ($tmp_month > 12) {
|
||
$tmp_month = 1;
|
||
$tmp_year++;
|
||
}
|
||
$data[] = $tmp_year . $explode . str_pad($tmp_month, 2, '0', STR_PAD_LEFT);
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
private function getPercentage($params)
|
||
{
|
||
return number_format_com($params['num'] / $params['sum'] * 100, 2, '') . "%";
|
||
}
|
||
|
||
private function getTime($day, $type = 0)
|
||
{
|
||
$s_time = "{$day}-01 00:00:00";//本月一日
|
||
$e_time = date('Y-m-d', strtotime("$s_time +1 month -1 day")) . ' 23:59:59';//本月最后一日
|
||
if ($type == 1) {
|
||
$s_time = strtotime($s_time);
|
||
$e_time = strtotime($e_time);
|
||
} else if ($type == 2) {
|
||
$s_time = "{$day}-01";
|
||
$e_time = date('Y-m-d', strtotime("$s_time +1 month -1 day"));
|
||
}
|
||
return ['s_time' => $s_time, 'e_time' => $e_time];
|
||
}
|
||
|
||
//数据列表
|
||
public function lists()
|
||
{
|
||
}
|
||
|
||
//展示单条数据
|
||
public function get()
|
||
{
|
||
}
|
||
|
||
//添加单条数据
|
||
public function add()
|
||
{
|
||
}
|
||
|
||
//编辑单条数据
|
||
public function edit()
|
||
{
|
||
}
|
||
|
||
//删除单条数据
|
||
public function del()
|
||
{
|
||
}
|
||
|
||
//批量操作(默认修改状态)
|
||
public function batch()
|
||
{
|
||
}
|
||
|
||
//导出数据列表
|
||
public function export()
|
||
{
|
||
}
|
||
|
||
} |