123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002 |
- <?php
- /**
- * Created by Sublime.
- * User: Williamslife Wang
- * Date: 17/10/18
- * Time: 上午11:20
- */
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Admin;
- use Illuminate\Http\Request;
- use App\Templates;
- use App\TemplatesSalers;
- use App\TemplatesSource;
- use App\TemplatesSourceSalers;
- use App\AdminRole;
- use App\TemplatesLog;
- use App\TemplatesBackimg;
- use App\CustDetail;
- use App\Order;
- use App\RedisModel;
- use App\Services\OssServices as oss;
- use Illuminate\Support\Facades\DB;
- class TemplateController extends Controller
- {
- /**
- * @return \Illuminate\View\View
- */
- public function index(Request $request)
- {
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Templates::where('is_del',0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Templates::where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- # 获取销售
- $salers = TemplatesSalers::select('templates_salers.*', 'admin.realname as admin_name')->leftJoin('admin', 'admin.id', '=', 'templates_salers.admin_id')->where('templates_salers.t_id', $v['id'])->where('templates_salers.is_del', 0)->where('admin.is_use', 1)->get();
- $v['salers'] = $salers;
- $v['salers_k'] = count($salers);
- }
- return view('template/index', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- ]);
- }
- /**
- * @return \Illuminate\View\View
- */
- public function create()
- {
- return view('template/create');
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function store(Request $request)
- {
- $this->validate($request, [
- 'url' => 'required|unique:templates',
- ], [
- 'url.required' => '模板链接不能为空',
- 'url.unique' => '该模板链接已存在',
-
- ]);
- $template = new Templates();
- $template->url = trim($request->input('url'));
- $template->note = trim($request->input('note'));
-
- //图片上传 阿里云oss
- if ($request->hasFile('img') && $request->file('img')->isValid()) {
- $file = $request->file('img');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $template->img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- $template->save();
- return redirect('/admin/template/index')->with('info', '添加模板成功');
- }
- /**
- * @param $id
- * @return \Illuminate\View\View
- */
- public function edit($id)
- {
- $template = Templates::findOrFail($id);
- return view('template/edit', ['template' => $template, 'id'=>$id]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function update(Request $request)
- {
- $id = (int)$request->input('id');
- $this->validate($request, [
- 'url' => 'required',
- ], [
- 'url.required' => '模板链接不能为空',
- 'url.unique' => '该模板链接已存在',
-
- ]);
- $template = Templates::findOrFail($id);
- $template->url = trim($request->input('url'));
- $template->note = trim($request->input('note'));
-
- //图片上传 阿里云oss
- if ($request->hasFile('img') && $request->file('img')->isValid()) {
- $file = $request->file('img');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $template->img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- $template->save();
- return redirect('/admin/template/index')->with('info', '修改模板成功');
- }
- /**
- * @param $id
- * @return \Illuminate\Http\RedirectResponse
- */
- public function assignSalers(Request $request)
- {
- $s_id = (int)$request->input('id');
- $salers = $request->input('salers');
- $assign_type = (int)$request->input('assign_type');
- if( empty($salers) ){
- // exit('<script>parent.layer.msg("分配失败,未选择销售!",{icon:2,time:2000}); var index=parent.layer.getFrameIndex(window.name); parent.layer.close(index);</script>');
- echo '<script>parent.layer.msg("分配失败,未选择销售!",{icon:2,time:2000}); var index=parent.layer.getFrameIndex(window.name); parent.layer.close(index);</script>';die;
- }
- if($assign_type == 1){
- //平均分配
- $count = count($salers);
- $weight_avg = round(1000 / $count) / 10;
- }else{
- $weights = array();
- $total = 0;
- foreach( $salers as $k=>$id ){
- $weight_id = 'weight'.$id;
- $weights[$k] = trim( $request->input($weight_id) );
- if(!$weights[$k]){
- $weights[$k] = 0;
- }
- $total += $weights[$k];
- }
- if($total>100){
- // exit('<script>parent.layer.msg("分配权重之和不能超过100!",{icon:2,time:2000}); var index=parent.layer.getFrameIndex(window.name); parent.layer.close(index);</script>');
- echo '<script>parent.layer.msg("分配权重之和不能超过100!",{icon:2,time:2000}); var index=parent.layer.getFrameIndex(window.name); parent.layer.close(index);</script>';die;
- }
- }
- //如果已经有 ,删除未选中
- TemplatesSourceSalers::where('s_id', $s_id)->whereNotIn('admin_id',$salers)->update(['is_del'=>1]);
- foreach( $salers as $k=>$admin_id ){
- if($assign_type == 1){
- $weight = $weight_avg;
- }else{
- $weight = $weights[$k];
- }
- TemplatesSourceSalers::updateOrCreate( ['s_id'=>$s_id, 'admin_id'=>$admin_id], ['is_del'=>0, 'weight'=>$weight] );
- }
- // exit('<script>parent.layer.msg("操作成功!",{icon:1,time:1000}); var index=parent.layer.getFrameIndex(window.name); parent.location.href="/admin/template/sourceindex";parent.layer.close(index);</script>');
- echo '<script>parent.layer.msg("操作成功!",{icon:1,time:1000}); var index=parent.layer.getFrameIndex(window.name); parent.location.href="/admin/template/sourceindex";parent.layer.close(index);</script>';die;
- }
- /**
- * 获取分配销售
- * @return \Illuminate\View\View
- */
- public function getAssigns(Request $request){
- $id = $request->input('id');
- #销售筛选
- //$saler_ids = DB::table('admin_role')->where('role_name', '销售')->lists('user_id');
- $adminList = Admin::select('id', 'realname', 'username', 'team_id')->where('id','>', 1)->where('is_use', 1)->where(function ($query) {
- $query->whereNotNull('qrcode')->where('qrcode', '<>', '');
- })->get();
- $adminList = json_decode(json_encode($adminList), true);
- $assign_admin = TemplatesSourceSalers::where('s_id', $id)->where('is_del', 0)->lists('weight', 'admin_id');
- $team_info = DB::table('teams')->lists('name', 'id');
- foreach($adminList as &$admin){
- if( isset($assign_admin[$admin['id']]) ){
- $admin['weight'] = $assign_admin[$admin['id']];
- }else{
- $admin['weight'] = '';
- }
- if(isset($team_info[$admin['team_id']])){
- $admin['team_name'] = $team_info[$admin['team_id']];
- }else{
- $admin['team_name'] = '';
- }
- }
- return view('template/assign', [
- 'adminlist' =>$adminList,
- 'id' =>$id
- ]);
- }
- /**
- * @return \Illuminate\View\View
- */
- public function logindex(Request $request)
- {
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = TemplatesLog::count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = TemplatesLog::orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- # 获取销售名字
- $v['admin_name'] = Admin::where('id', $v['admin_id'])->pluck('realname');
- }
- return view('template/logindex', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- ]);
- }
- /**
- * 销售引流模板分配日志报表
- */
- public function templateLogReport(Request $request){
- $admin_id = (int)$request->input('admin_id');
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = TemplatesLog::select('idate','admin_id')->where(function($query) use($admin_id, $stime, $etime, $sale_ids){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
- })->groupBy('idate')->groupBy('admin_id')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = TemplatesLog::select(DB::raw('idate,admin_id,count(1) as tcount'))->where(function($query) use($admin_id, $stime, $etime, $sale_ids){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
- })->groupBy('idate')->groupBy('admin_id')->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //获取销售当天加粉情况
- $custDetail = CustDetail::select('fan_add', 'new_reply', 'new_consult', 'old_consult', 'admin_name')->where('dtime', $v['idate'])->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- if(!empty($custDetail)){
- $v['fan_add'] = $custDetail->fan_add;
- $v['new_reply'] = $custDetail->new_reply;
- $v['old_consult'] = $custDetail->old_consult;
- $v['new_consult'] = $custDetail->new_consult;
- $v['admin_name'] = $custDetail->admin_name;
- }else{
- $v['fan_add'] = '';
- $v['new_reply'] = '';
- $v['old_consult'] = '';
- $v['new_consult'] = '';
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
- }
- //pv_count
- $pv_count = TemplatesLog::where('idate', $v['idate'])->where('admin_id', $v['admin_id'])->where('type', 1)->count();
- $v['pv_count'] = $pv_count;
- $v['long_count'] = $v['tcount'] - $v['pv_count'];
- //点击率
- $v['click_rate'] = $v['pv_count']>0 && $v['long_count']>0 ? round( $v['long_count'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('template/templateLogReport', ['result'=>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'self_role' =>$self_role,
- ]);
- }
- /**
- * 销售引流模板分配日志报表
- */
- public function templateLogReport_export(Request $request){
- $admin_id = (int)$request->input('admin_id');
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = TemplatesLog::select(DB::raw('idate,admin_id,count(1) as tcount'))->where(function($query) use($admin_id, $stime, $etime, $sale_ids){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
- })->groupBy('idate')->groupBy('admin_id')->orderBy('idate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //获取销售当天加粉情况
- $custDetail = CustDetail::select('fan_add', 'new_reply', 'new_consult', 'old_consult', 'admin_name')->where('dtime', $v['idate'])->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- if(!empty($custDetail)){
- $v['fan_add'] = $custDetail->fan_add;
- $v['new_reply'] = $custDetail->new_reply;
- $v['old_consult'] = $custDetail->old_consult;
- $v['new_consult'] = $custDetail->new_consult;
- $v['admin_name'] = $custDetail->admin_name;
- }else{
- $v['fan_add'] = '';
- $v['new_reply'] = '';
- $v['old_consult'] = '';
- $v['new_consult'] = '';
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
- }
- //pv_count
- $pv_count = TemplatesLog::where('idate', $v['idate'])->where('admin_id', $v['admin_id'])->where('type', 1)->count();
- $v['pv_count'] = $pv_count;
- $v['long_count'] = $v['tcount'] - $v['pv_count'];
- //点击率
- $v['click_rate'] = $v['pv_count']>0 && $v['long_count']>0 ? round( $v['long_count'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- }
- $indexKey = ['idate','admin_name','pv_count','long_count','fan_add','click_rate','change_rate','new_reply','new_consult','old_consult'];
- $title = ['日期', '销售名', 'PV量', '长按次数', '加粉数', '点击率', '转化率', '新粉回复数', '新粉询价数', '老粉询价数'];
- $filename = 'xiaoshoudaofen_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /*
- * @return \Illuminate\View\View
- */
- public function sourceindex(Request $request)
- {
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = TemplatesSource::where('is_del',0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = TemplatesSource::where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- # 获取销售
- $salers = TemplatesSourceSalers::select('templates_source_salers.*', 'admin.realname as admin_name')->leftJoin('admin', 'admin.id', '=', 'templates_source_salers.admin_id')->where('templates_source_salers.s_id', $v['id'])->where('templates_source_salers.is_del', 0)->where('admin.is_use', 1)->get();
- $v['salers'] = $salers;
- $v['salers_k'] = count($salers);
- # 获取模板图
- $v['img'] = Templates::where('id', $v['t_id'])->pluck('img');
- }
- return view('template/sourceindex', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- ]);
- }
- /**
- * @return \Illuminate\View\View
- */
- public function sourcecreate()
- {
- $templates = Templates::where('is_del', 0)->whereNotNull('url')->get();
- $back_imgs = TemplatesBackimg::where('is_del', 0)->whereNotNull('back_img')->where('t_id', 1)->lists('back_img');
- return view('template/sourcecreate', ['templates'=>$templates, 'back_imgs'=>$back_imgs]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function sourcestore(Request $request)
- {
- $t_id = intval($request->input('t_id'));
- #查次模板是否是html自定义
- $html_info = Templates::select('is_html')->where('id', $t_id)->first();
- $is_html = $html_info->is_html;
- $back_vali_str = '';
- if($t_id != 4 && $is_html != 1){
- $back_vali_str = 'required';
- }
- $this->validate($request, [
- 't_id' => 'required',
- 'back_img' => $back_vali_str,
- ], [
- 't_id.required' => '模板不能为空',
- 'back_img.required' => '背景图不能为空',
-
- ]);
- $template = new TemplatesSource();
- $template->t_id = $t_id;
- $template->note = trim($request->input('note'));
-
- $template->back_img = trim($request->input('back_img'));
- $template->is_html = $is_html;
- //图片上传 阿里云oss
- /*
- if ($request->hasFile('img') && $request->file('img')->isValid()) {
- $file = $request->file('img');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $template->back_img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- #保存背景图
- TemplatesBackimg::insert(['back_img'=>$template->back_img]);
- }
- */
- //新模板数据
- if($template->t_id == 4){
- $arr = array();
- $arr['title'] = trim($request->input('title'));
- $arr['text1'] = trim($request->input('text1'));
- $arr['text2'] = trim($request->input('text2'));
- $arr['text3'] = trim($request->input('text3'));
- $arr['img1'] = '';
- $arr['img2_1'] = '';
- $arr['img2_2'] = '';
- $arr['img3'] = '';
- if ($request->hasFile('img1') && $request->file('img1')->isValid()) {
- $file = $request->file('img1');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img1']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- if ($request->hasFile('img2_1') && $request->file('img2_1')->isValid()) {
- $file = $request->file('img2_1');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img2_1']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- if ($request->hasFile('img2_2') && $request->file('img2_2')->isValid()) {
- $file = $request->file('img2_2');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img2_2']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- if ($request->hasFile('img3') && $request->file('img3')->isValid()) {
- $file = $request->file('img3');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img3']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- $template->new_context = json_encode($arr);
- }
- if($template->save()){
- //生成连接
- $template->url = $this->createSourceUrl($template->t_id, $template->id);
- $template->save();
- }
- return redirect('/admin/template/sourceindex')->with('info', '添加模板成功');
- }
- /**
- * @param $id
- * @return \Illuminate\View\View
- */
- public function sourceedit($id)
- {
- $templates = Templates::where('is_del', 0)->whereNotNull('url')->get();
- $template = TemplatesSource::findOrFail($id);
- $back_imgs = TemplatesBackimg::where('is_del', 0)->whereNotNull('back_img')->where('t_id', $template->t_id)->lists('back_img');
- $new_context = array();
- if($template->t_id==4){
- $new_context = json_decode($template->new_context,true);
- }
- return view('template/sourceedit', ['template' => $template, 'id'=>$id, 'templates' => $templates, 'back_imgs'=>$back_imgs, 'new_context'=>$new_context]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function sourceupdate(Request $request)
- {
- $id = (int)$request->input('id');
-
- $template = TemplatesSource::findOrFail($id);
- $template->note = trim($request->input('note'));
- $template->back_img = trim($request->input('back_img'));
- //是否是新图文模板
- //新模板数据
- if($template->t_id == 4){
- $new_context = json_decode($template->new_context, true);
- $arr = $new_context;
- $arr['title'] = trim($request->input('title'));
- $arr['text1'] = trim($request->input('text1'));
- $arr['text2'] = trim($request->input('text2'));
- $arr['text3'] = trim($request->input('text3'));
- if ($request->hasFile('img1') && $request->file('img1')->isValid()) {
- $file = $request->file('img1');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img1']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- if ($request->hasFile('img2_1') && $request->file('img2_1')->isValid()) {
- $file = $request->file('img2_1');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img2_1']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- if ($request->hasFile('img2_2') && $request->file('img2_2')->isValid()) {
- $file = $request->file('img2_2');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img2_2']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- if ($request->hasFile('img3') && $request->file('img3')->isValid()) {
- $file = $request->file('img3');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $arr['img3']=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- $template->new_context = json_encode($arr);
- }
- $template->save();
- return redirect('/admin/template/sourceindex')->with('info', '修改成功');
- }
- public function createSourceUrl($t_id, $id){
- $url = Templates::where('id', $t_id)->pluck('url');
- if($url){
- return $url . '?id='.$id;
- }
- return false;
- }
- public function uploadBack(Request $request){
- $img = $request->input('img');
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img, $res)) {
- //获取图片类型
- $type = $res[2];
- // 临时文件
- $tmpfname = tempnam("/image/", "FOO");
- //保存图片
- $handle = fopen($tmpfname, "w");
- //阿里云oss上传的文件目录
- if (fwrite($handle, base64_decode(str_replace($res[1], '', $img))))
- {
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($type, $tmpfname, 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- fclose($handle);
- unlink($tmpfname);
- $img=$file['oss-request-url'];
- $back_img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- #保存背景图
- $t_id = (int)$request->input('t_id');
- $res = TemplatesBackimg::insert(['back_img'=>$back_img, 't_id'=>$t_id]);
- // exit($back_img);
- echo (string)$back_img;die;
- }
- // exit('0');
- echo '0';die;
- }
- }
- public function getTempback($t_id){
- $result = TemplatesBackimg::where('t_id', $t_id)->lists('back_img');
- // exit(json_encode($result));
- echo json_encode($result);die;
- }
- /**
- * 销售引流模板分配月报
- */
- public function templateLogMonthReport(Request $request){
- $admin_id = (int)$request->input('admin_id');
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $is_fx = env('IS_FX');
- $redisKey = "seafood.templateLogMonthReport".$is_fx.'_'.$team_id;
- if( empty($admin_id) && empty($stime) && empty($etime) ){
- $result = RedisModel::get($redisKey);
- if(!empty($result)){
- $result = json_decode($result, true);
- $count = count($result);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = array_slice($result, $offset, $pageSize);
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('template/templateLogMonthReport', ['result'=>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'self_role' =>$self_role,
- ]);
- }
- }
- $mstime = '';
- $metime = '';
- if($stime){
- $mstime = date('Y-m-1', strtotime($stime));
- }
- if($etime){
- $metime = date('Y-m-1', strtotime($etime.' +1 month'));
- }
- $_start = '2019-11-01';
- $count = TemplatesLog::select(DB::raw('mdate,admin_id'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($mstime) $query->where('create_time', '>=', $mstime);
- if($metime) $query->where('create_time', '<', $metime);
- if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
- })->groupBy('mdate')->groupBy('admin_id')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = TemplatesLog::select(DB::raw('mdate,admin_id,count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($mstime) $query->where('create_time', '>=', $mstime);
- if($metime) $query->where('create_time', '<', $metime);
- if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
- })->groupBy('mdate')->groupBy('admin_id')->orderBy('mdate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //起止时间
- $s_time = date('Y-m-1', strtotime($v['mdate']));
- $e_time = date('Y-m-1', strtotime($v['mdate'].' +1 month'));
- //获取销售当月加粉情况
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add'))->whereRaw('left(dtime,7)="'.$v['mdate'].'"')->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- if(!empty($custDetail)){
- $v['fan_add'] = $custDetail->fan_add;
- }else{
- $v['fan_add'] = '';
- }
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
- $v['long_count'] = $v['tcount'] - $v['pv_count'];
- //点击率
- //$v['click_rate'] = $v['pv_count']>0 && $v['long_count']>0 ? round( $v['long_count'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- //1.获取当月新粉
- $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['mdate'].'"')->lists('phone');
- $order = Order::select(DB::raw('count(1) as order_count, count(distinct(receiverMobile)) as cust_count, sum(receivedAmount) as order_amount'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->whereRaw('left(createTime,7)="'.$v['mdate'].'"')->where('is_del', 0)->first();
- //当月新粉下单单数
- $v['order_count'] = $order->order_count;
- //当月新粉下单人数
- $v['cust_count'] = $order->cust_count;
- //当月粉丝复购单数
- $v['fugou_order_count'] = $order->order_count - $order->cust_count;
- //当月粉丝总销售额
- $v['order_amount'] = $order->order_amount;
- //新粉下单率
- $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
- //新粉复购率
- //复购用户数 = 下单超过一单的用户数
- $order_cust_fugou = Order::select(DB::raw('count(1) as counts,receiverMobile'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->where('is_del', 0)->whereRaw('left(createTime,7)="'.$v['mdate'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
- $customer_fugou_count = count($order_cust_fugou);
- $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
- //新粉订单复购率
- $v['order_fugou_rate'] = $order->cust_count>0 ? round($v['fugou_order_count'] / $order->cust_count, 4) * 100 . '%' : '';
- //当月粉丝人均销售额
- $v['avg_amount'] = $v['fan_add']>0 ? round($order->order_amount / $v['fan_add'], 2) : '';
- }
- $sort_c = array_column($result, 'avg_amount');
- array_multisort($sort_c,SORT_DESC,$result);
- if( empty($admin_id) && empty($stime) && empty($etime) ){
- RedisModel::set($redisKey, json_encode($result));
- RedisModel::expire($redisKey, 36000);
- }
-
- $result = array_slice($result, $offset, $pageSize);
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('template/templateLogMonthReport', ['result'=>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'self_role' =>$self_role,
- ]);
- }
- /**
- * 销售引流模板分配月报
- */
- public function templateLogMonthReport_export(Request $request){
- $admin_id = (int)$request->input('admin_id');
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $is_fx = env('IS_FX');
- $redisKey = "seafood.templateLogMonthReport".$is_fx.'_'.$team_id;
- if( empty($admin_id) && empty($stime) && empty($etime) ){
- $result = RedisModel::get($redisKey);
- if(!empty($result)){
- $result = json_decode($result, true);
- $indexKey = ['mdate','admin_name','pv_count','long_count','fan_add','cust_count','order_count','fugou_order_count','order_amount','change_rate','new_order_rate','cust_fugou_rate','order_fugou_rate','avg_amount'];
- $title = ['月份', '销售名', '当月PV量', '当月长按次数', '当月上报加粉数', '当月新粉下单人数', '新粉下单单数', '当月粉丝复购单数', '当月粉丝总销售额', '粉丝转化率', '新粉下单率', '新粉复购率', '新粉订单复购率', '当月粉丝人均销售额'];
- $filename = 'daofenyuebao_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- }
- $mstime = '';
- $metime = '';
- if($stime){
- $mstime = date('Y-m-1', strtotime($stime));
- }
- if($etime){
- $metime = date('Y-m-1', strtotime($etime.' +1 month'));
- }
- $_start = '2019-11-01';
- $result = TemplatesLog::select(DB::raw('mdate,admin_id,count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
- if($admin_id>0) $query->where('admin_id', $admin_id);
- if($mstime) $query->where('create_time', '>=', $mstime);
- if($metime) $query->where('create_time', '<', $metime);
- if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
- })->groupBy('mdate')->groupBy('admin_id')->orderBy('mdate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //获取销售当月加粉情况
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add'))->whereRaw('left(dtime,7)="'.$v['mdate'].'"')->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- if(!empty($custDetail)){
- $v['fan_add'] = $custDetail->fan_add;
- }else{
- $v['fan_add'] = '';
- }
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
- $v['long_count'] = $v['tcount'] - $v['pv_count'];
- //点击率
- //$v['click_rate'] = $v['pv_count']>0 && $v['long_count']>0 ? round( $v['long_count'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
- //1.获取当月新粉
- $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['mdate'].'"')->lists('phone');
- $order = Order::select(DB::raw('count(1) as order_count, count(distinct(receiverMobile)) as cust_count, sum(receivedAmount) as order_amount'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->whereRaw('left(createTime,7)="'.$v['mdate'].'"')->where('is_del', 0)->first();
- //当月新粉下单单数
- $v['order_count'] = $order->order_count;
- //当月新粉下单人数
- $v['cust_count'] = $order->cust_count;
- //当月粉丝复购单数
- $v['fugou_order_count'] = $order->order_count - $order->cust_count;
- //当月粉丝总销售额
- $v['order_amount'] = $order->order_amount;
- //新粉下单率
- $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
- //新粉复购率
- //复购用户数 = 下单超过一单的用户数
- $order_cust_fugou = Order::select(DB::raw('count(1) as counts,receiverMobile'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->where('is_del', 0)->whereRaw('left(createTime,7)="'.$v['mdate'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
- $customer_fugou_count = count($order_cust_fugou);
- $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
- //新粉订单复购率
- $v['order_fugou_rate'] = $order->cust_count>0 ? round($v['fugou_order_count'] / $order->cust_count, 4) * 100 . '%' : '';
- //当月粉丝人均销售额
- $v['avg_amount'] = $v['fan_add']>0 ? round($order->order_amount / $v['fan_add'], 2) : '';
- }
- $sort_c = array_column($result, 'avg_amount');
- array_multisort($sort_c,SORT_DESC,$result);
- if( empty($admin_id) && empty($stime) && empty($etime) ){
- RedisModel::set($redisKey, json_encode($result));
- RedisModel::expire($redisKey, 36000);
- }
- $indexKey = ['mdate','admin_name','pv_count','long_count','fan_add','cust_count','order_count','fugou_order_count','order_amount','change_rate','new_order_rate','cust_fugou_rate','order_fugou_rate','avg_amount'];
- $title = ['月份', '销售名', '当月PV量', '当月长按次数', '当月上报加粉数', '当月新粉下单人数', '新粉下单单数', '当月粉丝复购单数', '当月粉丝总销售额', '粉丝转化率', '新粉下单率', '新粉复购率', '新粉订单复购率', '当月粉丝人均销售额'];
- $filename = 'daofenyuebao_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- }
|