Files
liche/api/controllers/plan/Customer.php
T
2023-04-20 10:36:49 +08:00

297 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Notes:客户数据操作
*/
class Customer extends HD_Controller
{
private $log_file;
public function __construct()
{
parent::__construct();
$this->log_file = 'customer.log';
}
/**
* Notes:生成客户来源日志
* Created by: qianhy
* https://liche-api-dev.xiaoyu.com/plan/customer/statplan?sd=1
* https://api.liche.cn/plan/items/customer/statplan
*/
public function statplan()
{
$params = $this->input->get();
$hour = date('H.i');
if (($hour >= 23.50 && $hour <= 23.59) || $params['sd']) {
if ($params['sd']) {
echo "开始执行生成客户来源日志[{$hour}]";
}
} else {
echo '[23:50]后才会开始生成客户来源日志[' . $hour . ']';
return;
}
$this->load->model('receiver/Receiver_customer_stat_plan_model', 'statplan');
$this->load->model('receiver/Receiver_customer_stat_data_model', 'statdata');
$this->load->model('receiver/Receiver_customer_stat_data_log_model', 'statdatalog');
$where = array('s_date <= DATE(now()) and DATE(now()) <= e_date and last_date < DATE(now())'=> null);
$plans = $this->statplan->select($where, '', 0, 0);
if (!$plans){
echo 'no stat plan '. date('Y-m-d H:i:s');
return;
}
foreach ($plans as $plan){
$spid = $plan['id'];
$of_id = $plan['of_id'];
$of2_id = $plan['of2_id'];
$s_date = $plan['s_date'];
$stat_date = date('Y-m-d');
$c_time = time();
# 先删除今日数据
$this->statdata->delete(array('spid' => $spid, 'stat_date' => $stat_date));
$this->statdatalog->delete(array('spid' => $spid, 'stat_date' => $stat_date));
# 1 生成统计数据
$sql = "
select * from (
select 1 as group_id, '<b>总数</b>' as k, count(*) as v from lc_receiver_customers c where of_id = @of_id and of2_id = @of2_id and c_time >= UNIX_TIMESTAMP('@s_date')
union all
select 3 as group_id, concat(ct.name) as k, count(*) as v from lc_receiver_customers c left join lc_biz b on c.biz_id = b.id left join lc_sys_city ct on b.city_id = ct.city_id where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') group by k
union all
select c.biz_id * 10 as group_id, if(c.biz_id,concat('<b>',b.biz_name,'</b>'),'<b>未分配门店</b>') as k, count(*) as v from lc_receiver_customers c left join lc_biz b on c.biz_id = b.id where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') group by group_id, k
union all
select (c.biz_id * 10 + 1) as group_id, if(c.admin_id,concat(' - ',b.biz_name,'-', u.uname), ' - 未分配') as k, count(*) as v from lc_receiver_customers c left join lc_biz b on c.biz_id = b.id left join lc_app_licheb_users u on c.admin_id = u.id where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') group by group_id,k
union all
select 10007 as group_id, '<b>战败</b>' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 group by k
union all
select 10008 as group_id, '<b>战败-无购车意向</b>' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (103, 107))) group by k
union all
select 10009 as group_id, ' - 战败-无购车意向-无效线索' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (103))) group by k
union all
select 10009 as group_id, ' - 战败-无购车意向-异地线索' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (107))) group by k
union all
select 10020 as group_id, '<b>战败-有购车意向</b>' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (100, 110, 111))) group by k
union all
select 10021 as group_id, ' - 战败-有购车意向-放弃购买' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (100))) group by k
union all
select 10021 as group_id, ' - 战败-有购车意向-购买其他品牌' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (110))) group by k
union all
select 10021 as group_id, ' - 战败-有购车意向-对产品不满意' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 3 and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (111))) group by k
union all
select 10030 as group_id, '<b>加V(扣除 战败-无购车意向)</b>' as k, count(*) as v from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and id not in (
select c.id from lc_receiver_customers c
where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date')
and c.status = 3
and id in (select c_id from lc_receiver_customers_tagdata where t_id in (select id from lc_receiver_customers_tag where tag_type = 1 and pid in (103, 107)))
) and wxgr = 1 group by k
union all
select 10040 as group_id, '<b>未见客户的见面意向</b>' as k, count(*) as v from lc_receiver_customers_tagdata td join lc_receiver_customers_tag t on td.t_id = t.id where t.tag_type = 2 and t.pid in (120) and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 0) group by k
union all
select 10041 as group_id, concat(' - 未见客户的见面意向','-', t.name) as k, count(*) as v from lc_receiver_customers_tagdata td join lc_receiver_customers_tag t on td.t_id = t.id where t.tag_type = 2 and t.pid in (120) and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 0) group by k
union all
select 10050 as group_id, '<b>到店客户的购车意向</b>' as k, count(*) as v from lc_receiver_customers_tagdata td join lc_receiver_customers_tag t on td.t_id = t.id where t.tag_type = 2 and t.pid in (121) and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 1) group by k
union all
select 10051 as group_id, concat(' - 到店客户的购车意向','-', t.name) as k, count(*) as v from lc_receiver_customers_tagdata td join lc_receiver_customers_tag t on td.t_id = t.id where t.tag_type = 2 and t.pid in (121) and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 1) group by k
union all
select 10060 as group_id, '<b>订单数</b>' as k, count(*) as v from lc_receiver_orders_v2 o where o.`status` in (0, 1) and customer_id in (select id from lc_receiver_customers c where of_id = @of_id and of2_id = @of2_id and c_time >= UNIX_TIMESTAMP('@s_date'))
union all
select 10061 as group_id, concat(' - 订单数','-',b.`name`,' ', s.`name`) as k, count(*) as v from lc_receiver_orders_v2 o left join lc_auto_brand b on o.brand_id = b.id left join lc_auto_series s on o.s_id = s.id where o.`status` in (0, 1) and customer_id in (select id from lc_receiver_customers c where of_id = @of_id and of2_id = @of2_id and c_time >= UNIX_TIMESTAMP('@s_date')) group by k
union all
select 10090 as group_id, '<b>加企微</b>' as k, count(*) as v from lc_receiver_customers c where of_id = @of_id and of2_id = @of2_id and c_time >= UNIX_TIMESTAMP('@s_date') and wxqy = 1
) t
order by group_id, k
";
$sql = str_replace('@of_id', $of_id, $sql);
$sql = str_replace('@of2_id', $of2_id, $sql);
$sql = str_replace('@s_date', $s_date, $sql);
$rows = $this->statplan->db->query($sql)->result_array();
if (!$rows){
echo "spid {$spid} no data";
continue;
}
$data = [];
foreach ($rows as $row){
$row['spid'] = $spid;
$row['stat_date'] = $stat_date;
$row['c_time'] = $c_time;
$data[] = $row;
}
$res = $this->statdata->add_batch($data);
# 2 生成统计数据的log
$sql = "
insert into lc_receiver_customers_stat_data_log(
spid,
stat_date,
c_time,
c_id,
biz_id,
biz_name,
city_id,
city_name,
admin_id,
admin_name,
status,
wxqy,
wxgr,
tag_id_1_1,
tag_id_1_1_name,
tag_id_1_2,
tag_id_1_2_name,
tag_id_2_1,
tag_id_2_1_pid,
tag_id_2_1_name,
tag_id_2_1_pid_name,
tag_id_2_2,
tag_id_2_2_pid,
tag_id_2_2_name,
tag_id_2_2_pid_name,
o_id,
brand_id,
brand_name,
s_id,
s_name
)
select
@spid as spid,
date(now()) as stat_date,
UNIX_TIMESTAMP() as c_time,
c.id as c_id,
c.biz_id,
b.biz_name,
b.city_id,
ct.name as city_name,
c.admin_id,
u.uname as admin_name,
c.status,
c.wxqy,
c.wxgr,
tg11.tag_id_pid as tag_id_1_1,
tg11.tag_id_pid_name as tag_id_1_1_name,
tg12.tag_id_pid as tag_id_1_2,
tg12.tag_id_pid_name as tag_id_1_2_name,
tg21.tag_id as tag_id_2_1,
tg21.tag_id_pid as tag_id_2_1_pid,
tg21.tag_id_name as tag_id_2_1_name,
tg21.tag_id_pid_name as tag_id_2_1_pid_name,
tg22.tag_id as tag_id_2_2,
tg22.tag_id_pid as tag_id_2_2_pid,
tg22.tag_id_name as tag_id_2_2_name,
tg22.tag_id_pid_name as tag_id_2_2_pid_name,
null as o_id,
null as brand_id,
'' as brand_name,
null as s_id,
'' as s_name
from lc_receiver_customers c
left join lc_biz b on c.biz_id = b.id
left join lc_sys_city ct on b.city_id = ct.city_id
left join lc_app_licheb_users u on c.admin_id = u.id
left join (
select distinct td.c_id, t.pid as tag_id_pid, tp.name as tag_id_pid_name
from lc_receiver_customers_tagdata td left join lc_receiver_customers_tag t on td.t_id = t.id
left join lc_receiver_customers_tag tp on t.pid = tp.id
where t.tag_type = 1
and t.pid in (103, 107)
and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date'))
) tg11 on c.id = tg11.c_id
left join (
select distinct td.c_id, t.pid as tag_id_pid, tp.name as tag_id_pid_name
from lc_receiver_customers_tagdata td left join lc_receiver_customers_tag t on td.t_id = t.id
left join lc_receiver_customers_tag tp on t.pid = tp.id
where t.tag_type = 1
and t.pid in (100, 110, 111)
and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date'))
) tg12 on c.id = tg12.c_id
left join (
select td.c_id, td.t_id as tag_id, t.name as tag_id_name, t.pid as tag_id_pid, tp.name as tag_id_pid_name
from lc_receiver_customers_tagdata td join lc_receiver_customers_tag t on td.t_id = t.id
left join lc_receiver_customers_tag tp on t.pid = tp.id
where t.tag_type = 2
and t.pid in (120)
and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 0)
) tg21 on c.id = tg21.c_id
left join (
select td.c_id, td.t_id as tag_id, t.name as tag_id_name, t.pid as tag_id_pid, tp.name as tag_id_pid_name
from lc_receiver_customers_tagdata td join lc_receiver_customers_tag t on td.t_id = t.id
left join lc_receiver_customers_tag tp on t.pid = tp.id
where t.tag_type = 2
and t.pid in (121)
and td.c_id in (select id from lc_receiver_customers c where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date') and c.status = 1)
) tg22 on c.id = tg22.c_id
where c.of_id = @of_id and c.of2_id = @of2_id and c.c_time >= UNIX_TIMESTAMP('@s_date')
union all
select
@spid as spid,
date(now()) as stat_date,
UNIX_TIMESTAMP() as c_time,
c.id as c_id,
c.biz_id,
b.biz_name,
b.city_id,
ct.name as city_name,
c.admin_id,
u.uname as admin_name,
c.status,
c.wxqy,
c.wxgr,
null as tag_id_1_1,
'' as tag_id_1_1_name,
null as tag_id_1_2,
'' as tag_id_1_2_name,
null as tag_id_2_1,
null as tag_id_2_1_pid,
'' as tag_id_2_1_name,
'' as tag_id_2_1_pid_name,
null as tag_id_2_2,
null as tag_id_2_2_pid,
'' as tag_id_2_2_name,
'' as tag_id_2_2_pid_name,
o.id as o_id,
o.brand_id,
brand.`name` as brand_name,
o.s_id,
s.`name` as s_name
from lc_receiver_orders_v2 o
left join lc_auto_brand brand on o.brand_id = brand.id
left join lc_auto_series s on o.s_id = s.id
left join lc_receiver_customers c on o.customer_id = c.id
left join lc_biz b on c.biz_id = b.id
left join lc_sys_city ct on b.city_id = ct.city_id
left join lc_app_licheb_users u on c.admin_id = u.id
where o.`status` in (0, 1)
and customer_id in (select id from lc_receiver_customers c where of_id = @of_id and of2_id = @of2_id and c_time >= UNIX_TIMESTAMP('@s_date'))
";
$sql = str_replace('@spid', $spid, $sql);
$sql = str_replace('@of_id', $of_id, $sql);
$sql = str_replace('@of2_id', $of2_id, $sql);
$sql = str_replace('@s_date', $s_date, $sql);
$res_log = $this->statdatalog->db->query($sql);
if ($res && $res_log){
$this->statplan->update(array('last_date'=>$stat_date), array('id'=>$spid));
echo "spid {$spid} success: {$res}{$res_log}";
}
else{
echo "spid {$spid} failure{$res}{$res_log}";
}
}
}
}