No Description

RoiTeamTotal.php 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\CustTotal;
  6. use App\CustDetail;
  7. use App\Admin;
  8. class RoiTeamTotal extends Command {
  9. protected $signature = 'RoiTeamTotal';
  10. /**
  11. * The console command description.
  12. *
  13. * @var string
  14. */
  15. protected $description = '团队汇总roi';
  16. public function handle()
  17. {
  18. $this->RoiTeamTotal();
  19. }
  20. public function RoiTeamTotal(){
  21. $timeArr = array(
  22. 1 => '-7 day',
  23. 2 => '-15 day',
  24. 3 => '-30 day',
  25. 4 => '-45 day',
  26. 5 => '-60 day',
  27. 6 => '-90 day',
  28. );
  29. //获取各个团队销售id
  30. $team_ids = Admin::getTeams();
  31. $salerIds = $this->getTeamSalers($team_ids);
  32. for($i=1;$i<6;$i++){
  33. $stime = date('Y-m-d', strtotime($timeArr[$i]));
  34. $etime = $date = date('Y-m-d');
  35. //计算date i天内的roi
  36. $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, team_id'))->whereIn('team_id', $team_ids)->where('is_del',0)->where('dtime', $stime)->groupBy('team_id')->get();
  37. $result = json_decode(json_encode($result), true);
  38. foreach($result as $k=>$v){
  39. $data = array();
  40. $data['ad_time'] = $stime;
  41. $data['type'] = $i;
  42. //新粉收入
  43. //当日加粉
  44. $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
  45. #当日加粉n日订单总计:
  46. $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();
  47. #当日新粉成单:
  48. $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();
  49. // 1.当日新粉成单数
  50. $data['new_fan_order_count'] = $new_order->order_count;
  51. // 2.当日新粉成交额
  52. $data['new_fan_order_amount'] = $new_order->order_amount;
  53. // 3.当日粉丝总成交额
  54. $data['order_amount'] = $order->order_amount;
  55. // 15日总单数
  56. $data['order_count'] = $order->order_count;
  57. $data['cust_count'] = $order->cust_count;
  58. //销售
  59. $saler_ids = $salerIds[$v['team_id']];
  60. $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
  61. $data['gzh_count'] = $v['total_fan_add'];
  62. $data['cost'] = $v['total_cost'];
  63. $data['team_id'] = $v['team_id'];
  64. //插入数据
  65. $res = $this->insertData($data);
  66. }
  67. }
  68. }
  69. /**
  70. * @param idate:统计日期 rate:成单率 date:昨日
  71. *
  72. */
  73. public function insertData($data){
  74. return DB::table('roi_team_total')->insert($data);
  75. }
  76. public function getTeamSalers($team_ids){
  77. $data = array();
  78. foreach($team_ids as $team_id){
  79. $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
  80. }
  81. return $data;
  82. }
  83. }