12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\AdCost;
- class DisRoiByDay60 extends Command {
- protected $signature = 'DisRoiByDay60';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '地域60日roi';
- public function handle()
- {
- $this->DisRoiByDay60();
- }
- public function DisRoiByDay60(){
- $stime = date('Y-m-d', strtotime('-60 day'));
- $etime = $date = date('Y-m-d');
- //计算date 60天内的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();
- if(empty($result)){
- return false;
- }
- $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');
- #当日加粉60日订单总计:
- $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;
- // 60日总单数
- $data['order_count'] = $order->order_count;
- //新粉roi
- //$v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //60日累计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_60')->insert($data);
- }
- }
|