123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- namespace App\Console\Commands;
- use App\Http\Controllers\Api\V2\PatientController;
- use App\Models\Docter;
- use App\Models\DocterOrganization;
- use App\Models\Order;
- use App\Models\SystemConfig;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- class overTimeOrder extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'order {type}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '超时';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $type = $this->argument('type');
- if(!in_array($type,['orderOut','appiontOut','thenOut','thenLose','orderCancel','orderComplete','todayReminder'])){
- dd('请输入正确的参数');
- }
- if($type == 'orderOut'){
- //订单超时
- $this->clinicOverTimeOrders();
- dd('订单超时检查执行时间'.date('Y-m-d H:i:s'));
- } else if($type == 'appiontOut'){
- //预约超时
- $this->AppointReminder();
- }else if($type == 'thenOut'){
- //认证到期
- $this->OutThenReminder();
- }else if($type == 'thenLose'){
- //认证失效
- $this->InvalidThenReminder();
- } else if($type == 'orderCancel'){
- $this->orderCancel();
- dd('订单接单超时检查执行时间'.date('Y-m-d H:i:s'));
- }else if ($type = 'orderComplete'){
- // 图文||电话订单自动完成
- $this->timeOrdersComplete();
- }else if ($type = "todayReminder"){
- // 明日预约提醒
- $this->TodayReminder();
- }
- dd('ok');
- }
- /**
- * Notes:确认超时提醒 当天的预约订单,医生还未点击完成的订单,23:00给医生发送提醒
- * Author: 白猫!
- * DateTime: 2021/3/4 17:45
- * @param array $params
- */
- public function AppointReminder($id=''){
- if (!empty($id)){
- order_trace(['类型'=>'订单超时提醒','订单id'=>$id,'记录时间'=>date('Ymd His',time())],'info');
- $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$id])->first();
- $send = send_wechatSubscription_message('appoint_reminder',[$Order['docter']['openid'], "pages/index/index",$Order['order_sn'],$Order['user']['nickname'],$Order['user']['phone'],$Order['created_at']]);
- }else{
- $menNewIds = [];
- $startTime = date('Y-m-d',time());
- $endTime = date('Y-m-d',time())." 23:59:59";
- $startTime = strtotime($startTime);
- $endTime = strtotime($endTime);
- $Order = Order::with('orderPatient')->where(['order_status'=>7,'payment_status'=>2])->orWhere(function ($query){
- $query->where(['order_status'=>3,'payment_status'=>2]);
- })->whereBetween('appoint_date',[$startTime,$endTime])->get();
- foreach ($Order as $k=>$v){
- if($v['product_type']==3 || $v['product_type']==4 || $v['product_type']==5){
- if (time() > $v['appoint_end_time']){
- if ($v['docter']['openid']){
- $menNewIds[$k] = $v['id'];
- $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
- }
- }
- }
- }
- order_trace(['订单类型'=>'预约订单','订单id组'=>$menNewIds,'超时时间'=>date('Ymd His',time())],'info');
- if ($menNewIds){
- // 操作门诊订单为已超时
- Order::whereIn('id',$menNewIds)->update(['order_status'=>6,'order_notes'=>'医生确认超时']);
- }
- }
- }
- /**
- * Notes:签约失效提醒
- * Author: 白猫!
- * DateTime: 2021/3/4 17:59
- */
- public function InvalidThenReminder(){
- $list = DocterOrganization::with('docter','organization')->get();
- if($list){
- foreach ($list as $k=>$v){
- if (!\Cache::has($v['id'].'-InvalidThenReminder')) {
- if ($v['docter']['openid']&& time()>=strtotime($v['authentication_end_time'])){
- order_trace(['类型'=>'签约失效提醒','医生openid'=>$v['docter']['openid'],'记录时间'=>date('Ymd His',time())],'info');
- $send = send_wechatSubscription_message('out_then_reminder',[$v['docter']['openid'], "pages/index/index", $v['organization']['name'],date('Y-m-d',strtotime($v['authentication_end_time']))]);
- if ($send){
- \Cache::forever($v['id'].'-InvalidThenReminder',$v['id']);
- }else{
- order_trace(['公共方法发送微信模板消息失败', '请求类型' => 'InvalidThenReminder' ?? '', '返回数据' => $send ?? ''], 'error');
- }
- }
- }
- }
- }
- }
- /**
- * Notes: 认证到期提醒
- * Author: 白猫!
- * DateTime: 2021/3/4 18:00
- */
- public function OutThenReminder(){
- $list = DocterOrganization::with('docter','organization')->get();
- if($list){
- foreach ($list as $k=>$v){
- if (!\Cache::has($v['id'].'-OutThenReminder')) {
- if ($v['docter']['openid']&& (strtotime($v['authentication_end_time'])-strtotime($v['authentication_time']))<=(1*60*60*24)){
- order_trace(['类型'=>'认证到期提醒','医生openid'=>$v['docter']['openid'],'记录时间'=>date('Ymd His',time())],'info');
- $send = send_wechatSubscription_message('out_then_reminder',[$v['docter']['openid'], "pages/index/index", $v['organization']['name'],date('Y-m-d',strtotime($v['authentication_end_time']))]);
- if ($send){
- \Cache::forever($v['id'].'-OutThenReminder',$v['id']);
- }else{
- order_trace(['公共方法发送微信模板消息失败', '请求类型' => 'OutThenReminder' ?? '', '返回数据' => $send ?? ''], 'error');
- }
- }
- }
- }
- }
- }
- /**
- * Notes:明日预约提醒 (定时)
- * Author: 白猫!
- * DateTime: 2021/3/4 17:54
- */
- public function TodayReminder(){
- $list = Order::with('docter','orderPatient','organization')->get();
- if($list){
- foreach ($list as $k=>$v){
- if ($v['docter']['openid']&& date('Y-m-d',$v['order_patient']['appoint_start_time'])==date("Y-m-d",strtotime("+1 day"))){
- order_trace(['类型'=>'预约提醒','订单id'=>$v['id'],'记录时间'=>date('Ymd His',time())],'info');
- $send = send_wechatSubscription_message('today_reminder', [
- $v['docter']['openid'],
- "pages/index/index",
- $v['docter']['name'],
- date("Y-m-d",strtotime("+1 day")),
- "门诊预约",
- count($list),
- !empty($v['organization']['name'])?$v['organization']['name']:'',
- ]);
- }
- }
- }
- }
- /**
- * Notes:图文电话超时自动完成!
- * Author: 白猫!
- * DateTime: 2021/3/4 17:44
- */
- public function timeOrdersComplete(){
- $config_chat = SystemConfig::get('docter_config','chat_complete_time');
- $config_phone = SystemConfig::get('docter_config','phone_complete_time');
- // 换算为秒
- $config_chat = $config_chat*60;
- $config_phone = $config_phone*60;
- $inOrder = Order::with('orderPatient')->where(['order_status'=>3,'payment_status'=>2])->get();
- $catNewIds = [];
- foreach ($inOrder as $k=>$v){
- if ($v['product_type']==1){
- if ((time()-$v['receiving_time'])>=$config_chat){
- Docter::where('id', '=', $v['docter_id'])->increment('service_persons');
- $catNewIds[$k] = $v['id'];
- $this->ReceivingReminderOK($v['id']);
- }
- }else if($v['product_type']==2){
- if ((time()-$v['receiving_time'])>=$config_phone){
- Docter::where('id', '=', $v['docter_id'])->increment('service_persons');
- $catNewIds[$k] = $v['id'];
- $this->ReceivingReminderOK($v['id']);
- }
- }
- }
- if ($catNewIds){
- // 操作图文和电话订单为已完成
- Order::whereIn('id',$catNewIds)->update(['order_status'=>4,'end_time'=>time()]);
- }
- }
- /**
- * Notes:门诊24小时后未确认超时
- * Author: 白猫!
- * DateTime: 2021/3/4 17:48
- */
- public function clinicOverTimeOrders(){
- $startTime = date('Y-m-d',time());
- $endTime = date('Y-m-d',time())." 23:59:59";
- $startTime = strtotime($startTime);
- $endTime = strtotime($endTime);
- $inOrder = Order::with('orderPatient')->where(['order_status'=>7,'payment_status'=>2])->orWhere(function ($query){
- $query->where(['order_status'=>3,'payment_status'=>2]);
- })->whereBetween('appoint_date',[$startTime,$endTime])->get();
- $menNewIds = [];
- foreach ($inOrder as $k=>$v){
- if($v['product_type']==3 || $v['product_type']==4 || $v['product_type']==5){
- if (time() > $v['appoint_end_time']){
- $menNewIds[$k] = $v['id'];
- $this->AppointReminder($v['id']);
- }
- }
- }
- order_trace(['订单类型'=>'门诊订单','订单id组'=>$menNewIds,'超时时间'=>date('Ymd His',time())],'info');
- if ($menNewIds){
- // 操作门诊订单为已超时
- Order::whereIn('id',$menNewIds)->update(['order_status'=>6,'order_notes'=>'医生确认超时']);
- }
- }
- /**
- * 没有医生接单的情况
- * 订单接单超时自动取消 电话 图文(定时)
- */
- public function orderCancel(){
- $config_chat = SystemConfig::get('docter_config','chat_cancel_time');
- $config_phone = SystemConfig::get('docter_config','phone_cancel_time');
- // 换算为秒
- $config_chat = intval($config_chat)*60;
- $config_phone = intval($config_phone)*60;
- $inOrder = Order::with('orderPatient')->with('orderUser')->with('docter')->where('product_type','<',3)->where(['order_status'=>2,'payment_status'=>2])->get();
- foreach ($inOrder as $k=>$v){
- if ($v['product_type']==1){
- if ((time()-intval($v['payment_time']))>=$config_phone){
- $catNewIds[$k] = $v['id'];
- $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
- //取消成功才发送消息
- if($res){
- $this->cancelUserMsg($v);
- $this->cancelDocterMsg($v);
- }
- }
- }else if($v['product_type']==2){
- if ((time()-intval($v['payment_time']))>=$config_chat){
- $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
- //取消成功才发送消息
- if($res){
- $this->cancelUserMsg($v);
- $this->cancelDocterMsg($v);
- }
- }
- }
- }
- }
- public function cancelUserMsg($order)
- {
- $openid = $order->orderUser->openid;
- $pt = Order::getProductType();
- $type = $pt[$order->product_type];
- $msgArr = [
- $openid,
- $order->order_sn,
- $type,
- ($order->payment_amount/100),
- // $order->created_at,
- date('Y-m-d H:i:s'),
- $order->orderPatient->name,
- $order->docter->name.'医生',
- '医生超时未接单自动取消',
- '订单状态:已退款 ',
- ];
- $minArr = [$openid,$order->order_sn,$type,'医生超时未接单自动取消'];
- send_wechat_message(7,$msgArr,$minArr);
- }
- public function cancelDocterMsg($order)
- {
- $openid = $order->docter->openid;
- // $openid = 'oflME5eixHMij2TIVyy52WbfaQvA';
- $pt = Order::getProductType();
- $type = $pt[$order->product_type];
- $msgArr = [
- $openid,
- '',
- $order->docter->name,
- $order->order_sn,
- $type,
- ($order->payment_amount/100).'元',
- // $order->created_at,
- date('Y-m-d H:i:s'),
- '医生超时未接单自动取消',
- ];
- $res = send_wechatSubscription_message('cancel_reminder',$msgArr);
- }
- /**
- * 完成订单
- * @param $order_id
- */
- public function ReceivingReminderOK($order_id){
- $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first();
- $type = '';
- if ($Order['product_type']==1){
- $type = '电话咨询已结束';
- }elseif ($Order['product_type']==2){
- $type = '图文咨询已结束 ';
- }elseif ($Order['product_type']==3){
- $type = '门诊预约';
- }
- order_trace(['订单类型'=>$type,'订单id'=>$Order,'操作时间'=>date('Ymd His',time())],'info');
- if ($Order){
- if ($Order['user']['openid']){
- $send = send_wechatSubscription_message('receiving_reminderok',[
- $Order['user']['openid'],
- "pages/index",
- $Order['order_sn'],
- $type,
- date('Y-m-d H:i:s',$Order['receiving_time']),
- date('Y-m-d H:i:s',time()),
- ],'wechat_small_program');
- }
- }
- }
- }
|