CheckCommission.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Order;
  5. use App\Models\User;
  6. use Illuminate\Support\Facades\DB;
  7. class CheckCommission extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'check:commission';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '返佣服务';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->autoCheck();
  38. $this->autoPayCommission();
  39. }
  40. public function autoCheck()
  41. {
  42. if ((int)config('v2board.commission_auto_check_enable', 1)) {
  43. Order::where('commission_status', 0)
  44. ->where('invite_user_id', '!=', NULL)
  45. ->whereIn('status', [3, 4])
  46. ->where('updated_at', '<=', strtotime('-3 day', time()))
  47. ->update([
  48. 'commission_status' => 1
  49. ]);
  50. }
  51. }
  52. public function autoPayCommission()
  53. {
  54. $order = Order::where('commission_status', 1)
  55. ->where('invite_user_id', '!=', NULL)
  56. ->get();
  57. foreach ($order as $item) {
  58. DB::beginTransaction();
  59. $inviter = User::find($item->invite_user_id);
  60. if (!$inviter) continue;
  61. $inviter->commission_balance = $inviter->commission_balance + $item->commission_balance;
  62. if (!$inviter->save()) {
  63. DB::rollBack();
  64. continue;
  65. }
  66. $item->commission_status = 2;
  67. if (!$item->save()){
  68. DB::rollBack();
  69. continue;
  70. }
  71. DB::commit();
  72. }
  73. }
  74. }