CompanyChargeDayJiesuan.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Console\Commands\Jiesuan;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Redis;
  6. use Modules\Jiesuan\Services\CompanyUserMoneyService;
  7. use Modules\Jiesuan\Services\ConstService;
  8. class CompanyChargeDayJiesuan extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'Jiesuan:CompanyChargeDayJiesuan {--date= : 结算日期}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '投放公司每日充值结算';
  22. /**
  23. * Execute the console command.
  24. */
  25. public function handle(): void
  26. {
  27. $date = $this->option('date');
  28. $chargeInfos = DB::table('orders')
  29. ->where('created_at', '>=', $date)
  30. ->where('created_at', '<=', $date . ' 23:59:59')
  31. ->where('status', '<>', 'UNPAID')
  32. ->where('puser_id', '<>', 0)
  33. ->select(
  34. 'puser_id',
  35. DB::raw('sum(price) as charge_money')
  36. )->groupBy('puser_id')
  37. ->get()->keyBy('puser_id');
  38. $tuikuanInfos = $this->getTuikuanInfo($date);
  39. $puserIds = $chargeInfos->pluck('puser_id')->merge($tuikuanInfos->pluck('puser_id'))->unique();
  40. if($puserIds->isNotEmpty()) {
  41. $jiesuanInfo = collect();
  42. foreach ($puserIds as $puserId) {
  43. $jiesuanInfo->put($puserId, (object)[
  44. 'charge_money' => $chargeInfos->get($puserId)->charge_money ?? 0,
  45. 'tuikuan_money' => $tuikuanInfos->get($puserId)->tuikuan_money ?? 0,
  46. ]);
  47. }
  48. $defaultShareRate = DB::table('company_share_rates')->where('company_uid', 0)->value('share_rate') ?? '89.1';
  49. $now = date('Y-m-d H:i:s');
  50. $puserIds->chunk(2)->map(function ($item) use ($jiesuanInfo, $defaultShareRate, $date, $now){
  51. $rates = DB::table('company_share_rates')->whereIn('company_uid', $item)
  52. ->select('company_uid', 'share_rate')
  53. ->get()->keyBy('company_uid');
  54. $insertData = [];
  55. foreach ($item as $i) {
  56. $share_rate = $rates->get($i)->share_rate ?? $defaultShareRate;
  57. $jiesuanInfo->get($i)->share_rate = $share_rate;
  58. $jiesuanInfo->get($i)->jiesuan_money =
  59. intval(($jiesuanInfo->get($i)->charge_money - $jiesuanInfo->get($i)->tuikuan_money) * $share_rate) / 100;
  60. $jiesuanInfo->get($i)->tuikuan_money = intval($jiesuanInfo->get($i)->tuikuan_money * $share_rate) / 100;
  61. $insertData[] = [
  62. 'company_uid' => $i,
  63. 'jiesuan_date' => $date,
  64. 'charge_money' => $jiesuanInfo->get($i)->charge_money,
  65. 'tuikuan_money' => $jiesuanInfo->get($i)->tuikuan_money,
  66. 'share_rate' => $jiesuanInfo->get($i)->share_rate,
  67. 'jiesuan_money' => $jiesuanInfo->get($i)->jiesuan_money,
  68. 'created_at' => $now,
  69. 'updated_at' => $now,
  70. ];
  71. }
  72. DB::transaction(function () use ($insertData, $now){
  73. DB::table('jiesuan_records')->insert($insertData);
  74. foreach ($insertData as $item) {
  75. $beforeUserMoneyInfo = CompanyUserMoneyService::userMoneyInfo($item['company_uid']);
  76. $companyUserMoney = DB::table('company_user_money')
  77. ->where('company_uid', $item['company_uid'])->first();
  78. if($companyUserMoney) {
  79. $afterTotalIncome = bcadd($companyUserMoney->total_income, $item['charge_money'], 2);
  80. DB::table('company_user_money')
  81. ->where(['company_uid' => $item['company_uid']])
  82. ->update([
  83. 'total_income' => $afterTotalIncome,
  84. 'total_tuikuan' => bcadd($companyUserMoney->total_tuikuan, $item['tuikuan_money'], 2),
  85. 'updated_at' => $now
  86. ]);
  87. } else {
  88. $afterTotalIncome = $item['charge_money'];
  89. DB::table('company_user_money')
  90. ->insert([
  91. 'company_uid' => $item['company_uid'],
  92. 'total_income' => $afterTotalIncome,
  93. 'total_tuikuan' => $item['tuikuan_money'],
  94. 'created_at' => $now, 'updated_at' => $now,
  95. ]);
  96. }
  97. $currentUserMoneyInfo = CompanyUserMoneyService::userMoneyInfo($item['company_uid']);
  98. CompanyUserMoneyService::log($item['company_uid'], ConstService::MONEY_CHANGE_TYPE_DAY_JIESUAN,
  99. $beforeUserMoneyInfo, $currentUserMoneyInfo);
  100. }
  101. });
  102. });
  103. }
  104. }
  105. private function getTuikuanInfo($date) {
  106. return DB::table('orders_refund_verify')
  107. ->where(['refund_status' => 1])
  108. ->where('pay_at', '>=', $date)
  109. ->where('pay_at', '<=', $date. ' 23:59:59')
  110. ->where('puser_id', '<>', 0)
  111. ->select(
  112. 'puser_id',
  113. DB::raw('sum(refund_price) as tuikuan_money')
  114. )->groupBy('puser_id')
  115. ->get()->keyBy('puser_id');
  116. }
  117. }