No Description

RoiTeamTotalHistory.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 RoiTeamTotalHistory extends Command {
  8. protected $signature = 'RoiTeamTotalHistory';
  9. /**
  10. * The console command description.
  11. *
  12. * @var string
  13. */
  14. protected $description = 'Roi汇总历史';
  15. public function handle()
  16. {
  17. $this->RoiTeamTotalHistory();
  18. }
  19. public function RoiTeamTotalHistory(){
  20. $timeArr = array(
  21. 1 => '7',
  22. 2 => '15',
  23. 3 => '30',
  24. 4 => '45',
  25. 5 => '60',
  26. );
  27. //获取各个团队销售id
  28. $salerIds = $this->getTeamSalers();
  29. for($i=1;$i<6;$i++){
  30. $j_min = $timeArr[$i];
  31. for($j=64; $j>=$j_min; $j--){
  32. $stime = date('Y-m-d', strtotime('-'.$j.' day'));
  33. $e = $j-$j_min;
  34. $etime = $date = date('Y-m-d', strtotime('-'.$e.' day'));
  35. //计算date i天内的roi
  36. $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add'))->whereIn('team_id', [1,3,5])->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. #当日加粉15日订单总计:
  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. /**
  71. * @param idate:统计日期 rate:成单率 date:昨日
  72. *
  73. */
  74. public function insertData($data){
  75. return DB::table('roi_team_total')->insert($data);
  76. }
  77. public function getTeamSalers(){
  78. $data = array();
  79. $data[1] = DB::table('admin')->where('team_id', 1)->lists('id');
  80. $data[3] = DB::table('admin')->where('team_id', 3)->lists('id');
  81. $data[5] = DB::table('admin')->where('team_id', 5)->lists('id');
  82. return $data;
  83. }
  84. }