overTimeOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. $startTime = date('Y-m-d',time());
  83. $endTime = date('Y-m-d',time())." 23:59:59";
  84. $startTime = strtotime($startTime);
  85. $endTime = strtotime($endTime);
  86. $Order = Order::with('orderPatient')->where(['order_status'=>7,'payment_status'=>2])->orWhere(function ($query){
  87. $query->where(['order_status'=>3,'payment_status'=>2]);
  88. })->whereBetween('appoint_date',[$startTime,$endTime])->get();
  89. foreach ($Order as $k=>$v){
  90. if($v['product_type']==3 || $v['product_type']==4 || $v['product_type']==5){
  91. if (time() > $v['appoint_end_time']){
  92. if ($v['docter']['openid']){
  93. $menNewIds[$k] = $v['id'];
  94. $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
  95. }
  96. }
  97. }
  98. }
  99. order_trace(['订单类型'=>'预约订单','订单id组'=>$menNewIds,'超时时间'=>date('Ymd His',time())],'info');
  100. if ($menNewIds){
  101. // 操作门诊订单为已超时
  102. Order::whereIn('id',$menNewIds)->update(['order_status'=>6,'order_notes'=>'医生确认超时']);
  103. }
  104. }
  105. }
  106. /**
  107. * Notes:签约失效提醒
  108. * Author: 白猫!
  109. * DateTime: 2021/3/4 17:59
  110. */
  111. public function InvalidThenReminder(){
  112. $list = DocterOrganization::with('docter','organization')->get();
  113. if($list){
  114. foreach ($list as $k=>$v){
  115. if ($v['docter']['openid']&& time()>=strtotime($v['authentication_end_time'])){
  116. order_trace(['类型'=>'签约失效提醒','医生openid'=>$v['docter']['openid'],'记录时间'=>date('Ymd His',time())],'info');
  117. $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']))]);
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * Notes: 认证到期提醒
  124. * Author: 白猫!
  125. * DateTime: 2021/3/4 18:00
  126. */
  127. public function OutThenReminder(){
  128. $list = DocterOrganization::with('docter','organization')->get();
  129. if($list){
  130. foreach ($list as $k=>$v){
  131. if ($v['docter']['openid']&& (strtotime($v['authentication_end_time'])-strtotime($v['authentication_time']))<=(1*60*60*24)){
  132. order_trace(['类型'=>'认证到期提醒','医生openid'=>$v['docter']['openid'],'记录时间'=>date('Ymd His',time())],'info');
  133. $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']))]);
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * Notes:明日预约提醒 (定时)
  140. * Author: 白猫!
  141. * DateTime: 2021/3/4 17:54
  142. */
  143. public function TodayReminder(){
  144. $list = Order::with('docter','orderPatient','organization')->get();
  145. if($list){
  146. foreach ($list as $k=>$v){
  147. if ($v['docter']['openid']&& date('Y-m-d',$v['order_patient']['appoint_start_time'])==date("Y-m-d",strtotime("+1 day"))){
  148. order_trace(['类型'=>'预约提醒','订单id'=>$v['id'],'记录时间'=>date('Ymd His',time())],'info');
  149. $send = send_wechatSubscription_message('today_reminder', [
  150. $v['docter']['openid'],
  151. "pages/index/index",
  152. $v['docter']['name'],
  153. date("Y-m-d",strtotime("+1 day")),
  154. "门诊预约",
  155. count($list),
  156. !empty($v['organization']['name'])?$v['organization']['name']:'',
  157. ]);
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * Notes:图文电话超时自动完成!
  164. * Author: 白猫!
  165. * DateTime: 2021/3/4 17:44
  166. */
  167. public function timeOrdersComplete(){
  168. $config_chat = SystemConfig::get('docter_config','chat_complete_time');
  169. $config_phone = SystemConfig::get('docter_config','phone_complete_time');
  170. // 换算为秒
  171. $config_chat = $config_chat*60;
  172. $config_phone = $config_phone*60;
  173. $inOrder = Order::with('orderPatient')->where(['order_status'=>3,'payment_status'=>2])->get();
  174. $catNewIds = [];
  175. foreach ($inOrder as $k=>$v){
  176. if ($v['product_type']==1){
  177. if ((time()-$v['receiving_time'])>=$config_chat){
  178. Docter::where('id', '=', $v['docter_id'])->increment('service_persons');
  179. $catNewIds[$k] = $v['id'];
  180. $this->ReceivingReminderOK($v['id']);
  181. }
  182. }else if($v['product_type']==2){
  183. if ((time()-$v['receiving_time'])>=$config_phone){
  184. Docter::where('id', '=', $v['docter_id'])->increment('service_persons');
  185. $catNewIds[$k] = $v['id'];
  186. $this->ReceivingReminderOK($v['id']);
  187. }
  188. }
  189. }
  190. if ($catNewIds){
  191. // 操作图文和电话订单为已完成
  192. Order::whereIn('id',$catNewIds)->update(['order_status'=>4]);
  193. }
  194. }
  195. /**
  196. * Notes:门诊24小时后未确认超时
  197. * Author: 白猫!
  198. * DateTime: 2021/3/4 17:48
  199. */
  200. public function clinicOverTimeOrders(){
  201. $startTime = date('Y-m-d',time());
  202. $endTime = date('Y-m-d',time())." 23:59:59";
  203. $startTime = strtotime($startTime);
  204. $endTime = strtotime($endTime);
  205. $inOrder = Order::with('orderPatient')->where(['order_status'=>7,'payment_status'=>2])->orWhere(function ($query){
  206. $query->where(['order_status'=>3,'payment_status'=>2]);
  207. })->whereBetween('appoint_date',[$startTime,$endTime])->get();
  208. $menNewIds = [];
  209. foreach ($inOrder as $k=>$v){
  210. if($v['product_type']==3 || $v['product_type']==4 || $v['product_type']==5){
  211. if (time() > $v['appoint_end_time']){
  212. $menNewIds[$k] = $v['id'];
  213. $this->AppointReminder($v['id']);
  214. }
  215. }
  216. }
  217. order_trace(['订单类型'=>'门诊订单','订单id组'=>$menNewIds,'超时时间'=>date('Ymd His',time())],'info');
  218. if ($menNewIds){
  219. // 操作门诊订单为已超时
  220. Order::whereIn('id',$menNewIds)->update(['order_status'=>6,'order_notes'=>'医生确认超时']);
  221. }
  222. }
  223. /**
  224. * 没有医生接单的情况
  225. * 订单接单超时自动取消 电话 图文(定时)
  226. */
  227. public function orderCancel(){
  228. $config_chat = SystemConfig::get('docter_config','chat_cancel_time');
  229. $config_phone = SystemConfig::get('docter_config','phone_cancel_time');
  230. // 换算为秒
  231. $config_chat = intval($config_chat)*60;
  232. $config_phone = intval($config_phone)*60;
  233. $inOrder = Order::with('orderPatient')->with('orderUser')->with('docter')->where('product_type','<',3)->where(['order_status'=>2,'payment_status'=>2])->get();
  234. foreach ($inOrder as $k=>$v){
  235. if ($v['product_type']==1){
  236. if ((time()-intval($v['payment_time']))>=$config_phone){
  237. $catNewIds[$k] = $v['id'];
  238. $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
  239. //取消成功才发送消息
  240. if($res){
  241. $this->cancelUserMsg($v);
  242. $this->cancelDocterMsg($v);
  243. }
  244. }
  245. }else if($v['product_type']==2){
  246. if ((time()-intval($v['payment_time']))>=$config_chat){
  247. $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
  248. //取消成功才发送消息
  249. if($res){
  250. $this->cancelUserMsg($v);
  251. $this->cancelDocterMsg($v);
  252. }
  253. }
  254. }
  255. }
  256. }
  257. public function cancelUserMsg($order)
  258. {
  259. $openid = $order->orderUser->openid;
  260. $pt = Order::getProductType();
  261. $type = $pt[$order->product_type];
  262. $msgArr = [
  263. $openid,
  264. $order->order_sn,
  265. $type,
  266. ($order->payment_amount/100),
  267. // $order->created_at,
  268. date('Y-m-d H:i:s'),
  269. $order->orderPatient->name,
  270. $order->docter->name.'医生',
  271. '医生超时未接单自动取消',
  272. '订单状态:已退款 ',
  273. ];
  274. $minArr = [$openid,$order->order_sn,$type,'医生超时未接单自动取消'];
  275. send_wechat_message(7,$msgArr,$minArr);
  276. }
  277. public function cancelDocterMsg($order)
  278. {
  279. $openid = $order->docter->openid;
  280. // $openid = 'oflME5eixHMij2TIVyy52WbfaQvA';
  281. $pt = Order::getProductType();
  282. $type = $pt[$order->product_type];
  283. $msgArr = [
  284. $openid,
  285. '',
  286. $order->docter->name,
  287. $order->order_sn,
  288. $type,
  289. ($order->payment_amount/100).'元',
  290. // $order->created_at,
  291. date('Y-m-d H:i:s'),
  292. '医生超时未接单自动取消',
  293. ];
  294. $res = send_wechatSubscription_message('cancel_reminder',$msgArr);
  295. }
  296. /**
  297. * 完成订单
  298. * @param $order_id
  299. */
  300. public function ReceivingReminderOK($order_id){
  301. $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first();
  302. $type = '';
  303. if ($Order['product_type']==1){
  304. $type = '电话咨询已结束';
  305. }elseif ($Order['product_type']==2){
  306. $type = '图文咨询已结束 ';
  307. }elseif ($Order['product_type']==3){
  308. $type = '门诊预约';
  309. }
  310. order_trace(['订单类型'=>$type,'订单id'=>$Order,'操作时间'=>date('Ymd His',time())],'info');
  311. if ($Order){
  312. if ($Order['user']['openid']){
  313. $send = send_wechatSubscription_message('receiving_reminderok',[
  314. $Order['user']['openid'],
  315. "pages/index",
  316. $Order['order_sn'],
  317. $type,
  318. $Order['payment_amount'],
  319. ],'wechat_small_program');
  320. }
  321. }
  322. }
  323. }