123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace App\Console\Commands\Star;
- use App\Models\JuxingAdAccount;
- use App\Models\JxStarLiveInfoList;
- use App\Models\JxStarLiveOrderList;
- use App\Models\JxStarLiveTaskList;
- use App\Support\Log;
- use App\Support\RedisModel;
- use Illuminate\Console\Command;
- use kwaiSDK\JuXing;
- class LiveOrderDataSync extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Star:LiveOrderDataSync';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '星直播订单列表接口';
- protected $limit = 100;
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- $this->info(date('H:i') . ' 开始执行');
- $this->start();
- $this->info(date('H:i') . ' 结束执行');
- return 0;
- }
- public function start()
- {
- $curTime = time();
- while (true) {
- if ((time() - $curTime) >= 60) break;
- $dataJson = RedisModel::rPop(JxStarLiveTaskList::JUXING_STAR_LIVE_ORDER_SYNC_TASKID_LIST);
- if (empty($dataJson)) {
- sleep(2);
- continue;
- }
- $data = json_decode($dataJson, true);
- if (!isset($data['advertiser_id'], $data['task_id'])) {
- # 输出错误日志
- Log::error('数据参数不合法', $data, 'Star:LiveOrderDataSync');
- continue;
- }
- $this->getOrderData($data);
- // 控频
- sleep(1);
- }
- }
- public function getOrderData($data)
- {
- $advertiserId = $data['advertiser_id'];
- $taskId = $data['task_id'];
- $accessToken = JuxingAdAccount::getAccessToken($advertiserId);
- $juxing = new JuXing($accessToken);
- $rst = $juxing->investData()->starLiveOrderList([
- 'advertiser_id' => $advertiserId,
- 'task_id' => $taskId
- ]);
- if (!isset($rst['code']) || ($rst['code'] != 0)) {
- # 输出错误日志并异常处理
- $msg = $rst['message'] ?? null;
- Log::error('请求接口错误:'.$msg, $data, 'Star:LiveOrderDataSync');
- return false;
- }
- $rstData = $rst['data']['star_live_order_detail_resp'] ?? [];
- if (empty($rstData)) {
- # 输出记录日志
- Log::info('请求返回订单数据为空', $data, 'Star:LiveOrderDataSync');
- return true;
- }
- foreach ($rstData as $datum) {
- $this->allotmentData($advertiserId, $taskId, $datum);
- }
- return 0;
- }
- public function allotmentData($advertiserId, $taskId, $datum)
- {
- # 提取直播数据
- $liveStreamInfo = $datum['live_stream_info'] ?? [];
- unset($datum['live_stream_info']);
- # 直播数据汇总入订单
- $datum['live_duration'] = 0;
- $datum['item_show_cnt'] = 0;
- $datum['item_click_cnt'] = 0;
- $datum['sale_product_cnt'] = 0;
- $datum['sale_amount'] = 0;
- # 插入直播入库队列
- if (!empty($liveStreamInfo)) {
- $orderId = $datum['order_id'] ?? null;
- foreach ($liveStreamInfo as $live) {
- # 直播数据汇总入订单
- $datum['live_duration'] += ($live['live_duration'] ?? 0);
- $datum['item_show_cnt'] += ($live['item_show_cnt'] ?? 0);
- $datum['item_click_cnt'] += ($live['item_click_cnt'] ?? 0);
- $datum['sale_product_cnt'] += ($live['sale_product_cnt'] ?? 0);
- $datum['sale_amount'] += ($live['sale_amount'] ?? 0);
- $liveItem = [
- 'order_id' => $orderId,
- 'data' => $live
- ];
- RedisModel::lPush(JxStarLiveInfoList::JUXING_STAR_LIVE_INFO_INDB_LIST, json_encode($liveItem));
- # 输出记录日志
- // Log::info('入库直播数据入队列成功', $liveItem, 'Star:LiveOrderDataSync');
- }
- } else {
- # 输出记录日志
- Log::info('请求返回订单中直播数据为空', $datum, 'Star:LiveOrderDataSync');
- }
- # 插入订单入库队列
- $orderItem = [
- 'advertiser_id' => $advertiserId,
- 'task_id' => $taskId,
- 'data' => $datum
- ];
- RedisModel::lPush(JxStarLiveOrderList::JUXING_STAR_LIVE_ORDER_INDB_LIST, json_encode($orderItem));
- # 输出记录日志
- // Log::info('入库订单数据入队列成功', $orderItem, 'Star:LiveOrderDataSync');
- return 0;
- }
- }
|