ReportUserAndOrderToChannel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/12/24
  6. * Time: 16:47
  7. */
  8. namespace App\Console\Commands\Channel;
  9. use App\Modules\Channel\Services\ReportToChannelUserAndOrderService;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Console\Command;
  12. use DB;
  13. class ReportUserAndOrderToChannel extends Command
  14. {
  15. /**
  16. * 执行命令 php artisan generate_order_day_stat
  17. *
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'channel:ReportUserAndOrderToChannel {--type=}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '渠道订单推送异常重试';
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. const SUCCESS = 'success';
  35. public function handle(){
  36. $date = date('Y-m-d H:i:s',time()-3600);
  37. $type = $this->option('type');
  38. if($type == 'users'){
  39. $this->reportUsers($date);
  40. }
  41. if($type == 'orders'){
  42. $this->reportOrders($date);
  43. }
  44. }
  45. private function reportOrders($date){
  46. $result = ReportToChannelUserAndOrderService::getOrderErrorRecord($date);
  47. if(!$result) return ;
  48. $client = new Client(['timeout'=>5.0]);
  49. foreach ($result as $item){
  50. $distribution_channel_id = $item->distribution_channel_id;
  51. $uid = $item->uid;
  52. $price = $item->price;
  53. $order_created_at = $item->order_created_at;
  54. $link = ReportToChannelUserAndOrderService::getReportUrl($distribution_channel_id,ReportToChannelUserAndOrderService::$user_type);
  55. try{
  56. $result = $client->request('post',$link,['form_params'=>[
  57. 'uid'=>$uid,
  58. 'distribution_channel_id'=>$distribution_channel_id,
  59. 'price'=>$price,
  60. 'order_created_at'=>$order_created_at,
  61. 'source'=>'wangduyun',
  62. 'event'=>'order'
  63. ]])->getBody()->getContents();
  64. if($result && str_contains(strtolower($result),self::SUCCESS)){
  65. ReportToChannelUserAndOrderService::updateOrderErrorRecord($item->id,['report_status'=>2]);
  66. return ;
  67. }
  68. $error = 'result error ,'.$result;
  69. }catch (\Exception $e){
  70. $error = 'exception,'.$e->getMessage();
  71. }
  72. ReportToChannelUserAndOrderService::updateOrderErrorRecord($item->id,['report_status'=>1,'error_msg'=>$error]);
  73. }
  74. }
  75. private function reportUsers($date){
  76. $result = ReportToChannelUserAndOrderService::getUserErrorRecord($date);
  77. if(!$result) return ;
  78. $client = new Client(['timeout'=>5.0]);
  79. foreach ($result as $item){
  80. $distribution_channel_id = $item->distribution_channel_id;
  81. $uid = $item->uid;
  82. $register_ip = $item->register_ip;
  83. $user_agent = $item->user_agent;
  84. $user_created_at = $item->user_created_at;
  85. $link = ReportToChannelUserAndOrderService::getReportUrl($distribution_channel_id,ReportToChannelUserAndOrderService::$user_type);
  86. try{
  87. $result = $client->request('post',$link,['form_params'=>[
  88. 'uid'=>$uid,
  89. 'distribution_channel_id'=>$distribution_channel_id,
  90. 'register_ip'=>$register_ip,
  91. 'user_agent'=>$user_agent,
  92. 'user_created_at'=>$user_created_at,
  93. 'event'=>'user',
  94. 'source'=>'wangduyun'
  95. ]])->getBody()->getContents();
  96. if($result && str_contains(strtolower($result),self::SUCCESS)){
  97. ReportToChannelUserAndOrderService::updateUserErrorRecord($item->id,['report_status'=>2]);
  98. return ;
  99. }
  100. $error = 'result error ,'.$result;
  101. }catch (\Exception $e){
  102. $error = 'exception,'.$e->getMessage();
  103. }
  104. ReportToChannelUserAndOrderService::updateUserErrorRecord($item->id,['report_status'=>1,'error_msg'=>$error]);
  105. }
  106. }
  107. }