FailOrderAlertV2.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Modules\Subscribe\Services\OrderService;
  5. use Redis;
  6. use App\Libs\AliSMS;
  7. use Log;
  8. use DB;
  9. use App\Modules\Subscribe\Models\Order;
  10. class FailOrderAlertV2 extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'foav2';
  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. $this->payAlert();
  41. }
  42. private function payAlert()
  43. {
  44. $pay_ids = $this->getPayId();
  45. if(!$pay_ids){
  46. return '';
  47. }
  48. foreach ($pay_ids as $pay_id){
  49. $this->payAlertOne($pay_id);
  50. }
  51. return '';
  52. }
  53. private function payAlertOne($pay_id)
  54. {
  55. $phone_arr = ['15868100210', '18668029091', '15262937943','18367118561'];
  56. $pay_info = Redis::hgetall('pay_merchant:'.$pay_id);
  57. $is_need_alert = false;
  58. $ntime = time();
  59. $time_str = 180;
  60. // 10min 15min 1.5min 3min 未支付的订单报警规则 96/103 倍道 97树羽
  61. $alert_time_diff = ['1'=>180,'2'=>180];
  62. if($pay_info)
  63. {
  64. $last_alert_time = isset($pay_info['last_alert_time']) ? (int)$pay_info['last_alert_time'] : 0;//上次报警时间
  65. if($ntime - $last_alert_time > 180)//2小时内不重复报警
  66. {
  67. $unpaid_num = isset($pay_info['unpaid_num']) ? (int)$pay_info['unpaid_num'] : 0;
  68. $last_create_time = isset($pay_info['last_create_time']) ? (int)$pay_info['last_create_time'] : 0;
  69. $order_num = 10;
  70. if($unpaid_num >= $order_num)//超过30条都是失败订单报警
  71. {
  72. $is_need_alert = true;
  73. $content = '订单预警!!!支付通道: '.$pay_id.',最近'.$order_num.'个订单都是未支付订单,尽快排查。。';
  74. $template_type = 'order_remind';
  75. $back_pay = DB::table('pay_merchants')
  76. ->where('name','BD_BACK_UP_MAIN')
  77. ->where('is_enabled',1)
  78. ->select('id')->orderBy('id','desc')->first();
  79. if($back_pay){
  80. DB::table('distribution_channels')->where('pay_merchant_id',$pay_id)->update([
  81. 'pay_merchant_id'=>$back_pay->id,
  82. 'updated_at'=>date('Y-m-d H:i:s')
  83. ]);
  84. DB::table('pay_merchants')->where('id',$pay_id)->update(['name'=>'BD_DEFAULT2','updated_at'=>date('Y-m-d H:i:s')]);
  85. DB::table('pay_merchants')->where('id',$back_pay->id)->update(['name'=>'BD_DEFAULT','updated_at'=>date('Y-m-d H:i:s')]);
  86. $param = ['pay_id'=>$pay_id,'new_pay_id' => $back_pay->id];
  87. foreach ($phone_arr as $phone){
  88. $this->sendSms($phone, 'pay_channel_change',$param);
  89. $this->sendSms($phone, 'order_remind',$param);
  90. }
  91. }else{
  92. $param = ['pay_id'=>$pay_id];
  93. foreach ($phone_arr as $phone){
  94. $this->sendSms($phone, $template_type,$param);
  95. }
  96. }
  97. }
  98. if(isset($alert_time_diff[$pay_id]) && $last_create_time && ($ntime - $last_create_time) > $alert_time_diff[$pay_id] ){
  99. $is_need_alert = true;
  100. $content = '支付通道: '.$pay_id.' ,最近'.$time_str.'秒内没有产生一条订单,尽快排查';
  101. $template_type = 'order_halfhour_remind';
  102. $param = ['pay_id'=>$pay_id];
  103. foreach ($phone_arr as $phone){
  104. $this->sendSms($phone, $template_type,$param);
  105. }
  106. }
  107. if($is_need_alert)//更新报警时间
  108. {
  109. Redis::hset('pay_merchant:'.$pay_id,'last_alert_time',$ntime);
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * 发送预警
  116. */
  117. private function sendSms($phone, $template_type,$param){
  118. if(empty($phone)){
  119. return false;
  120. }
  121. return AliSMS::send($phone, $template_type,$param);
  122. }
  123. private function getPayId(){
  124. $sql = 'select distinct pay_merchant_id from distribution_channels where pay_merchant_id <>0 and id !=14';
  125. $res = DB::select($sql);
  126. $id_array = [];
  127. if($res){
  128. foreach ($res as $v){
  129. $id_array[] = $v->pay_merchant_id;
  130. }
  131. }
  132. return $id_array;
  133. }
  134. }