结算预收益
parent
73821d14ed
commit
8ab8684f9e
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\Distribution;
|
||||
|
||||
use App\Models\DistributionPreIncome;
|
||||
use App\Models\WalletLog;
|
||||
use App\Services\WalletService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
class PreIncomeSettleCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'distribution:pre-income-settle';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '分销预收益结算';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @param \App\Services\WalletService $walletService
|
||||
* @return int
|
||||
*/
|
||||
public function handle(WalletService $walletService)
|
||||
{
|
||||
DistributionPreIncome::processing()->chunkById(200, function ($preIncomes) use ($walletService) {
|
||||
$preIncomes->load('user');
|
||||
|
||||
foreach ($preIncomes as $preIncome) {
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$walletService->changeBalance(
|
||||
$preIncome->user,
|
||||
bcmul($preIncome->total_revenue, 100),
|
||||
WalletLog::ACTION_DISTRIBUTION_PRE_INCOME,
|
||||
$preIncome->remarks,
|
||||
$preIncome
|
||||
);
|
||||
|
||||
$preIncome->update([
|
||||
'completed_at' => now(),
|
||||
'status' => DistributionPreIncome::STATUS_PROCESSED,
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
} catch (Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -73,6 +73,22 @@ class DistributionPreIncome extends Model
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅查询结算中的预收益
|
||||
*/
|
||||
public function scopeProcessing($query)
|
||||
{
|
||||
return $query->where('status', static::STATUS_PROCESSING);
|
||||
}
|
||||
|
||||
/**
|
||||
* 此预收益的所属的用户
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 此预收益的变更记录
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ class WalletLog extends Model
|
|||
public const ACTION_WITHDRAW_BANK = 4;
|
||||
public const ACTION_WITHDRAW_BALACNE = 5;
|
||||
public const ACTION_WITHDRAW_FAILED = 6;
|
||||
public const ACTION_DISTRIBUTION_PRE_INCOME = 10;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ class AppServiceProvider extends ServiceProvider
|
|||
'after_sale' => \App\Models\AfterSale::class,
|
||||
'wallet_to_bank_log' => \App\Models\WalletToBankLog::class,
|
||||
'balance_log' => \App\Models\BalanceLog::class,
|
||||
'distribution_pre_income' => \App\Models\DistributionPreIncome::class,
|
||||
]);
|
||||
|
||||
JsonResource::withoutWrapping();
|
||||
|
|
|
|||
Loading…
Reference in New Issue