FailOrderAlert.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\SMS;
  7. use Log;
  8. use DB;
  9. use App\Jobs\SendTexts;
  10. class FailOrderAlert extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'foa {--qrcode}';
  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. $options = $this->option('qrcode');
  41. if($options){
  42. return $this->forceSubscribeErrorAlert();
  43. } else{
  44. $this->tooLongNoOrderAlert();
  45. return $this->start();
  46. }
  47. }
  48. private function start(){
  49. if(!$this->isCanAlert()) return 1;
  50. $sorce = $this->getPaySource();
  51. if(!$sorce){
  52. return false;
  53. }
  54. foreach ($sorce as $v){
  55. $is_all_fail= $this->isAllFail($v->source);
  56. $too_long = $this->lastOrderTooLong();
  57. $content = '';
  58. if($is_all_fail){
  59. $content = '订单预警,'.$v->source.'通道最近20个订单都是未支付订单,尽快排查';
  60. }
  61. if($too_long){
  62. $content = $v->source.'通道最近半小时没有产生一条订单,尽快排查';
  63. }
  64. if(!$content) return 2;
  65. $phone_arr = ['15868100210','15088790066','13858057394','18668029091','18668420256'];
  66. //$phone_arr = ['18668029091'];
  67. foreach ($phone_arr as $phone){
  68. $this->sendAndRecord(1,$phone,$content);
  69. }
  70. $key = 'distribution_channel_id:orderalert';
  71. $field = 'sendalert';
  72. Redis::hset($key,$field,time());
  73. }
  74. return 3;
  75. }
  76. private function forceSubscribeErrorAlert(){
  77. $errors = Redis::SMEMBERS('force_subscribe_qrcode:error');
  78. if(!$errors){
  79. return false;
  80. }
  81. $res = [];
  82. foreach ($errors as $value){
  83. $temp = explode('_',$value);
  84. $res[$temp[0]][] = $temp[1];
  85. }
  86. $phone_arr = ['15868100210','15088790066','13858057394','18668029091','18668420256'];
  87. //$phone_arr = ['18668029091'];
  88. if($res){
  89. foreach ($res as $k=>$v){
  90. if(count($v) >= 5){
  91. $content = 'appid='.$k.'的公众号获取强关二维码出问题了!!,一分钟之内出现'.count($v).'次';
  92. foreach ($phone_arr as $phone){
  93. SMS::send($phone,$content);
  94. }
  95. //break;
  96. }
  97. }
  98. }
  99. Redis::srem('force_subscribe_qrcode:error',$errors);
  100. return false;
  101. }
  102. private function tooLongNoOrderAlert(){
  103. $last = DB::table('orders')->orderBy('id','desc')->first();
  104. if(time()-strtotime($last->created_at) >= 150){
  105. $phone_arr = ['15868100210','15088790066','13858057394','18668029091','18668420256'];
  106. //$phone_arr = ['18668029091'];
  107. $content = '订单预警,最近两分半没有产生一条订单,尽快排查';
  108. foreach ($phone_arr as $phone){
  109. echo SMS::send($phone,$content);
  110. }
  111. }
  112. }
  113. private function getAllChannel(){
  114. return OrderService::getAllChannelId();
  115. }
  116. private function getPaySource(){
  117. $sql = 'select distinct pay_merchant_id from distribution_channels where pay_merchant_id <>0';
  118. $res = DB::select($sql);
  119. $id_array = [];
  120. if($res){
  121. foreach ($res as $v){
  122. $id_array[] = $v->pay_merchant_id;
  123. }
  124. }
  125. $ssql = 'select distinct source from pay_merchants where id in('.implode(',',$id_array).')';
  126. $source = DB::select($ssql);;
  127. return $source;
  128. }
  129. /**
  130. * 最近20订单是否全部失败
  131. * @param $channel
  132. * @param int $num
  133. * @return bool
  134. */
  135. private function isAllFail($source,$num=20){
  136. $order = OrderService::getLatestOrderByChannleId($source,$num);
  137. if(!$order) return false;
  138. if($order && count($order) < $num){
  139. return false;
  140. }
  141. foreach ($order as $val){
  142. if($val->status == 'PAID'){
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. /**
  149. * 最后一条订单距离现在是否超过半小时
  150. * @return bool
  151. */
  152. private function lastOrderTooLong(){
  153. $order = OrderService::getLastOrder();
  154. if(time()-strtotime($order->created_at) > 1800){
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * 是否可以发送预警
  161. * @param $channel
  162. * @return bool
  163. */
  164. private function isCanAlert(){
  165. $key = 'distribution_channel_id:orderalert';
  166. $field = 'sendalert';
  167. $res = Redis::hget($key,$field);
  168. if($res && (time()-$res) < 1800 ){
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * 发送预警,并记录
  175. * @param $channel_info
  176. * @param $content
  177. * @return bool
  178. */
  179. private function sendAndRecord($channel,$phone,$content){
  180. if(empty($phone)){
  181. return false;
  182. }
  183. $status = SMS::send($phone,$content);
  184. $record = json_encode([
  185. 'phone'=>$phone,
  186. 'status'=>$status,
  187. 'time'=>time()
  188. ]);
  189. $key = 'distribution_channel_id:orderalert:record';
  190. Redis::rpush($key,$record);
  191. }
  192. }