| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Console\Commands;
- use App\Repositories\AgentAttrsDayRepository;
- use App\Repositories\AgentRepository;
- use App\Repositories\BalanceLogRepository;
- use App\Repositories\CommissionLogRepository;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- class CommissionMoneyToBalance extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'commission_money:balance {--date=}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '当日提成金额划给当前代理余额';
- /**
- * @var null|CommissionLogRepository
- */
- protected $commissionLogRepo = null;
- /**
- * @var null|AgentRepository
- */
- protected $agentRepo = null;
- /**
- * @var null|BalanceLogRepository
- */
- protected $balanceLogRepo = null;
- /**
- * @var null|AgentAttrsDayRepository
- */
- protected $attrsDayRepo = null;
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $date = $this->getArgsDate();
- $items = $this->getCommissionLogRepo()->makeModel()->query()->whereDate('date', $date)->distinct('agent_uid')->groupBy('agent_uid')->get(['agent_uid']);
- if ($items->count() > 0) {
- $changeNote = '[系统][计划任务]CommissionMoneyToBalance, 当日提成划到余额';
- foreach ($items as $item) {
- $agentUid = $item->agent_uid;
- $dayObj = $this->getAttrsDayRepo()->makeModel()->query()->whereDate('td_date', $date)->where(['agent_uid' => $agentUid])->first(['id', 'is_used']);
- if (isset($dayObj->is_used) && $dayObj->is_used == 1) {
- // 已划过钱,跳过.
- continue;
- }
- $amountCommissionMoney = $this->getCommissionLogRepo()->makeModel()->query()->whereDate('date', $date)->where(['agent_uid' => $agentUid])->sum('commission_money');
- $agentObj = $this->getAgentRepo()->makeModel()->query()->where(['uid' => $agentUid])->first();
- if (!empty($agentObj)) {
- $prevAgent = clone $agentObj;
- $agentObj->balance += $amountCommissionMoney;
- $saveStatus = $agentObj->save();
- if ($saveStatus) {
- $balaceStatus = $this->getBalanceLogRepo()->record($agentObj, $prevAgent, $changeNote);
- if ($balaceStatus && !empty($dayObj)) {
- // DB里记录已经划过钱了,不再重复划.
- $dayObj->is_used = 1;
- $dayObj->save();
- }
- }
- }
- }
- $this->comment('['. Carbon::now()->toDateTimeString() .'][系统][计划任务]['. $date .']CommissionMoneyToBalance, 当日提成划到余额');
- }
- }
- /**
- * 获取日期参数.
- * @return string
- */
- private function getArgsDate()
- {
- $oldDate = $date = $this->option('date');
- if (empty($date)) {
- // 没有传参数, 拿昨天的
- $date = Carbon::now()->subDays(1)->toDateString();
- }
- return $date;
- }
- /**
- * @return CommissionLogRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
- */
- private function getCommissionLogRepo()
- {
- if (!$this->commissionLogRepo instanceof CommissionLogRepository) {
- $this->commissionLogRepo = app(CommissionLogRepository::class);
- }
- return $this->commissionLogRepo;
- }
- /**
- * @return AgentRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
- */
- private function getAgentRepo()
- {
- if (!$this->agentRepo instanceof AgentRepository) {
- $this->agentRepo = app(AgentRepository::class);
- }
- return $this->agentRepo;
- }
- /**
- * @return BalanceLogRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
- */
- private function getBalanceLogRepo()
- {
- if (!$this->balanceLogRepo instanceof BalanceLogRepository) {
- $this->balanceLogRepo = app(BalanceLogRepository::class);
- }
- return $this->balanceLogRepo;
- }
- /**
- * @return AgentAttrsDayRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
- */
- private function getAttrsDayRepo()
- {
- if (!$this->attrsDayRepo instanceof AgentAttrsDayRepository) {
- $this->attrsDayRepo = app(AgentAttrsDayRepository::class);
- }
- return $this->attrsDayRepo;
- }
- }
|