Преглед на файлове

网读 同步书籍到 fire

zhoulj преди 4 години
родител
ревизия
aaf65f5101

+ 107 - 0
app/Console/Commands/Report/ReportBooks.php

@@ -0,0 +1,107 @@
+<?php
+
+
+namespace App\Console\Commands\Report;
+
+
+use App\Modules\Book\Models\Book;
+use App\Modules\Book\Models\BookConfig;
+use GuzzleHttp\Client;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+use Hashids;
+
+class ReportBooks extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'report:books';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '上报书籍';
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+
+        $client = new Client(['timeout' => 10, 'verify' => false]);
+
+        // 获取书籍id
+        $offset_time = 30;//min
+        $is_full_push = true;
+        $bookConfigs = BookConfig::getBooksByOffsetTime($offset_time,$is_full_push);
+        $bids        = collect($bookConfigs)->pluck('bid')->all();
+        
+        \Log::info('report:books:bids:'.json_encode($bids));
+        \Log::info('report:books:bids_all_num:'.count($bids));
+
+        // 书籍列表
+        $books = Book::getBooksByIds($bids);
+        if ($books) {
+            $reportData = [
+                'platform' => 'wangduyun',
+                'list'     => []
+            ];
+
+            $num = 1;
+            foreach ($books as $book) {
+                $bid                  = getProp($book, 'id');
+                $book_config = BookConfig::getOneByBid($bid);
+                
+                $reportData['list'][] = [
+                    'bid'           => getProp($book, 'id'),
+                    'name'          => getProp($book_config, 'book_name'),
+                    'category_id'   => getProp($book, 'category_id'),
+                    'category_name' => getProp($book, 'category_name'),
+                    'shelf_time' => !empty(getProp($book_config, 'shelf_time'))?getProp($book_config, 'shelf_time'):'1970-01-01',
+                    'shelf_status' => getProp($book_config, 'is_on_shelf'),
+                    'hash_bid' => Hashids::encode($bid),
+                    'created_at' => date("Y-m-d H:i:s", strtotime(getProp($book_config, 'created_at'))),
+                    'updated_at' => date("Y-m-d H:i:s", strtotime(getProp($book_config, 'updated_at'))),
+                ];
+                
+//                 \Log::info('$reportData:'.$num);\Log::info($reportData);
+                // 循环推,兼容全量
+                if($num >=500){
+                    // 执行上报
+                    $client->post(env('REPORT_URI') . '/api/reportBooks', [
+                        'headers' => [
+                            'x-code' => 'Mvnx1Yr3O8i!TS5u'
+                        ],
+                        'json'    => $reportData
+                    ]);
+                    
+                    $num = 1;
+                    $reportData['list'] = [];
+                }
+                
+                $num++;
+                
+            }
+            
+            // 最后一波500内的推送
+            // 执行上报
+            $client->post(env('REPORT_URI') . '/api/reportBooks', [
+                'headers' => [
+                    'x-code' => 'Mvnx1Yr3O8i!TS5u'
+                ],
+                'json'    => $reportData
+            ]);
+            
+
+
+        }
+    }
+
+}

+ 71 - 0
app/Console/Commands/Report/ReportOrders.php

@@ -0,0 +1,71 @@
+<?php
+
+
+namespace App\Console\Commands\Report;
+
+use App\Modules\Trade\Models\Order;
+use GuzzleHttp\Client;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class ReportOrders extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'report:orders';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '上报成功订单';
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        // 获取最新同步id
+        $key     = 'report:delivery:last:order:id';
+        $orderId = (int)Redis::get($key);
+        $client  = new Client(['timeout' => 10, 'verify' => false]);
+
+        // 获取订单
+        $orders = Order::getOrdersById($orderId);
+        if ($orders) {
+            $reportData = [
+                'platform' => 'wangduyun',
+                'list'     => []
+            ];
+
+            foreach ($orders as $order) {
+                $orderId              = getProp($order, 'id');
+                $reportData['list'][] = [
+                    'uid'        => getProp($order, 'uid'),
+                    'order_no'   => getProp($order, 'trade_no'),
+                    'price'      => getProp($order, 'price'),
+                    'channel_id' => getProp($order, 'distribution_channel_id'),
+                    'pay_time'   => getProp($order, 'pay_end_at'),
+                ];
+            }
+
+            // 执行上报
+            $client->post(env('REPORT_URI') . '/api/reportOrders', [
+                'headers' => [
+                    'x-code' => 'Mvnx1Yr3O8i!TS5u'
+                ],
+                'json'    => $reportData
+            ]);
+
+            // 更新uid
+            Redis::set($key, $orderId);
+        }
+    }
+
+}

+ 70 - 0
app/Console/Commands/Report/ReportUsers.php

@@ -0,0 +1,70 @@
+<?php
+
+
+namespace App\Console\Commands\Report;
+
+
+use GuzzleHttp\Client;
+use App\Modules\User\Models\User;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class ReportUsers extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'report:users';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '上报注册用户';
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        // 获取最新同步uid
+        $key    = 'report:delivery:last:uid';
+        $uid    = (int)Redis::get($key);
+        $client = new Client(['timeout' => 10, 'verify' => false]);
+
+        // 获取批量用户信息
+        $users = User::getUsersByUid($uid);
+        if ($users) {
+            $reportData = [
+                'platform' => 'wangduyun',
+                'list'     => []
+            ];
+
+            foreach ($users as $user) {
+                $uid                  = getProp($user, 'id');
+                $reportData['list'][] = [
+                    'uid'           => $uid,
+                    'channel_id'    => getProp($user, 'distribution_channel_id'),
+                    'register_time' => $user->created_at->format('Y-m-d H:i:s')
+                ];
+            }
+
+            // 执行上报
+            $client->post(env('REPORT_URI') . '/api/reportRegisters', [
+                'headers' => [
+                    'x-code' => 'Mvnx1Yr3O8i!TS5u'
+                ],
+                'json'    => $reportData
+            ]);
+
+            // 更新uid
+            Redis::set($key, $uid);
+        }
+    }
+
+}

+ 70 - 0
app/Console/Commands/Report/SyncOfficialAccounts.php

@@ -0,0 +1,70 @@
+<?php
+
+
+namespace App\Console\Commands\Report;
+
+
+use App\Modules\OfficialAccount\Models\OfficialAccount;
+use GuzzleHttp\Client;
+use Illuminate\Console\Command;
+
+class SyncOfficialAccounts extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'sync:official:accounts';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '同步公众号到投放后台';
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        // 获取所有公众号
+        $accounts = OfficialAccount::getAllOfficialAccountDB();
+        $accounts = $accounts ? $accounts->toArray() : [];
+        if (empty($accounts)) {
+            dd('查询不到公众号');
+        }
+
+        $client      = new Client(['timeout' => 10, 'verify' => false]);
+        $accountsArr = array_chunk($accounts, 50);
+        foreach ($accountsArr as $accountList) {
+
+            $reportData = [
+                'platform' => 'wangduyun',
+                'list'     => []
+            ];
+
+            foreach ($accountList as $account) {
+                $reportData['list'][] = [
+                    'id'         => getProp($account, 'id'),
+                    'name'       => getProp($account, 'nickname'),
+                    'app_id'     => getProp($account, 'appid'),
+                    'channel_id' => getProp($account, 'distribution_channel_id'),
+                ];
+            }
+
+            // 执行上报
+            $url = 'https://firetrack.zhuishuyun.com/api/syncOfficialAccounts';
+            $client->post($url, [
+                'headers' => [
+                    'x-code' => 'Mvnx1Yr3O8i!TS5u'
+                ],
+                'json'    => $reportData
+            ]);
+        }
+    }
+
+}

+ 4 - 0
app/Console/Kernel.php

@@ -402,6 +402,10 @@ class Kernel extends ConsoleKernel
 //         $schedule->command('cp_agent_day_stat')->dailyAt('01:44');
         $schedule->command('new_user_charge_day_statistic')->dailyAt('05:44');
 
+        
+        // 同步书籍到精准投放后台
+        $schedule->command('report:books')->everyFiveMinutes();
+        
 
     }
 }

+ 30 - 0
app/Modules/Book/Models/BookConfig.php

@@ -1566,4 +1566,34 @@ class BookConfig extends Model
     {
         return self::select('bid')->where('book_name', 'like', '%' . $book_name . '%')->get();
     }
+    
+    
+    static function getBooksByBid($bid)
+    {
+        return self::where('id', '>', $bid)
+        ->where('is_on_shelf', 2)
+        ->where('test_status', 0)
+        ->orderBy('id', 'asc')->limit(500)->get();
+    }
+    
+    static function getOneByBid($bid)
+    {
+        return self::where('bid',  $bid)->first();
+    }
+    
+    static function getBooksByOffsetTime($offset_time=30,$is_full_push=false)
+    {
+        if($is_full_push){
+            return self::whereIn('is_on_shelf', [1,2])
+            ->orderBy('id', 'asc')->get();
+        }else{
+            $start_updated_time = date("Y-m-d H:i:s",time() - $offset_time*60);
+            return self::where('updated_at', '>=', $start_updated_time)
+            ->whereIn('is_on_shelf', [1,2])
+            ->orderBy('id', 'asc')->limit(500)->get();
+        }
+        
+    }
+    
+    
 }