Browse Source

巨量账户级充值回传

liuzejian 1 năm trước cách đây
mục cha
commit
a5baf2be4e

+ 1 - 0
app/Jobs/Callback/JuliangAccountReportRanse.php

@@ -122,6 +122,7 @@ class JuliangAccountReportRanse implements ShouldQueue
                 'updated_at' => $now,
                 'ranse_start_at' => $this->info['ranseStartAt'],
                 'ranse_end_at' => $this->info['ranseEndAt'],
+                'optimizer_uid' => $promotion->uid,
             ]);
     }
 }

+ 17 - 6
app/Jobs/Callback/ReportCharge.php

@@ -10,7 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
-use Predis\Command\Traits\DB;
+use Illuminate\Support\Facades\DB;
 
 class ReportCharge implements ShouldQueue
 {
@@ -40,16 +40,27 @@ class ReportCharge implements ShouldQueue
      */
     public function handle(): void
     {
-        /**
-         * 1, 所有的订单日志都记录
-         */
         $traceContext = TraceContext::newFromParent($this->info['traceInfo']);
         myLog('reportCharge')->info('开始处理订单回传', [
             'orderInfo' => $this->info,
             'traceInfo' => $traceContext->getTraceInfo()
         ]);
 
-        $reportService = new JuliangAccountReportChargeService($this->info['uid'], $this->info['orderId'], $this->info['traceInfo']);
-        $reportService->report();
+        $order = DB::table('orders')->where('id', $this->info['orderId'])->first();
+        if($order && $order->promotion_id) {
+            $promotion = DB::table('promotions')->where('id', $order->promotion_id)->first();
+            switch ($promotion->callback_type ?? 0) {
+                case 0:
+                    myLog('reportCharge')->error('end:订单染色配置有问题', [
+                        'traceInfo' => $traceContext->getTraceInfo()
+                    ]);
+                    break;
+                case 1:
+                    $reportService = new JuliangAccountReportChargeService($this->info['uid'], $order, $this->info['traceInfo']);
+                    $reportService->report();
+                    break;
+            }
+        }
+
     }
 }

+ 46 - 28
app/Service/Callback/JuliangAccountReportChargeService.php

@@ -6,10 +6,12 @@ use App\Service\Callback\Tiktok\TiktokEventReportService;
 use App\Service\Util\Support\Trace\TraceContext;
 use Illuminate\Support\Facades\DB;
 
+/**
+ * 巨量账户级订单回传业务
+ */
 class JuliangAccountReportChargeService
 {
     private $uid;
-    private $orderId;
     /**
      * @var TraceContext
      */
@@ -17,24 +19,24 @@ class JuliangAccountReportChargeService
     private $order;
     private $result;
     private $trackRecord;
-    private $ranseInfo;
     private $callbackConfig;
     private $promotion;
     private $rateConfig;
-    public function __construct($uid, $orderId, $traceInfo)
+    public function __construct($uid, $order, $traceInfo)
     {
+        $this->order = $order;
         $this->uid = $uid;
-        $this->orderId = $orderId;
         $this->traceContext = TraceContext::newFromParent($traceInfo);
         $this->result = [
             'need_report' => true,
             'config_rate' => 0,
             'current_rate' => 0,
+            'report_success' => false,
+            'info_type' => 'ok',
         ];
     }
 
     public function report() {
-        $this->order = DB::table('orders')->where('id', $this->orderId)->first();
         $this->fillRanseInfo();
         $this->fillTrackInfo();
         $this->judgeOrderAlreadyDeal();
@@ -42,21 +44,21 @@ class JuliangAccountReportChargeService
         $this->judgeChargeMoney();
         $this->judgePromotionProtect();
         $this->judgeCallbackRate();
-
-        dump($this->result);
         $this->reportJuliang();
-
         $this->saveRecord();
     }
 
     private function saveRecord() {
+        dump($this->result);
+
         if($this->result['save_record'] ?? true) {
             $now = date('Y-m-d H:i:s');
-            if($this->result['info_type'] == 'promotion_protect') {
+            if(($this->result['info_type'] ?? '') == 'promotion_protect') {
                 if($this->result['report_success']) {
                     $protectedRecord = DB::table('juliang_account_promotion_protect_record')
                         ->where(['adv_promotion_id' => $this->trackRecord->adv_promotion_id,
-                            'optimizer_uid' => $this->promotion->uid, 'is_enabeld' => 1])
+                            'optimizer_uid' => $this->promotion->uid, 'is_enabled' => 1])
+                        ->orderBy('id','desc')
                         ->first();
                     if($protectedRecord) {
                         DB::table('juliang_account_promotion_protect_record')
@@ -66,7 +68,7 @@ class JuliangAccountReportChargeService
                     } else {
                         DB::table('juliang_account_promotion_protect_record')
                             ->insert([
-                                'adv_promotion_id' => $this->trackRecord->adv_promotion_id,
+                                'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
                                 'optimizer_uid' => $this->promotion->uid,
                                 'advertiser_id' => $this->callbackConfig->adv_account_id,
                                 'protected_num' => 1,
@@ -85,7 +87,7 @@ class JuliangAccountReportChargeService
                 DB::table('callback_report_charge_record')
                     ->insert([
                         'uid' => $this->uid,
-                        'order_id' => $this->orderId,
+                        'order_id' => $this->order->id,
                         'order_no' => $this->order->trade_no,
                         'optimizer_uid' => $this->promotion->uid,
                         'filter_type' => $this->result['info_type'] ?? 'ok',
@@ -94,8 +96,8 @@ class JuliangAccountReportChargeService
                         'report_result' => \json_encode($this->result['report_result'] ?? []),
                         'created_at' => $now,
                         'updated_at' => $now,
-                        'advertiser_id' => $this->trackRecord->advertiser_id,
-                        'adv_promotion_id' => $this->trackRecord->adv_promotion_id,
+                        'advertiser_id' => $this->trackRecord->advertiser_id ?? '',
+                        'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
                         'config_rate' => $this->result['config_rate'],
                         'current_rate' => $this->result['current_rate'],
                     ]);
@@ -104,7 +106,7 @@ class JuliangAccountReportChargeService
                     ->where(['id' => $chargeRecord->id])
                     ->update([
                         'uid' => $this->uid,
-                        'order_id' => $this->orderId,
+                        'order_id' => $this->order->id,
                         'order_no' => $this->order->trade_no,
                         'optimizer_uid' => $this->promotion->uid,
                         'filter_type' => $this->result['info_type'] ?? 'ok',
@@ -112,21 +114,32 @@ class JuliangAccountReportChargeService
                         'report_success' => intval($this->result['report_success'] ?? false),
                         'report_result' => \json_encode($this->result['report_result'] ?? []),
                         'updated_at' => $now,
-                        'advertiser_id' => $this->trackRecord->advertiser_id,
-                        'adv_promotion_id' => $this->trackRecord->adv_promotion_id,
+                        'advertiser_id' => $this->trackRecord->advertiser_id ?? '',
+                        'adv_promotion_id' => $this->trackRecord->adv_promotion_id ?? '',
                         'config_rate' => $this->result['config_rate'],
                         'current_rate' => $this->result['current_rate'],
                     ]);
             }
 
-            DB::table('juliang_account_rate_config_log')
-                ->where(['id' => $this->rateConfig->id])
-                ->update([
-                    'report_count' => $this->rateConfig->report_count + intval($this->result['report_success']),
-                    'unreport_count' => $this->rateConfig->report_count + intval(!$this->result['report_success']),
-                    'total_count' => $this->rateConfig->total_count + 1,
-                    'updated_at' => $now,
-                ]);
+            if($this->rateConfig) {
+                if('ok' == $this->result['info_type'] && $this->result['report_success']) {
+                    DB::table('juliang_account_rate_config_log')
+                        ->where(['id' => $this->rateConfig->id])
+                        ->update([
+                            'report_count' => $this->rateConfig->report_count + 1,
+                            'total_count' => $this->rateConfig->total_count + 1,
+                            'updated_at' => $now,
+                        ]);
+                } elseif ('rate_filter' == $this->result['info_type']) {
+                    DB::table('juliang_account_rate_config_log')
+                        ->where(['id' => $this->rateConfig->id])
+                        ->update([
+                            'unreport_count' => $this->rateConfig->unreport_count + 1,
+                            'total_count' => $this->rateConfig->total_count + 1,
+                            'updated_at' => $now,
+                        ]);
+                }
+            }
         }
     }
 
@@ -166,7 +179,7 @@ class JuliangAccountReportChargeService
         } else {
             $this->result['need_report'] = false;
             $this->result['info_type'] = 'rate_filter';
-            $this->result['info_type'] = '比率过滤';
+            $this->result['info_str'] = '比率过滤';
             $this->result['config_rate'] = $configRate;
             $this->result['current_rate'] = $currentRate;
         }
@@ -235,7 +248,7 @@ class JuliangAccountReportChargeService
                 'promotion_id' => $this->trackRecord->ranse_id,
             ])->where('created_at', '>=', $this->trackRecord->ranse_start_at)
             ->where('created_at', '<=', $this->trackRecord->ranse_end_at)
-            ->where('id', '<', $this->orderId)
+            ->where('id', '<', $this->order->id)
             ->count() == 0;
         if($isFirstCharge) {
             $this->result['need_report'] = true;
@@ -286,12 +299,17 @@ class JuliangAccountReportChargeService
         $reportResult = $service->reportCharge((object)[
             'callback' => $this->trackRecord->callback
         ]);
-        $reportContent = \json_decode($reportResult->content, true);
+        $reportContent = \json_decode($reportResult['content'], true);
         if($reportResult['result']  && 0 == $reportContent['code']) {
             $this->result['report_success'] = true;
         } else {
             $this->result['report_success'] = false;
         }
+
+        /**
+         * 要删除
+         */
+        $this->result['report_success'] = (bool)rand(0,1);
         $this->result['report_result'] = $reportResult;
         $this->result['need_report'] = false;
     }

+ 1 - 1
app/Service/Callback/Tiktok/TiktokEventReportService.php

@@ -49,7 +49,7 @@ class TiktokEventReportService
     /**
      * 付费上报
      */
-    public function reportCharge(DouyinUser $user, $amount)
+    public function reportCharge($user, $amount=0)
     {
         return $this->report($this->report_url, [
             'event_type' => $this->charge_event_type,

+ 31 - 13
tests/Jobs/Callback/ReportChargeTest.php

@@ -12,30 +12,48 @@ class ReportChargeTest extends \Tests\TestCase
 
     public function testHandle()
     {
-//        $this->prepareData();
-        $traceContext = new TraceContext();
-        $reportCharge = new ReportCharge(['uid' => 1000, 'orderId' => 10001,
-            'traceInfo' => $traceContext->getTraceInfo()]);
-        $reportCharge->handle();
+        $this->prepareData();
+        foreach (range(1, 150) as $i) {
+            $traceContext = new TraceContext();
+            $reportCharge = new ReportCharge(['uid' => 1000 + $i, 'orderId' => 10000 + $i,
+                'traceInfo' => $traceContext->getTraceInfo()]);
+            $reportCharge->handle();
+        }
     }
 
     private function prepareData() {
         $promotionId = 100;
         $optimizerId = 5;
-        $orders = [
-            [
-                'id' => 10000,
-                'uid' => 1000,
+        foreach(range(1, 150) as $i) {
+            $orders[] = [
+                'id' => 10000 + $i,
+                'uid' => 1000 + $i,
                 'promotion_id' => $promotionId,
                 'user_id' => $optimizerId,
-                'price' => rand(1, 1000),
+                'price' => rand(40, 70),
                 'pay_product_id' => 1,
                 'status' => 'PAID',
                 'trade_no' => uniqid(),
-                'created_at' => '2023-05-19 10:00:01',
+                'created_at' => '2023-05-19 14:44:01',
                 'updated_at' => date('Y-m-d H:i:s'),
-            ]
-        ];
+            ];
+            $ranses[] = [
+                'uid' => 1000 + $i,
+                'ranse_id' => 100,
+                'callback' => uniqid(),
+                'ranse_start_at' => '2023-05-19 14:44:01',
+                'ranse_end_at' => '2023-05-19 15:44:01',
+                'advertiser_id' => '1234568',
+                'adv_promotion_id' => '234',
+                'report_result' => '[]',
+                'created_at' => date('Y-m-d H:i:s'),
+                'updated_at' => date('Y-m-d H:i:s'),
+            ];
+        }
+        DB::table('orders')->where('id', '>=', 10001)->delete();
         DB::table('orders')->insert($orders);
+        DB::table('callback_report_ranse_record')->delete();
+        DB::table('callback_report_charge_record')->delete();
+        DB::table('callback_report_ranse_record')->insert($ranses);
     }
 }