Nav apraksta

DisRoiByDay60.php 3.1KB

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