12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\CustTotal;
- use App\CustDetail;
- use App\Admin;
- class RoiTeamTotal extends Command {
- protected $signature = 'RoiTeamTotal';
-
- protected $description = '团队汇总roi';
- public function handle()
- {
- $this->RoiTeamTotal();
- }
- public function RoiTeamTotal(){
- $timeArr = array(
- 1 => '-7 day',
- 2 => '-15 day',
- 3 => '-30 day',
- 4 => '-45 day',
- 5 => '-60 day',
- );
-
- $team_ids = Admin::getTeams();
- $salerIds = $this->getTeamSalers($team_ids);
- for($i=1;$i<6;$i++){
- $stime = date('Y-m-d', strtotime($timeArr[$i]));
- $etime = $date = date('Y-m-d');
-
- $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();
- $result = json_decode(json_encode($result), true);
-
- foreach($result as $k=>$v){
- $data = array();
- $data['ad_time'] = $stime;
- $data['type'] = $i;
-
-
- $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
-
- $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();
-
- $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();
-
-
- $data['new_fan_order_count'] = $new_order->order_count;
-
- $data['new_fan_order_amount'] = $new_order->order_amount;
-
-
- $data['order_amount'] = $order->order_amount;
-
- $data['order_count'] = $order->order_count;
- $data['cust_count'] = $order->cust_count;
-
-
- $saler_ids = $salerIds[$v['team_id']];
- $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
- $data['gzh_count'] = $v['total_fan_add'];
- $data['cost'] = $v['total_cost'];
- $data['team_id'] = $v['team_id'];
-
- $res = $this->insertData($data);
- }
- }
-
- }
-
- public function insertData($data){
- return DB::table('roi_team_total')->insert($data);
- }
- public function getTeamSalers($team_ids){
- $data = array();
- foreach($team_ids as $team_id){
- $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- return $data;
- }
- }
|