liuzejian преди 1 година
родител
ревизия
7e135f5f00

+ 156 - 0
app/Console/Commands/Jiesuan/CompanyChargeDayJiesuan.php

@@ -0,0 +1,156 @@
+<?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');
+    }
+}

+ 6 - 0
app/Console/Kernel.php

@@ -16,6 +16,12 @@ class Kernel extends ConsoleKernel
     protected function schedule(Schedule $schedule)
     {
         $schedule->command('Callback:JuliangAccountRateConfigRefresh')->everyMinute();
+        /**
+         * 投放渠道日结算
+         */
+        $schedule->command('Jiesuan:CompanyChargeDayJiesuan', [
+            '--date' => date('Y-m-d', strtotime('yesterday'))
+        ])->dailyAt('01:00');
     }
 
     /**

+ 46 - 0
tests/Console/Commands/Jiesuan/CompanyChargeDayJiesuanTest.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace Tests\Console\Commands\Jiesuan;
+
+use App\Console\Commands\Jiesuan\CompanyChargeDayJiesuan;
+use Illuminate\Support\Facades\DB;
+use PHPUnit\Framework\TestCase;
+
+class CompanyChargeDayJiesuanTest extends \Tests\TestCase
+{
+
+    public function testHandle()
+    {
+        $this->prepareData();
+    }
+
+    private function prepareData() {
+
+        $startTimestamp = strtotime('yesterday') - 24 * 3600;
+
+        foreach (range(1, 100) as $i) {
+            $orders[] = [
+                'uid' => 100000+$i,
+                'promotion_id' => 1000,
+                'user_id' => 50 + $i,
+                'puser_id' => [2, 15][rand(0,1)],
+                'price' => rand(20, 100),
+                'miniprogram_id' => 1,
+                'pay_product_id' => 1,
+                'create_ip' => '192.168.0.1',
+                'status' => ['PAID', 'UNPAID'][rand(0,1)],
+                'trade_no' => uniqid('fake-1-'),
+                'transaction_id' => uniqid('fake-2-'),
+                'third_orderid' => uniqid('fake-3-'),
+                'order_type' => 'COIN',
+                'pay_merchant_source' => 'PALMPAY',
+                'video_id' => 0,
+                'video_series_sequence' => 0,
+                'created_at' => date('Y-m-d H:i:s', $startTimestamp + $i * 800),
+                'updated_at' => date('Y-m-d H:i:s', $startTimestamp + $i * 800),
+            ];
+        }
+
+        DB::table('orders')->insert($orders);
+    }
+}

+ 11 - 5
tests/Jiesuan/Http/Controllers/BankAccountControllerTest.php

@@ -14,7 +14,8 @@ class BankAccountControllerTest extends UsedTestCase
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/bankAccount/hezuoInfo', []);
-        $res->dump();
+//        $res->dump();
+        $this->dumpJson($res);
     }
     public function testaddCompanyCard()
     {
@@ -67,28 +68,33 @@ class BankAccountControllerTest extends UsedTestCase
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/bankAccount/listShangwu');
-        $res->dump();
+//        $res->dump();
+        $this->dumpJson($res);
     }
     public function testlistBank()
     {
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/bankAccount/listBank');
-        $res->dump();
+//        $res->dump();
+        $this->dumpJson($res);
     }
+
     public function testlistCompanyCard()
     {
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/bankAccount/listCompanyCard');
-        $res->dump();
+//        $res->dump();
+        $this->dumpJson($res);
     }
     public function testlistAvailableBankCard()
     {
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/bankAccount/listAvailableBankCard');
-        $res->dump();
+//        $res->dump();
+        $this->dumpJson($res);
     }
 
 

+ 4 - 2
tests/Jiesuan/Http/Controllers/JiesuanControllerTest.php

@@ -14,14 +14,15 @@ class JiesuanControllerTest extends UsedTestCase
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/jiesuan/list');
-        $res->dump();
+        $this->dumpJson($res);
     }
     public function testaccountInfo()
     {
         $res = $this->withHeaders([
             'Authorization' => 'Bearer '. $this->token,
         ])->json('get','http://localhost/api/jiesuanManage/jiesuan/accountInfo');
-        $res->dump();
+//        $res->dump();
+        $this->dumpJson($res);
     }
  public function testtixian()
     {
@@ -41,5 +42,6 @@ class JiesuanControllerTest extends UsedTestCase
 //            'status' => 5
         ]);
         $res->dump();
+//        $this->dumpJson($res);
     }
 }