input('has_pay', 1); $is_approved = $request->input('is_approved', 1); $options = [ 'has_pay'=>$has_pay, 'is_approved'=>$is_approved, ]; CrmScreenUser::dispatch($options)->onQueue('crm.screen.users'); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } //筛选后的用户列表 public function users(Request $request) { try { $keyword = $request->input('keyword'); $users = CrmUser::with('user:id,nickname,photo,app_avatar'); if ($keyword) { $keyword = trim($keyword); $users = $users->whereHas("user", function($sql) use($keyword){ $sql->where('nickname', 'like', '%'.$keyword.'%') ->orWhere('mobile', 'like', '%'.$keyword.'%'); }); } $users = $users->orderBy('id', 'desc')->paginate(); return $this->success('ok', $users); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } //数据统计-数字 public function dataStateNum(Request $request) { try { $start_date = $request->input('start_date'); $end_date = $request->input('end_date'); $end_date = $end_date?$end_date.' 23:59:59':null; $today = [date('Y-m-d'), date('Y-m-d 23:59:59')]; $yestoday = [date('Y-m-d', strtotime('-1 days')), date('Y-m-d 23:59:59', strtotime('-1 days'))]; //累计资源 $total_count = CrmUser::count(); //今日标记人数 $t_tag_count = CrmUserLog::whereBetween('tagged_at', $today)->count(); //昨日标记人数 $y_tag_count = CrmUserLog::whereBetween('tagged_at', $yestoday)->count(); //客服 $s_total_count = CrmUserLog::whereHas('role', function($sql){ $sql->where('type', 'c_service'); }); if ($start_date && $end_date) { $s_total_count = $s_total_count->whereBetween('created_at', [$start_date, $end_date]); } $s_total_count = $s_total_count->count(); $st_tag_count = CrmUserLog::whereHas('role', function($sql){ $sql->where('type', 'c_service'); })->whereBetween('tagged_at', $today)->count(); $sy_tag_count = CrmUserLog::whereHas('role', function($sql){ $sql->where('type', 'c_service'); })->whereBetween('tagged_at', $yestoday)->count(); //销售 $t_total_count = CrmUserLog::whereHas('role', function($sql){ $sql->where('type', 't_service'); }); if ($start_date && $end_date) { $t_total_count = $t_total_count->whereBetween('created_at', [$start_date, $end_date]); } $t_total_count = $t_total_count->count(); $tt_tag_count = CrmUserLog::whereHas('role', function($sql){ $sql->where('type', 't_service'); })->whereBetween('tagged_at', $today)->count(); $ty_tag_count = CrmUserLog::whereHas('role', function($sql){ $sql->where('type', 't_service'); })->whereBetween('tagged_at', $yestoday)->count(); //累计用户 $user_count = CrmUser::count(); return $this->success('ok', compact('total_count', 't_tag_count', 'y_tag_count', 's_total_count', 'st_tag_count','sy_tag_count','t_total_count','tt_tag_count','ty_tag_count','user_count')); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } //数据统计-柱状图 public function dataStateCol(Request $request) { try { $c_service = $this->getServices('c_service'); $t_service = $this->getServices('t_service'); return $this->success('ok', compact('c_service', 't_service')); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function getServices($type) { $start_date = request()->input('start_date'); $end_date = request()->input('end_date'); $end_date = $end_date?$end_date.' 23:59:59':null; $users = User::whereHas('crmRole', function($sql) use($type){ $sql->where('type', $type); })->select('id', 'nickname')->orderBy('id', 'desc')->get(); $name_arr = $gain_count_arr = $comment_count_arr = $tag_count_arr = []; foreach($users as $user) { $gain_count = CrmUserLog::whereHas('role', function($sql) use($type){ $sql->where('type', $type); })->where('role_user_id', $user->id); //备注数 $comment_count= ClientComment::whereHas('crmRole', function($sql) use($type){ $sql->where('type', $type); })->where('maker_user_id', $user->id)->where('type', 'crm'); //标记数 $tag_count = CrmUserLog::whereHas('role', function($sql) use($type){ $sql->where('type', $type); })->where('role_user_id', $user->id)->whereNotNull('tagged_at'); if ($start_date && $end_date) { $gain_count = $gain_count->whereBetween('created_at', [$start_date, $end_date]); $comment_count = $comment_count->whereBetween('created_at', [$start_date, $end_date]); $tag_count = $tag_count->whereBetween('tagged_at', [$start_date, $end_date]); } $gain_count_arr[] = $gain_count->count(); $comment_count_arr[] = $comment_count->count(); $tag_count_arr[] = $tag_count->count(); $name_arr[] = $user->nickname; } $c_service = [ 'name_arr'=>$name_arr, 'gain_count_arr'=>$gain_count_arr, 'comment_count_arr'=>$comment_count_arr, 'tag_count_arr'=>$tag_count_arr, ]; return $c_service; } public function dataStatePie(Request $request) { try { $a_grade = $this->getGrades('A'); $b_grade = $this->getGrades('B'); $c_grade = $this->getGrades('C'); $grades = ['A'=>$a_grade, 'B'=>$b_grade, 'C'=>$c_grade]; $m_sex = $this->getSexs(1); $f_sex = $this->getSexs(2); $o_sex = $this->getSexs(0); $sexs = ['male'=>$m_sex, 'female'=>$f_sex, 'other'=>$o_sex]; return $this->success('ok', compact('grades', 'sexs')); } catch (\Exception $e) { dd($e->getMessage()); $this->getError($e); return $this->failure(); } } public function getGrades($grade) { $g_start_date = request()->input('start_date'); $g_end_date = request()->input('end_date'); $g_end_date = $g_end_date?$g_end_date.' 23:59:59':null; //评级 $obj = CrmUser::whereNotNull("grade"); if ($g_start_date && $g_end_date) { $obj = $obj->whereBetween('graded_at', [$g_start_date, $g_end_date]); } $obj = $obj->where('grade', $grade)->count(); return $obj; } public function getSexs($sex) { //性别 $s_start_date = request()->input('start_date'); $s_end_date = request()->input('end_date'); $s_end_date = $s_end_date?$s_end_date.' 23:59:59':null; $obj = CrmUserLog::orderby('id', 'desc'); if ($s_start_date && $s_start_date) { $obj = $obj->whereBetween('created_at', [$s_start_date, $s_end_date]); } if ($sex == 1) { $obj = $obj->whereHas('user', function($sql) { $sql->where('sex', 1); })->count(); }elseif ($sex == 2) { $obj = $obj->whereHas('user', function($sql){ $sql->where('sex', 2); })->count(); }else { $obj = $obj->whereHas('user', function($sql){ $sql->whereNotIn('sex', [1,2]); })->count(); } return $obj; } //领取用户 public function gainUsers(Request $request) { \DB::beginTransaction(); try { $user = auth()->user(); $total = 30; $route_name = $request->route()->getName(); if ($route_name == 'teacher.gain.users') {//老师角色 $role = $user->crmTService; }elseif($route_name == 'service.gain.users') {//客服角色 $role = $user->crmCService; }else { return $this->failure("操作失败,权限不足"); } if (empty($role)) return $this->failure("操作失败,权限不足"); //当前有多少用户 $count = CrmUserLog::where('role_id', $role->id)->where('status', 1)->count(); if ($count >= $total) return $this->failure("操作失败,领取上限"); $d_value= $total - (int)$count; if ($d_value <= 0) $this->failure("领取失败,请先跟进当前用户"); if ($d_value > 30 || $d_value < 0) throw new \Exception("领取失败"); $crm_users = CrmUser::where('status', 0); if ($role->type == 'c_service') {//客服 $crm_users = $crm_users->where('step', 'default'); $step = "pre_sale"; }else {//情感老师 $crm_users = $crm_users->where('step', 'sale'); $step = 'sale'; } $users = $crm_users->whereHas('user', function ($sql){ $sql->NotHidden(); })->limit($d_value)->select('user_id')->get(); if (empty($users->count())) return $this->failure("领取失败,用户池中没有更多用户"); foreach($users as $user) { CrmUserLog::create(['user_id'=>$user->user_id, 'role_id'=>$role->id, 'role_user_id'=>$role->user_id, 'status'=>1]); //修改状态 CrmUser::where("user_id", $user->user_id)->update(['step'=>$step, 'status'=>1]); } \DB::commit(); $data = [ 'action'=>'gain', 'role_type'=>$role->type, 'maker_user_id'=>auth()->id(), ]; AddCrmRoleComment::dispatch($data)->onQueue('love'); return $this->success('ok'); } catch (\Exception $e) { \DB::rollback(); $this->getError($e); return $this->failure(); } } public function mobileUser(Request $request) { try { $role = auth()->user()->crmCService; if (!$role) return $this->failure("操作失败,权限不足"); $mobile = $request->input('mobile'); if (empty($mobile)) return $this->failure("请输入手机号"); $user = User::where('mobile', $mobile)->with('crmUser:id,user_id,step')->select('id', 'nickname','name', 'mobile', 'photo', 'app_avatar')->first(); if (empty($user)) return $this->failure("该手机号用户不存在"); if (empty($user->crmUser) || $user->crmUser->step == 'default') { $user->follower = $user->follower_id = ''; }elseif ($user->crmUser->step != 'default') { $follow_user = CrmUserLog::with('user:id,nickname')->where('user_id', $user->id)->orderBy('id', 'desc')->first(); if ($follow_user && $follow_user->roleUser) { $user->follower =$follow_user->roleUser->nickname?:'无名'; $user->follower_id = $follow_user->roleUser->id; }else{ $user->follower = $user->follower_id = ''; } } if (empty($user)) return $this->failure('未查询到用户'); return $this->success('ok', $user); }catch (\Exception $e){ $this->getError($e); return $this->failure(); } } /** * 根据手机号领取用户 * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View */ public function gainUser(Request $request, $user_id) { try { $role = auth()->user()->crmCService; if (!$role) return $this->failure("操作失败,权限不足"); $user = User::find($user_id); if (empty($user)) return $this->failure('未查询到用户'); $crm_user = CrmUser::firstOrCreate(['user_id'=>$user->id]); // if ($crm_user->step && $crm_user->step != 'default') return $this->failure("用户已被客服领取"); CrmUser::where('id', $crm_user->id)->update(['step'=>'pre_sale', 'status'=>1]); CrmUserLog::create(['user_id'=>$crm_user->user_id, 'role_id'=>$role->id, 'role_user_id'=>$role->user_id, 'status'=>1]); $data = [ 'action'=>'gain', 'maker_user_id'=>auth()->id(), 'role_type'=>$role->type, ]; AddCrmRoleComment::dispatch($data)->onQueue('love'); return $this->success("Ok", $crm_user); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } //服务用户列表 public function userLogs(Request $request, $user_id=null) { try { if ($user_id) { $user = User::find($user_id); }else { $user = auth()->user(); } $role = $user->crmCService; if (empty($role)) return $this->success('ok', []); $keyword = $request->input('keyword'); $users = CrmUserLog::withCount('comments')->with('user:id,nickname,photo,app_avatar,sex,name,belief', 'user.profileCourtship:user_id,province,city,state,birthday,degree', 'crmUser:user_id,grade,step'); if ($keyword) { $keyword = trim($keyword); $users = $users->whereHas("user", function($sql) use($keyword){ $sql->where('nickname', 'like', '%'.$keyword.'%') ->orWhere('name', 'like', '%'.$keyword.'%') ->orWhere('id', 'like', '%'.$keyword.'%') ->orWhere('mobile', 'like', '%'.$keyword.'%'); }); } $status = $request->input('status'); $users = $users->where(function($sql) use($role, $status) { if (is_numeric($status) && $status == 0) { $sql->where('role_id', $role->id)->where('status', 0)->whereNotNull('tagged_at')->whereHas('crmUser', function ($sql) { $sql->where("step", '<>', 'no_value'); }); } elseif (is_numeric($status) && $status == 2){ $sql->where('role_id', $role->id)->where('status', 0)->whereNotNull('tagged_at')->whereHas('crmUser', function ($sql) { $sql->where("step", 'no_value'); }); } else { $sql->where('role_id', $role->id)->where('status', 1); } }); $users = $users->orderBy('id', 'desc')->paginate(); foreach($users as $user) { $user->user->age = ($user->user->profileCourtship)?\CommonUtilsService::getAge($user->user->profileCourtship->birthday).'岁':null; $user->need_follow = ($user->crmUser->step != 'no_value')?1:0; $user->user->has_mobile = 1; $user->grade = $user->crmUser->grade; unset($user->crmUser); } return $this->success('ok', $users); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function teacherUserLogs(Request $request, $user_id=null) { try { if ($user_id) { $user = User::find($user_id); }else { $user = auth()->user(); } $role = $user->crmTService; if (empty($role)) return $this->success('ok', []); $keyword = $request->input('keyword'); $users = CrmUserLog::withCount('comments')->with('user:id,nickname,photo,app_avatar,sex,name,belief', 'user.profileCourtship:user_id,province,city,state,birthday,degree', 'crmUser:user_id,grade,step')->whereHas('crmUser'); if ($keyword) { $keyword = trim($keyword); $users = $users->whereHas("user", function($sql) use($keyword){ $sql->where('nickname', 'like', '%'.$keyword.'%') ->orWhere('mobile', 'like', '%'.$keyword.'%'); }); } $status = $request->input('status'); if (is_numeric($status) && $status == 0) { $users = $users->whereHas('crmUser', function ($sql) { $sql->whereNull('grade'); })->where('role_id', $role->id)->where('status', 0); }elseif (is_numeric($status) && $status == 2) { $users = $users->whereHas('crmUser', function ($sql) { $sql->whereNotNull('grade'); })->where('role_id', $role->id)->where('status', 0); }else { $users = $users->where('role_id', $role->id)->where('status', 1); } if (is_numeric($status) && $status == 2) { $users = $users->whereHas('crmUser', function ($sql) { $sql->whereNotNull('grade'); }); } $grade = $request->input('grade'); if ($grade) { $users = $users->whereHas('crmUser', function ($sql) use($grade){ $sql->where('grade', $grade); }); } $users = $users->orderBy('id', 'desc')->paginate(); foreach($users as $user) { $user->user->age = ($user->user->profileCourtship)?\CommonUtilsService::getAge($user->user->profileCourtship->birthday).'岁':null; $user->need_follow = ($user->crmUser->step != 'no_value')?1:0; $user->user->has_mobile = 1; $user->grade = $user->crmUser->grade; if ($status != 0 && $status != 2) { $user->complete_portrait = $user->user->completePortrait(); }else { //时间倒计时 $user->commented_at = $user->crmEndTime(); } unset($user->crmUser); } return $this->success('ok', $users); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function serviceTagUser(Request $request, $user_id) { \DB::beginTransaction(); try { $role_user_id = auth()->id(); $log = CrmUserLog::where(['user_id'=>$user_id, 'role_user_id'=>$role_user_id, 'status'=>1])->first(); if (!$log) return $this->failure("操作失败,用户不存在"); $status = $request->input('status'); if (!is_numeric($status)) throw new \Exception("未选择标记类型"); if ($status) {//下一步 $step = 'sale'; } else { $step = 'no_value'; } CrmUser::where('user_id',$user_id)->update(['step'=>$step, 'status'=>0]); CrmUserLog::where('id', $log->id)->update(['status'=>0, 'tagged_at'=>date('Y-m-d H:i:s')]); $data = [ 'user_id' => $user_id, 'maker_user_id' => $role_user_id, 'action'=>'c_tag', ]; $t_user_id = $request->input("t_user_id"); if ($t_user_id) { $role = CrmRole::where('user_id', $t_user_id)->where("type", 't_service')->first(); if (empty($role)) throw new \Exception("指定的老师角色不存在"); $data['action'] = 'assign'; CrmUser::where('user_id',$user_id)->update(['status'=>1]); CrmUserLog::create(['user_id'=>$user_id, 'role_id'=>$role->id, 'role_user_id'=>$role->user_id, 'status'=>1]); $this->changeUserLogsNotice($log, $t_user_id, $type='t_service'); } \DB::commit(); AddCrmRoleComment::dispatch($data)->onQueue('love'); return $this->success('ok'); } catch (\Exception $e) { \DB::rollback(); $this->getError($e); return $this->failure(); } } //老师标记用户 public function teacherTagUser(Request $request, $user_id) { \DB::beginTransaction(); try { $role_user_id = auth()->id(); $log = CrmUserLog::where(['user_id'=>$user_id, 'role_user_id'=>$role_user_id, 'status'=>1])->first(); if (!$log) return $this->failure("操作失败,用户不存在"); //是否完成用户画像 // $targe_user = User::find($user_id); // $portrait = $targe_user->completePortrait(); // if (empty($portrait)) return $this->failure('请先完善用户画像'); $status = $request->input('status'); if (!is_numeric($status)) throw new \Exception("未选择标记类型"); if ($status) {//下一步 $step = 'after_sale'; } else { $step = 'no_value'; } $grade = $request->input('grade'); $commented_at = null; if ($grade == 'A') { $commented_at = date("Y-m-d H:i:s", strtotime('+7 days')); }elseif($grade == 'B') { $commented_at = date("Y-m-d H:i:s", strtotime('+14 days')); }elseif($grade == 'C') { $commented_at = date("Y-m-d H:i:s", strtotime('+30 days')); } CrmUser::where('user_id',$user_id)->update(['status'=>0, 'step'=>$step, 'grade'=>$grade, 'graded_at'=>date('Y-m-d H:i:s')]); CrmUserLog::where('id', $log->id)->update(['status'=>0,'tagged_at'=>date('Y-m-d H:i:s'), 'commented_at'=>$commented_at]); $data = [ 'user_id' => $user_id, 'maker_user_id' => $role_user_id, 'action'=>'t_tag', ]; AddCrmRoleComment::dispatch($data)->onQueue('love'); \DB::commit(); return $this->success('ok'); } catch (\Exception $e) { \DB::rollback(); $this->getError($e); return $this->failure(); } } public function serviceRoleDataState(Request $request, $user_id=null) { try { if ($user_id) { $user = User::find($user_id); }else { $user = auth()->user(); } $role = $user->crmCService; if (empty($role)) return $this->success('ok', ['gain_count'=>0, 'comment_count'=>0, 'tag_count'=>0, 'state'=>[]]); $start_date = $request->input('start_date', date("Y-m-d", strtotime('-7 days'))); $end_date = $request->input('end_date', date('Y-m-d 23:59:59')); //领取数 $gain_count = CrmUserLog::where('role_id', $role->id)->whereBetween('created_at', [$start_date, $end_date])->count(); //备注数 $comment_count = ClientComment::where('maker_user_id', $role->user_id)->where('type', 'crm')->whereBetween('created_at', [$start_date, $end_date])->count(); //标记标记 $tag_count = CrmUserLog::where('role_id', $role->id)->whereBetween('tagged_at', [$start_date, $end_date])->count(); //柱状图 // $date_arr = $gain_count_arr = $comment_count_arr = $tag_count_arr = []; // for ($i=0; $i < 7; $i++) { // $start_time = date('Y-m-d', strtotime('-'.$i.' days')); // $end_time = date('Y-m-d 23:59:59', strtotime('-'.$i.' days')); // $gain_count_arr[] = CrmUserLog::where('role_user_id', $user_id)->whereBetween('created_at', [$start_time, $end_time])->count(); // $comment_count_arr[] = ClientComment::where('maker_user_id', $user_id)->where('type', 'crm')->whereBetween('created_at', [$start_time, $end_time])->count(); // $tag_count_arr[] = CrmUserLog::where('role_user_id', $user_id)->whereBetween('tagged_at', [$start_time, $end_time])->count(); // $date_arr[] = $start_time; // } // $state = [ // 'date_arr'=>$date_arr, // 'gain_count_arr'=>$gain_count_arr, // 'comment_count_arr'=>$comment_count_arr, // 'tag_count_arr'=>$tag_count_arr, // ]; $state = []; return $this->success('ok', compact('gain_count', 'comment_count', 'tag_count', 'state')); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function teacherRoleDataState(Request $request, $user_id=null) { try { if ($user_id) { $user = User::find($user_id); }else{ $user = auth()->user(); } $role = $user->crmTService; if (empty($role)) return $this->success('ok', ['gain_count'=>0, 'comment_count'=>0, 'tag_count'=>0, 'state'=>[]]); $start_date = $request->input('start_date', date("Y-m-d", strtotime('-7 days'))); $end_date = $request->input('end_date', date('Y-m-d 23:59:59')); //领取数 $gain_count = CrmUserLog::where('role_id', $role->id)->whereBetween('created_at', [$start_date, $end_date])->count(); //备注数 $comment_count = ClientComment::where('maker_user_id', $role->user_id)->where('type', 'crm_t')->whereBetween('created_at', [$start_date, $end_date])->count(); //标记标记 $tag_count = CrmUserLog::where('role_id', $role->id)->whereBetween('tagged_at', [$start_date, $end_date])->count(); //柱状图 // $date_arr = $gain_count_arr = $comment_count_arr = $tag_count_arr = []; // for ($i=0; $i < 7; $i++) { // $start_time = date('Y-m-d', strtotime('-'.$i.' days')); // $end_time = date('Y-m-d 23:59:59', strtotime('-'.$i.' days')); // $gain_count_arr[] = CrmUserLog::where('role_user_id', $user_id)->whereBetween('created_at', [$start_time, $end_time])->count(); // $comment_count_arr[] = ClientComment::where('maker_user_id', $user_id)->where('type', 'crm')->whereBetween('created_at', [$start_time, $end_time])->count(); // $tag_count_arr[] = CrmUserLog::where('role_user_id', $user_id)->whereBetween('tagged_at', [$start_time, $end_time])->count(); // $date_arr[] = $start_time; // } // $state = [ // 'date_arr'=>$date_arr, // 'gain_count_arr'=>$gain_count_arr, // 'comment_count_arr'=>$comment_count_arr, // 'tag_count_arr'=>$tag_count_arr, // ]; $state = []; return $this->success('ok', compact('gain_count', 'comment_count', 'tag_count', 'state')); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function commentUser(Request $request, $user_id) { DB::beginTransaction(); try { //用户状态 $crm_user = CrmUser::where('user_id', $user_id)->first(); if (empty($crm_user)) throw new \Exception("crm用户不存在"); $role = CrmRole::where('user_id', auth()->id())->where('type', 'c_service')->first(); if (!$role) return $this->failure("操作失败,暂无权限"); $link_way = $request->input('link_way'); $comment = $request->input('comment'); $result = $request->input('result'); $pics = $request->input('pics'); if (!$link_way) return $this->failure("请输入沟通方式"); if (!$comment) return $this->failure("请输入备注内容"); $data = [ 'user_id'=>$user_id, 'maker_user_id'=>auth()->id(), "type"=> "crm", 'comment'=>$comment, 'link_way'=>$link_way, 'result'=>$result, 'pics'=>$pics?json_encode($pics):null, ]; ClientComment::create($data); DB::commit(); $data['action'] = 'c_comment'; //角色添加备注记录 AddCrmRoleComment::dispatch($data)->onQueue('love'); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } public function teacherCommentUser(Request $request, $user_id) { DB::beginTransaction(); try { //用户状态 $crm_user = CrmUser::where('user_id', $user_id)->first(); if (empty($crm_user)) throw new \Exception("crm用户不存在"); $role = CrmRole::where('user_id', auth()->id())->where('type', 't_service')->first(); if (!$role) return $this->failure("操作失败,暂无权限"); $link_way = $request->input('link_way'); $comment = $request->input('comment'); $result = $request->input('result'); $pics = $request->input('pics'); if (!$link_way) return $this->failure("请输入沟通方式"); if (!$comment) return $this->failure("请输入备注内容"); $data = [ 'user_id'=>$user_id, 'maker_user_id'=>auth()->id(), "type"=> 'crm_t', 'comment'=>$comment, 'link_way'=>$link_way, 'result'=>$result, 'pics'=>$pics?json_encode($pics):null, ]; ClientComment::create($data); //更新老师第一次备注时间 $log = CrmUserLog::where('user_id', $user_id)->where('role_id', $role->id)->where('role_user_id', $role->user_id)->first(); if (empty($log->f_commented_at)) { CrmUserLog::where('id', $log->id)->update(['f_commented_at'=>date('Y-m-d H:i:s')]); } DB::commit(); $data['action'] = 't_comment'; //角色添加备注记录 AddCrmRoleComment::dispatch($data)->onQueue('love'); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } public function updateComment(Request $request, $comment_id) { try { $role = CrmRole::where('user_id', auth()->id())->where('type', 'c_service')->first(); if (!$role) return $this->failure("操作失败,暂无权限"); $link_way = $request->input('link_way'); $comment = $request->input('comment'); $result = $request->input('result'); $pics = $request->input('pics'); if (!$link_way) return $this->failure("请输入沟通方式"); if (!$comment) return $this->failure("请输入备注内容"); $obj = ClientComment::find($comment_id); if ($obj->type == 'crm_t') return $this->failure("暂不支持修改销售备注"); $obj->comment = $comment; $obj->link_way = $link_way; $obj->result = $result; $obj->pics = $pics?json_encode($pics):null; $obj->save(); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function teacherUpdateComment(Request $request, $comment_id) { try { $role = CrmRole::where('user_id', auth()->id())->where('type', 't_service')->first(); if (!$role) return $this->failure("操作失败,暂无权限"); $link_way = $request->input('link_way'); $comment = $request->input('comment'); $result = $request->input('result'); $pics = $request->input('pics'); if (!$link_way) return $this->failure("请输入沟通方式"); if (!$comment) return $this->failure("请输入备注内容"); $obj = ClientComment::find($comment_id); if ($obj->type == 'crm') return $this->failure("暂不支持修改客服备注"); $obj->comment = $comment; $obj->link_way = $link_way; $obj->result = $result; $obj->pics = $pics?json_encode($pics):null; $obj->save(); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function comments(Request $request, $user_id) { try { $comments = ClientComment::with('user:id,nickname', 'makerUser:id,nickname')->where('user_id', $user_id)/*->whereIn('type', ['crm', 'crm_t'])*/; $keyword = $request->input('keyword'); if ($keyword) { $keyword = trim($keyword); $comments = $comments->where(function($sql) use($keyword){ $sql->where('link_way', 'like', '%'.$keyword.'%') ->orWhere('comment', 'like', '%'.$keyword.'%') ->orWhere('result', 'like', '%'.$keyword.'%'); }); } $comments = $comments->orderBy('id', 'desc')->paginate(); foreach($comments as $comment) { $comment->pics = json_decode($comment->pics); $comment->type = in_array($comment->type, ['mobile', 'crm', 'crm_t'])?"管理员":"系统"; } return $this->success('ok', $comments); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function crmRoles(Request $request) { try { $roles = CrmRole::with('user:id,nickname,name,photo,app_avatar,mobile'); $keyword = $request->input('keyword'); if($keyword) { $keyword = trim($keyword); $roles = $roles->whereHas('user', function ($sql) use($keyword) { $sql->where('nickname', 'like', '%'.$keyword.'%'); }); } $type = $request->input('type'); if (empty($type)) throw new \Exception("缺少角色类型参数.type"); $roles = $roles->where('type', $type); $limit = $request->input('limit', 15); if ($limit == 15) { $c_type = ($type == 'c_service')?'crm':"crm_t"; $roles = $roles->withCount(['userLogs','tagUserLogs', 'comments'=>function($sql) use($c_type){ $sql->where('type', $c_type); }]); } $roles = $roles->paginate($limit); return $this->success("ok", $roles); }catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } public function changeUserLogs(Request $request, $log_id) { DB::beginTransaction(); try { $type = $request->input('type'); if (empty($type)) throw new \Exception("缺少角色类型参数.type"); $log = CrmUserLog::with('crmUser')->where(['id'=>$log_id, 'status'=>1])->first(); if (empty($log) || empty($log->crmUser)) throw new \Exception("操作失败,用户记录不存在"); if (empty($log->status)) return $this->failure("操作失败,用户状态暂不支持转移跟进"); $t_user_id = $request->input("t_user_id"); if (($type == 'c_service' && $log->crmUser->step != 'pre_sale') || ($type == 't_service' && $log->crmUser->step != 'sale')) throw new \Exception("用户类型和指派的角色不符"); if ($t_user_id) { if ($t_user_id == $log->role_user_id) return $this->success('ok'); $role = CrmRole::where(['user_id'=>$t_user_id, 'type'=>$type])->first(); if (empty($role)) throw new \Exception("目标角色没有角色记录"); CrmUserLog::where('id', $log->id)->update(['status'=>0]); CrmUserLog::updateOrCreate(['user_id'=>$log->user_id, 'role_id'=>$role->id, 'role_user_id'=>$t_user_id], ['status'=>1]); }else { CrmUserLog::where('id', $log->id)->update(['status'=>0]); $step = ($type== 'c_service')?'default':'pre_sale'; CrmUser::where('user_id', $log->user_id)->update(['step'=>$step, 'status'=>0]); } //通知todo $this->changeUserLogsNotice($log, $t_user_id, $type); DB::commit(); return $this->success('ok'); }catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } public function changeUserLogsNotice($log, $t_user_id, $type) { $role_type = ($type == 'c_service')?'客服':'销售'; //通知被转移客服/老师 $openid = $log->roleWechat?$log->roleWechat->official_openid:null; if ($openid) { $data['touser'] = $openid; $data['template_id'] = config('wechat.tpls.crm_role_data'); $data['url'] = 'https://love.ufutx.com/admin_pro/#/login'; $data['data'] = [ 'first' => '您的跟进客户有变化:', 'keyword1' => $role_type, 'keyword2' => '用户'.$log->user->nickname.'被转交指派给他人跟进', 'remark' => '详情请进入后台系统查看', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); } //通知指定客服/老师 if ($t_user_id) { $t_openid = Wechat::where('user_id', $t_user_id)->value('official_openid'); if ($t_openid) { //之前跟进客服/老师 $nickname = $log->roleUser?'【'.$log->roleUser->nickname.'】':''; $data['touser'] = $t_openid; $data['template_id'] = config('wechat.tpls.crm_role_data'); $data['url'] = 'https://love.ufutx.com/admin_pro/#/login'; $data['data'] = [ 'first' => '您有新的被指派跟进客户:', 'keyword1' => $role_type, 'keyword2' => '用户'.$log->user->nickname.'被'.$nickname.'指派给您进行跟进', 'remark' => '详情请进入后台系统查看', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); } } } /** * 客服释放用户 */ public function serviceReleaseUser(Request $request, $user_id) { try { $role_user = auth()->user(); $log = CrmUserLog::with('crmUser')->where("user_id", $user_id)->where('status', 1)->where('role_user_id', $role_user->id)->first(); if (empty($log) || empty($log->crmUser)) throw new \Exception("没有用户记录"); if ($log->crmUser->step != 'pre_sale') return $this->failure("用户当前状态不可释放"); DB::beginTransaction(); //修改关联状态 CrmUserLog::where('id', $log->id)->update(['status'=>0]); //修改用户状态 CrmUser::where('user_id', $user_id)->update(['step'=>"default", "status"=>0]); DB::commit(); return $this->success('ok'); }catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } public function adminReleaseUser(Request $request, $user_id) { try { // $role_user = auth()->user(); // $log = CrmUserLog::with('crmUser')->where("user_id", $user_id)->where('status', 1)->where('role_user_id', $role_user->id)->first(); // if (empty($log) || empty($log->crmUser)) throw new \Exception("没有用户记录"); // if ($log->crmUser->step != 'pre_sale') return $this->failure("用户当前状态不可释放"); DB::beginTransaction(); //修改关联状态 CrmUserLog::where('user_id', $user_id)->update(['status'=>0]); //修改用户状态 CrmUser::where('user_id', $user_id)->update(['step'=>"default", "status"=>0]); DB::commit(); return $this->success('ok'); }catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } public function serviceTransferUser(Request $request, $user_id) { try { $role_user = auth()->user(); $log = CrmUserLog::with('crmUser')->where("user_id", $user_id)->where('status', 1)->where('role_user_id', $role_user->id)->first(); if (empty($log) || empty($log->crmUser)) throw new \Exception("没有用户记录"); if ($log->crmUser->step != 'pre_sale') return $this->failure("用户当前状态不可转让"); $s_user_id = $request->input("s_user_id"); if (empty($s_user_id)) return $this->failure("请选择客服"); if ($s_user_id == $log->role_user_id) return $this->failure("暂不支持转给自己"); $role = CrmRole::where("user_id", $s_user_id)->where('type', 'c_service')->first(); if (empty($role)) return $this->failure("该用户不是客服角色"); DB::beginTransaction(); //修改当前关联状态 CrmUserLog::where('id', $log->id)->update(['status'=>0]); //增加新的关联状态 CrmUserLog::create([ 'user_id'=>$user_id, 'role_id'=>$role->id, 'role_user_id'=>$role->user_id, 'status'=>1, ]); DB::commit(); //通知 $this->changeUserLogsNotice($log, $s_user_id, 'c_service'); return $this->success('ok'); }catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } public function saveProfile(Request $request) { try { $role_user_id = $request->input('role_user_id'); if (empty($role_user_id)) { $role_user_id = auth()->id(); } $data = []; $qrcode = $request->input('qrcode'); $sort = $request->input('sort'); if ($qrcode) $data['qrcode'] = $qrcode; if ($sort) $data['sort'] = $sort; $res = CrmRole::where('user_id', $role_user_id)->update($data); if (!$res) $this->failure('更新资料失败'); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function getProfile(Request $request) { try { $role_user_id = $request->input('role_user_id'); if (empty($role_user_id)) { $role_user_id = auth()->id(); } $data = CrmRole::where('user_id', $role_user_id)->first(); return $this->success('ok-user_id:'.$role_user_id, $data); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } public function changeIsShow(Request $request) { try { $role_user_id = $request->input('role_user_id'); $is_show = $request->input('is_show'); $data = ['is_show' => $is_show]; CrmRole::where('user_id', $role_user_id)->where('type','c_service')->update($data); return $this->success('ok', $data); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } }