Bläddra i källkod

巨量广告账户级配置

liuzejian 2 år sedan
förälder
incheckning
650b8c0a2e

+ 83 - 0
app/Console/Commands/Callback/JuliangAccountRateConfigRefresh.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace App\Console\Commands\Callback;
+
+use App\Modules\Advertiser\Models\AdvertiserPromotionConfigTime;
+use App\Modules\Advertiser\Models\TiktokAdvertiser;
+use App\Modules\Advertiser\Services\AdvertiserService;
+use App\Modules\Advertiser\Services\TimeConfigService;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class JuliangAccountRateConfigRefresh extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Callback:JuliangAccountRateConfigRefresh';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '巨量账户级回传比例自动刷新';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle(): void
+    {
+        $this->start();
+    }
+
+    public function start(){
+        $list = DB::table('juliang_account_promotion_config_time')
+            ->where('is_enable',1)->orderBy('id')->get();
+        foreach ($list as  $item) {
+            $is_exec = $this->timeThan($item->config_time);
+            if (!$is_exec || $item->next_exec_time != date("Y-m-d")) {
+                continue;
+            }
+            $this->setReportType($item);
+        }
+    }
+
+    /**
+     * 判断任务时间点和 当前的时间点比对
+     * @param  [type] $str [description]
+     * @return [type]      [description]
+     */
+    private function timeThan($str)
+    {
+        $config_time = strtotime(date('Y-m-d').' '.$str);
+        if (time() >= $config_time) {
+            return true;
+        }
+        return false;
+    }
+
+
+    public  function setReportType($item){
+        $now = date('Y-m-d H:i:s');
+        $res = DB::table('juliang_account_promotion_config_time')->where('id',$item->id)
+            ->update(['latest_exec_time'=> date('Y-m-d H:i:s',time()),
+                'next_exec_time'=>date("Y-m-d",strtotime('+1 day')),
+                'updated_at'  => $now]);
+        if ($res) {
+            DB::table('juliang_account_rate_config_log')
+                ->where(['company_uid' => $item->company_uid, 'account_id' => $item->account_id, 'is_enabled' => 1])
+                ->update(['is_enabled' => 0, 'updated_at' => $now]);
+            DB::table('juliang_account_rate_config_log')
+                ->insert([
+                    'company_uid' => $item->company_uid,
+                    'account_id' => $item->account_id,
+                    'config_per' => $item->config_per,
+                    'created_at' => $now,
+                    'updated_at' => $now,
+                ]);
+        }
+    }
+}

+ 1 - 25
app/Console/Kernel.php

@@ -15,31 +15,7 @@ class Kernel extends ConsoleKernel
      */
     protected function schedule(Schedule $schedule)
     {
-        // $schedule->command('inspire')->hourly();
-        /**
-         * 从掌维同步cp订阅统计数据
-         */
-        $schedule->command('ContentManage:SyncCpSubscribeStatisticDataFromZW')->dailyAt('13:00');
-        /**
-         * cp结算月统计
-         */
-        $schedule->command('ContentManage:CpSubscribeMonthStatisticData', [
-            '--month' => date_sub(date_create(), date_interval_create_from_date_string('1 month'))->format('Y-m')
-        ])->monthlyOn(1, '15:00');
-        /**
-         * 版权到期通知,每周一上午9点执行.
-         */
-        $schedule->command('ContentManage:BookCopyrightAlert')->weekly()->mondays()->at('09:00');
-        /**
-         * 新的cp方书籍进入内容中台, 通知
-         */
-        $schedule->command('ContentManage:NewCpBookComeIn')->dailyAt("08:30");
-        /**
-         * 保存书籍结算模式
-         */
-        $schedule->command('ContentManage:SaveBookSettlementMode')->daily();
-
-        $schedule->command('ContentManage:cpcollection')->everyMinute();
+        $schedule->command('Callback:JuliangAccountRateConfigRefresh')->everyMinute();
     }
 
     /**

+ 119 - 7
modules/Callback/Http/Controllers/JuliangAccountController.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace Modules\Video\Http\Controllers;
+namespace Modules\Callback\Http\Controllers;
 
 use Catch\Base\CatchController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
@@ -31,7 +31,7 @@ class JuliangAccountController extends CatchController
 
     public function addAccount(Request $request) {
         $this->validate($request, [
-            'account_id' => 'required|string|max:64',
+            'account_id' => 'required',
             'account_name' => 'required|string|max:64',
             'callback_state' => 'required|integer|in:0,1',
             'protect_num' => 'required|integer|min:0',
@@ -46,21 +46,36 @@ class JuliangAccountController extends CatchController
             ->exists()) {
             CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_EXISTS);
         }
+        if($request->input('callback_rate_time_config') &&
+            !$this->is_time_cross($request->input('callback_rate_time_config'))) {
+            CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
+        }
         $now = date('Y-m-d H:i:s');
         DB::table('juliang_account_callback_config')
             ->insert([
-                'adv_account_id' => $request->input('acccount_id'),
+                'adv_account_id' => $request->input('account_id'),
                 'adv_account_name' => $request->input('account_name'),
                 'callback_state' => $request->input('callback_state'),
                 'protect_num' => $request->input('protect_num'),
                 'callback_default_rate' => $request->input('callback_default_rate'),
-                'callback_rate_time_config' => \json_encode($request->input('callback_rate_time_config', '[]')),
+                'callback_rate_time_config' => \json_encode($request->input('callback_rate_time_config', [])),
                 'callback_min_money' => $request->input('callback_min_money'),
                 'callback_max_money' => $request->input('callback_max_money'),
                 'company_uid' => $this->getCompanyUid(),
                 'created_at' => $now,
                 'updated_at' => $now,
             ]);
+        DB::table('juliang_account_rate_config_log')
+            ->insert([
+                'company_uid' => $this->getCompanyUid(),
+                'account_id' => $request->input('account_id'),
+                'config_per' => $request->input('callback_default_rate'),
+                'created_at' => $now,
+                'updated_at' => $now,
+            ]);
+        if($request->input('callback_rate_time_config')) {
+            $this->saveTimeConfig($this->getCompanyUid(), $request->input('account_id'), $request);
+        }
         return 'ok';
     }
 
@@ -74,6 +89,10 @@ class JuliangAccountController extends CatchController
             'callback_min_money' => 'required|min:0',
             'callback_max_money' => 'required|min:0'
         ]);
+        if($request->input('callback_rate_time_config') &&
+            !$this->is_time_cross($request->input('callback_rate_time_config'))) {
+            CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
+        }
         $now = date('Y-m-d H:i:s');
         foreach ($request->input('ids') as $id) {
             DB::table('juliang_account_callback_config')
@@ -82,7 +101,7 @@ class JuliangAccountController extends CatchController
                     'callback_state' => $request->input('callback_state'),
                     'protect_num' => $request->input('protect_num'),
                     'callback_default_rate' => $request->input('callback_default_rate'),
-                    'callback_rate_time_config' => \json_encode($request->input('callback_rate_time_config', '[]')),
+                    'callback_rate_time_config' => \json_encode($request->input('callback_rate_time_config', [])),
                     'callback_min_money' => $request->input('callback_min_money'),
                     'callback_max_money' => $request->input('callback_max_money'),
                     'updated_at' => $now,
@@ -93,11 +112,104 @@ class JuliangAccountController extends CatchController
             ->where('company_uid', $this->getCompanyUid())
             ->select('adv_account_id')->get()->pluck('adv_account_id');
         if($advAccountIds->isNotEmpty()) {
-            DB::table('juliang_account_recharge_report_records')
+            DB::table('juliang_account_rate_config_log')
+                ->where('company_uid', $this->getCompanyUid())
                 ->whereIn('account_id', $advAccountIds)
-                ->update(['is_eanbled' => 0, 'updated_at' => $now]);
+                ->where('is_enabled', 1)
+                ->update(['is_enabled' => 0, 'updated_at' => $now]);
+            foreach ($advAccountIds as $accountId) {
+                DB::table('juliang_account_rate_config_log')
+                    ->insert([
+                        'company_uid' => $this->getCompanyUid(),
+                        'account_id' => $accountId,
+                        'config_per' => $request->input('callback_default_rate'),
+                        'created_at' => $now,
+                        'updated_at' => $now,
+                    ]);
+                $this->saveTimeConfig($this->getCompanyUid(), $accountId, $request);
+            }
         }
 
         return 'ok';
     }
+
+    /**
+     * 判断时 设置的时间范围是否有交集
+     *
+     * @param string $config_time 开始时间1
+     * @return bool
+     */
+    protected  function is_time_cross($config_time) {
+        $timeline = [];
+        foreach ($config_time as $key => $value) {
+            $start_time = $value['start_time'];
+            $start = explode(':', $start_time);
+            $cur_start = (int)$start[0] * 60 + $start[1];
+            $end_time = $value['end_time'];
+
+            $end = explode(':', $end_time);
+            $cur_end = (int)$end[0] * 60 + $end[1];
+            if ($cur_end <= $cur_start) {
+                return false;
+            }
+
+            if ($timeline) {
+                foreach ($timeline as $k => $v) {
+                    if ($cur_start >= $v['start'] && $cur_start <= $v['end']) {
+                        return false;
+                    }
+                    if ($cur_end >= $v['start'] && $cur_end <= $v['end']) {
+                        return false;
+                    }
+                }
+            }
+            $timeline[] = ['start'=>$cur_start,'end'=>$cur_end];
+        }
+        return true;
+    }
+
+
+    public  function saveTimeConfig($companyUid, $accountId, $configInfo )
+    {
+        DB::table('juliang_account_promotion_config_time')
+            ->where('is_enable',1)
+            ->where('company_uid',$companyUid)
+            ->where('account_id',$accountId)->update(['is_enable'=>0]);
+        $time_config = $configInfo['callback_rate_time_config'];
+        if (empty($time_config)) {
+            return false;
+        }
+        $is = $this->is_time_cross($time_config);
+        if (!$is) {
+            return false;
+        }
+        $data = [];
+        $temp['company_uid'] = $companyUid;
+
+        $temp['account_id'] = $accountId;
+        $temp['other_data']['callback_default_rate'] = $configInfo['callback_default_rate'];
+        $temp['other_data']['callback_min_money'] = $configInfo['callback_min_money'];
+        $temp['other_data']['callback_max_money'] = $configInfo['callback_max_money'];
+        $temp['other_data']['protect_num'] = $configInfo['protect_num'];
+        $temp['other_data']['callback_state'] = $configInfo['callback_state'];
+        $temp['other_data'] = json_encode($temp['other_data']);
+        $temp['next_exec_time'] = date('Y-m-d');
+
+        $temp['is_enable'] = 1;
+        $temp['created_at'] = date('Y-m-d H:i:s',time());
+
+        foreach ($time_config as $value) {
+            $start_time = $value['start_time'];
+            $end_time = $value['end_time'];
+            $temp['config_time'] = $start_time;
+            $temp['config_per'] = $value['config_per'];
+            $data[] = $temp;
+            //结束后 百分比改为默认的
+            $temp['config_time'] = $end_time;
+            $temp['config_per'] = $configInfo['callback_default_rate'];
+            $data[] = $temp;
+        }
+        //插入设置最新的时间段百分比
+        DB::table('juliang_account_promotion_config_time')->insert($data);
+    }
 }

+ 1 - 3
modules/Callback/Providers/CallbackServiceProvider.php

@@ -1,10 +1,8 @@
 <?php
 
-namespace Modules\Video\Providers;
+namespace Modules\Callback\Providers;
 
-use Catch\CatchAdmin;
 use Catch\Providers\CatchModuleServiceProvider;
-use Modules\ContentManage\Middlewares\ContentManageGate;
 
 class CallbackServiceProvider extends CatchModuleServiceProvider
 {

+ 1 - 5
modules/Callback/routes/route.php

@@ -1,11 +1,7 @@
 <?php
 
 use Illuminate\Support\Facades\Route;
-use Modules\System\Http\Controllers\NoticesController;
-use Modules\System\Http\Controllers\NoticeTypesController;
-use Modules\Video\Http\Controllers\EpisodeController;
-use Modules\Video\Http\Controllers\VideoCategoryController;
-use Modules\Video\Http\Controllers\JuliangAccountController;
+use Modules\Callback\Http\Controllers\JuliangAccountController;
 
 Route::prefix('callback')->group(function () {
     Route::prefix('juliangAccount')->group(function(){

+ 1 - 0
modules/Common/Errors/Errors.php

@@ -13,4 +13,5 @@ class Errors
     public const  MINIPROGRAM_STATUS_ERROR= [500005, '小程序未启用'];
     public const  UPLOAD_IMAGE_ERROR= [500006, '上传图片不成功'];
     public const  JULIANG_ACCOUNT_EXISTS= [500101, '巨量广告账户已经存在'];
+    public const  CALLBACK_RATE_TIME_RANGE_ERROR= [500102, '回传比例时间区间不合法'];
 }

+ 5 - 5
modules/Video/routes/route.php

@@ -5,18 +5,18 @@ use Modules\System\Http\Controllers\NoticesController;
 use Modules\System\Http\Controllers\NoticeTypesController;
 use Modules\Video\Http\Controllers\EpisodeController;
 use Modules\Video\Http\Controllers\VideoCategoryController;
-use Modules\Video\Http\Controllers\JuliangAccountController;
+use Modules\Video\Http\Controllers\VideoController;
 
 Route::prefix('videoStock')->group(function () {
-    Route::get('video/list', [JuliangAccountController::class, 'list']);
+    Route::get('video/list', [VideoController::class, 'list']);
     Route::get('episode/list', [EpisodeController::class, 'list']);
     Route::get('episode/downloadList', [EpisodeController::class, 'downloadList']);
     Route::get('videoCategory/list', [VideoCategoryController::class, 'list']);
 
 
-    Route::post('video/update', [JuliangAccountController::class, 'update']);
-    Route::post('video/add', [JuliangAccountController::class, 'add']);
-    Route::post('video/setChargeConfig', [JuliangAccountController::class, 'setChargeConfig']);
+    Route::post('video/update', [VideoController::class, 'update']);
+    Route::post('video/add', [VideoController::class, 'add']);
+    Route::post('video/setChargeConfig', [VideoController::class, 'setChargeConfig']);
     Route::post('episode/add', [EpisodeController::class, 'add']);
 });
 

+ 50 - 0
tests/Callback/Http/Controllers/JuliangAccountControllerTest.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Tests\Callback\Http\Controllers;
+
+use Modules\Callback\Http\Controllers\JuliangAccountController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class JuliangAccountControllerTest extends UsedTestCase
+{
+
+    public function testAddAccount()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/callback/juliangAccount/addAccount', [
+            'account_id' => 1234568,
+            'account_name' => 'jkljlkj',
+            'callback_state' => 1,
+            'protect_num' => 3,
+            'callback_default_rate' => 50,
+            'callback_rate_time_config' => [
+                ['start_time' => '00:35', 'end_time' => '12:34', 'config_per' => 20],
+                ['start_time' => '13:39', 'end_time' => '19:34', 'config_per' => 30],
+            ],
+            'callback_min_money' => 20,
+            'callback_max_money' => 50,
+        ]);
+
+        $res->dump();
+    }
+
+    public function testupdateCallbackConfig() {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/callback/juliangAccount/updateCallbackConfig', [
+            'ids' => [4],
+            'callback_state' => 0,
+            'protect_num' => 3,
+            'callback_default_rate' => 44,
+            'callback_rate_time_config' => [
+                ['start_time' => '15:00', 'end_time' => '15:10', 'config_per' => 40],
+                ['start_time' => '15:20', 'end_time' => '15:30', 'config_per' => 56],
+            ],
+            'callback_min_money' => 40,
+            'callback_max_money' => 60,
+        ]);
+        $res->dump();
+    }
+}

+ 2 - 2
tests/UsedTestCase.php

@@ -13,9 +13,9 @@ abstract class UsedTestCase extends BaseTestCase
     {
         parent::setUp(); // TODO: Change the autogenerated stub
         $tokenInfo = $this->post('http://localhost/api/login', [
-            'email' => 'catch@admin.com',
+//            'email' => 'catch@admin.com',
             'remember' => false,
-//            'email' => 'xiaoli@qq.com',
+            'email' => 'xiaoli@qq.com',
             'password' => 'catchadmin',
 //        'email' => 'aa@test.com',
         ])->json();