overTimeOrder.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\DocterOrganization;
  4. use App\Models\Order;
  5. use App\Models\SystemConfig;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. class overTimeOrder extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'order {type}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '超时';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $type = $this->argument('type');
  39. if(!in_array($type,['orderOut','appiontOut','thenOut','thenLose','orderCancel'])){
  40. dd('请输入正确的参数');
  41. }
  42. if($type == 'orderOut'){
  43. //订单超时
  44. $this->overTimeOrers();
  45. dd('订单超时检查执行时间'.date('Y-m-d H:i:s'));
  46. } else if($type == 'appiontOut'){
  47. //预约超时
  48. $this->AppointReminder();
  49. }else if($type == 'thenOut'){
  50. //认证到期
  51. $this->OutThenReminder();
  52. }else if($type == 'thenLose'){
  53. //认证失效
  54. $this->InvalidThenReminder();
  55. } else if($type == 'orderCancel'){
  56. $this->orderCancel();
  57. dd('订单接单超时检查执行时间'.date('Y-m-d H:i:s'));
  58. }
  59. dd('ok');
  60. }
  61. /**
  62. * 确认超时提醒 当天的预约订单,医生还未点击完成的订单,23:00给医生发送提醒
  63. */
  64. public function AppointReminder(){
  65. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  66. $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  67. $Order = Order::with(['orderPatient','docter','user'])->where(['order_status'=>3,'payment_status'=>2,'product_type'=>3])->whereBetween('receiving_time',[$beginToday,$endToday])->get();
  68. foreach ($Order as $k=>$v){
  69. if ($v['docter']){
  70. if ($v['docter']['openid']){
  71. $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * 签约失效提醒
  78. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  79. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  80. * @throws \GuzzleHttp\Exception\GuzzleException
  81. */
  82. public function InvalidThenReminder(){
  83. $list = DocterOrganization::with('docter','organization')->get();
  84. if($list){
  85. foreach ($list as $k=>$v){
  86. if ($v['docter']['openid']&& time()>=strtotime($v['authentication_end_time'])){
  87. $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']))]);
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * 认证到期提醒
  94. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  95. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  96. * @throws \GuzzleHttp\Exception\GuzzleException
  97. */
  98. public function OutThenReminder(){
  99. $list = DocterOrganization::with('docter','organization')->get();
  100. if($list){
  101. foreach ($list as $k=>$v){
  102. if ($v['docter']['openid']&& (strtotime($v['authentication_end_time'])-strtotime($v['authentication_time']))<=(1*60*60*24)){
  103. $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']))]);
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * 有医生接单的情况
  110. * 订单超时自动完成 电话 图文 门诊(定时)
  111. */
  112. public function overTimeOrers(){
  113. $config_chat = SystemConfig::get('docter_config','chat_complete_time');
  114. $config_phone = SystemConfig::get('docter_config','phone_complete_time');
  115. // 换算为秒
  116. $config_chat = intval($config_chat)*60;
  117. $config_phone = intval($config_phone)*60;
  118. $inOrder = Order::with('orderPatient')->where(['order_status'=>3,'payment_status'=>2])->get();
  119. $catNewIds = [];
  120. $menNewIds = [];
  121. foreach ($inOrder as $k=>$v){
  122. if ($v['product_type']==1){
  123. if ((time()-intval($v['receiving_time']))>=$config_phone){
  124. $catNewIds[$k] = $v['id'];
  125. }
  126. }else if($v['product_type']==2){
  127. if ((time()-$v['receiving_time'])>=$config_chat){
  128. echo $v->order_sn.PHP_EOL;
  129. $catNewIds[$k] = $v['id'];
  130. }
  131. }else if($v['product_type']==3){
  132. if ((time()-$v['receiving_time'])>=(1*60*60*24)){
  133. $menNewIds[$k] = $v['id'];
  134. }
  135. }
  136. }
  137. // echo '图文订单'.count($catNewIds).'个 ---'.'门诊订单'.count($menNewIds).'个';
  138. if ($catNewIds || $menNewIds){
  139. // 操作图文和电话订单为已完成
  140. Order::whereIn('id',$catNewIds)->update(['order_status'=>4]);
  141. // 操作门诊订单为已超时
  142. Order::whereIn('id',$menNewIds)->update(['order_status'=>6]);
  143. }
  144. }
  145. /**
  146. * 没有医生接单的情况
  147. * 订单接单超时自动取消 电话 图文(定时)
  148. */
  149. public function orderCancel(){
  150. $config_chat = SystemConfig::get('docter_config','chat_cancel_time');
  151. $config_phone = SystemConfig::get('docter_config','phone_cancel_time');
  152. // 换算为秒
  153. $config_chat = intval($config_chat)*60;
  154. $config_phone = intval($config_phone)*60;
  155. $inOrder = Order::with('orderPatient')->with('orderUser')->with('docter')->where('product_type','<',3)->where(['order_status'=>2,'payment_status'=>2])->get();
  156. foreach ($inOrder as $k=>$v){
  157. if ($v['product_type']==1){
  158. if ((time()-intval($v['payment_time']))>=$config_phone){
  159. $catNewIds[$k] = $v['id'];
  160. $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
  161. //取消成功才发送消息
  162. if($res){
  163. $this->cancelUserMsg($v);
  164. $this->cancelDocterMsg($v);
  165. }
  166. }
  167. }else if($v['product_type']==2){
  168. if ((time()-intval($v['payment_time']))>=$config_chat){
  169. $res = Order::orderCancel($v['id'],'医生超时未接单自动取消');
  170. //取消成功才发送消息
  171. if($res){
  172. $this->cancelUserMsg($v);
  173. $this->cancelDocterMsg($v);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. public function cancelUserMsg($order)
  180. {
  181. $openid = $order->orderUser->openid;
  182. $pt = Order::getProductType();
  183. $type = $pt[$order->product_type];
  184. $msgArr = [
  185. $openid,
  186. $order->order_sn,
  187. $type,
  188. ($order->payment_amount/100),
  189. // $order->created_at,
  190. date('Y-m-d H:i:s'),
  191. $order->orderPatient->name,
  192. $order->docter->name.'医生',
  193. '医生超时未接单自动取消',
  194. '订单状态:已退款 ',
  195. ];
  196. $minArr = [$openid,$order->order_sn,$type,'医生超时未接单自动取消'];
  197. send_wechat_message(7,$msgArr,$minArr);
  198. }
  199. public function cancelDocterMsg($order)
  200. {
  201. $openid = $order->docter->openid;
  202. // $openid = 'oflME5eixHMij2TIVyy52WbfaQvA';
  203. $pt = Order::getProductType();
  204. $type = $pt[$order->product_type];
  205. $msgArr = [
  206. $openid,
  207. '',
  208. $order->docter->name,
  209. $order->order_sn,
  210. $type,
  211. ($order->payment_amount/100).'元',
  212. // $order->created_at,
  213. date('Y-m-d H:i:s'),
  214. '医生超时未接单自动取消',
  215. ];
  216. $res = send_wechatSubscription_message('cancel_reminder',$msgArr);
  217. }
  218. }