浏览代码

Merge branch 'liuzj-permission-dev' into test

liuzejian 2 年之前
父节点
当前提交
f8c30ae93d

+ 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();
     }
 
     /**

+ 215 - 0
modules/Callback/Http/Controllers/JuliangAccountController.php

@@ -0,0 +1,215 @@
+<?php
+
+namespace Modules\Callback\Http\Controllers;
+
+use Catch\Base\CatchController;
+use Illuminate\Foundation\Validation\ValidatesRequests;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Modules\Common\Errors\Errors;
+use Modules\Common\Exceptions\CommonBusinessException;
+use Modules\User\Http\Controllers\UserTrait;
+
+class JuliangAccountController extends CatchController
+{
+    use UserTrait;
+    use ValidatesRequests;
+
+    public function list(Request $request) {
+        $advAccountId = $request->input('account_id');
+        $advAccountName = $request->input('account_name');
+
+        return DB::table('juliang_account_callback_config')
+            ->where(['company_uid' => $this->getCompanyUid()])
+            ->when($advAccountId, function ($query, $advAccountId) {
+                return $query->where('adv_account_id' , $advAccountId);
+            })->when($advAccountName, function ($query, $advAccountName) {
+                return $query->where('adv_account_name', 'like', '%'. $advAccountName. '%');
+            })
+            ->paginate($request->input('limit', 30));
+    }
+
+    public function addAccount(Request $request) {
+        $this->validate($request, [
+            'account_id' => 'required',
+            'account_name' => 'required|string|max:64',
+            'callback_state' => 'required|integer|in:0,1',
+            'protect_num' => 'required|integer|min:0',
+            'default_rate' => 'required|min:0|max:100',
+            'rate_time_config' => 'nullable|array',
+            'min_money' => 'required|min:0',
+            'max_money' => 'required|min:0'
+        ]);
+
+        if(DB::table('juliang_account_callback_config')
+            ->where(['company_uid' => $this->getCompanyUid(), 'adv_account_id' => $request->input('account_id')])
+            ->exists()) {
+            CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_EXISTS);
+        }
+        if($request->input('rate_time_config') &&
+            !$this->is_time_cross($request->input('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('account_id'),
+                'adv_account_name' => $request->input('account_name'),
+                'state' => $request->input('state'),
+                'protect_num' => $request->input('protect_num'),
+                'default_rate' => $request->input('default_rate'),
+                'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
+                'min_money' => $request->input('min_money'),
+                'max_money' => $request->input('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('default_rate'),
+                'created_at' => $now,
+                'updated_at' => $now,
+            ]);
+        if($request->input('rate_time_config')) {
+            $this->saveTimeConfig($this->getCompanyUid(), $request->input('account_id'), $request);
+        }
+        return 'ok';
+    }
+
+    public function updateCallbackConfig(Request $request) {
+        $this->validate($request, [
+            'ids' => 'required|array',
+            'state' => 'required|integer|in:0,1',
+            'protect_num' => 'required|integer|min:0',
+            'default_rate' => 'required|min:0|max:100',
+            'rate_time_config' => 'nullable|array',
+            'min_money' => 'required|min:0',
+            'max_money' => 'required|min:0'
+        ]);
+        if($request->input('callback_rate_time_config') &&
+            !$this->is_time_cross($request->input('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')
+                ->where(['id' => $id, 'company_uid' => $this->getCompanyUid()])
+                ->update([
+                    'state' => $request->input('state'),
+                    'protect_num' => $request->input('protect_num'),
+                    'default_rate' => $request->input('default_rate'),
+                    'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
+                    'min_money' => $request->input('min_money'),
+                    'max_money' => $request->input('max_money'),
+                    'updated_at' => $now,
+                ]);
+        }
+        $advAccountIds = DB::table('juliang_account_callback_config')
+            ->whereIn('id', $request->input('ids'))
+            ->where('company_uid', $this->getCompanyUid())
+            ->select('adv_account_id')->get()->pluck('adv_account_id');
+        if($advAccountIds->isNotEmpty()) {
+            DB::table('juliang_account_rate_config_log')
+                ->where('company_uid', $this->getCompanyUid())
+                ->whereIn('account_id', $advAccountIds)
+                ->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('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['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']['default_rate'] = $configInfo['default_rate'];
+        $temp['other_data']['min_money'] = $configInfo['min_money'];
+        $temp['other_data']['max_money'] = $configInfo['max_money'];
+        $temp['other_data']['protect_num'] = $configInfo['protect_num'];
+        $temp['other_data']['state'] = $configInfo['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['default_rate'];
+            $data[] = $temp;
+        }
+        //插入设置最新的时间段百分比
+        DB::table('juliang_account_promotion_config_time')->insert($data);
+    }
+}

+ 32 - 0
modules/Callback/Installer.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace Modules\ContentManage;
+
+use Catch\Support\Module\Installer as ModuleInstaller;
+use Modules\ContentManage\Providers\ContentManageServiceProvider;
+
+class Installer extends ModuleInstaller
+{
+    protected function info(): array
+    {
+        // TODO: Implement info() method.
+        return [
+            'title' => '内容中台',
+            'name' => 'contentManage',
+            'path' => 'contentManage',
+            'keywords' => '内容中台',
+            'description' => '内容中台管理模块',
+            'provider' => ContentManageServiceProvider::class
+        ];
+    }
+
+    protected function requirePackages(): void
+    {
+        // TODO: Implement requirePackages() method.
+    }
+
+    protected function removePackages(): void
+    {
+        // TODO: Implement removePackages() method.
+    }
+}

+ 29 - 0
modules/Callback/Providers/CallbackServiceProvider.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Modules\Callback\Providers;
+
+use Catch\Providers\CatchModuleServiceProvider;
+
+class CallbackServiceProvider extends CatchModuleServiceProvider
+{
+    /**
+     * middlewares
+     *
+     * @return string[]
+     */
+    protected function middlewares(): array
+    {
+       return [];
+    }
+
+    /**
+     * route path
+     *
+     * @return string|array
+     */
+    public function moduleName(): string|array
+    {
+        // TODO: Implement path() method.
+        return 'callback';
+    }
+}

+ 10 - 0
modules/Callback/README.md

@@ -0,0 +1,10 @@
+#内容中台管理模块
+关于内容中台 相关的后台接口,对外api接口,都写在这里
+配置文件放在: config目录下,读取配置文件示例:config('contentManage.zhushuyunpublicapi.public_domain');
+config("模块名.配置文件名.配置项");
+
+数据库模型文件放在:Models 目录下面
+服务层文件放在:Services 目录下面
+控制器放在:modules/ContentManage/Http/Controllers 目录下面
+中间件放在:modules/ContentManage/Middlewares 目录下面
+路由只能现在 modules/ContentManage/rout/route.php 文件里

+ 13 - 0
modules/Callback/routes/route.php

@@ -0,0 +1,13 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+use Modules\Callback\Http\Controllers\JuliangAccountController;
+
+Route::prefix('callback')->group(function () {
+    Route::prefix('juliangAccount')->group(function(){
+        Route::get('list', [JuliangAccountController::class, 'list']);
+        Route::post('addAccount', [JuliangAccountController::class, 'addAccount']);
+        Route::post('updateCallbackConfig', [JuliangAccountController::class, 'updateCallbackConfig']);
+    });
+});
+

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

@@ -12,4 +12,6 @@ class Errors
     public const  VIDEO_NOT_EXISTS= [500008, '视频不存在'];
     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, '回传比例时间区间不合法'];
 }

+ 10 - 0
modules/User/Http/Controllers/UserTrait.php

@@ -76,4 +76,14 @@ trait UserTrait
         return compact('loginUser', 'loginUserRoles', 'operateUserRoles', 'operateUser');
     }
 
+    public function getCompanyUid() {
+        $currentUserRoles = $this->listUserRoles();
+        if($currentUserRoles->contains('company')) {
+            return $this->getCurrentUser()->id;
+        }
+        if($currentUserRoles->contains('optimizer')) {
+            return $this->getCurrentUser()->pid;
+        }
+        return null;
+    }
 }

+ 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',
+            'state' => 1,
+            'protect_num' => 3,
+            'default_rate' => 50,
+            '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],
+            ],
+            'min_money' => 20,
+            '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],
+            'state' => 0,
+            'protect_num' => 3,
+            'default_rate' => 44,
+            '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],
+            ],
+            'min_money' => 40,
+            '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();

+ 1 - 1
tests/Video/Http/Controllers/VideoControllerTest.php

@@ -2,7 +2,7 @@
 
 namespace Tests\Video\Http\Controllers;
 
-use Modules\Video\Http\Controllers\VideoController;
+use Modules\Video\Http\Controllers\JuliangAccountController;
 use PHPUnit\Framework\TestCase;
 use Tests\UsedTestCase;