overTimeOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Controllers\Api\V2\PatientController;
  4. use App\Models\Docter;
  5. use App\Models\DocterOrganization;
  6. use App\Models\Order;
  7. use App\Models\SystemConfig;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\Log;
  10. class overTimeOrder extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'order {type}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '超时';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $type = $this->argument('type');
  41. if(!in_array($type,['orderOut','appiontOut','thenOut','thenLose','orderCancel','orderComplete','todayReminder'])){
  42. dd('请输入正确的参数');
  43. }
  44. if($type == 'orderOut'){
  45. //订单超时
  46. $this->clinicOverTimeOrders();
  47. dd('订单超时检查执行时间'.date('Y-m-d H:i:s'));
  48. } else if($type == 'appiontOut'){
  49. //预约超时
  50. $this->AppointReminder();
  51. }else if($type == 'thenOut'){
  52. //认证到期
  53. $this->OutThenReminder();
  54. }else if($type == 'thenLose'){
  55. //认证失效
  56. $this->InvalidThenReminder();
  57. } else if($type == 'orderCancel'){
  58. $this->orderCancel();
  59. dd('订单接单超时检查执行时间'.date('Y-m-d H:i:s'));
  60. }else if ($type = 'orderComplete'){
  61. // 图文||电话订单自动完成
  62. $this->timeOrdersComplete();
  63. }else if ($type = "todayReminder"){
  64. // 明日预约提醒
  65. $this->TodayReminder();
  66. }
  67. dd('ok');
  68. }
  69. /**
  70. * Notes:确认超时提醒 当天的预约订单,医生还未点击完成的订单,23:00给医生发送提醒
  71. * Author: 白猫!
  72. * DateTime: 2021/3/4 17:45
  73. * @param array $params
  74. */
  75. public function AppointReminder($id=''){
  76. if (!empty($id)){
  77. // order_trace(['类型'=>'订单超时提醒','订单id'=>$id,'记录时间'=>date('Ymd His',time())],'info');
  78. $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$id])->first();
  79. $send = send_wechatSubscription_message('appoint_reminder',[$Order['docter']['openid'], "pages/index/index",$Order['order_sn'],$Order['user']['nickname'],$Order['user']['phone'],$Order['created_at']]);
  80. }else{
  81. $menNewIds = [];
  82. // $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  83. // $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  84. $Order = Order::with(['orderPatient','docter','user'])->where(['order_status'=>7,'payment_status'=>2])->orWhere(function ($query){
  85. $query->where(['order_status'=>3,'payment_status'=>2]);
  86. })->where('appoint_end_time','<',time())->get();
  87. foreach ($Order as $k=>$v){
  88. if($v['product_type']==3 || $v['product_type']==4 || $v['product_type']==5){
  89. // if (time() > $v['appoint_end_time']){
  90. if ($v['docter']['openid']){
  91. $menNewIds[$k] = $v['id'];
  92. $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
  93. }
  94. // }
  95. }
  96. }
  97. order_trace(['订单类型'=>'预约订单','订单id组'=>$menNewIds,'超时时间'=>date('Ymd His',time())],'info');
  98. if ($menNewIds){
  99. // 操作门诊订单为已超时
  100. Order::whereIn('id',$menNewIds)->update(['order_status'=>6,'order_notes'=>'医生确认超时']);
  101. }
  102. }
  103. }
  104. /**
  105. * Notes:签约失效提醒
  106. * Author: 白猫!
  107. * DateTime: 2021/3/4 17:59
  108. */
  109. public function InvalidThenReminder(){
  110. $list = DocterOrganization::with('docter','organization')->get();
  111. if($list){
  112. foreach ($list as $k=>$v){
  113. if ($v['docter']['openid']&& time()>=strtotime($v['authentication_end_time'])){
  114. order_trace(['类型'=>'签约失效提醒','医生openid'=>$v['docter']['openid'],'记录时间'=>date('Ymd His',time())],'info');
  115. $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']))]);
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Notes: 认证到期提醒
  122. * Author: 白猫!
  123. * DateTime: 2021/3/4 18:00
  124. */
  125. public function OutThenReminder(){
  126. $list = DocterOrganization::with('docter','organization')->get();
  127. if($list){
  128. foreach ($list as $k=>$v){
  129. if ($v['docter']['openid']&& (strtotime($v['authentication_end_time'])-strtotime($v['authentication_time']))<=(1*60*60*24)){
  130. order_trace(['类型'=>'认证到期提醒','医生openid'=>$v['docter']['openid'],'记录时间'=>date('Ymd His',time())],'info');
  131. $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']))]);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Notes:明日预约提醒 (定时)
  138. * Author: 白猫!
  139. * DateTime: 2021/3/4 17:54
  140. */
  141. public function TodayReminder(){
  142. $list = Order::with('docter','orderPatient','organization')->get();
  143. if($list){
  144. foreach ($list as $k=>$v){
  145. if ($v['docter']['openid']&& date('Y-m-d',$v['order_patient']['appoint_start_time'])==date("Y-m-d",strtotime("+1 day"))){
  146. order_trace(['类型'=>'预约提醒','订单id'=>$v['id'],'记录时间'=>date('Ymd His',time())],'info');
  147. $send = send_wechatSubscription_message('today_reminder', [
  148. $v['docter']['openid'],
  149. "pages/index/index",
  150. $v['docter']['name'],
  151. date("Y-m-d",strtotime("+1 day")),
  152. "门诊预约",
  153. count($list),
  154. !empty($v['organization']['name'])?$v['organization']['name']:'',
  155. ]);
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * Notes:图文电话超时自动完成!
  162. * Author: 白猫!
  163. * DateTime: 2021/3/4 17:44
  164. */
  165. public function timeOrdersComplete(){
  166. $config_chat = SystemConfig::get('docter_config','chat_complete_time');
  167. $config_phone = SystemConfig::get('docter_config','phone_complete_time');
  168. // 换算为秒
  169. $config_chat = $config_chat*60;
  170. $config_phone = $config_phone*60;
  171. $inOrder = Order::with('orderPatient')->where(['order_status'=>3,'payment_status'=>2])->get();
  172. $catNewIds = [];
  173. foreach ($inOrder as $k=>$v){
  174. if ($v['product_type']==1){
  175. if ((time()-$v['receiving_time'])>=$config_chat){
  176. Docter::where('id', '=', $v['docter_id'])->increment('service_persons');
  177. $catNewIds[$k] = $v['id'];
  178. $this->ReceivingReminderOK($v['id']);
  179. }
  180. }else if($v['product_type']==2){
  181. if ((time()-$v['receiving_time'])>=$config_phone){
  182. Docter::where('id', '=', $v['docter_id'])->increment('service_persons');
  183. $catNewIds[$k] = $v['id'];
  184. $this->ReceivingReminderOK($v['id']);
  185. }
  186. }
  187. }
  188. if ($catNewIds){
  189. // 操作图文和电话订单为已完成
  190. Order::whereIn('id',$catNewIds)->update(['order_status'=>4]);
  191. }
  192. }
  193. /**
  194. * Notes:门诊24小时后未确认超时
  195. * Author: 白猫!
  196. * DateTime: 2021/3/4 17:48
  197. */
  198. public function clinicOverTimeOrders(){
  199. $inOrder = Order::with('orderPatient')->where(['order_status'=>7,'payment_status'=>2])->orWhere(function ($query){
  200. $query->where(['order_status'=>3,'payment_status'=>2]);
  201. })->where('appoint_end_time','<',time())->get();
  202. $menNewIds = [];
  203. foreach ($inOrder as $k=>$v){
  204. if($v['product_type']==3 || $v['product_type']==4 || $v['product_type']==5){
  205. // if (time() > $v['appoint_end_time']){
  206. $menNewIds[$k] = $v['id'];
  207. $this->AppointReminder($v['id']);
  208. // }
  209. }
  210. }
  211. order_trace(['订单类型'=>'门诊订单','订单id组'=>$menNewIds,'超时时间'=>date('Ymd His',time())],'info');
  212. if ($menNewIds){
  213. // 操作门诊订单为已超时
  214. Order::whereIn('id',$menNewIds)->update(['order_status'=>6,'order_notes'=>'医生确认超时']);
  215. }
  216. }
  217. /**
  218. * 没有医生接单的情况
  219. * 订单接单超时自动取消 电话 图文(定时)
  220. */
  221. public function orderCancel(){
  222. $config_chat = SystemConfig::get('docter_config','chat_cancel_time');
  223. $config_phone = SystemConfig::get('docter_config','phone_cancel_time');
  224. // 换算为秒
  225. $config_chat = intval($config_chat)*60;
  226. $config_phone = intval($config_phone)*60;
  227. $inOrder = Order::with('orderPatient')->with('orderUser')->with('docter')->where('product_type','<',3)->where(['order_status'=>2,'payment_status'=>2])->get();
  228. foreach ($inOrder as $k=>$v){
  229. if ($v['product_type']==1){
  230. if ((time()-intval($v['payment_time']))>=$config_phone){
  231. $catNewIds[$k] = $v['id'];
  232. $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
  233. //取消成功才发送消息
  234. if($res){
  235. $this->cancelUserMsg($v);
  236. $this->cancelDocterMsg($v);
  237. }
  238. }
  239. }else if($v['product_type']==2){
  240. if ((time()-intval($v['payment_time']))>=$config_chat){
  241. $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
  242. //取消成功才发送消息
  243. if($res){
  244. $this->cancelUserMsg($v);
  245. $this->cancelDocterMsg($v);
  246. }
  247. }
  248. }
  249. }
  250. }
  251. public function cancelUserMsg($order)
  252. {
  253. $openid = $order->orderUser->openid;
  254. $pt = Order::getProductType();
  255. $type = $pt[$order->product_type];
  256. $msgArr = [
  257. $openid,
  258. $order->order_sn,
  259. $type,
  260. ($order->payment_amount/100),
  261. // $order->created_at,
  262. date('Y-m-d H:i:s'),
  263. $order->orderPatient->name,
  264. $order->docter->name.'医生',
  265. '医生超时未接单自动取消',
  266. '订单状态:已退款 ',
  267. ];
  268. $minArr = [$openid,$order->order_sn,$type,'医生超时未接单自动取消'];
  269. send_wechat_message(7,$msgArr,$minArr);
  270. }
  271. public function cancelDocterMsg($order)
  272. {
  273. $openid = $order->docter->openid;
  274. // $openid = 'oflME5eixHMij2TIVyy52WbfaQvA';
  275. $pt = Order::getProductType();
  276. $type = $pt[$order->product_type];
  277. $msgArr = [
  278. $openid,
  279. '',
  280. $order->docter->name,
  281. $order->order_sn,
  282. $type,
  283. ($order->payment_amount/100).'元',
  284. // $order->created_at,
  285. date('Y-m-d H:i:s'),
  286. '医生超时未接单自动取消',
  287. ];
  288. $res = send_wechatSubscription_message('cancel_reminder',$msgArr);
  289. }
  290. /**
  291. * 完成订单
  292. * @param $order_id
  293. */
  294. public function ReceivingReminderOK($order_id){
  295. $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first();
  296. $type = '';
  297. if ($Order['product_type']==1){
  298. $type = '电话咨询已结束';
  299. }elseif ($Order['product_type']==2){
  300. $type = '图文咨询已结束 ';
  301. }elseif ($Order['product_type']==3){
  302. $type = '门诊预约';
  303. }
  304. order_trace(['订单类型'=>$type,'订单id'=>$Order,'操作时间'=>date('Ymd His',time())],'info');
  305. if ($Order){
  306. if ($Order['user']['openid']){
  307. $send = send_wechatSubscription_message('receiving_reminderok',[
  308. $Order['user']['openid'],
  309. "pages/index",
  310. $Order['order_sn'],
  311. $type,
  312. $Order['payment_amount'],
  313. ],'wechat_small_program');
  314. }
  315. }
  316. }
  317. }