input('t_id'); if(!$t_id) $t_id = 1; //查询未离职销售 $_open_ids = Admin::where('is_use', 1)->where('is_qrcode_use', 1)->lists('id'); //获取背景图 $back_img = TemplatesSource::where('id', $t_id)->pluck('back_img'); $new_context = TemplatesSource::where('id', $t_id)->pluck('new_context'); $new_data = array(); $title = ''; if(!empty($new_context)){ $new_context = json_decode($new_context, true); $title = $new_context['title']; $new_data[0]['text'] = $new_context['text1']; $new_data[0]['img'] = [$new_context['img1']]; $new_data[1]['text'] = $new_context['text2']; $new_data[1]['img'] = [$new_context['img2_1'], $new_context['img2_2']]; $new_data[2]['text'] = $new_context['text3']; $new_data[2]['img'] = [$new_context['img3']]; } $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, 'title'=>$title, 'new_context'=>$new_data]); } /** * 获取真实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; $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) : ''; //总新粉单数 $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.createTime','>=',$_start)->first(); $data['new_order_count'] = $new_order->order_count; //总老粉单数 $data['old_order_count'] = $data['order_count'] - $data['new_order_count']; //总复购订单 $data['fugou_order_count'] = $data['order_count'] - $data['cust_count']; //总复购率 $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : ''; //查询历史数据 $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){ $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) : ''; $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : ''; } $result = array_merge([$data], $result); exit(json_encode($result)); } }