CompanyChargeDayJiesuan.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. class CompanyChargeDayJiesuan extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'Jiesuan:CompanyChargeDayJiesuan {--date= : 结算日期}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '投放公司每日充值结算';
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle(): void
  25. {
  26. $date = $this->option('date');
  27. $chargeInfos = DB::table('orders')
  28. ->where('created_at', '>=', $date)
  29. ->where('created_at', '<=', $date . ' 23:59:59')
  30. ->where('status', '<>', 'UNPAID')
  31. ->where('puser_id', '<>', 0)
  32. ->select(
  33. 'puser_id',
  34. DB::raw('sum(price) as charge_money')
  35. )->groupBy('puser_id')
  36. ->get()->keyBy('puser_id');
  37. $tuikuanInfos = $this->getTuikuanInfo($date);
  38. $puserIds = $chargeInfos->pluck('puser_id')->merge($tuikuanInfos->pluck('puser_id'))->unique();
  39. if($puserIds->isNotEmpty()) {
  40. $jiesuanInfo = collect();
  41. foreach ($puserIds as $puserId) {
  42. $jiesuanInfo->put($puserId, (object)[
  43. 'charge_money' => $chargeInfos->get($puserId)->charge_money ?? 0,
  44. 'tuikuan_money' => $tuikuanInfos->get($puserId)->tuikuan_money ?? 0,
  45. ]);
  46. }
  47. $defaultShareRate = DB::table('company_share_rates')->where('company_uid', 0)->value('share_rate') ?? '89.1';
  48. $now = date('Y-m-d H:i:s');
  49. $puserIds->chunk(2)->map(function ($item) use ($jiesuanInfo, $defaultShareRate, $date, $now){
  50. $rates = DB::table('company_share_rates')->whereIn('company_uid', $item)
  51. ->select('company_uid', 'share_rate')
  52. ->get()->keyBy('company_uid');
  53. $insertData = [];
  54. foreach ($item as $i) {
  55. $share_rate = $rates->get($i)->share_rate ?? $defaultShareRate;
  56. $jiesuanInfo->get($i)->share_rate = $share_rate;
  57. $jiesuanInfo->get($i)->jiesuan_money =
  58. intval(($jiesuanInfo->get($i)->charge_money - $jiesuanInfo->get($i)->tuikuan_money) * $share_rate) / 100;
  59. $jiesuanInfo->get($i)->tuikuan_money = intval($jiesuanInfo->get($i)->tuikuan_money * $share_rate) / 100;
  60. $insertData[] = [
  61. 'company_uid' => $i,
  62. 'jiesuan_date' => $date,
  63. 'charge_money' => $jiesuanInfo->get($i)->charge_money,
  64. 'tuikuan_money' => $jiesuanInfo->get($i)->tuikuan_money,
  65. 'share_rate' => $jiesuanInfo->get($i)->share_rate,
  66. 'jiesuan_money' => $jiesuanInfo->get($i)->jiesuan_money,
  67. 'created_at' => $now,
  68. 'updated_at' => $now,
  69. ];
  70. }
  71. DB::transaction(function () use ($insertData, $now){
  72. DB::table('jiesuan_records')->insert($insertData);
  73. foreach ($insertData as $item) {
  74. $beforeUserMoneyInfo = CompanyUserMoneyService::userMoneyInfo($item['company_uid']);
  75. $companyUserMoney = DB::table('company_user_money')
  76. ->where('company_uid', $item['company_uid'])->first();
  77. if($companyUserMoney) {
  78. $beforeTotalIncome = $companyUserMoney->total_income;
  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. 'updated_at' => $now
  85. ]);
  86. } else {
  87. $beforeTotalIncome = 0;
  88. $afterTotalIncome = $item['charge_money'];
  89. DB::table('company_user_money')
  90. ->insert([
  91. 'company_uid' => $item['company_uid'],
  92. 'total_income' => $afterTotalIncome,
  93. 'created_at' => $now, 'updated_at' => $now,
  94. ]);
  95. }
  96. $currentUserMoneyInfo = CompanyUserMoneyService::userMoneyInfo($item['company_uid']);
  97. DB::table('company_user_money_change_logs')
  98. ->insert([
  99. 'type' => 1,
  100. 'company_uid' => $item['company_uid'],
  101. 'created_at' => $now,
  102. 'log' => \json_encode([
  103. 'desc' => sprintf('总充值变动:[%s元-->%s元],当前总打款:[%s元],总退款变动:[%s元-->%s元],当前审核中:[%s元],余额变动:[%s元-->%s元]',
  104. $beforeTotalIncome, $currentUserMoneyInfo->total_income,$currentUserMoneyInfo->total_dakuan,
  105. $beforeUserMoneyInfo->total_tuikuan,
  106. $currentUserMoneyInfo->total_tuikuan, $currentUserMoneyInfo->tixian_money,
  107. $beforeUserMoneyInfo->yue_money,
  108. $currentUserMoneyInfo->yue_money),
  109. 'before' => [
  110. 'total_income' => $beforeUserMoneyInfo->total_income,
  111. 'total_dakuan' => $beforeUserMoneyInfo->total_dakuan,
  112. 'total_tuikuan' => $beforeUserMoneyInfo->total_tuikuan,
  113. 'tixian_money' => $beforeUserMoneyInfo->tixian_money,
  114. 'yue_money' => $beforeUserMoneyInfo->yue_money,
  115. ],
  116. 'after' => [
  117. 'total_income' => $currentUserMoneyInfo->total_income,
  118. 'total_dakuan' => $currentUserMoneyInfo->total_dakuan,
  119. 'total_tuikuan' => $currentUserMoneyInfo->total_tuikuan,
  120. 'tixian_money' => $currentUserMoneyInfo->tixian_money,
  121. 'yue_money' => $currentUserMoneyInfo->yue_money,
  122. ],
  123. 'created_at' => $now,
  124. ], JSON_UNESCAPED_UNICODE)
  125. ]);
  126. }
  127. });
  128. });
  129. }
  130. }
  131. private function getTuikuanInfo($date) {
  132. return DB::table('orders_refund_verify')
  133. ->where(['refund_status' => 1])
  134. ->where('pay_at', '>=', $date)
  135. ->where('pay_at', '<=', $date. ' 23:59:59')
  136. ->where('puser_id', '<>', 0)
  137. ->select(
  138. 'puser_id',
  139. DB::raw('sum(refund_price) as tuikuan_money')
  140. )->groupBy('puser_id')
  141. ->get()->keyBy('puser_id');
  142. }
  143. }