6
0
Fork 0
jiqu-library-server/app/Console/Commands/QuotaV1SendCommand.php

73 lines
2.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\QuotaV1SendJob;
use App\Models\QuotaV1SendLog;
use App\Models\UserInfo;
use App\Models\WalletLog;
use App\Services\WalletService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class QuotaV1SendCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'quota-v1:send';
/**
* The console command description.
*
* @var string
*/
protected $description = '老配额分红';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
foreach (QuotaV1SendJob::where('status', 1)->cursor() as $job) {
$totalQuotaV1 = UserInfo::where('quota_v1', '>', 0)->sum('quota_v1');
if ($totalQuotaV1 > 0) {//总配额大于0才开始分
UserInfo::with('user')->where('quota_v1', '>', 0)->chunkById(100, function ($userInfos) use ($totalQuotaV1, $job) {
$walletService = new WalletService();
//依次分红
foreach ($userInfos as $userInfo) {
// if ($userInfo->bonusable) {//只针对享受分红的人发放
$quotaV1amount = round(bcmul(bcdiv($job->amount, $totalQuotaV1, 5), $userInfo->quota_v1, 3));
if ($quotaV1amount >0) {
$log = new QuotaV1SendLog();
$log->user_id = $userInfo->user_id;
$log->job_id = $job->id;
$log->amount = $quotaV1amount;
$log->save();
// try {
// DB::beginTransaction();
// $log->update(['status'=>1]);
// $walletService->changeBalance($userInfo->user, $log->amount, WalletLog::ACTION_QUOTA_V1, '老配额分红', $log);
// DB::commit();
// } catch (Throwable $th) {
// DB::rollBack();
// report($th);
// }
}
// }
}
});
}
$job->update([
'status' => 2,
]);
}
return Command::SUCCESS;
}
}