Geen omschrijving

RoiTotalHistory.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\CustTotal;
  6. use App\CustDetail;
  7. class RoiTotalHistory extends Command {
  8. protected $signature = 'RoiTotalHistory';
  9. /**
  10. * The console command description.
  11. *
  12. * @var string
  13. */
  14. protected $description = 'Roi汇总历史';
  15. public function handle()
  16. {
  17. $this->RoiTotalHistory();
  18. }
  19. public function RoiTotalHistory(){
  20. $timeArr = array(
  21. 1 => '7',
  22. 2 => '15',
  23. 3 => '30',
  24. 4 => '45',
  25. 5 => '60',
  26. );
  27. for($i=1;$i<6;$i++){
  28. $j_min = $timeArr[$i];
  29. for($j=64; $j>=$j_min; $j--){
  30. $stime = date('Y-m-d', strtotime('-'.$j.' day'));
  31. $e = $j-$j_min;
  32. $etime = $date = date('Y-m-d', strtotime('-'.$e.' day'));
  33. //计算date i天内的roi
  34. $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add'))->where('is_del',0)->where('dtime', $stime)->first();
  35. $result = json_decode(json_encode($result), true);
  36. $data = array();
  37. $data['ad_time'] = $stime;
  38. $data['type'] = $i;
  39. //新粉收入
  40. //当日加粉
  41. $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
  42. #当日加粉15日订单总计:
  43. $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, count(distinct(receiverMobile)) as cust_count'))->whereIn('receiverMobile', $phones)->where('createTime', '>=', $stime)->where('createTime', '<', $etime)->where('is_del', 0)->first();
  44. #当日新粉成单:
  45. $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();
  46. // 1.当日新粉成单数
  47. $data['new_fan_order_count'] = $new_order->order_count;
  48. // 2.当日新粉成交额
  49. $data['new_fan_order_amount'] = $new_order->order_amount;
  50. // 3.当日粉丝总成交额
  51. $data['order_amount'] = $order->order_amount;
  52. // 15日总单数
  53. $data['order_count'] = $order->order_count;
  54. $data['cust_count'] = $order->cust_count;
  55. $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->sum('fan_add');
  56. $data['gzh_count'] = $result['total_fan_add'];
  57. $data['cost'] = $result['total_cost'];
  58. //插入数据
  59. $res = $this->insertData($data);
  60. }
  61. }
  62. }
  63. /**
  64. * @param idate:统计日期 rate:成单率 date:昨日
  65. *
  66. */
  67. public function insertData($data){
  68. return DB::table('roi_total')->insert($data);
  69. }
  70. }