Nenhuma Descrição

RoiSalerTotalHistory.php 4.7KB

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