Sin descripción

RoiSalerTotal.php 4.4KB

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