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');
$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();
$sql = "
select * from (
select 1 as group_id, '总数' 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.biz_name,''),'未分配门店') 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, '战败' 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, '战败-无购车意向' 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, '战败-有购车意向' 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, '加V(扣除 战败-无购车意向)' 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_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, '未见客户的见面意向' 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, '到店客户的购车意向' 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, '订单数' 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, '加企微' 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);
if ($res){
$this->statplan->update(array('last_date'=>$stat_date), array('id'=>$spid));
echo "spid {$spid} success: {$res}";
}
else{
echo "spid {$spid} failure";
}
}
}
}