overTimeOrder.php 13 KB

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