No Description

RoiTeamTotal.php 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 RoiTeamTotal extends Command {
  8. protected $signature = 'RoiTeamTotal';
  9. /**
  10. * The console command description.
  11. *
  12. * @var string
  13. */
  14. protected $description = '团队汇总roi';
  15. public function handle()
  16. {
  17. $this->RoiTeamTotal();
  18. }
  19. public function RoiTeamTotal(){
  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. //获取各个团队销售id
  28. $salerIds = $this->getTeamSalers();
  29. for($i=1;$i<6;$i++){
  30. $stime = date('Y-m-d', strtotime($timeArr[$i]));
  31. $etime = $date = date('Y-m-d');
  32. //计算date i天内的roi
  33. $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, team_id'))->whereIn('team_id', [1,3,5])->where('is_del',0)->where('dtime', $stime)->groupBy('team_id')->get();
  34. $result = json_decode(json_encode($result), true);
  35. foreach($result as $k=>$v){
  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. #当日加粉n日订单总计:
  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)->where('team_id', $v['team_id'])->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)->where('team_id', $v['team_id'])->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. //销售
  56. $saler_ids = $salerIds[$v['team_id']];
  57. $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
  58. $data['gzh_count'] = $v['total_fan_add'];
  59. $data['cost'] = $v['total_cost'];
  60. $data['team_id'] = $v['team_id'];
  61. //插入数据
  62. $res = $this->insertData($data);
  63. }
  64. }
  65. }
  66. /**
  67. * @param idate:统计日期 rate:成单率 date:昨日
  68. *
  69. */
  70. public function insertData($data){
  71. return DB::table('roi_team_total')->insert($data);
  72. }
  73. public function getTeamSalers(){
  74. $data = array();
  75. $data[1] = DB::table('admin')->where('team_id', 1)->lists('id');
  76. $data[3] = DB::table('admin')->where('team_id', 3)->lists('id');
  77. $data[5] = DB::table('admin')->where('team_id', 5)->lists('id');
  78. return $data;
  79. }
  80. }