Nav apraksta

RoiTotal.php 2.9KB

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