Files
spacestation/api/controllers/plan/Licheb.php
T
小鱼开发 3f699dde1c sms
2024-06-25 15:47:43 +08:00

316 lines
13 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:狸车宝任务
* Created on: 2021/10/19 17:15
* Created by: dengbw
*/
class Licheb extends HD_Controller
{
private $log_file;
public function __construct()
{
parent::__construct();
$this->log_file = 'licheb.log';
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
}
/**
* Notes:更新客户回访记录
* Created on: 2022/05/24 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/visit_data?sd=1
* https://api.liche.cn/plan/licheb/visit_data
*/
public function visit_data()
{
$params = $this->input->get();
$hour = date('H.i');
if (($hour > 00.00 && $hour < 02.00) || $params['sd']) {
if ($params['sd']) {
echo "开始执行客户回访[{$hour}]<br>";
}
} else {
echo '[00.00-02.00]执行客户回访[' . $hour . ']<br>';
return;
}
$this->load->model('receiver/receiver_customers_visit_data_model', 'mdCustomerVisitData');
$time = strtotime("-1 day");//昨天此时的时间戳
$size = 200;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$t_day = date('Y-m-d', $time);
$redisKey = 'app_licheb_visit_data_id_' . $t_day;
if ($rds) {
$redis->delete($redisKey);
}
$id = $redis->get($redisKey);
!$id && $id = 0;
$log = [];
$where = ['id >' => $id, 'status<>' => 2, 't_day' => $t_day];
$res = $this->mdCustomerVisitData->select($where, 'id asc', 1, $size, 'id,c_id,biz_id,sales_id,level,pid');
if (!$res) {
echo '执行到当前客户回访id' . $id . '暂无数据';
return;
}
$this->load->model('receiver/receiver_customers_visit_sales_model', 'mdCustomerVisitSales');
$this->load->model('receiver/receiver_customer_oplogs_model', 'mdCustomerOpLogs');
$s_time = strtotime($t_day . ' 00:00:00');
$e_time = strtotime($t_day . ' 23:59:59');
$today = date('Y-m-d');//今天
foreach ($res as $key => $value) {
$id = $value['id'];
$re_opl = $this->mdCustomerOpLogs->get(['customer_id' => $value['c_id'], 'uid' => $value['sales_id'], 'type in(0,1,2)' => null, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
if ($re_opl) {
//已跟进
$this->mdCustomerVisitData->update(['status' => 2], ['id' => $value['id']]);
} else {
//只跟进status 0未见客户1到店客户
$re_cust = $this->mdCustomers->get(['id' => $value['c_id'], 'status in(0,1)' => null]);
if (!$re_cust) {
continue;
}
$where_vis = ['c_id' => $value['c_id'], 'sales_id' => $value['sales_id'], 't_day' => $today];
$re_vis = $this->mdCustomerVisitData->get($where_vis);
if (!$re_vis) {
//新增明天延期数据
$where_vis['pid'] = $value['pid'] ? $value['pid'] : $value['id'];//逾期id
$where_vis['biz_id'] = $value['biz_id'];
$where_vis['status'] = 3;
$where_vis['level'] = $value['level'];
$where_vis['c_time'] = time();
$add_id = $this->mdCustomerVisitData->add($where_vis);
if ($add_id) {
//更新回访时间
$this->mdCustomers->update(['visit_time' => $today], ['id' => $value['c_id']]);
$params['sd'] && $log[] = $where_vis;
}
}
}
}
$redis->save($redisKey, $id, 24 * 3600);//保存最后id
if ($params['sd']) {
echo '<br>执行到当前客户回访id' . $redis->get($redisKey);
echo ' 日期:' . $t_day;
echo '<br>成功新增:<br>';
if ($log) {
echo json_encode($log, JSON_UNESCAPED_UNICODE);
echo '<br>';
}
echo '数据库获取:<br>';
echo json_encode($res, JSON_UNESCAPED_UNICODE);
}
}
/**
* Notes:客户回访顾问统计
* Created on: 2022/05/24 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/visit_sales?sd=1
* https://api.liche.cn/plan/licheb/visit_sales
*/
public function visit_sales()
{
$params = $this->input->get();
$hour = date('H.i');
if (($hour > 02.00 && $hour < 04.00) || $params['sd']) {
if ($params['sd']) {
echo "开始执行库顾问日志[{$hour}]<br>";
}
} else {
echo '[02.00-04.00]执行顾问日志[' . $hour . ']<br>';
return;
}
$this->load->model('app/licheb/app_licheb_users_model', 'mdUsers');
$this->load->model('receiver/receiver_customers_visit_data_model', 'mdCustomerVisitData');
$this->load->model('receiver/receiver_customers_visit_sales_model', 'mdCustomerVisitSales');
$time = strtotime("-1 day");//昨天此时的时间戳
$size = 200;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$t_day = date('Y-m-d', $time);
$redisKey = 'app_licheb_visit_sales_id_' . $t_day;
// if ($rds) {
// $redis->delete($redisKey);
// }
$id = $redis->get($redisKey);
!$id && $id = 0;
$log = [];
$where = ['id >' => $id, 'group_id in (1,2,3)' => null, 'status' => 1];
$res = $this->mdUsers->select($where, 'id asc', 1, $size, 'id,biz_id');
if (!$res) {
echo '执行到当前顾问id' . $id . '暂无数据';
return;
}
foreach ($res as $key => $value) {
$sales_id = $id = $value['id'];
$biz_id = intval($value['biz_id']);
if ($biz_id == 1) {//门店用户不写入
continue;
}
$where_sal = ['t_day' => $t_day, 'sales_id' => $sales_id, 'biz_id' => $biz_id];
$re_sal = $this->mdCustomerVisitSales->get($where_sal);
if (!$re_sal) {//判断当天是否已加入
$t_follows = $this->mdCustomerVisitData->count(array_merge($where_sal, ['status' => 1]));
$h_follows = $this->mdCustomerVisitData->count(array_merge($where_sal, ['status' => 2]));
$w_follows = $this->mdCustomerVisitData->count(array_merge($where_sal, ['status' => 3]));
#这里的逾期未跟进数 需要把 已改派 的过滤掉
#$w_follows = $this->mdCustomerVisitData->count(array_merge($where_sal, ['status' => 3], ['c_id in (select id from lc_receiver_customers where cs_biz_id >= 0)' => null]));
$add_item = ['t_day' => $t_day, 'sales_id' => $sales_id, 'biz_id' => $biz_id, 't_follows' => $t_follows, 'h_follows' => $h_follows, 'w_follows' => $w_follows, 'c_time' => time()];
$add_id = $this->mdCustomerVisitSales->add($add_item);
if ($add_id && $params['sd']) {
$log[] = array('id' => $sales_id, 'add_item' => $add_item);
}
}
}
$redis->save($redisKey, $id, 24 * 3600);//保存最后id
if ($params['sd']) {
echo '<br>执行到当前顾问id' . $redis->get($redisKey);
echo ' 日期:' . $t_day;
echo '<br>成功新增:<br>';
if ($log) {
echo json_encode($log, JSON_UNESCAPED_UNICODE);
echo '<br>';
}
echo '数据库获取:<br>';
echo json_encode($res, JSON_UNESCAPED_UNICODE);
}
}
/**
* Notes:给销售发送待回访短信
* Created on: 2021/10/19 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/visit_sales_sms?sd=1
* https://api.liche.cn/plan/licheb/visit_sales_sms
*/
public function visit_sales_sms()
{
$params = $this->input->get();
$hour = date('H.i');
if (($hour > 09.00 && $hour < 10.00) || $params['sd']) {
if ($params['sd']) {
echo "开始执行发短信给销售[{$hour}]<br>";
}
} else {
echo '[09.00-10.00]点过才会开始执行发短信给销售[' . $hour . ']<br>';
return;
}
$size = 100;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$t_day = date('Y-m-d');
$redisKey = 'visit_sales_sms_id_' . $t_day;
if ($rds) {
$redis->delete($redisKey);
}
$id = $redis->get($redisKey);
!$id && $id = 0;
$log = array();
$this->load->model('receiver/receiver_customers_visit_data_model', 'mdCustomerVisitData');
$this->load->model('app/licheb/app_licheb_users_model', 'mdUsers');
$where = array('id >' => $id, 'status' => 1, 'group_id in (1,2)' => null);
$res_u = $this->mdUsers->select($where, 'id asc', 1, $size, 'id,biz_id,mobile');
if (!$res_u) {
echo '执行到当前销售id' . $id . '暂无数据';
return;
}
foreach ($res_u as $key => $value) {
$id = $value['id'];
$biz_id = $value['biz_id'];
if ($biz_id == '1') {
continue;
}
$where_v = ['a.biz_id' => $biz_id, 'a.status in(0,1)' => null, 'b.t_day' => $t_day, 'b.sales_id' => $id, 'b.status<>' => 2];
$visit_count = $this->mdCustomerVisitData->count_visit($where_v);
if ($visit_count) {//发送短信
b2m_send_sms($value['mobile'], '【理车宝】您好,今天共有 '.$visit_count.' 个客户需要回访,客户线索来之不易,请及时跟进~');
$log[] = array('id' => $id, 'mobile' => $value['mobile'], 'count' => $visit_count);
}
}
$redis->save($redisKey, $id, 24 * 3600);//保存最后销售id
if ($params['sd']) {
echo '<br>执行到当前销售id' . $redis->get($redisKey);
echo '<br>成功新增:<br>';
if ($log) {
echo json_encode($log, JSON_UNESCAPED_UNICODE);
echo '<br>';
}
echo '数据库获取:<br>';
echo json_encode($res_u, JSON_UNESCAPED_UNICODE);
}
}
/**
* Notes:给店长发送待分配短信
* Created on: 2021/10/19 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/customer_manager_sms
* https://api.liche.cn/plan/licheb/customer_manager_sms
*/
public function customer_manager_sms()
{
$params = $this->input->get();
$hour = date('H.i');
if (($hour > 09.00 && $hour < 10.00) || $params['sd']) {
if ($params['sd']) {
echo "开始执行发短信给店长[{$hour}]<br>";
}
} else {
echo '[09.00-10.00]点过才会开始执行发短信给店长[' . $hour . ']<br>';
return;
}
$this->load->model('receiver/receiver_customers_visit_model', 'mdCustomersVisit');
$size = 100;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$t_day = date('Y-m-d');
$redisKey = 'customer_manager_sms_id_' . $t_day;
if ($rds) {
$redis->delete($redisKey);
}
$id = $redis->get($redisKey);
!$id && $id = 0;
$log = array();
$this->load->model('app/licheb/app_licheb_users_model', 'mdUsers');
$where = array('id >' => $id, 'status' => 1, 'group_id' => 2);
$res_u = $this->mdUsers->select($where, 'id asc', 1, $size, 'id,biz_id,mobile');
if (!$res_u) {
echo '执行到当前店长id' . $id . '暂无数据';
return;
}
foreach ($res_u as $key => $value) {
$id = $value['id'];
$biz_id = $value['biz_id'];
if ($biz_id == '1') {
continue;
}
$where_c = array("biz_id in ($biz_id)" => null, 'status' => 0, 'admin_id' => 0);
$Customers_count = $this->mdCustomers->count($where_c);
if ($Customers_count) {
//发送短信
b2m_send_sms($value['mobile'], '【理车宝】您好,门店有 '.$Customers_count.' 个客户线索尚未分配。请及时到小程序"理车宝-待分配客户”分配给销售顾问跟进。祝您生活愉快!');
$log[] = array('id' => $id, 'mobile' => $value['mobile'], 'count' => $Customers_count);
}
}
$redis->save($redisKey, $id, 24 * 3600);//保存最后销售id
if ($params['sd']) {
echo '<br>执行到当前店长id' . $redis->get($redisKey);
echo '<br>成功新增:<br>';
if ($log) {
echo json_encode($log, JSON_UNESCAPED_UNICODE);
echo '<br>';
}
echo '数据库获取:<br>';
echo json_encode($res_u, JSON_UNESCAPED_UNICODE);
}
}
}