Нет описания

RoiTeamTotal.php 3.7KB

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