Keine Beschreibung

LiveOrderDataSync.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Console\Commands\Star;
  3. use App\Models\JuxingAdAccount;
  4. use App\Models\JxStarLiveInfoList;
  5. use App\Models\JxStarLiveOrderList;
  6. use App\Models\JxStarLiveTaskList;
  7. use App\Support\Log;
  8. use App\Support\RedisModel;
  9. use Illuminate\Console\Command;
  10. use kwaiSDK\JuXing;
  11. class LiveOrderDataSync extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'Star:LiveOrderDataSync';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '星直播订单列表接口';
  25. protected $limit = 100;
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $this->info(date('H:i') . ' 开始执行');
  43. $this->start();
  44. $this->info(date('H:i') . ' 结束执行');
  45. return 0;
  46. }
  47. public function start()
  48. {
  49. $curTime = time();
  50. while (true) {
  51. if ((time() - $curTime) >= 60) break;
  52. $dataJson = RedisModel::rPop(JxStarLiveTaskList::JUXING_STAR_LIVE_ORDER_SYNC_TASKID_LIST);
  53. if (empty($dataJson)) {
  54. sleep(2);
  55. continue;
  56. }
  57. $data = json_decode($dataJson, true);
  58. if (!isset($data['advertiser_id'], $data['task_id'])) {
  59. # 输出错误日志
  60. Log::error('数据参数不合法', $data, 'Star:LiveOrderDataSync');
  61. continue;
  62. }
  63. $this->getOrderData($data);
  64. // 控频
  65. sleep(1);
  66. }
  67. }
  68. public function getOrderData($data)
  69. {
  70. $advertiserId = $data['advertiser_id'];
  71. $taskId = $data['task_id'];
  72. $accessToken = JuxingAdAccount::getAccessToken($advertiserId);
  73. $juxing = new JuXing($accessToken);
  74. $rst = $juxing->investData()->starLiveOrderList([
  75. 'advertiser_id' => $advertiserId,
  76. 'task_id' => $taskId
  77. ]);
  78. if (!isset($rst['code']) || ($rst['code'] != 0)) {
  79. # 输出错误日志并异常处理
  80. $msg = $rst['message'] ?? null;
  81. Log::error('请求接口错误:'.$msg, $data, 'Star:LiveOrderDataSync');
  82. return false;
  83. }
  84. $rstData = $rst['data']['star_live_order_detail_resp'] ?? [];
  85. if (empty($rstData)) {
  86. # 输出记录日志
  87. Log::info('请求返回订单数据为空', $data, 'Star:LiveOrderDataSync');
  88. return true;
  89. }
  90. foreach ($rstData as $datum) {
  91. $this->allotmentData($advertiserId, $taskId, $datum);
  92. }
  93. return 0;
  94. }
  95. public function allotmentData($advertiserId, $taskId, $datum)
  96. {
  97. # 提取直播数据
  98. $liveStreamInfo = $datum['live_stream_info'] ?? [];
  99. unset($datum['live_stream_info']);
  100. # 直播数据汇总入订单
  101. $datum['live_duration'] = 0;
  102. $datum['item_show_cnt'] = 0;
  103. $datum['item_click_cnt'] = 0;
  104. $datum['sale_product_cnt'] = 0;
  105. $datum['sale_amount'] = 0;
  106. # 插入直播入库队列
  107. if (!empty($liveStreamInfo)) {
  108. $orderId = $datum['order_id'] ?? null;
  109. foreach ($liveStreamInfo as $live) {
  110. # 直播数据汇总入订单
  111. $datum['live_duration'] += ($live['live_duration'] ?? 0);
  112. $datum['item_show_cnt'] += ($live['item_show_cnt'] ?? 0);
  113. $datum['item_click_cnt'] += ($live['item_click_cnt'] ?? 0);
  114. $datum['sale_product_cnt'] += ($live['sale_product_cnt'] ?? 0);
  115. $datum['sale_amount'] += ($live['sale_amount'] ?? 0);
  116. $liveItem = [
  117. 'order_id' => $orderId,
  118. 'data' => $live
  119. ];
  120. RedisModel::lPush(JxStarLiveInfoList::JUXING_STAR_LIVE_INFO_INDB_LIST, json_encode($liveItem));
  121. # 输出记录日志
  122. // Log::info('入库直播数据入队列成功', $liveItem, 'Star:LiveOrderDataSync');
  123. }
  124. } else {
  125. # 输出记录日志
  126. Log::info('请求返回订单中直播数据为空', $datum, 'Star:LiveOrderDataSync');
  127. }
  128. # 插入订单入库队列
  129. $orderItem = [
  130. 'advertiser_id' => $advertiserId,
  131. 'task_id' => $taskId,
  132. 'data' => $datum
  133. ];
  134. RedisModel::lPush(JxStarLiveOrderList::JUXING_STAR_LIVE_ORDER_INDB_LIST, json_encode($orderItem));
  135. # 输出记录日志
  136. // Log::info('入库订单数据入队列成功', $orderItem, 'Star:LiveOrderDataSync');
  137. return 0;
  138. }
  139. }