overTimeOrder.php 12 KB

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