ReportUserAndOrderToChannel.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 'event'=>'order'
  62. ]])->getBody()->getContents();
  63. if($result && str_contains(strtolower($result),self::SUCCESS)){
  64. ReportToChannelUserAndOrderService::updateOrderErrorRecord($item->id,['report_status'=>2]);
  65. return ;
  66. }
  67. $error = 'result error ,'.$result;
  68. }catch (\Exception $e){
  69. $error = 'exception,'.$e->getMessage();
  70. }
  71. ReportToChannelUserAndOrderService::updateOrderErrorRecord($item->id,['report_status'=>1,'error_msg'=>$error]);
  72. }
  73. }
  74. private function reportUsers($date){
  75. $result = ReportToChannelUserAndOrderService::getUserErrorRecord($date);
  76. if(!$result) return ;
  77. $client = new Client(['timeout'=>5.0]);
  78. foreach ($result as $item){
  79. $distribution_channel_id = $item->distribution_channel_id;
  80. $uid = $item->uid;
  81. $register_ip = $item->register_ip;
  82. $user_agent = $item->user_agent;
  83. $user_created_at = $item->user_created_at;
  84. $link = ReportToChannelUserAndOrderService::getReportUrl($distribution_channel_id,ReportToChannelUserAndOrderService::$user_type);
  85. try{
  86. $result = $client->request('post',$link,['form_params'=>[
  87. 'uid'=>$uid,
  88. 'distribution_channel_id'=>$distribution_channel_id,
  89. 'register_ip'=>$register_ip,
  90. 'user_agent'=>$user_agent,
  91. 'user_created_at'=>$user_created_at,
  92. 'event'=>'user'
  93. ]])->getBody()->getContents();
  94. if($result && str_contains(strtolower($result),self::SUCCESS)){
  95. ReportToChannelUserAndOrderService::updateUserErrorRecord($item->id,['report_status'=>2]);
  96. return ;
  97. }
  98. $error = 'result error ,'.$result;
  99. }catch (\Exception $e){
  100. $error = 'exception,'.$e->getMessage();
  101. }
  102. ReportToChannelUserAndOrderService::updateUserErrorRecord($item->id,['report_status'=>1,'error_msg'=>$error]);
  103. }
  104. }
  105. }