No Description

RoiTeamTotalHistory.php 4.0KB

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