overTimeOrder.php 11 KB

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