From e3f8ef6a34ee84afb1e5613c31f80fdf10beb010 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 31 Oct 2022 19:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=A8=A1=E5=9D=97=E6=94=B6?= =?UTF-8?q?=E7=9B=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Kernel.php | 6 +- app/Jobs/SendThisMonthModuleEarnings.php | 56 ++++++++++++++++ app/Notifications/ModuleEarnings.php | 66 +++++++++++++++++++ .../notifications/module/earnings.blade.php | 38 +++++++++++ 4 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 app/Jobs/SendThisMonthModuleEarnings.php create mode 100644 app/Notifications/ModuleEarnings.php create mode 100644 resources/views/notifications/module/earnings.blade.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 3d829b8..917500c 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -19,6 +19,7 @@ use App\Jobs\ClearTasks; use App\Jobs\DeleteHost; use App\Jobs\Remote; +use App\Jobs\SendThisMonthModuleEarnings; use Illuminate\Console\Scheduling\Schedule; use Laravel\Lumen\Console\Kernel as ConsoleKernel; @@ -56,8 +57,6 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // - // dispatch HostCost job $schedule->job(new HostCost())->everyFiveMinutes(); // $schedule->job(new UserSave())->everyTenMinutes(); @@ -74,5 +73,8 @@ protected function schedule(Schedule $schedule) $schedule->job(new AutoCloseWorkOrder())->everyFiveMinutes(); $schedule->job(new JobsCalcModule())->everyFiveMinutes(); + + // 每天晚上 20 点,发送模块收益 + $schedule->job(new SendThisMonthModuleEarnings())->dailyAt('20:00'); } } diff --git a/app/Jobs/SendThisMonthModuleEarnings.php b/app/Jobs/SendThisMonthModuleEarnings.php new file mode 100644 index 0000000..468c67e --- /dev/null +++ b/app/Jobs/SendThisMonthModuleEarnings.php @@ -0,0 +1,56 @@ + 0, + 'drops' => 0, + ]; + + $rate = config('drops.module_rate'); + + Module::chunk( + 100, + function ($modules) use ($default, $rate) { + foreach ($modules as $module) { + $data = [ + 'transactions' => [ + 'this_month' => Cache::get('this_month_balance_and_drops_' . $module->id, $default), + 'last_month' => Cache::get('last_month_balance_and_drops_' . $module->id, $default), + ], + 'rate' => $rate, + ]; + + return (new ModuleEarnings($module)) + ->toGroup($data); + } + } + ); + } +} diff --git a/app/Notifications/ModuleEarnings.php b/app/Notifications/ModuleEarnings.php new file mode 100644 index 0000000..539a3b6 --- /dev/null +++ b/app/Notifications/ModuleEarnings.php @@ -0,0 +1,66 @@ +module = $module; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toGroup($notifiable) + { + if (!isset($notifiable['transactions'])) { + return; + } + + $module = $this->module; + + $view = 'notifications.module.earnings'; + + // make wecom_key visible + $wecom_key = $module->wecom_key ?? config('settings.wecom.robot_hook.billing'); + + $resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [ + 'msgtype' => 'markdown', + 'markdown' => [ + 'content' => view($view, [ + 'module' => $module, + 'data' => $notifiable, + ])->render(), + ], + ]); + + if ($resp->failed()) { + Log::error('发送模块盈利到企业微信时失败', [ + 'module' => $module->id, + 'data' => $notifiable, + 'resp' => $resp->json(), + ]); + } + } +} diff --git a/resources/views/notifications/module/earnings.blade.php b/resources/views/notifications/module/earnings.blade.php new file mode 100644 index 0000000..f17d0af --- /dev/null +++ b/resources/views/notifications/module/earnings.blade.php @@ -0,0 +1,38 @@ +## 收益汇报 +# {{ $module->name }} +================================== +## 本月 +#### 现金 {{ round($data['transactions']['this_month']['balance'], 2) }} 元 +#### Drops {{ round($data['transactions']['this_month']['drops'], 4) }} +================================== +## 上个月 +#### 现金 {{ round($data['transactions']['last_month']['balance'], 2) }} 元 +#### Drops {{ round($data['transactions']['last_month']['drops'], 4) }} + + +{{-- +$module = $this->http->get('modules')->json()['data']; + +$total = $module['transactions']['this_month']['balance']; + +$drops = $module['transactions']['this_month']['drops'] / $module['rate']; + +if ($drops < 0) { $drops=0; } $total +=$drops; $total=round($total, 2); $module=[ 'balance'=> + $module['transactions']['this_month']['balance'], + 'drops' => $module['transactions']['this_month']['drops'], + 'total' => $total, + ]; + +

收益

+
+

+ 本月收益 +

+

+ 直接扣费金额: {{ $module['balance'] }} 元 +

+

+ Drops: {{ $module['drops'] }} +

+

本月总计收入 CNY: {{ $module['total'] }}

+
--}}