CommissionMoneyToBalance.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Repositories\AgentAttrsDayRepository;
  4. use App\Repositories\AgentRepository;
  5. use App\Repositories\BalanceLogRepository;
  6. use App\Repositories\CommissionLogRepository;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. class CommissionMoneyToBalance extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'commission_money:balance {--date=}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '当日提成金额划给当前代理余额';
  23. /**
  24. * @var null|CommissionLogRepository
  25. */
  26. protected $commissionLogRepo = null;
  27. /**
  28. * @var null|AgentRepository
  29. */
  30. protected $agentRepo = null;
  31. /**
  32. * @var null|BalanceLogRepository
  33. */
  34. protected $balanceLogRepo = null;
  35. /**
  36. * @var null|AgentAttrsDayRepository
  37. */
  38. protected $attrsDayRepo = null;
  39. /**
  40. * Create a new command instance.
  41. *
  42. * @return void
  43. */
  44. public function __construct()
  45. {
  46. parent::__construct();
  47. }
  48. /**
  49. * Execute the console command.
  50. *
  51. * @return mixed
  52. */
  53. public function handle()
  54. {
  55. $date = $this->getArgsDate();
  56. $items = $this->getCommissionLogRepo()->makeModel()->query()->whereDate('date', $date)->distinct('agent_uid')->groupBy('agent_uid')->get(['agent_uid']);
  57. if ($items->count() > 0) {
  58. $changeNote = '[系统][计划任务]CommissionMoneyToBalance, 当日提成划到余额';
  59. foreach ($items as $item) {
  60. $agentUid = $item->agent_uid;
  61. $dayObj = $this->getAttrsDayRepo()->makeModel()->query()->whereDate('td_date', $date)->where(['agent_uid' => $agentUid])->first(['id', 'is_used']);
  62. if (isset($dayObj->is_used) && $dayObj->is_used == 1) {
  63. // 已划过钱,跳过.
  64. continue;
  65. }
  66. $amountCommissionMoney = $this->getCommissionLogRepo()->makeModel()->query()->whereDate('date', $date)->where(['agent_uid' => $agentUid])->sum('commission_money');
  67. $agentObj = $this->getAgentRepo()->makeModel()->query()->where(['uid' => $agentUid])->first();
  68. if (!empty($agentObj)) {
  69. $prevAgent = clone $agentObj;
  70. $agentObj->balance += $amountCommissionMoney;
  71. $saveStatus = $agentObj->save();
  72. if ($saveStatus) {
  73. $balaceStatus = $this->getBalanceLogRepo()->record($agentObj, $prevAgent, $changeNote);
  74. if ($balaceStatus && !empty($dayObj)) {
  75. // DB里记录已经划过钱了,不再重复划.
  76. $dayObj->is_used = 1;
  77. $dayObj->save();
  78. }
  79. }
  80. }
  81. }
  82. $this->comment('['. Carbon::now()->toDateTimeString() .'][系统][计划任务]['. $date .']CommissionMoneyToBalance, 当日提成划到余额');
  83. }
  84. }
  85. /**
  86. * 获取日期参数.
  87. * @return string
  88. */
  89. private function getArgsDate()
  90. {
  91. $oldDate = $date = $this->option('date');
  92. if (empty($date)) {
  93. // 没有传参数, 拿昨天的
  94. $date = Carbon::now()->subDays(1)->toDateString();
  95. }
  96. return $date;
  97. }
  98. /**
  99. * @return CommissionLogRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
  100. */
  101. private function getCommissionLogRepo()
  102. {
  103. if (!$this->commissionLogRepo instanceof CommissionLogRepository) {
  104. $this->commissionLogRepo = app(CommissionLogRepository::class);
  105. }
  106. return $this->commissionLogRepo;
  107. }
  108. /**
  109. * @return AgentRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
  110. */
  111. private function getAgentRepo()
  112. {
  113. if (!$this->agentRepo instanceof AgentRepository) {
  114. $this->agentRepo = app(AgentRepository::class);
  115. }
  116. return $this->agentRepo;
  117. }
  118. /**
  119. * @return BalanceLogRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
  120. */
  121. private function getBalanceLogRepo()
  122. {
  123. if (!$this->balanceLogRepo instanceof BalanceLogRepository) {
  124. $this->balanceLogRepo = app(BalanceLogRepository::class);
  125. }
  126. return $this->balanceLogRepo;
  127. }
  128. /**
  129. * @return AgentAttrsDayRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
  130. */
  131. private function getAttrsDayRepo()
  132. {
  133. if (!$this->attrsDayRepo instanceof AgentAttrsDayRepository) {
  134. $this->attrsDayRepo = app(AgentAttrsDayRepository::class);
  135. }
  136. return $this->attrsDayRepo;
  137. }
  138. }