分离 收益回报

改进 只发送有收益的模块
This commit is contained in:
iVampireSP.com 2023-01-17 21:06:27 +08:00
parent 942184c07e
commit bfa4d9f7dd
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 55 additions and 84 deletions

View File

@ -4,7 +4,8 @@
use App\Jobs\Job;
use App\Models\Module;
use App\Notifications\Modules\ModuleEarnings;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class SendModuleEarningsJob extends Job
{
@ -27,10 +28,61 @@ public function handle(): void
{
(new Module)->chunk(100, function ($modules) {
foreach ($modules as $module) {
(new ModuleEarnings($module))
->toGroup($module->calculate());
$this->send($module);
}
});
}
private function send(Module $module): void
{
$data = $module->calculate();
if (!$data) {
return;
}
// make wecom_key visible
$wecom_key = $module->wecom_key ?? config('settings.wecom.robot_hook.billing');
$text = "# $module->name 收益";
foreach ($data as $year => $months) {
// 排序 months 从小到大
ksort($months);
$total = 0;
$total_should = 0;
foreach ($months as $month => $m) {
$total += round($m['balance'], 2);
$total_should += round($m['should_balance'], 2);
$text .= <<<EOF
==========
{$year} {$month}
实收: {$total}
应得: $total_should
EOF;
}
}
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
'msgtype' => 'markdown',
'markdown' => [
'content' => $text,
],
]);
if ($resp->failed()) {
Log::error('发送模块盈利到企业微信时失败', [
'module' => $module->id,
'data' => $data,
'resp' => $resp->json(),
]);
}
}
}

View File

@ -1,81 +0,0 @@
<?php
namespace App\Notifications\Modules;
use App\Models\Module;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class ModuleEarnings extends Notification
{
use Queueable;
protected Module $module;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Module $module)
{
$this->module = $module;
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return void
*/
public function toGroup(mixed $notifiable): void
{
$module = $this->module;
// make wecom_key visible
$wecom_key = $module->wecom_key ?? config('settings.wecom.robot_hook.billing');
$text = "# $module->name 收益";
foreach ($notifiable as $year => $months) {
// 排序 months 从小到大
ksort($months);
$total = 0;
$total_should = 0;
foreach ($months as $month => $m) {
$total += round($m['balance'], 2);
$total_should += round($m['should_balance'], 2);
$text .= <<<EOF
==========
{$year} {$month}
实收: {$total}
应得: $total_should
EOF;
}
}
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
'msgtype' => 'markdown',
'markdown' => [
'content' => $text,
],
]);
if ($resp->failed()) {
Log::error('发送模块盈利到企业微信时失败', [
'module' => $module->id,
'data' => $notifiable,
'resp' => $resp->json(),
]);
}
}
}