2022-11-19 04:37:18 +00:00
|
|
|
<?php
|
|
|
|
|
2023-01-13 14:11:56 +00:00
|
|
|
namespace App\Jobs\Module;
|
2022-11-19 04:37:18 +00:00
|
|
|
|
2023-01-13 14:11:56 +00:00
|
|
|
use App\Jobs\Job;
|
2022-11-19 04:37:18 +00:00
|
|
|
use App\Models\Module;
|
2023-01-17 13:06:27 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-19 04:37:18 +00:00
|
|
|
|
2022-12-28 13:17:54 +00:00
|
|
|
class SendModuleEarningsJob extends Job
|
2022-11-19 04:37:18 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-11 12:59:17 +00:00
|
|
|
public function handle(): void
|
2022-11-19 04:37:18 +00:00
|
|
|
{
|
2023-01-10 13:42:27 +00:00
|
|
|
(new Module)->chunk(100, function ($modules) {
|
2022-11-19 04:37:18 +00:00
|
|
|
foreach ($modules as $module) {
|
2023-01-17 13:06:27 +00:00
|
|
|
$this->send($module);
|
2022-11-19 04:37:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-01-17 13:06:27 +00:00
|
|
|
|
|
|
|
private function send(Module $module): void
|
|
|
|
{
|
|
|
|
$data = $module->calculate();
|
|
|
|
|
2023-01-30 16:14:07 +00:00
|
|
|
if (! $data) {
|
2023-01-17 13:06:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 16:14:07 +00:00
|
|
|
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$wecom_key, [
|
2023-01-17 13:06:27 +00:00
|
|
|
'msgtype' => 'markdown',
|
|
|
|
'markdown' => [
|
|
|
|
'content' => $text,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($resp->failed()) {
|
|
|
|
Log::error('发送模块盈利到企业微信时失败', [
|
|
|
|
'module' => $module->id,
|
|
|
|
'data' => $data,
|
|
|
|
'resp' => $resp->json(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2022-11-19 04:37:18 +00:00
|
|
|
}
|