Files
liche/common/libraries/Qyrobot.php
T
2022-08-08 17:39:32 +08:00

672 lines
28 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Desc: 企业微信机器人
* Date: 2021/10/09
* Time: 11:01
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Qyrobot{
private $key = 'dfed1a38-c4e0-4904-94eb-306e9755be04'; //正式
private $fbook_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/64c25369-a164-44ce-b3bc-92e807ae1421'; //飞书正式群
private $ci;
public function __construct($params=[]){
$this->ci = & get_instance();
if($_SERVER['CI_ENV'] == 'development' || $params['test']){ //测试环境
$this->key = '0b644923-4e5c-46be-9a87-6dfcb023d09c';
$this->fbook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/b612d56e-c9b2-4e57-87b0-e6d1101944f5";
}
$this->ci->load->library('mycurl');
}
/**
* 支付定金推送消息
* @param $oid (lc_receiver_orders表)订单id
*/
public function deposit_notify($oid){
return false;
$this->ci->load->model('receiver/order/receiver_orders_model');
$this->ci->load->model("biz/biz_model");
$this->ci->load->model('area_model');
$this->ci->load->model('auto/auto_brand_model');
$this->ci->load->model('auto/auto_series_model');
$this->ci->load->model('auto/auto_attr_model');
$this->ci->load->model('app/liche/app_liche_orders_model');
$this->ci->load->model('app/liche/app_liche_orders_model');
$this->ci->load->model('app/licheb/app_licheb_users_model');
$this->ci->load->model('app/licheb/app_licheb_channel_biz_model');
$order_row = $this->ci->receiver_orders_model->get(['id'=>$oid]);
if(!$order_row){
return false;
}
$biz_row = $this->ci->biz_model->get(['id'=>$order_row['biz_id']]);
if($biz_row['type']==3){ //代理店
$channel_users = $this->ci->app_licheb_channel_biz_model->select(['biz_id'=>$biz_row['id']],'','','','uid');
$user_ids = array_column($channel_users,'uid');
$user_ids_str = implode(',',$user_ids);
if($user_ids_str && $user_ids){
$where = [
'group_id' => 4,
'status' => 1,
"id in ($user_ids_str)" => null
];
$users = $this->ci->app_licheb_users_model->select($where,'id desc',1,10,'uname,biz_id'); //渠道经理
$user_info = '';
if($users){
$user_info = implode(' ',array_column($users,'uname'));
}else{ //获取城市
$city_row = $this->ci->area_model->get(['city_id'=>$biz_row['city_id']]);
$user_info = $city_row['city_name'];
}
}
$biz_info = "{$user_info}渠道 {$biz_row['biz_name']}";
}else{
$user = $this->ci->app_licheb_users_model->get(['id'=>$order_row['admin_id']],'uname'); //获取销售员
$biz_info = "{$biz_row['biz_name']} {$user['uname']}";
}
$brand_row = $this->ci->auto_brand_model->get(['id'=>$order_row['brand_id']],'name');
$series_row = $this->ci->auto_series_model->get(['id'=>$order_row['s_id']],'name');
$attr_arr = [$order_row['v_id'], $order_row['cor_id']];
$attr = $this->ci->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
$v_name = isset($attr[$order_row['v_id']]) ? $attr[$order_row['v_id']][0]['title'] : '';
$cor_name = isset($attr[$order_row['cor_id']]) ? $attr[$order_row['cor_id']][0]['title'] : '';
$car_info = "{$brand_row['name']} {$series_row['name']} {$v_name} {$cor_name}";
$where = [
'type' => 1,
'status' => 1,
'pay_price>=' => 100
];
$where["pay_time>="] = date('Y-m-d 00:00:00');
$today_count = $this->ci->app_liche_orders_model->count($where);//今日成交订单数
$where['pay_time>='] = date('Y-m-01 00:00:00');
$month_count = $this->ci->app_liche_orders_model->count($where);//本月成交订单数
//意向金等于定金的支付订单
$sql = "select count(*) as total from lc_app_liche_orders as a left join lc_receiver_orders as b on a.o_id=b.id where a.total_price=b.deposit and a.type=4 and a.status=1 and a.pay_price>=50 and a.pay_time>='".date('Y-m-d 00:00:00')."'";
$today_query = $this->ci->db->query($sql)->row_array();
$sql = "select count(*) as total from lc_app_liche_orders as a left join lc_receiver_orders as b on a.o_id=b.id where a.total_price=b.deposit and a.type=4 and a.status=1 and a.pay_price>=50 and a.pay_time>='".date('Y-m-01 00:00:00')."'";
$month_query = $this->ci->db->query($sql)->row_array();
$today_query['total'] && $today_count += $today_query['total'];
$month_query['total'] && $month_count += $month_query['total'];
$url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$this->key;
$data = [
'msgtype' => 'markdown',
'markdown' => [
'content' => "🎉🎉🎉 **喜讯** 🎉🎉🎉 \n
恭喜 <font color=\"warning\">{$biz_info}</font> 成交 <font color=\"warning\">{$car_info}</font> 一台。\n
>本日累计成交: <font color=\"warning\">{$today_count}</font> 单
>本月累计成交: <font color=\"warning\">{$month_count}</font> 单\n
### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻"
]
];
$res = $this->ci->mycurl->httpPost($url,$data,'is_json');
$result = json_decode($res,true);
return $result;
}
/**
* 新订单支付后推送消息
* @param $oid
* @param int $type type=0 当天 type=1 补报
* @param string $date 日期type=1必填
* @return mixed
*/
public function deposit_notify_v2($oid, $type=0, $date=''){
$this->ci->load->model('receiver/order/receiver_orders_v2_model');
$this->ci->load->model("biz/biz_model");
$this->ci->load->model('area_model');
$this->ci->load->model('auto/auto_brand_model');
$this->ci->load->model('auto/auto_series_model');
$this->ci->load->model('auto/auto_attr_model');
$this->ci->load->model('app/liche/app_liche_orders_model');
$this->ci->load->model('app/liche/app_liche_orders_model');
$this->ci->load->model('app/licheb/app_licheb_users_model');
$this->ci->load->model('app/licheb/app_licheb_channel_biz_model');
$order_row = $this->ci->receiver_orders_v2_model->get(['id'=>$oid]);
if(!$order_row){
return false;
}
$biz_row = $this->ci->biz_model->get(['id'=>$order_row['biz_id']]);
if($biz_row['type']==3){
$channel_users = $this->ci->app_licheb_channel_biz_model->select(['biz_id'=>$biz_row['id']],'','','','uid');
$user_ids = array_column($channel_users,'uid');
$user_ids_str = implode(',',$user_ids);
$user_info = '';
if($user_ids_str && $user_ids){
$where = [
'group_id' => 4,
'status' => 1,
"id in ($user_ids_str)" => null
];
$users = $this->ci->app_licheb_users_model->select($where,'id desc',1,10,'uname,biz_id'); //渠道经理
if($users){
$user_info = implode(' ',array_column($users,'uname'));
}else{ //获取城市
$city_row = $this->ci->area_model->get(['city_id'=>$biz_row['city_id']]);
$user_info = $city_row['city_name'];
}
}
$biz_info = "{$user_info}渠道 {$biz_row['biz_name']}";
}elseif($biz_row['type']==4){ //合伙店
$biz_info = "合伙店 {$biz_row['biz_name']}";
}else{
$user = $this->ci->app_licheb_users_model->get(['id'=>$order_row['sale_id']],'uname'); //获取销售员
$biz_info = "{$biz_row['biz_name']} {$user['uname']}";
}
$brand_row = $this->ci->auto_brand_model->get(['id'=>$order_row['brand_id']],'name');
$series_row = $this->ci->auto_series_model->get(['id'=>$order_row['s_id']],'name');
$attr_arr = [$order_row['v_id'], $order_row['cor_id']];
$attr = $this->ci->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
$v_name = isset($attr[$order_row['v_id']]) ? $attr[$order_row['v_id']][0]['title'] : '';
$cor_name = isset($attr[$order_row['cor_id']]) ? $attr[$order_row['cor_id']][0]['title'] : '';
$car_info = "{$brand_row['name']} {$series_row['name']} {$v_name} {$cor_name}";
$min_price = 100;
if($type){
if(!$date){
return false;
}
$today_s_time = date('Y-m-d 00:00:00',strtotime($date));
$today_e_time = date('Y-m-d 23:59:59',strtotime($date));
$month_s_time = date('Y-m-01 00:00:00',strtotime($date));
$month_e_time = date('Y-m-t 23:59:59',strtotime($date));
}else{
$today_s_time = date('Y-m-d 00:00:00');
$today_e_time = date('Y-m-d H:i:s');
$month_s_time = date('Y-m-01 00:00:00');
$month_e_time = date('Y-m-t H:i:s');
}
$sql = "select count(*) as total from (select min(pay_time) as pay_time from lc_app_liche_orders
where o_id>=10000 and status=1 and pid!=0 and pay_price>0 and pay_time!='0000-00-00 00:00:00' and type <> 4 and pay_price>{$min_price} group by o_id order by o_id desc)a
where pay_time>='{$today_s_time}' and pay_time<='{$today_e_time}'";
$today_query = $this->ci->db->query($sql)->row_array();
$sql = "select count(*) as total from (select min(pay_time) as pay_time from lc_app_liche_orders
where o_id>=10000 and status=1 and pid!=0 and pay_price>0 and pay_time!='0000-00-00 00:00:00' and type <> 4 and pay_price>{$min_price} group by o_id order by o_id desc)a
where pay_time>='{$month_s_time}' and pay_time<='{$month_e_time}'";
$month_query = $this->ci->db->query($sql)->row_array();
$today_count = $today_query['total'];
$month_count = $month_query['total'];
//旧支付信息
$where = [
'type' => 1,
'status' => 1,
'pay_price>=' => 100,
'o_id<' => 10000,
];
$where["pay_time>="] = $today_s_time;
$where["pay_time<="] = $today_e_time;
$old_today_count = $this->ci->app_liche_orders_model->count($where);//今日成交订单数
$where["pay_time>="] = $month_s_time;
$where["pay_time<="] = $month_e_time;
$old_month_count = $this->ci->app_liche_orders_model->count($where);//本月成交订单数
//意向金等于定金的支付订单
$sql = "select count(*) as total from lc_app_liche_orders as a left join lc_receiver_orders as b on a.o_id=b.id where a.total_price=b.deposit and a.type=4 and a.status=1 and a.pay_price>=50 and a.pay_time>='{$today_s_time}' and a.pay_time<='{$today_e_time}'";
$old_today_query = $this->ci->db->query($sql)->row_array();
$sql = "select count(*) as total from lc_app_liche_orders as a left join lc_receiver_orders as b on a.o_id=b.id where a.total_price=b.deposit and a.type=4 and a.status=1 and a.pay_price>=50 and a.pay_time>='{$month_s_time}' and a.pay_time<='{$month_e_time}'";
$old_month_query = $this->ci->db->query($sql)->row_array();
$old_today_query['total'] && $old_today_count += $old_today_query['total'];
$old_month_query['total'] && $old_month_count += $old_month_query['total'];
$old_today_count && $today_count += $old_today_count;
$old_month_count && $month_count += $old_month_count;
$url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$this->key;
if(!$type){
$content = "🎉🎉🎉 **喜讯** 🎉🎉🎉 \n
恭喜 <font color=\"warning\">{$biz_info}</font> 成交 <font color=\"warning\">{$car_info}({$order_row['owner_name']})</font> 一台。\n
>本日累计成交: <font color=\"warning\">{$today_count}</font> 单
>本月累计成交: <font color=\"warning\">{$month_count}</font> 单\n
### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻";
}else{
$day = date('d日',strtotime($date));
$m_day = date('m月d日',strtotime($date));
$month = date('m',strtotime($date));
$month_show = $month==date('m') ? '本月' : $month.'月';
$content = "🎉🎉🎉 **喜讯-补报** 🎉🎉🎉 \n
恭喜 <font color=\"warning\">{$biz_info} </font> {$day}成交 <font color=\"warning\">{$car_info}</font> 一台。\n
>{$m_day}累计成交: <font color=\"warning\">{$today_count}</font> 单
>{$month_show}累计成交: <font color=\"warning\">{$month_count}</font> 单\n
### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻";
}
$data = [
'msgtype' => 'markdown',
'markdown' => [
'content' => $content
]
];
//$res = $this->ci->mycurl->httpPost($url,$data,'is_json');
//$result = json_decode($res,true);
//发送飞书
$info = [
'biz_info' => $biz_info,
'day' => $day,
'car_info' => $car_info,
'owner_name' => $order_row['owner_name'],
'm_day' => $m_day,
'today_count' => $today_count,
'month_show' => $month_show,
'month_count' => $month_count
];
$send_msg = $this->fbook_formart($info,$type);
$result = $this->send_fb($send_msg);
return $result;
}
public function fbook_formart($info,$type){
if(!$type){
$title = '🎉🎉🎉 **喜讯** 🎉🎉🎉';
$content = [
[
[
'tag' => 'text',
'text' => ' 恭喜',
],
[
'tag' => 'a',
'text' => $info['biz_info'],
],
[
'tag' => 'text',
'text' => '成交',
],
[
'tag' => 'a',
'text' => $info['car_info']."({$info['owner_name']})",
],
[
'tag' => 'text',
'text' => '一台。',
],
],
[
[
'tag' => 'text',
'text' => '本日累计成交:',
],
[
'tag' => 'a',
'text' => $info['today_count'],
],
[
'tag' => 'text',
'text' => '单',
],
],
[
[
'tag' => 'text',
'text' => '本月累计成交:',
],
[
'tag' => 'a',
'text' => $info['month_count'],
],
[
'tag' => 'text',
'text' => '单',
],
],
[
[
'tag' => 'text',
'text' => '### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻',
],
],
];
}else{
$title = '🎉🎉🎉 **喜讯-补报** 🎉🎉🎉 ';
$content = [
[
[
'tag' => 'text',
'text' => ' 恭喜',
],
[
'tag' => 'a',
'text' => $info['biz_info'],
],
[
'tag' => 'a',
'text' => $info['day'],
],
[
'tag' => 'text',
'text' => '成交',
],
[
'tag' => 'a',
'text' => $info['car_info'],
],
[
'tag' => 'text',
'text' => '一台。',
],
],
[
[
'tag' => 'a',
'text' => $info['m_day'],
],
[
'tag' => 'text',
'text' => '累计成交:',
],
[
'tag' => 'a',
'text' => $info['today_count'],
],
[
'tag' => 'text',
'text' => '单',
],
],
[
[
'tag' => 'a',
'text' => $info['month_show'],
],
[
'tag' => 'text',
'text' => '累计成交:',
],
[
'tag' => 'a',
'text' => $info['month_count'],
],
[
'tag' => 'text',
'text' => '单',
],
],
[
[
'tag' => 'text',
'text' => '### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻',
],
],
];
}
$msg = [
'title' => $title,
'content' => $content,
];
$data = [
'msg_type' => 'post',
'content' => [
'post' => [
"zh_cn" => $msg
]
],
];
return $data;
}
//推送飞书
public function send_fb($msg){
$res = $this->ci->mycurl->httpPost($this->fbook_url,$msg,'is_json');
$result = json_decode($res,true);
return $result;
}
/**
* 新订单支付后推送消息
* @param $oid
* @param int $type type=0 当天 type=1 补报
* @param string $date 日期type=1必填
* @return mixed
*/
public function deposit_notify_v3($oid, $type=0, $date=''){
$this->ci->load->model('receiver/order/receiver_orders_v2_model');
$this->ci->load->model("biz/biz_model");
$this->ci->load->model('area_model');
$this->ci->load->model('auto/auto_brand_model');
$this->ci->load->model('auto/auto_series_model');
$this->ci->load->model('auto/auto_attr_model');
$this->ci->load->model('app/liche/app_liche_orders_model');
$this->ci->load->model('app/liche/app_liche_orders_model');
$this->ci->load->model('app/licheb/app_licheb_users_model');
$this->ci->load->model('app/licheb/app_licheb_channel_biz_model');
$order_row = $this->ci->receiver_orders_v2_model->get(['id'=>$oid]);
if(!$order_row){
return false;
}
$biz_row = $this->ci->biz_model->get(['id'=>$order_row['biz_id']]);
if($biz_row['type']==3){
$channel_users = $this->ci->app_licheb_channel_biz_model->select(['biz_id'=>$biz_row['id']],'','','','uid');
$user_ids = array_column($channel_users,'uid');
$user_ids_str = implode(',',$user_ids);
$user_info = '';
if($user_ids_str && $user_ids){
$where = [
'group_id' => 4,
'status' => 1,
"id in ($user_ids_str)" => null
];
$users = $this->ci->app_licheb_users_model->select($where,'id desc',1,10,'uname,biz_id'); //渠道经理
if($users){
$user_info = implode(' ',array_column($users,'uname'));
}else{ //获取城市
$city_row = $this->ci->area_model->get(['city_id'=>$biz_row['city_id']]);
$user_info = $city_row['city_name'];
}
}
$biz_info = "{$user_info}渠道 {$biz_row['biz_name']}";
}elseif($biz_row['type']==4){ //合伙店
$biz_info = "合伙店 {$biz_row['biz_name']}";
}else{
$user = $this->ci->app_licheb_users_model->get(['id'=>$order_row['sale_id']],'uname'); //获取销售员
$biz_info = "{$biz_row['biz_name']} {$user['uname']}";
}
$brand_row = $this->ci->auto_brand_model->get(['id'=>$order_row['brand_id']],'name');
$series_row = $this->ci->auto_series_model->get(['id'=>$order_row['s_id']],'name');
$attr_arr = [$order_row['v_id'], $order_row['cor_id']];
$attr = $this->ci->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
$v_name = isset($attr[$order_row['v_id']]) ? $attr[$order_row['v_id']][0]['title'] : '';
$cor_name = isset($attr[$order_row['cor_id']]) ? $attr[$order_row['cor_id']][0]['title'] : '';
$car_info = "{$brand_row['name']} {$series_row['name']} {$v_name} {$cor_name}";
$min_price = 100;
if($type){
if(!$date){
return false;
}
$today_s_time = date('Y-m-d 00:00:00',strtotime($date));
$today_e_time = date('Y-m-d 23:59:59',strtotime($date));
$month_s_time = date('Y-m-01 00:00:00',strtotime($date));
$month_e_time = date('Y-m-t 23:59:59',strtotime($date));
}else{
$today_s_time = date('Y-m-d 00:00:00');
$today_e_time = date('Y-m-d H:i:s');
$month_s_time = date('Y-m-01 00:00:00');
$month_e_time = date('Y-m-t H:i:s');
}
$sql = "select brand_id,count(a.o_id) as total from (select o_id,min(pay_time) as pay_time from lc_app_liche_orders
where o_id>=10000 and status=1 and pid!=0 and pay_price>0 and pay_time!='0000-00-00 00:00:00' and type <> 4 and pay_price>{$min_price} group by o_id order by o_id desc) as a
left join lc_receiver_orders_v2 as b on a.o_id=b.id
where a.pay_time>='{$today_s_time}' and a.pay_time<='{$today_e_time}' group by b.brand_id";
$today_rows = $this->ci->db->query($sql)->result_array();
$today_count = 0;
$today_info = [];
if($today_rows){
foreach ($today_rows as $key => $val) {
$today_count += $val['total'];
$row = $this->ci->auto_brand_model->get(['id'=>$val['brand_id']],'name');
$today_info[] = [
'brand_name' => $row['name'],
'num' => $val['total']
];
}
}
$sql = "select brand_id,count(a.o_id) as total from (select o_id,min(pay_time) as pay_time from lc_app_liche_orders
where o_id>=10000 and status=1 and pid!=0 and pay_price>0 and pay_time!='0000-00-00 00:00:00' and type <> 4 and pay_price>{$min_price} group by o_id order by o_id desc) as a
left join lc_receiver_orders_v2 as b on a.o_id=b.id
where a.pay_time>='{$month_s_time}' and a.pay_time<='{$month_e_time}' group by b.brand_id";
$month_rows = $this->ci->db->query($sql)->result_array();
$month_count = 0;
$month_info = [];
if($month_rows){
foreach ($month_rows as $key => $val) {
$month_count += $val['total'];
$row = $this->ci->auto_brand_model->get(['id'=>$val['brand_id']],'name');
$month_info[] = [
'brand_name' => $row['name'],
'num' => $val['total']
];
}
}
$day = date('d日',strtotime($date));
$m_day = date('m月d日',strtotime($date));
$month = date('m',strtotime($date));
$month_show = $month==date('m') ? '本月' : $month.'月';
//发送飞书
$info = [
'biz_info' => $biz_info,
'day' => $day,
'car_info' => $car_info,
'owner_name' => $order_row['owner_name'],
'm_day' => $m_day,
'today_count' => $today_count,
'month_show' => $month_show,
'month_count' => $month_count
];
$send_msg = $this->fbook_formart_v3($info,$type,$today_info,$month_info);
$result = $this->send_fb($send_msg);
return $result;
}
public function fbook_formart_v3($info,$type,$today_info,$month_info){
$title = $type ? '🎉🎉🎉 **喜讯-补报** 🎉🎉🎉' : '🎉🎉🎉 **喜讯** 🎉🎉🎉';
$header = [
[
'tag' => 'text',
'text' => ' 恭喜',
],
[
'tag' => 'a',
'text' => $info['biz_info'],
],
[
'tag' => 'text',
'text' => '成交',
],
[
'tag' => 'a',
'text' => $info['car_info']."({$info['owner_name']})",
],
[
'tag' => 'text',
'text' => '一台。',
],
];
$content[] = $header;
$content[] = [
[
'tag' => 'text',
'text' => '本日累计成交:',
],
[
'tag' => 'a',
'text' => "{$info['today_count']}",
],
[
'tag' => 'text',
'text' => $today_info ? '单。其中:' : '单',
],
];
if($today_info){
foreach ($today_info as $value) {
$temp_info = [
[
'tag' => 'a',
'text' => "{$value['brand_name']}",
],
[
'tag' => 'text',
'text' => '累计成交:',
],
[
'tag' => 'a',
'text' => "{$value['num']}",
],
[
'tag' => 'text',
'text' => '单',
],
];
$content[] = $temp_info;
}
}
$content[] = [
[
'tag' => 'text',
'text' => '本月累计成交:',
],
[
'tag' => 'a',
'text' => "{$info['month_count']}",
],
[
'tag' => 'text',
'text' => $month_info ? '单。其中:' : '单',
],
];
if($month_info){
foreach ($month_info as $value) {
$temp_info = [
[
'tag' => 'a',
'text' => "{$value['brand_name']}",
],
[
'tag' => 'text',
'text' => '累计成交:',
],
[
'tag' => 'a',
'text' => "{$value['num']}",
],
[
'tag' => 'text',
'text' => '单',
],
];
$content[] = $temp_info;
}
}
$content[] = [
[
'tag' => 'text',
'text' => '### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻',
],
];
$msg = [
'title' => $title,
'content' => $content,
];
$data = [
'msg_type' => 'post',
'content' => [
'post' => [
"zh_cn" => $msg
]
],
];
return $data;
}
}