Files
liche/api/controllers/plan/Licheb.php
T
2021-10-26 11:33:24 +08:00

187 lines
8.1 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/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');
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';
} else if ($level == 'd') {
$up_level = 'D';
}
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') {//隔一天打
$re_v = $this->mdCustomersVisit->get(array('day>=' => $day_1, 'day<=' => $day, 'c_id' => $c_id));
!$re_v && $if_add = 1;
} else if ($level == 'B') {//一周回访一次
$re_v = $this->mdCustomersVisit->get(array('day>=' => $day_7, 'day<=' => $day, 'c_id' => $c_id));
!$re_v && $if_add = 1;
} else if ($level == 'C') {//一个月打一次
$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
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
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);
}
}