No Description

DayGrandTeamTotalHistory.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Order;
  8. use App\Admin;
  9. class DayGrandTeamTotalHistory extends Command {
  10. protected $signature = 'DayGrandTeamTotalHistory';
  11. /**
  12. * The console command description.
  13. *
  14. * @var string
  15. */
  16. protected $description = '分团队每日汇总投放,成本,毛利等数据 历史处理';
  17. public function handle()
  18. {
  19. $this->dayGrandTeamTotal();
  20. }
  21. public function dayGrandTeamTotal(){
  22. $_start = '2019-09-04';
  23. for($i=140;$i>=1;$i--){
  24. $m = '-'.($i-1).' day';
  25. $n = '-'.$i.' day';
  26. $_end = date('Y-m-d', strtotime($m));
  27. $idate = date('Y-m-d', strtotime($n));
  28. //查看是否已有数据
  29. $if_e = DB::table('day_grand_team_total')->where('idate', $idate)->first();
  30. if(!empty($if_e)){
  31. echo "\n日期:".$idate." 已存在";
  32. return false;
  33. }
  34. $team_ids = Admin::getTeams();
  35. //获取各个团队销售id
  36. $salerIds = $this->getTeamSalers($team_ids);
  37. foreach($team_ids as $team_id){
  38. //获取团队销售
  39. $saler_ids = $salerIds[$team_id];
  40. $data = array();
  41. $data['idate'] = $idate;
  42. $data['team_id'] = $team_id;
  43. //总投入
  44. $data['throw_cost'] = CustTotal::where('is_del',0)->where('team_id',$team_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('total_cost');
  45. //订单信息
  46. $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('is_del',0)->where('team_id', $team_id)->where('createTime','<',$_end)->where('createTime','>=',$_start)->first();
  47. $data['goods_cost'] = $order->goods_cost;
  48. $data['freight_cost'] = $order->freight_cost;
  49. $data['aftersale_cost'] = $order->aftersale_cost;
  50. $data['refund_fee'] = $order->refund_fee;
  51. $data['order_count'] = $order->order_count;
  52. $data['order_amount'] = $order->order_amount;
  53. $data['cust_count'] = $order->cust_count;
  54. //加粉
  55. $data['fan_count'] = CustDetail::where('is_del',0)->whereIn('admin_id', $saler_ids)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
  56. //毛利 = 总销售额-总商品成本-总运费-总售后
  57. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  58. //总新粉单数
  59. $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.team_id', $team_id)->where('order.createTime','<',$_end)->where('order.createTime','>=',$_start)->first();
  60. $data['new_order_count'] = $new_order->order_count;
  61. //总老粉单数
  62. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  63. //总复购订单
  64. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  65. //7 - 60 roi汇总
  66. $s7 = $idate. ' -7 day';
  67. $date = date('Y-m-d', strtotime($s7));
  68. $data['cost7'] = DB::table('roi_team_total')->where('type', 1)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('cost');
  69. $data['order_count7'] = DB::table('roi_team_total')->where('type', 1)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_count');
  70. $data['order_amount7'] = DB::table('roi_team_total')->where('type', 1)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_amount');
  71. $s15 = $idate. ' -15 day';
  72. $date = date('Y-m-d', strtotime($s15));
  73. $data['cost15'] = DB::table('roi_team_total')->where('type', 2)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('cost');
  74. $data['order_count15'] = DB::table('roi_team_total')->where('type', 2)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_count');
  75. $data['order_amount15'] = DB::table('roi_team_total')->where('type', 2)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_amount');
  76. $s30 = $idate. ' -30 day';
  77. $date = date('Y-m-d', strtotime($s30));
  78. $data['cost30'] = DB::table('roi_team_total')->where('type', 3)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('cost');
  79. $data['order_count30'] = DB::table('roi_team_total')->where('type', 3)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_count');
  80. $data['order_amount30'] = DB::table('roi_team_total')->where('type', 3)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_amount');
  81. $s45 = $idate. ' -45 day';
  82. $date = date('Y-m-d', strtotime($s45));
  83. $data['cost45'] = DB::table('roi_team_total')->where('type', 4)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('cost');
  84. $data['order_count45'] = DB::table('roi_team_total')->where('type', 4)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_count');
  85. $data['order_amount45'] = DB::table('roi_team_total')->where('type', 4)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_amount');
  86. $s60 = $idate. ' -60 day';
  87. $date = date('Y-m-d', strtotime($s60));
  88. $data['cost60'] = DB::table('roi_team_total')->where('type', 5)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('cost');
  89. $data['order_count60'] = DB::table('roi_team_total')->where('type', 5)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_count');
  90. $data['order_amount60'] = DB::table('roi_team_total')->where('type', 5)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_amount');
  91. $s90 = $idate. ' -90 day';
  92. $date = date('Y-m-d', strtotime($s90));
  93. $data['cost90'] = DB::table('roi_team_total')->where('type', 6)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('cost');
  94. $data['order_count90'] = DB::table('roi_team_total')->where('type', 6)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_count');
  95. $data['order_amount90'] = DB::table('roi_team_total')->where('type', 6)->where('team_id', $team_id)->where('ad_time', '<=', $date)->sum('order_amount');
  96. $res = DB::table('day_grand_team_total')->insert($data);
  97. echo "\n日期:".$idate." 团队:".$team_id." 插入结果:".$res;
  98. }
  99. }
  100. }
  101. public function getTeamSalers($team_ids){
  102. $data = array();
  103. foreach($team_ids as $team_id){
  104. $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
  105. }
  106. return $data;
  107. }
  108. }