123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Console\Commands\Jiesuan;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use Modules\Jiesuan\Services\CompanyUserMoneyService;
- class CompanyChargeDayJiesuan extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Jiesuan:CompanyChargeDayJiesuan {--date= : 结算日期}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '投放公司每日充值结算';
- /**
- * Execute the console command.
- */
- public function handle(): void
- {
- $date = $this->option('date');
- $chargeInfos = DB::table('orders')
- ->where('created_at', '>=', $date)
- ->where('created_at', '<=', $date . ' 23:59:59')
- ->where('status', '<>', 'UNPAID')
- ->where('puser_id', '<>', 0)
- ->select(
- 'puser_id',
- DB::raw('sum(price) as charge_money')
- )->groupBy('puser_id')
- ->get()->keyBy('puser_id');
- $tuikuanInfos = $this->getTuikuanInfo($date);
- $puserIds = $chargeInfos->pluck('puser_id')->merge($tuikuanInfos->pluck('puser_id'))->unique();
- if($puserIds->isNotEmpty()) {
- $jiesuanInfo = collect();
- foreach ($puserIds as $puserId) {
- $jiesuanInfo->put($puserId, (object)[
- 'charge_money' => $chargeInfos->get($puserId)->charge_money ?? 0,
- 'tuikuan_money' => $tuikuanInfos->get($puserId)->tuikuan_money ?? 0,
- ]);
- }
- $defaultShareRate = DB::table('company_share_rates')->where('company_uid', 0)->value('share_rate') ?? '89.1';
- $now = date('Y-m-d H:i:s');
- $puserIds->chunk(2)->map(function ($item) use ($jiesuanInfo, $defaultShareRate, $date, $now){
- $rates = DB::table('company_share_rates')->whereIn('company_uid', $item)
- ->select('company_uid', 'share_rate')
- ->get()->keyBy('company_uid');
- $insertData = [];
- foreach ($item as $i) {
- $share_rate = $rates->get($i)->share_rate ?? $defaultShareRate;
- $jiesuanInfo->get($i)->share_rate = $share_rate;
- $jiesuanInfo->get($i)->jiesuan_money =
- intval(($jiesuanInfo->get($i)->charge_money - $jiesuanInfo->get($i)->tuikuan_money) * $share_rate) / 100;
- $jiesuanInfo->get($i)->tuikuan_money = intval($jiesuanInfo->get($i)->tuikuan_money * $share_rate) / 100;
- $insertData[] = [
- 'company_uid' => $i,
- 'jiesuan_date' => $date,
- 'charge_money' => $jiesuanInfo->get($i)->charge_money,
- 'tuikuan_money' => $jiesuanInfo->get($i)->tuikuan_money,
- 'share_rate' => $jiesuanInfo->get($i)->share_rate,
- 'jiesuan_money' => $jiesuanInfo->get($i)->jiesuan_money,
- 'created_at' => $now,
- 'updated_at' => $now,
- ];
- }
- DB::transaction(function () use ($insertData, $now){
- DB::table('jiesuan_records')->insert($insertData);
- foreach ($insertData as $item) {
- $beforeUserMoneyInfo = CompanyUserMoneyService::userMoneyInfo($item['company_uid']);
- $companyUserMoney = DB::table('company_user_money')
- ->where('company_uid', $item['company_uid'])->first();
- if($companyUserMoney) {
- $beforeTotalIncome = $companyUserMoney->total_income;
- $afterTotalIncome = bcadd($companyUserMoney->total_income, $item['charge_money'], 2);
- DB::table('company_user_money')
- ->where(['company_uid' => $item['company_uid']])
- ->update([
- 'total_income' => $afterTotalIncome,
- 'updated_at' => $now
- ]);
- } else {
- $beforeTotalIncome = 0;
- $afterTotalIncome = $item['charge_money'];
- DB::table('company_user_money')
- ->insert([
- 'company_uid' => $item['company_uid'],
- 'total_income' => $afterTotalIncome,
- 'created_at' => $now, 'updated_at' => $now,
- ]);
- }
- $currentUserMoneyInfo = CompanyUserMoneyService::userMoneyInfo($item['company_uid']);
- DB::table('company_user_money_change_logs')
- ->insert([
- 'type' => 1,
- 'company_uid' => $item['company_uid'],
- 'created_at' => $now,
- 'log' => \json_encode([
- 'desc' => sprintf('总充值变动:[%s元-->%s元],当前总打款:[%s元],总退款变动:[%s元-->%s元],当前审核中:[%s元],余额变动:[%s元-->%s元]',
- $beforeTotalIncome, $currentUserMoneyInfo->total_income,$currentUserMoneyInfo->total_dakuan,
- $beforeUserMoneyInfo->total_tuikuan,
- $currentUserMoneyInfo->total_tuikuan, $currentUserMoneyInfo->tixian_money,
- $beforeUserMoneyInfo->yue_money,
- $currentUserMoneyInfo->yue_money),
- 'before' => [
- 'total_income' => $beforeUserMoneyInfo->total_income,
- 'total_dakuan' => $beforeUserMoneyInfo->total_dakuan,
- 'total_tuikuan' => $beforeUserMoneyInfo->total_tuikuan,
- 'tixian_money' => $beforeUserMoneyInfo->tixian_money,
- 'yue_money' => $beforeUserMoneyInfo->yue_money,
- ],
- 'after' => [
- 'total_income' => $currentUserMoneyInfo->total_income,
- 'total_dakuan' => $currentUserMoneyInfo->total_dakuan,
- 'total_tuikuan' => $currentUserMoneyInfo->total_tuikuan,
- 'tixian_money' => $currentUserMoneyInfo->tixian_money,
- 'yue_money' => $currentUserMoneyInfo->yue_money,
- ],
- 'created_at' => $now,
- ], JSON_UNESCAPED_UNICODE)
- ]);
- }
- });
- });
- }
- }
- private function getTuikuanInfo($date) {
- return DB::table('orders_refund_verify')
- ->where(['refund_status' => 1])
- ->where('pay_at', '>=', $date)
- ->where('pay_at', '<=', $date. ' 23:59:59')
- ->where('puser_id', '<>', 0)
- ->select(
- 'puser_id',
- DB::raw('sum(refund_price) as tuikuan_money')
- )->groupBy('puser_id')
- ->get()->keyBy('puser_id');
- }
- }
|