sunhao 5 years ago
parent
commit
32e7962f9a
1 changed files with 106 additions and 1 deletions
  1. 106 1
      app/Http/Controllers/Admin/StatisticsController.php

+ 106 - 1
app/Http/Controllers/Admin/StatisticsController.php

@@ -1482,7 +1482,7 @@ class StatisticsController extends Controller
1482 1482
         $ret = AdCost::select('id')->where(function($query) use($stime, $etime, $city){    
1483 1483
             if($stime) $query->where('ad_time', '>=', $stime);
1484 1484
             if($etime) $query->where('ad_time', '<=', $etime);
1485
-            if($city) $query->where('city', '=', $city);
1485
+            if($city) $query->where('city', 'like', '%'.$city.'%');
1486 1486
         })->groupBy('ad_time')->groupBy('city')->get();
1487 1487
         $ret = json_decode(json_encode($ret), true);
1488 1488
 
@@ -1651,6 +1651,111 @@ class StatisticsController extends Controller
1651 1651
         $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
1652 1652
         return Order::export_excel($result, $filename, $indexKey, $title);
1653 1653
     }
1654
+
1655
+    /**
1656
+     * 地区汇总roi
1657
+     */
1658
+    public function districtTotalRoi(Request $request){
1659
+        $page = (int)$request->input('page');
1660
+        $pageSize = 20;
1661
+        if($page<=0){
1662
+            $page = 1;
1663
+        }
1664
+
1665
+        $offset = ($page-1) * $pageSize;
1666
+        $city = $request->input('city');
1667
+
1668
+        if($city !== null){
1669
+            $city = str_replace('市', '', $city);
1670
+        }       
1671
+
1672
+        $ret = AdCost::select('id')->where(function($query) use($city){    
1673
+            if($city) $query->where('city', 'like', '%'.$city.'%');
1674
+        })->groupBy('city')->get();
1675
+        $ret = json_decode(json_encode($ret), true);
1676
+
1677
+        $count = count($ret);
1678
+        if ($count > 1) {
1679
+            // 总页数
1680
+            $pages = ceil($count/$pageSize);
1681
+        }else{
1682
+            // 总页数
1683
+            $pages = 1;
1684
+        }
1685
+
1686
+        $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where(function($query) use($city){    
1687
+            if($city) $query->where('city', 'like', '%'.$city.'%');
1688
+        })->groupBy('city')->offset($offset)->limit($pageSize)->get();
1689
+
1690
+        $result = json_decode(json_encode($result), true);
1691
+
1692
+        foreach($result as $k=>&$v){
1693
+            //新粉收入
1694
+            //加粉
1695
+            $city_name = str_replace('市', '', $v['city']);
1696
+            $phones = DB::table('customers')->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
1697
+            #订单总计:
1698
+            $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
1699
+            
1700
+            #当日新粉成单:            
1701
+            $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['ad_time'])->where('createTime','<=', $v['ad_time'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
1702
+            // 1.当日新粉成单数
1703
+            $v['new_order_count'] = $new_order->order_count;
1704
+            // 2.当日新粉成交额 
1705
+            $v['new_order_amount'] = $new_order->order_amount;
1706
+            // 3.当日粉丝总成交额
1707
+            $v['order_amount'] = $order->order_amount;
1708
+            // 总单数
1709
+            $v['order_count'] = $order->order_count;
1710
+            //新粉roi
1711
+            $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
1712
+            //累计roi
1713
+            $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
1714
+            $v['new_fan_count'] = count($phones);
1715
+
1716
+        }
1717
+
1718
+        //总计
1719
+        $total_data = [
1720
+            'cost' => 0,
1721
+            'fan_count' => 0,
1722
+            'order_count' => 0,
1723
+            'order_amount' => 0,
1724
+            'roi' => 0,
1725
+            'conversion_times' => 0,
1726
+        ];
1727
+
1728
+        //投入
1729
+        $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){    
1730
+            if($stime) $query->where('ad_time', '>=', $stime);
1731
+            if($etime) $query->where('ad_time', '<=', $etime);
1732
+            if($city) $query->where('city', 'like', '%'.$city.'%');
1733
+        })->first();
1734
+        $total_data['cost'] = $adcost->total_cost;
1735
+        $total_data['conversion_times'] = $adcost->conversion_times;
1736
+        //总新粉
1737
+        $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){    
1738
+            if($stime) $query->where('fanTime', '>=', $stime);
1739
+            if($etime) $query->where('fanTime', '<=', $etime);
1740
+            if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
1741
+        })->lists('phone');
1742
+        $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
1743
+        //总收入
1744
+        $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
1745
+        $total_data['order_count'] = $order->order_count;
1746
+        $total_data['order_amount'] = $order->order_amount;
1747
+        $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
1748
+
1749
+        return view('statistics/districtRoi', ['result' =>$result,
1750
+            'page'              =>$page,
1751
+            'count'             =>$count,
1752
+            'pages'             =>$pages,                          
1753
+            'stime'             =>$stime,
1754
+            'etime'             =>$etime,          
1755
+            'city'             =>$city,          
1756
+            'total_data'             =>$total_data,          
1757
+            ]);
1758
+    }
1654 1759
 }
1655 1760
 
1656 1761