Nessuna descrizione

DisRoiByDay15.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\AdCost;
  6. class DisRoiByDay15 extends Command {
  7. protected $signature = 'DisRoiByDay15';
  8. /**
  9. * The console command description.
  10. *
  11. * @var string
  12. */
  13. protected $description = '地域15日roi';
  14. public function handle()
  15. {
  16. $this->DisRoiByDay15();
  17. }
  18. public function DisRoiByDay15(){
  19. $stime = date('Y-m-d', strtotime('-15 day'));
  20. $etime = $date = date('Y-m-d');
  21. //计算date 15天内的roi
  22. $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where('ad_time', $stime)->groupBy('city')->get();
  23. $result = json_decode(json_encode($result), true);
  24. foreach($result as $k=>&$v){
  25. $data = array();
  26. $data['ad_time'] = $stime;
  27. $data['city'] = $v['city'];
  28. //新粉收入
  29. //当日加粉
  30. $city_name = str_replace('市', '', $v['city']);
  31. $phones = DB::table('customers')->where('fanTime', $stime)->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  32. #当日加粉15日订单总计:
  33. $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();
  34. #当日新粉成单:
  35. $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();
  36. // 1.当日新粉成单数
  37. $data['new_fan_order_count'] = $new_order->order_count;
  38. // 2.当日新粉成交额
  39. $data['new_fan_order_amount'] = $new_order->order_amount;
  40. // 3.当日粉丝总成交额
  41. $data['order_amount'] = $order->order_amount;
  42. // 15日总单数
  43. $data['order_count'] = $order->order_count;
  44. //新粉roi
  45. //$v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  46. //15日累计roi
  47. //$v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  48. $data['fan_count'] = count($phones);
  49. $data['gzh_count'] = $v['conversion_times'];
  50. $data['cost'] = $v['total_cost'];
  51. //插入数据
  52. $res = $this->insertData($data);
  53. }
  54. }
  55. /**
  56. * @param idate:统计日期 rate:成单率 date:昨日
  57. *
  58. */
  59. public function insertData($data){
  60. return DB::table('district_roi_15')->insert($data);
  61. }
  62. }