Parcourir la source

更新回传比例

liuzejian il y a 1 an
Parent
commit
c1f30195bb

+ 28 - 57
app/Service/Callback/JLEventReportChargeService.php

@@ -21,7 +21,6 @@ class JLEventReportChargeService
     private $trackRecord;
     private $callbackConfig;
     private $promotion;
-    private $rateConfig;
     private $configLog;
     public function __construct($uid, $order, $traceInfo)
     {
@@ -164,60 +163,26 @@ class JLEventReportChargeService
                     }
                 }
 
-
+                $newConfigLog = DB::table('jl_event_rate_config_log')
+                    ->where(['id' => $this->configLog->id])
+                    ->select('report_count', 'unreport_count')
+                    ->first();
+                if(($newConfigLog->report_count ?? 0 ) + ($newConfigLog->unreport_count ?? 0 )) {
+                    $currentRate = bcdiv(($newConfigLog->report_count ?? 0 ) * 100,
+                        (($newConfigLog->report_count ?? 0 ) + ($newConfigLog->unreport_count ?? 0 )), 2);
+                } else {
+                    $currentRate = 0;
+                }
+                DB::table('callback_report_charge_record')->where([
+                    'order_no' => $this->order->trade_no
+                ])->update([
+                    'current_rate' => $currentRate,
+                ]);
             }
         }
     }
 
     /**
-     * 判断回传比例
-     */
-    private function judgeCallbackRate(){
-        if(!$this->result['need_report']) {
-            return;
-        }
-        if(!$this->result['continue_judge']) {
-            return;
-        }
-
-        $this->rateConfig = DB::table('juliang_account_rate_config_log')
-            ->where(['company_uid' => $this->promotion->uid,
-                'account_id' => $this->trackRecord->advertiser_id, 'is_enabled' => 1])
-            ->first();
-        if(!$this->rateConfig) {
-            $this->result['need_report'] = false;
-            $this->result['info_type'] = 'no_rate_config';
-            $this->result['info_str'] = '没有可用的回传比率设置';
-            return;
-        }
-        $configRate = $this->rateConfig->config_per;
-        if(0 == $configRate) {
-            $this->result['need_report'] = false;
-            $this->result['info_type'] = 'rate_eq_0';
-            $this->result['info_str'] = '设定比例为0不执行回传';
-            return;
-        }
-        $currentTotalCount = $this->rateConfig->total_count;
-        $currentReportCount = $this->rateConfig->report_count;
-        if(0 == $currentTotalCount) {
-            $currentRate = 0;
-        } else {
-            $currentRate = min(1, round($currentReportCount / $currentTotalCount, 4)) * 100;
-        }
-        if($currentRate <= $configRate) {
-            $this->result['need_report'] = true;
-            $this->result['config_rate'] = $configRate;
-            $this->result['current_rate'] = $currentRate;
-        } else {
-            $this->result['need_report'] = false;
-            $this->result['info_type'] = 'rate_filter';
-            $this->result['info_str'] = '比率过滤';
-            $this->result['config_rate'] = $configRate;
-            $this->result['current_rate'] = $currentRate;
-        }
-    }
-
-    /**
      * 判断订单是不是已经处理过了.如果处理过了,就不入库
      */
     private function judgeOrderAlreadyDeal() {
@@ -274,12 +239,11 @@ class JLEventReportChargeService
                 if($this->configLog->flag) {
                     $this->configLog->flag_report++;
                 } else {
-                    $this->configLog->flag_unport++;
+                    $this->configLog->flag_unreport++;
                     $this->result['continue_judge'] = false;
                     $this->result['need_report'] = false;
                     $this->result['info_type'] = 'rate_filter';
-                    $this->result['info_str'] = sprintf('当前配置传%s卡%s,本轮已传:%s,已卡%s', $sm[0], $sm[1],
-                    $this->configLog->flag_report, $this->configLog->flag_unport);
+                    $this->result['info_str'] = '比率过滤';
                 }
             }
         }
@@ -295,7 +259,7 @@ class JLEventReportChargeService
                 $obj->flag = 0;
             }
         } else {
-            if(($obj->unreport) < $m) {
+            if(($obj->flag_unreport) < $m) {
                 $obj->flag = 0;
             } else {
                 $obj->flag_unreport = 0;
@@ -359,9 +323,16 @@ class JLEventReportChargeService
             return;
         }
         $service = new TiktokEventReportService;
-        $reportResult = $service->reportCharge((object)[
-            'callback' => $this->trackRecord->callback
-        ]);
+        if('roi_report_all' == $this->result['info_type']) {
+            $reportResult = $service->roiReportCharge((object)[
+                'callback' => $this->trackRecord->callback
+            ]);
+        } else {
+            $reportResult = $service->reportCharge((object)[
+                'callback' => $this->trackRecord->callback
+            ]);
+        }
+
         $reportContent = \json_decode($reportResult['content'], true);
         if($reportResult['result']  && 0 == $reportContent['code']) {
             $this->result['report_success'] = true;

+ 22 - 0
app/Service/Callback/Tiktok/TiktokEventReportService.php

@@ -46,6 +46,18 @@ class TiktokEventReportService
 
     }
 
+    public function roiReportCharge($user) {
+        return $this->report($this->report_url, [
+            'event_type' => $this->purchase_roi_event_type,
+            'context' => [
+                'ad' => [
+                    'callback' => $user->callback
+                ]
+            ],
+            'timestamp' => time()
+        ]);
+    }
+
     /**
      * 付费上报
      */
@@ -120,6 +132,14 @@ class TiktokEventReportService
      */
     public function report(string $url, array $query_params)
     {
+
+        return [
+            'result' => rand(1, 5) > 3 ? true : false,
+            'content' => \json_encode(['code' => 0]),
+            'query_params' => $query_params,
+        ];
+
+
         $result = false;
         $content = '';
         try {
@@ -141,6 +161,8 @@ class TiktokEventReportService
             myLog('tiktok_event_report')->error('event_tiktok_ept:' . json_encode($query_params) . ' ept:' . $e->getMessage());
         } finally {
             $query_params['report_url'] = $url;
+
+
             return [
                 'result' => $result,
                 'content' => $content,

+ 62 - 62
tests/Jobs/Callback/ReportChargeTest.php

@@ -20,43 +20,43 @@ class ReportChargeTest extends \Tests\TestCase
             $reportCharge->handle();
         }
 
-        foreach (range(151, 163) as $i) {
-            $traceContext = new TraceContext();
-            $reportCharge = new ReportCharge(['uid' => 1000 + $i -150, 'orderId' => 10000 + $i,
-                'traceInfo' => $traceContext->getTraceInfo()]);
-            $reportCharge->handle();
-        }
-
-        foreach (range(164, 180) as $i) {
-            $traceContext = new TraceContext();
-            $reportCharge = new ReportCharge(['uid' => 1000 + $i, 'orderId' => 10000 + $i,
-                'traceInfo' => $traceContext->getTraceInfo()]);
-            $reportCharge->handle();
-        }
+//        foreach (range(151, 163) as $i) {
+//            $traceContext = new TraceContext();
+//            $reportCharge = new ReportCharge(['uid' => 1000 + $i -150, 'orderId' => 10000 + $i,
+//                'traceInfo' => $traceContext->getTraceInfo()]);
+//            $reportCharge->handle();
+//        }
+//
+//        foreach (range(164, 180) 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;
+        $promotionId = 5;
+        $optimizerId = 10;
         foreach(range(1, 150) as $i) {
             $orders[] = [
                 'id' => 10000 + $i,
                 'uid' => 1000 + $i,
                 'promotion_id' => $promotionId,
                 'user_id' => $optimizerId,
-                'price' => rand(10, 60),
+                'price' => rand(60, 70),
                 'pay_product_id' => 1,
                 'status' => 'PAID',
                 'trade_no' => uniqid(),
-                'created_at' => '2023-05-19 14:44:01',
+                'created_at' => '2023-07-04 13:'.(44 + rand(5, 10)),
                 'updated_at' => date('Y-m-d H:i:s'),
             ];
             $ranses[] = [
                 'uid' => 1000 + $i,
-                'ranse_id' => 100,
+                'ranse_id' => $promotionId,
                 'callback' => uniqid(),
-                'ranse_start_at' => '2023-05-19 14:44:01',
-                'ranse_end_at' => '2023-05-19 15:44:01',
+                'ranse_start_at' => '2023-07-04 13:44:01',
+                'ranse_end_at' => '2023-07-04 15:44:01',
                 'advertiser_id' => '1234568',
                 'adv_promotion_id' => '234',
                 'report_result' => '[]',
@@ -67,49 +67,49 @@ class ReportChargeTest extends \Tests\TestCase
             ];
         }
 
-        foreach (range(151, 163) as $i) {
-            $orders[] = [
-                'id' => 10000 + $i,
-                'uid' => 1000 + $i - 150,
-                'promotion_id' => $promotionId,
-                'user_id' => $optimizerId,
-                'price' => rand(40, 70),
-                'pay_product_id' => 1,
-                'status' => 'PAID',
-                'trade_no' => uniqid(),
-                'created_at' => '2023-05-19 14:'.(44 + rand(5, 10)),
-                'updated_at' => date('Y-m-d H:i:s'),
-            ];
-        }
+//        foreach (range(151, 163) as $i) {
+//            $orders[] = [
+//                'id' => 10000 + $i,
+//                'uid' => 1000 + $i - 150,
+//                'promotion_id' => $promotionId,
+//                'user_id' => $optimizerId,
+//                'price' => rand(40, 70),
+//                'pay_product_id' => 1,
+//                'status' => 'PAID',
+//                'trade_no' => uniqid(),
+//                'created_at' => '2023-07-04 13:'.(44 + rand(5, 10)),
+//                'updated_at' => date('Y-m-d H:i:s'),
+//            ];
+//        }
 
-        foreach(range(164, 180) as $i) {
-            $orders[] = [
-                'id' => 10000 + $i,
-                'uid' => 1000 + $i,
-                'promotion_id' => $promotionId,
-                'user_id' => $optimizerId,
-                'price' => rand(40, 70),
-                'pay_product_id' => 1,
-                'status' => 'PAID',
-                'trade_no' => uniqid(),
-                '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'),
-                'ranse_ip' =>'192.168.1.123',
-                'optimizer_uid' => 5
-            ];
-        }
+//        foreach(range(164, 180) as $i) {
+//            $orders[] = [
+//                'id' => 10000 + $i,
+//                'uid' => 1000 + $i,
+//                'promotion_id' => $promotionId,
+//                'user_id' => $optimizerId,
+//                'price' => rand(40, 70),
+//                'pay_product_id' => 1,
+//                'status' => 'PAID',
+//                'trade_no' => uniqid(),
+//                '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'),
+//                'ranse_ip' =>'192.168.1.123',
+//                'optimizer_uid' => 5
+//            ];
+//        }
         DB::table('orders')->where('id', '>=', 10001)->delete();
         DB::table('orders')->insert($orders);
         DB::table('callback_report_ranse_record')->delete();