overTimeOrder.php 14 KB

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