Files
liche/api/controllers/plan/Licheb.php
T
2022-07-12 16:28:43 +08:00

791 lines
39 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: 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();
$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');
$time = strtotime("-1 day");//昨天此时的时间戳
$size = 200;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$log_date = date('Y-m-d', $time);
$redisKey = 'app_licheb_users_log_id_' . $log_date;
if ($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('app/app_different_qy_log_model', 'mdDifferentQyLog');
$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 ($biz_id == 1 || $biz_id == 110) {//门店用户不写入
continue;
}
$re_log = $this->mdUsersLog->get(['log_date' => $log_date, 'sale_id' => $sale_id, 'biz_id' => $biz_id]);
if ($re_log) {//判断当天是否已加入
continue;
}
$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} and cs_biz_id!=-1)" => null]
);
$t_num = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type' => 5, '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} and cs_biz_id!=-1)" => null]
);
$follows = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type in(0,1,2)' => null, '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} and cs_biz_id!=-1)" => 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} and cs_biz_id!=-1)" => null]
);
$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 = $c_num = $orders = 0;
if ($value['userid']) {
$qy_where = ['userid' => $value['userid'], 'c_time>=' => $s_time, 'c_time<=' => $e_time];
if ($type == 5) {//异业店
$qy_adds = $this->mdDifferentQyLog->count(array_merge($qy_where, ['change_type' => 'add_external_contact']));
$qy_dels = $this->mdDifferentQyLog->count(array_merge($qy_where, ["change_type in('del_external_contact','del_follow_user')" => null]));
} else {
$qy_adds = $this->mdWechatqyLog->count(array_merge($qy_where, ['change_type' => 'add_external_contact']));
$qy_dels = $this->mdWechatqyLog->count(array_merge($qy_where, ["change_type in('del_external_contact','del_follow_user')" => null]));
}
}
//改派数
$reassigns = $this->mdCustomers->count(['admin_id' => $sale_id, 'cs_biz_id' => -1, 'status>=' => 0, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
//接收数
$receives = $this->mdCustomers->count(['admin_id' => $sale_id, 'cs_biz_id>' => 0, 'status>=' => 0, 'c_time>=' => $s_time, 'c_time<=' => $e_time]);
$add_item = ['sale_id' => $sale_id, 'biz_id' => $biz_id, 'type' => $type, 'city_id' => $city_id, 'c_num' => $c_num
, 'a_num' => $a_num, 't_num' => $t_num, 'orders' => $orders, 'bills' => $bills, 'follows' => $follows, 'defeats' => $defeats
, 'log_date' => $log_date, 'qy_adds' => $qy_adds, 'qy_dels' => $qy_dels, 'reassigns' => $reassigns, 'receives' => $receives, 'c_time' => time()];
$customer_json = $c_num_json = $order_json = '';
foreach ($offlineSources as $key1 => $value1) {
//客户数
$customer_num1 = $this->mdCustomers->count(['of_id' => $key1, 'admin_id' => $sale_id, 'cs_biz_id<>' => -1
, '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, 'cs_biz_id<>' => -1]);
if ($customer_num2) {//二级
$list[$key2] = ['name' => $value2, 'num' => $customer_num2];
}
}
$customer_json[$key1] = ['name' => $value1['name'], 'num' => $customer_num1, 'list' => $list];
}
//进店人数
$c_num1 = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type' => 4, 'log' => '客户到店', 'c_time>=' => $s_time, 'c_time<=' => $e_time,
"customer_id in(select id from lc_receiver_customers where of_id={$key1} and admin_id={$sale_id}
and biz_id={$biz_id} and cs_biz_id!=-1)" => null], 'distinct(customer_id)');
if ($c_num1) {//一级
$c_num += $c_num1;
$add_item["c_num_{$key1}"] = $c_num1;
$list = '';
foreach ($value1['list'] as $key2 => $value2) {
$c_num2 = $this->mdCustomerOpLogs->count(['uid' => $sale_id, 'type' => 4, 'log' => '客户到店', 'c_time>=' => $s_time, 'c_time<=' => $e_time,
"customer_id in(select id from lc_receiver_customers where of_id={$key1} and of2_id={$key2}
and admin_id={$sale_id} and biz_id={$biz_id} and cs_biz_id!=-1)" => null], 'distinct(customer_id)');
if ($c_num2) {//二级
$list[$key2] = ['name' => $value2, 'num' => $c_num2];
}
}
$c_num_json[$key1] = ['name' => $value1['name'], 'num' => $c_num1, 'list' => $list];
}
//订单数
$orders1 = $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",
"customer_id in(select id from lc_receiver_customers where of_id={$key1} and admin_id={$sale_id}
and biz_id={$biz_id} and cs_biz_id!=-1)" => null]);
if ($orders1) {//一级
$orders += $orders1;
$add_item["order_{$key1}"] = $orders1;
$list = '';
foreach ($value1['list'] as $key2 => $value2) {
$orders2 = $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",
"customer_id in(select id from lc_receiver_customers where of_id={$key1} and of2_id={$key2}
and admin_id={$sale_id} and biz_id={$biz_id} and cs_biz_id!=-1)" => null]);
if ($orders2) {//二级
$list[$key2] = ['name' => $value2, 'num' => $orders2];
}
}
$order_json[$key1] = ['name' => $value1['name'], 'num' => $orders1, 'list' => $list];
}
}
$customer_json && $add_item['customer_json'] = json_encode($customer_json, JSON_UNESCAPED_UNICODE);
$c_num_json && $add_item['c_num_json'] = json_encode($c_num_json, JSON_UNESCAPED_UNICODE);
$order_json && $add_item['order_json'] = json_encode($order_json, JSON_UNESCAPED_UNICODE);
$add_item['customers'] = $customers;
$add_item['c_num'] = $c_num;
$add_item['orders'] = $orders;
$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: 2022/7/11 17:15
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/licheb/bizs_log?sd=1
* https://api.liche.cn/plan/licheb/bizs_log
*/
public function bizs_log()
{
$params = $this->input->get();
$hour = date('H.i');
if (($hour > 03.00 && $hour < 05.00) || $params['sd']) {
if ($params['sd']) {
echo "开始执行库门店日志[{$hour}]";
}
} else {
echo '[00.00-03.00]执行门店日志[' . $hour . ']';
return;
}
$this->load->model("biz/biz_model", 'mdBiz');
$time = strtotime("-1 day");//昨天此时的时间戳
$size = 200;//每次最多处理多少条
$rds = intval($this->input->get('rds'));
$redis = &load_cache('redis');
$log_date = date('Y-m-d', $time);
$redisKey = 'app_licheb_bizs_log_id_' . $log_date;
if ($rds) {
$redis->delete($redisKey);
}
$id = $redis->get($redisKey);
!$id && $id = 0;
$log = [];
$where = ['id >' => $id, 'status' => 1];
$res = $this->mdBiz->select($where, 'id asc', 1, $size, 'id,type,city_id');
if (!$res) {
echo '执行到当前门店id' . $id . '暂无数据';
return;
}
$this->load->model('app/licheb/app_licheb_users_log_model', 'mdUsersLog');
$this->load->model('app/licheb/app_licheb_bizs_log_model', 'mdBizsLog');
foreach ($res as $key => $value) {
$biz_id = $id = $value['id'];
if ($biz_id == 1 || $biz_id == 110) {//门店不写入
continue;
}
$re = $this->mdBizsLog->get(['biz_id' => $biz_id, 'log_date' => $log_date]);
if ($re) {
continue;
}
$a_num = $t_num = $bills = $follows = $defeats = $qy_adds = $qy_dels = $reassigns = $receives = 0;
$c_num = $orders = $customers = 0;
$c_num_ary = $order_ary = $customer_ary = [];
$c_num_json = $order_json = $customer_json = '';
$res2 = $this->mdUsersLog->select(['biz_id' => $biz_id, 'log_date' => $log_date], 'id asc');
foreach ($res2 as $key2 => $value2) {
$c_num += $value2['c_num'];
$orders += $value2['orders'];
$customers += $value2['customers'];
$a_num += $value2['a_num'];
$t_num += $value2['t_num'];
$bills += $value2['bills'];
$follows += $value2['follows'];
$defeats += $value2['defeats'];
$qy_adds += $value2['qy_adds'];
$qy_dels += $value2['qy_dels'];
$reassigns += $value2['reassigns'];
$receives += $value2['receives'];
//一级统计
for ($ii = 1; $ii < 5; ++$ii) {
$c_num_ary[$ii] += $value2['c_num_' . $ii];
$order_ary[$ii] += $value2['order_' . $ii];
$customer_ary[$ii] += $value2['customer_' . $ii];
}
//详情统计
if ($value2['c_num_json']) {
$c_num_json_u = json_decode($value2['c_num_json'], true);
foreach ($c_num_json_u as $k1 => $v1) {
$list = '';
foreach ($v1['list'] as $k2 => $v2) {
$num2 = intval($c_num_json[$k1]['list'][$k2]['num']) + $v2['num'];
//二级
$num2 && $list[$k2] = ['name' => $v2['name'], 'num' => $num2];
}
$num1 = intval($c_num_json[$k1]['num']) + $v1['num'];
$c_num_json[$k1] = ['name' => $v1['name'], 'num' => $num1, 'list' => $list];
}
}
if ($value2['order_json']) {
$order_json_u = json_decode($value2['order_json'], true);
foreach ($order_json_u as $k1 => $v1) {
$list = '';
foreach ($v1['list'] as $k2 => $v2) {
$num2 = intval($order_json[$k1]['list'][$k2]['num']) + $v2['num'];
//二级
$num2 && $list[$k2] = ['name' => $v2['name'], 'num' => $num2];
}
$num1 = intval($order_json[$k1]['num']) + $v1['num'];
$order_json[$k1] = ['name' => $v1['name'], 'num' => $num1, 'list' => $list];
}
}
if ($value2['customer_json']) {
$customer_json_u = json_decode($value2['customer_json'], true);
foreach ($customer_json_u as $k1 => $v1) {
$list = '';
foreach ($v1['list'] as $k2 => $v2) {
$num2 = intval($customer_json[$k1]['list'][$k2]['num']) + $v2['num'];
//二级
$num2 && $list[$k2] = ['name' => $v2['name'], 'num' => $num2];
}
$num1 = intval($customer_json[$k1]['num']) + $v1['num'];
$customer_json[$k1] = ['name' => $v1['name'], 'num' => $num1, 'list' => $list];
}
}
}
$add_item = ['biz_id' => $biz_id, 'type' => $value['type'], 'city_id' => $value['city_id'], 'c_num' => $c_num
, 'a_num' => $a_num, 't_num' => $t_num, 'orders' => $orders, 'bills' => $bills, 'follows' => $follows, 'defeats' => $defeats
, 'log_date' => $log_date, 'qy_adds' => $qy_adds, 'qy_dels' => $qy_dels, 'reassigns' => $reassigns, 'receives' => $receives, 'c_time' => time()];
for ($ii = 1; $ii < 5; ++$ii) {
$add_item['c_num_' . $ii] = intval($c_num_ary[$ii]);
$add_item['order_' . $ii] = intval($order_ary[$ii]);
$add_item['customer_' . $ii] = intval($customer_ary[$ii]);
}
$c_num_json && $add_item['c_num_json'] = json_encode($c_num_json, JSON_UNESCAPED_UNICODE);
$order_json && $add_item['order_json'] = json_encode($order_json, JSON_UNESCAPED_UNICODE);
$customer_json && $add_item['customer_json'] = json_encode($customer_json, JSON_UNESCAPED_UNICODE);
$add_id = $this->mdBizsLog->add($add_item);
if ($add_id) {
$log[] = ['id' => $add_id, 'biz_id' => $biz_id, 'mobile' => $log_date];
}
}
$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: 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) {//2已跟进
$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 < 4' => null, 'status' => 1];
$res = $this->mdUsers->select($where, 'id asc', 1, $size, 'id,biz_id,userid');
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 || $biz_id == 110) {//门店用户不写入
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]));
$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' => 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 = ['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) {//发送短信
send_alisms(array('mobile' => $value['mobile'], 'template' => 'SMS_242696263', 'param' => ['count' => $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' || $biz_id == '110') {
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) {//发送短信
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, 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_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;
}
$this->load->model('receiver/receiver_customers_visit_model', 'mdCustomersVisit');
$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;
}
$this->load->model('receiver/receiver_customers_visit_model', 'mdCustomersVisit');
$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);
}
}
}