Forráskód Böngészése

推广统计,即使没有数据,也进行每日数据的插入

liuzejian 1 éve
szülő
commit
a06ca61b5d

+ 13 - 14
app/Console/Commands/Statistic/CompanyDayCharge.php

@@ -49,20 +49,19 @@ class CompanyDayCharge extends Command
                     DB::raw("sum(if(status <> 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_pay_count"),
                     DB::raw("sum(if(status = 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_unpay_count"),
                 )->first();
-            if($info) {
-                DB::table('tj_company_day_charge')
-                    ->insert([
-                        'day_at' => $date,
-                        'pay_money' => $info->pay_money,
-                        'common_pay_count' => $info->common_pay_count,
-                        'common_unpay_count' => $info->common_unpay_count,
-                        'vip_pay_count' => $info->vip_pay_count,
-                        'vip_unpay_count' => $info->vip_unpay_count,
-                        'company_uid' => $companyUid,
-                        'created_at' => $now,
-                        'updated_at' => $now,
-                    ]);
-            }
+            DB::table('tj_company_day_charge')
+                ->insert([
+                    'day_at' => $date,
+                    'pay_money' => $info->pay_money ?? 0,
+                    'common_pay_count' => $info->common_pay_count ?? 0,
+                    'common_unpay_count' => $info->common_unpay_count ?? 0,
+                    'vip_pay_count' => $info->vip_pay_count ?? 0,
+                    'vip_unpay_count' => $info->vip_unpay_count ?? 0,
+                    'company_uid' => $companyUid,
+                    'created_at' => $now,
+                    'updated_at' => $now,
+                ]);
+
         }
 
     }

+ 12 - 15
app/Console/Commands/Statistic/CompanyMonthCharge.php

@@ -54,21 +54,18 @@ class CompanyMonthCharge extends Command
                     DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
                     DB::raw("sum(vip_pay_count) as vip_pay_count"),
                 )->first();
-            $now = date('Y-m-d H:i:s');
-            if($info) {
-                DB::table('tj_company_month_charge')
-                    ->insert([
-                        'month_at' => $month,
-                        'pay_money' => $info->pay_money,
-                        'common_pay_count' => $info->common_pay_count,
-                        'common_unpay_count' => $info->common_unpay_count,
-                        'vip_pay_count' => $info->vip_pay_count,
-                        'vip_unpay_count' => $info->vip_unpay_count,
-                        'company_uid' => $companyUid,
-                        'created_at' => $now,
-                        'updated_at' => $now,
-                    ]);
-            }
+            DB::table('tj_company_month_charge')
+                ->insert([
+                    'month_at' => $month,
+                    'pay_money' => $info->pay_money ?? 0,
+                    'common_pay_count' => $info->common_pay_count ?? 0,
+                    'common_unpay_count' => $info->common_unpay_count ?? 0,
+                    'vip_pay_count' => $info->vip_pay_count ?? 0,
+                    'vip_unpay_count' => $info->vip_unpay_count ?? 0,
+                    'company_uid' => $companyUid,
+                    'created_at' => $now,
+                    'updated_at' => $now,
+                ]);
         }
     }
 }

+ 6 - 5
app/Console/Commands/Statistic/OptimizerMonthCharge.php

@@ -59,7 +59,12 @@ class OptimizerMonthCharge extends Command
                 ->get();
             $insertData = [];
             foreach ($result as $item) {
-                $info = (array)$item;
+                $info['pay_money'] = $item->pay_money ?? 0;
+                $info['common_pay_count'] = $item->common_pay_count ?? 0;
+                $info['common_unpay_count'] = $item->common_unpay_count ?? 0;
+                $info['vip_unpay_count'] = $item->vip_unpay_count ?? 0;
+                $info['vip_pay_count'] = $item->vip_pay_count ?? 0;
+                $info['miniprogram_id'] = $item->miniprogram_id ?? 0;
                 $info['created_at'] = $info['updated_at'] = $now;
                 $info['month_at'] = $month;
                 $info['user_id'] = $userId;
@@ -70,9 +75,5 @@ class OptimizerMonthCharge extends Command
             DB::table('tj_optimizer_month_charge')
                 ->insert($insertData);
         }
-
-
-        DB::table('tj_optimizer_month_charge')
-            ->insert($insertData);
     }
 }

+ 58 - 0
app/Console/Commands/Statistic/PromotionDayCharge.php

@@ -81,6 +81,64 @@ class PromotionDayCharge extends Command
             DB::table('tj_optimizer_day_charge')
                 ->insert($items->values()->toArray());
         }
+
+
+        DB::table('promotions')
+            ->leftJoin('users', 'users.id', '=' , 'promotions.uid')
+            ->select('promotions.id', 'promotions.uid', 'promotions.miniprogram_id', 'users.pid as puid')
+            ->orderBy('promotions.id')
+            ->chunk(100, function ($items) use ($date){
+                $promotionIds = $items->pluck('id');
+                $now = date('Y-m-d H:i:s');
+                $alreadyExistsPromotionIds = DB::table('tj_promotion_day_charge')
+                    ->where('day_at', $date)
+                    ->whereIn('promotion_id', $promotionIds)
+                    ->select('promotion_id')
+                    ->get()->pluck('promotion_id');
+                foreach ($items as $item) {
+                    if($alreadyExistsPromotionIds->contains($item->id)) {
+                        continue;
+                    }
+                    DB::table('tj_promotion_day_charge')
+                        ->insert([
+                            'promotion_id' => $item->id, 'day_at' => $date,
+                            'created_at' => $now, 'updated_at' => $now,
+                            'user_id' => $item->uid, 'puser_id' => $item->puid,
+                            'miniprogram_id' => $item->miniprogram_id
+                        ]);
+                }
+            });
+
+        DB::table('users')
+            ->join('user_has_roles', 'users.id', 'user_has_roles.user_id')
+            ->where(['user_has_roles.role_id' => 2, 'users.deleted_at' => 0])
+            ->distinct()
+            ->select('users.id', 'users.pid')
+            ->orderBy('users.id')
+            ->chunk(50, function ($items) use($date) {
+                $now = date('Y-m-d H:i:s');
+                foreach ($items as $item) {
+                    $miniprogramIds = DB::table('user_has_miniprograms')
+                        ->where('uid', $item->id)
+                        ->where('is_enabled', 1)
+                        ->select('miniprogram_id')
+                        ->get()->pluck('miniprogram_id');
+                    foreach ($miniprogramIds as $miniprogramId) {
+                        if(DB::table('tj_optimizer_day_charge')
+                            ->where([
+                                'day_at' => $date,
+                                'user_id' => $item->id, 'miniprogram_id' => $miniprogramId,
+                            ])->exists()) {
+                            continue;
+                        }
+                        DB::table('tj_optimizer_day_charge')
+                            ->insert([
+                                'day_at' => $date, 'user_id' => $item->id, 'miniprogram_id' => $miniprogramId,
+                                'puser_id' => $item->pid, 'created_at' => $now, 'updated_at' => $now
+                            ]);
+                    }
+                }
+            });
     }
 
     /**

+ 19 - 0
tests/Console/Commands/Statistic/PromotionDayChargeTest.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace Tests\Console\Commands\Statistic;
+
+use App\Console\Commands\Statistic\PromotionDayCharge;
+use PHPUnit\Framework\TestCase;
+
+class PromotionDayChargeTest extends \Tests\TestCase
+{
+
+    public function testHandle()
+    {
+        for($i = 0; $i > -27; $i--) {
+            $date = date('Y-m-d', strtotime(sprintf('%s day', $i)));
+            $data[] = 'php artisan Statistic:CompanyDayCharge --date='.$date;
+        }
+        dump(join(' && ', $data));
+    }
+}