暂无描述

RoiTeamTotal.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if(!empty($new_order)){
  47. // 1.当日新粉成单数
  48. $data['new_fan_order_count'] = $new_order->order_count;
  49. // 2.当日新粉成交额
  50. $data['new_fan_order_amount'] = $new_order->order_amount;
  51. }else{
  52. $data['new_fan_order_count'] = 0;
  53. $data['new_fan_order_amount'] = 0;
  54. }
  55. if(!empty($order)){
  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. }else{
  62. $data['order_amount'] = 0;
  63. $data['order_count'] = 0;
  64. $data['cust_count'] = 0;
  65. }
  66. //销售
  67. $saler_ids = $salerIds[$v['team_id']];
  68. $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
  69. $data['gzh_count'] = $v['total_fan_add'];
  70. $data['cost'] = $v['total_cost'];
  71. $data['team_id'] = $v['team_id'];
  72. //插入数据
  73. $res = $this->insertData($data);
  74. }
  75. }
  76. }
  77. /**
  78. * @param idate:统计日期 rate:成单率 date:昨日
  79. *
  80. */
  81. public function insertData($data){
  82. return DB::table('roi_team_total')->insert($data);
  83. }
  84. public function getTeamSalers(){
  85. $data = array();
  86. $data[1] = DB::table('admin')->where('team_id', 1)->lists('id');
  87. $data[3] = DB::table('admin')->where('team_id', 3)->lists('id');
  88. $data[5] = DB::table('admin')->where('team_id', 5)->lists('id');
  89. return $data;
  90. }
  91. }