Files
liche/api/controllers/plan/Licheb.php
T
2022-04-24 15:44:14 +08:00

423 lines
20 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');
$this->load->model('receiver/receiver_customers_visit_model', 'mdCustomersVisit');
}
/**
* Notes:顾问日志
* Created on: 2021/10/19 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/users_log?sd=1
* https://api.liche.cn/plan/licheb/users_log
*/
public function users_log()
{
$params = $this->input->get();
$time = strtotime("-1 day");//昨天此时的时间戳
$hour = date('H.i');
if (($hour > 00.00 && $hour < 03.00) || $params['sd']) {
if ($params['sd']) {
echo "开始执行库顾问日志[{$hour}]";
}
} else {
echo '[00.00-03.00]执行顾问日志[' . $hour . ']';
return;
}
$this->load->model('app/licheb/app_licheb_users_model', 'mdUsers');
$this->load->model('app/licheb/app_licheb_users_log_model', 'mdUsersLog');
$size = 200;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$redisKey = 'app_licheb_users_log_id';
$log_date = date('Y-m-d', $time);
$re_v = $this->mdUsersLog->get(array('log_date' => $log_date));//查找当天是否有数据
if (!$re_v || $rds) {
$redis->delete($redisKey);
}
$id = $redis->get($redisKey);
!$id && $id = 0;
$log = [];
$where = ['id >' => $id, 'group_id < 4' => null, 'status' => 1];
$res = $this->mdUsers->select($where, 'id asc', 1, $size, 'id,biz_id,userid');
if (!$res) {
echo '执行到当前顾问id' . $id . '暂无数据';
return;
}
$this->load->model('receiver/receiver_customer_oplogs_model', 'mdCustomerOpLogs');
$this->load->model('receiver/order/receiver_orders_v2_model', 'mdOrdersV2');
$this->load->model('app/app_lichene_qy_log_model', 'mdWechatqyLog');
$this->load->model("biz/biz_model", 'mdBiz');
$offlineSources = $this->mdCustomers->offlineSources();
$str_biz_ids = implode(',', array_unique(array_column($res, 'biz_id')));
$map_biz = $this->mdBiz->map('id', 'city_id,type,status', ["id in({$str_biz_ids})" => null]);
foreach ($res as $key => $value) {
$sale_id = $id = $value['id'];
$biz_id = intval($value['biz_id']);
if ($map_biz[$biz_id]['status'] == 1) {//判断门店状态
$re_log = $this->mdUsersLog->get(['log_date' => $log_date, 'sale_id' => $sale_id, 'biz_id' => $biz_id]);
if (!$re_log) {//判断当天是否已加入
$s_time = strtotime($log_date . ' 00:00:00');
$e_time = strtotime($log_date . ' 23:59:59');
$city_id = intval($map_biz[$biz_id]['city_id']);
$type = intval($map_biz[$biz_id]['type']);
$a_num = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type' => 4, 'c_time>=' => $s_time, 'c_time<=' => $e_time,
"customer_id in(select id from lc_receiver_customers where admin_id={$sale_id} and biz_id={$biz_id})" => null]
);
$follows = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type<>' => 3, 'c_time>=' => $s_time, 'c_time<=' => $e_time,
"customer_id in(select id from lc_receiver_customers where admin_id={$sale_id} and biz_id={$biz_id})" => null], 'distinct(customer_id)'
);
$defeats = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type' => 7, 'c_time>=' => $s_time, 'c_time<=' => $e_time,
"customer_id in(select id from lc_receiver_customers where admin_id={$sale_id} and biz_id={$biz_id})" => null]
);
$orders = $this->mdOrdersV2->count(['sale_id' => $sale_id, 'biz_id' => $biz_id, 'status>=' => 0,
'order_time>=' => "{$log_date} 00:00:00", 'order_time<=' => "{$log_date} 23:59:59"]
);
$bills = $this->mdOrdersV2->count(['sale_id' => $sale_id, 'biz_id' => $biz_id, 'status>=' => 0,
'bill_time>=' => "{$log_date} 00:00:00", 'bill_time<=' => "{$log_date} 23:59:59"]
);
$qy_adds = $qy_dels = $customers = 0;
if ($value['userid']) {
$qy_adds = $this->mdWechatqyLog->count(['userid' => $value['userid'], 'change_type' => 'add_external_contact'
, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
$qy_dels = $this->mdWechatqyLog->count(['userid' => $value['userid'], "change_type in('del_external_contact','del_follow_user')" => null
, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
}
$add_item = ['sale_id' => $sale_id, 'biz_id' => $biz_id, 'type' => $type, 'city_id' => $city_id, 'a_num' => $a_num
, 'orders' => $orders, 'bills' => $bills, 'follows' => $follows, 'defeats' => $defeats, 'log_date' => $log_date
, 'qy_adds' => $qy_adds, 'qy_dels' => $qy_dels, 'c_time' => time()];
$customer_json = '';
foreach ($offlineSources as $key1 => $value1) {
$customer_num1 = $this->mdCustomers->count(['of_id' => $key1, 'admin_id' => $sale_id
, 'status>=' => 0, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
if ($customer_num1) {//一级
$customers += $customer_num1;
$add_item["customer_{$key1}"] = $customer_num1;
$list = '';
foreach ($value1['list'] as $key2 => $value2) {
$customer_num2 = $this->mdCustomers->count(['of_id' => $key1, 'of2_id' => $key2, 'admin_id' => $sale_id
, 'status>=' => 0, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
if ($customer_num2) {//二级
$list[$key2] = ['name' => $value2, 'num' => $customer_num2];
}
}
$customer_json[$key1] = ['name' => $value1['name'], 'num' => $customer_num1, 'list' => $list];
}
}
$customer_json && $add_item['customer_json'] = json_encode($customer_json, JSON_UNESCAPED_UNICODE);
$add_item['customers'] = $customers;
$add_id = $this->mdUsersLog->add($add_item);
$add_id && $log[] = array('id' => $sale_id, 'addDate' => $add_item);
}
}
}
$redis->save($redisKey, $id, 24 * 3600);//保存最后id
if ($params['sd']) {
echo '<br>执行到当前顾问id' . $redis->get($redisKey);
echo ' 日期:' . $log_date;
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
* https://api.liche.cn/plan/licheb/visit_sales_sms
*/
public function visit_sales_sms()
{
$params = $this->input->get();
$hour = date('H');
if ($hour != '09' && !$params['sd']) {
echo '[9]点过后才会开始执行发短信给销售[' . $hour . ']';
return;
}
$size = 100;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$redisKey = 'visit_sales_sms_id';
$day = date('Y-m-d');
$re_v = $this->mdCustomersVisit->get(array('day' => $day));//查找当天是否有数据
if (!$re_v || $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' => 1);
$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'];
$where_v = array("a.biz_id in ($biz_id)" => null, 'a.admin_id' => $id, 'b.contact' => 1, 'b.status' => 1, 'b.day=' => $day);
$visit_count = $this->mdCustomersVisit->count_visit($where_v);
if ($visit_count) {//发送短信
send_alisms(array('mobile' => $value['mobile'], 'template' => 'SMS_226985259', 'param' => ['count' => $visit_count]));
$log[] = array('id' => $id, 'mobile' => $value['mobile'], 'count' => $visit_count);
}
}
$redis->save($redisKey, $id);//保存最后销售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');
if ($hour != '09' && !$params['sd']) {
echo '[9]点过后才会开始执行发短信给销售[' . $hour . ']';
return;
}
$size = 100;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$redisKey = 'customer_manager_sms_id';
$day = date('Y-m-d');
$re_v = $this->mdCustomersVisit->get(array('day' => $day));//查找当天是否有数据
if (!$re_v || $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'];
$where_c = array("biz_id in ($biz_id)" => null, 'status>=' => 0, 'admin_id' => 0);
$Customers_count = $this->mdCustomers->count($where_c);
if ($Customers_count) {//发送短信
send_alisms(array('mobile' => $value['mobile'], 'template' => 'SMS_226970475', 'param' => ['count' => $Customers_count]));
$log[] = array('id' => $id, 'mobile' => $value['mobile'], 'count' => $Customers_count);
}
}
$redis->save($redisKey, $id);//保存最后销售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_visit
* https://api.liche.cn/plan/licheb/customer_visit
*/
public function customer_visit()
{
$params = $this->input->get();
$hour = date('H');
if ($hour != '00' && !$params['sd']) {
echo '[0]点过后才会开始执行客户待回访[' . $hour . ']';
return;
}
$size = 100;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$redisVisit = 'customers_visit_cid';
$day = date('Y-m-d');
$day_1 = date('Y-m-d', strtotime('-1 day'));//前1天
$day_7 = date('Y-m-d', strtotime('-7 day'));//1周前
$day_30 = date('Y-m-d', strtotime('-1 month'));//1个月前
$re_v = $this->mdCustomersVisit->get(array('day' => $day));//查找当天是否有数据
if (!$re_v || $rds) {
$redis->delete($redisVisit);
}
$c_id = $redis->get($redisVisit);
!$c_id && $c_id = 0;
$log = array();
$where = array('id >' => $c_id, 'status in(0,1)' => null, 'admin_id >' => 0, 'level <>' => 'D'
, 'c_time >=' => strtotime('2021-10-25 00:00:00'), 'c_time <' => strtotime($day . ' 00:00:00'));
$res_c = $this->mdCustomers->select($where, 'id asc', 1, $size, 'id,level,c_time');
if (!$res_c) {
echo '执行到当前客户id' . $c_id . '暂无数据';
return;
}
foreach ($res_c as $key => $value) {
$c_id = $value['id'];
$level = $value['level'];
$if_add = 0;
// $up_level = '';
// if ($level == 'h') {
// $up_level = 'H';
// } else if ($level == 'a') {
// $up_level = 'A';
// } else if ($level == 'b') {
// $up_level = 'B';
// } else if ($level == 'c') {
// $up_level = 'C';
// }
// if ($up_level) {//小写更新为大写
// $ret = $this->mdCustomers->update(array('level' => $up_level), array('id' => $c_id));
// $ret && $level = $up_level;
// }
if ($level == 'H') {//每天回访一次
$re_v = $this->mdCustomersVisit->get(array('day' => $day, 'c_id' => $c_id));
!$re_v && $if_add = 1;
} else if ($level == 'A' && ($value['c_time'] < strtotime('-1 day'))) {//隔一天打
$re_v = $this->mdCustomersVisit->get(array('day>=' => $day_1, 'day<=' => $day, 'c_id' => $c_id));
!$re_v && $if_add = 1;
} else if ($level == 'B' && ($value['c_time'] < strtotime('-7 day'))) {//一周回访一次
$re_v = $this->mdCustomersVisit->get(array('day>=' => $day_7, 'day<=' => $day, 'c_id' => $c_id));
!$re_v && $if_add = 1;
} else if ($level == 'C' && ($value['c_time'] < strtotime('-1 month'))) {//一个月打一次
$re_v = $this->mdCustomersVisit->get(array('day>=' => $day_30, 'day<=' => $day, 'c_id' => $c_id));
!$re_v && $if_add = 1;
}
if ($if_add) {
$this->mdCustomersVisit->update(array('status' => 2), array('c_id' => $c_id, 'status' => 1));
$id = $this->mdCustomersVisit->add(array('c_id' => $c_id, 'day' => $day, 'level' => $level, 'c_time' => time()));
$log[] = array('id' => $c_id, 'add_id' => $id);
}
}
$redis->save($redisVisit, $c_id);//保存最后客户id
if ($params['sd']) {
echo '<br>执行到当前客户id' . $redis->get($redisVisit);
echo '日期:' . json_encode(array('day' => $day, 'day_1' => $day_1, 'day_7' => $day_7, 'day_30' => $day_30), JSON_UNESCAPED_UNICODE);
echo '<br>成功新增:<br>';
if ($log) {
echo json_encode($log, JSON_UNESCAPED_UNICODE);
echo '<br>';
}
echo '数据库获取:<br>';
echo json_encode($res_c, JSON_UNESCAPED_UNICODE);
}
}
/**
* Notes:更新客户等级
* Created on: 2021/10/19 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/customer_level
* https://api.liche.cn/plan/licheb/customer_level
*/
public function customer_level()
{
$params = $this->input->get();
$hour = date('H');
if ($hour != '23' && !$params['sd']) {
echo '[23]点过后才会更新客户等级[' . $hour . ']';
return;
}
$size = 100;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$redisVisit = 'customers_level_cid';
$day = date('Y-m-d');
$day_1 = date('Y-m-d', strtotime('-1 day'));//前1天
$day_7 = date('Y-m-d', strtotime('-7 day'));//1周前
$day_14 = date('Y-m-d', strtotime('-14 day'));//2周前
$day_30 = date('Y-m-d', strtotime('-1 month'));//1个月前
$re_v = $this->mdCustomersVisit->get(array('day' => $day));//查找当天是否有数据
if (!$re_v || $rds) {
$redis->delete($redisVisit);
}
$c_id = $redis->get($redisVisit);
!$c_id && $c_id = 0;
$log = array();
$where = array('id >' => $c_id, 'status in(0,1)' => null, 'admin_id >' => 0, 'level <>' => 'D'
, 'c_time >=' => strtotime('2021-10-25 00:00:00'), 'c_time <' => strtotime($day . ' 00:00:00'));
$res_c = $this->mdCustomers->select($where, 'id asc', 1, $size, 'id,level');
if (!$res_c) {
echo '执行到当前客户id' . $c_id . '暂无数据';
return;
}
$this->load->library('receiver/customers_entity');
foreach ($res_c as $key => $value) {
$c_id = $value['id'];
$level = $value['level'];
$up_level = '';
if ($level == 'H') {//每天回访一次,打7天后降为A
$count = $this->mdCustomersVisit->count(array('day>=' => $day_7, 'day<=' => $day, 'c_id' => $c_id, 'level' => $level));
$count >= 7 && $up_level = 'A';
} else if ($level == 'A') {//隔一天打,打2周后降B
$count = $this->mdCustomersVisit->count(array('day>=' => $day_14, 'day<=' => $day, 'c_id' => $c_id, 'level' => $level));
$count >= 7 && $up_level = 'B';
} else if ($level == 'B') {//一周回访一次,1个月后降C
$count = $this->mdCustomersVisit->count(array('day>=' => $day_30, 'day<=' => $day, 'c_id' => $c_id, 'level' => $level));
$count >= 4 && $up_level = 'C';
}
if ($up_level) {//降级更新
$ret = $this->mdCustomers->update(array('level' => $up_level), array('id' => $c_id));
if ($ret) {
$this->customers_entity->add_log($c_id, 0, '系统', '客户等级从' . $level . '降级到' . $up_level);
$log[] = array('id' => $c_id, 'level' => $level, 'up_level' => $up_level);
}
}
}
$redis->save($redisVisit, $c_id);//保存最后客户id
if ($params['sd']) {
echo '<br>执行到当前客户id' . $redis->get($redisVisit);
echo '日期:' . json_encode(array('day' => $day, 'day_1' => $day_1, 'day_7' => $day_7, 'day_14' => $day_14
, 'day_30' => $day_30), JSON_UNESCAPED_UNICODE);
echo '<br>成功更新:<br>';
if ($log) {
$_data = json_encode($log, JSON_UNESCAPED_UNICODE);
echo $_data;
echo '<br>';
debug_log('降级客户:' . $_data, $this->log_file);
}
echo '数据库获取:<br>';
echo json_encode($res_c, JSON_UNESCAPED_UNICODE);
}
}
}