ith5 2 месяцев назад
Родитель
Сommit
3ee3bae864

+ 11 - 0
app/v1/controller/customer/ReconciliationController.php

@@ -61,4 +61,15 @@ class ReconciliationController extends BaseController
     $result = $this->logic->getAdCost($params);
     return $this->success($result);
   }
+
+  // 广告KPI
+  public function getAdKpi(Request $request)
+  {
+    $params = $request->more([
+      ['auth_id', ''],
+      ['date', ''],
+    ]);
+    $result = $this->logic->getAdKpi($params);
+    return $this->success($result);
+  }
 }

+ 68 - 0
app/v1/logic/customer/ReconciliationLogic.php

@@ -207,4 +207,72 @@ class ReconciliationLogic extends BaseLogic
             'totalRow' => $totalRow
         ];
     }
+
+    /**
+     * 市场绩效
+     */
+    public function getAdKpi($params)
+    {
+        $date = $params['date'];
+        $params = $this->searchByAuth($params);
+
+        // 消耗支出,auth_id 分组
+        $costQuery = Db::connect('db_advert')
+            ->table('media_cost')
+            ->field('auth_id, SUM(money) as cost')
+            ->whereTime('tdate', 'between', $date)
+            ->group('auth_id');
+        if(!empty($params['auth_id'])){
+            $costQuery->where('media_cost.auth_id', 'in', implode(',', $params['auth_id']));
+        }
+        $cost = $costQuery->select()->toArray();
+        $costList = array_column($cost, 'cost', 'auth_id');
+
+
+
+     // 充值收入
+     $incomeQuery = Db::connect('db_game_log')
+            ->table('sdk_order_success')
+            ->field('auth_id, SUM(money) as income')
+            
+            ->whereTime('pay_time', 'between', $date)
+            ->group('auth_id');
+        if(!empty($params['auth_id'])){
+            $incomeQuery->where('sdk_order_success.auth_id', 'in', implode(',', $params['auth_id']));
+        }
+        $income = $incomeQuery->select()->toArray();
+        $incomeList = array_column($income, 'income', 'auth_id');
+
+
+        // 人员列表map
+        $authList = Db::connect('db_system')->table('sa_system_user')
+                    ->select()->toArray();
+        $authList = array_column($authList, 'username', 'id');
+
+        // 合并数据
+        $data = [];
+        $imcomeSum = 0;
+        $costSum = 0;
+        foreach($cost as $item){
+            $data[] = [
+                'auth_id' => $item['auth_id'],
+                'auth_name' => $authList[$item['auth_id']] ?? '官网',
+                'cost' => $costList[$item['auth_id']] ?? 0,
+                'income' => $incomeList[$item['auth_id']] ?? 0,
+            ];
+            $imcomeSum += $incomeList[$item['auth_id']] ?? 0;
+            $costSum += $costList[$item['auth_id']] ?? 0;
+        }
+
+        $totalRow = [
+            'auth_name' => '合计',
+            'cost' => $costSum,
+            'income' => $imcomeSum,
+        ];
+        return [
+            'data' => $data,
+            'totalRow' => $totalRow
+        ];
+
+    }
 }