DisRoiByDay30History(); } public function DisRoiByDay30History(){ for($i=36; $i>=30; $i--){ $stime = date('Y-m-d', strtotime('-'.$i.' day')); $e = $i-30; $etime = $date = date('Y-m-d', strtotime('-'.$e.' day')); //计算date 7天内的roi $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where('ad_time', $stime)->groupBy('city')->get(); $result = json_decode(json_encode($result), true); foreach($result as $k=>&$v){ $data = array(); $data['ad_time'] = $stime; $data['city'] = $v['city']; //新粉收入 //当日加粉 $city_name = str_replace('市', '', $v['city']); $phones = DB::table('customers')->where('fanTime', $stime)->where('receiverCity','like', '%'.$city_name.'%')->lists('phone'); #当日加粉7日订单总计: $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('createTime', '>=', $stime)->where('createTime', '<', $etime)->where('is_del', 0)->first(); #当日新粉成单: $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $stime)->where('createTime','<=', $stime.' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first(); // 1.当日新粉成单数 $data['new_fan_order_count'] = $new_order->order_count; // 2.当日新粉成交额 $data['new_fan_order_amount'] = $new_order->order_amount; // 3.当日粉丝总成交额 $data['order_amount'] = $order->order_amount; // 7日总单数 $data['order_count'] = $order->order_count; //新粉roi //$v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : ''; //7日累计roi //$v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : ''; $data['fan_count'] = count($phones); $data['gzh_count'] = $v['conversion_times']; $data['cost'] = $v['total_cost']; //插入数据 $res = $this->insertData($data); } } } /** * @param idate:统计日期 rate:成单率 date:昨日 * */ public function insertData($data){ return DB::table('district_roi_30')->insert($data); } }