input('t_id'); if(!$t_id) $t_id = 1; //查询未离职销售 $_open_ids = Admin::where('is_use', 1)->lists('id'); //获取背景图 $back_img = TemplatesSource::where('id', $t_id)->pluck('back_img'); $result = TemplatesSourceSalers::where('s_id', $t_id)->where('is_del', 0)->where('weight','>',0)->whereIn('admin_id', $_open_ids)->orderBy('weight', 'asc')->lists('weight', 'admin_id'); if(empty($result)){ return self::returnValue([],1001); } $saler_id = null; $rand = 0; //获取最大随机值 $max = array_sum($result) * 10; //随机一个值 $rand_re = mt_rand(1, $max); foreach($result as $k=>$weight){ $rand += $weight * 10; if($rand_re <= $rand){ $saler_id = $k; break; } } $saler_ids = array_keys($result); $ip = $this->getRealIp(); if($saler_id){ //记录行为 $log = array(); $log['t_id'] = $t_id; $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url'); $log['admin_id'] = $saler_id; $log['ip'] = $ip; TemplatesLog::insert($log); } $qrcode = Admin::where('id', $saler_id)->pluck('qrcode'); $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode'); return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id, 'qrcodes'=>$qrcodes, 'back_img'=>$back_img]); } /** * 获取真实ip */ public function getRealIp() { $ip=false; if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $ip = $_SERVER["HTTP_CLIENT_IP"]; } if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']); if ($ip) { array_unshift($ips, $ip); $ip = FALSE; } for ($i = 0; $i < count($ips); $i++) { if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) { $ip = $ips[$i]; break; } } } return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); } /** * 长按用户存日志 */ public function addLongLog(Request $request){ $saler_id = $request->input('saler_id'); $t_id = $request->input('t_id'); $ip = $this->getRealIp(); if($saler_id && $t_id){ //记录行为 $log = array(); $log['t_id'] = $t_id; $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url'); $log['admin_id'] = $saler_id; $log['ip'] = $ip; $log['type'] = 2; //长按 TemplatesLog::insert($log); } return self::returnValue([]); } /** * 每日累计汇总数据 */ public function dayGrandTotal(Request $request){ $days = (int)$request->input('days'); if(!$days){ $days = 7; } $stime = date('Y-m-d', strtotime('-'.$days.' day')); $data = array(); $_start = '2019-09-04'; //今日数据实时计算 $idate = date('Y-m-d'); $data['idate'] = $idate; //总投入 $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost'); //订单信息 $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('createTime','>=',$_start)->where('is_del',0)->first(); $data['goods_cost'] = $order->goods_cost; $data['freight_cost'] = $order->freight_cost; $data['aftersale_cost'] = $order->aftersale_cost; $data['refund_fee'] = $order->refund_fee; $data['order_count'] = $order->order_count; $data['order_amount'] = $order->order_amount; $data['cust_count'] = $order->cust_count; //加粉 //$data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add'); //毛利 = 总销售额-总商品成本-总运费-总售后 $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee; //查询历史数据 $result = DB::table('day_grand_total')->where('idate', '>', $stime)->orderBy('idate', 'desc')->get(); $result = json_decode(json_encode($result), true); foreach($result as $k=>&$v){ unset($v['fan_count']); } $result = array_merge([$data], $result); exit(json_encode($result)); } }