request->empcrm['org_id']); $customer = Customer::where([['id', '=', $customer_id], ['org_id', 'in', $team_orgs]])->find(); if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']); $settingObj = Setting::where(['root_id' => $this->request->empcrm['root_id'], 'name' => 'valid_check_person'])->find(); if (empty($settingObj)) return json(['code' => 1, 'msg' => '申请失败,未配置申请权限']); $setting = explode(',', $settingObj->content); if (in_array('xiaoshou', $setting) && $customer->employee_id == $this->request->empcrm['id']) { } elseif (in_array('sheji', $setting) && $customer->designer_id == $this->request->empcrm['id']) { } else { return json(['code' => 1, 'msg' => '申请失败,未开启申请权限']); } // 查找是否已经申请 $exist = CustomerStateCheck::where(['customer_id' => $customer_id, 'root_id' => $this->request->empcrm['root_id']])->find(); if ($exist) { if ($exist->check_state < 2) return json(['code' => 1, 'msg' => '申请失败,请勿重复申请']); else $exist->save(['check_state' => 0, 'check_employee_id' => null, 'check_time' => null, 'employee_id' => $this->request->empcrm['id']]); } else { // 创建申请 CustomerStateCheck::create([ 'customer_id' => $customer_id, 'employee_id' => $this->request->empcrm['id'], 'root_id' => $this->request->empcrm['root_id'], 'org_id' => $customer->org_id, 'state' => 1, 'check_state' => 0 ]); } // 添加跟进记录 $employee = Employee::find($this->request->empcrm['id']); CustomerVisitLog::create([ 'customer_id' => $customer_id, 'employee_id' => $this->request->empcrm['id'], 'remark' => '申请有效客户', 'user_id' => $employee->uid, 'type' => '' ]); return json(['code' => 0, 'msg' => '申请成功']); } /** * 申请列表 */ public function list(int $page = 1, int $limit = 20) { if (!$this->request->empcrm->is_manager) return json(['code' => 1, 'msg' => '没有权限']); if (!$this->request->isAjax()) { return View::fetch(); } $param = $this->request->except(['page', 'limit']); $conditionOr = []; $condition = [['root_id', '=', $this->request->empcrm['root_id']]]; $team_orgs = orgSubIds($this->request->empcrm['org_id']); $condition[] = ['org_id', 'in', $team_orgs]; if (isset($param['keyword'])) { // 查询员工姓名 $empId = Employee::where([['name', 'like', '%' . $param['keyword'] . '%'], ['root_id', '=', $this->request->empcrm['root_id']]])->column('id'); // 客户姓名 $cid = CustomerStateCheck::hasWhere('customer', function ($query) use ($param) { $query->where('name', 'like', '%' . $param['keyword'] . '%'); })->where(['CustomerStateCheck.root_id' => $this->request->empcrm['root_id']])->order('CustomerStateCheck.id', 'desc')->column('customer_id'); $conditionOr = [ [['customer_id', 'in', $cid]], [['employee_id', 'in', $empId]] ]; } if (isset($param['state']) && $param['state'] != '') { $condition[] = ['check_state', '=', $param['state']]; } $checkCustomerIds = CustomerStateCheck::where($condition)->where(function ($query) use ($conditionOr) { $query->whereOr($conditionOr); })->order('id', 'desc')->page($page, $limit)->column('check_state', 'customer_id'); $count = CustomerStateCheck::where($condition)->whereOr($conditionOr)->count(); $customerIds = array_keys($checkCustomerIds); $lastTime = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('customer_id')->column('max(addtime)', 'customer_id'); // 状态统计获取 $customersStateList = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } $data = Customer::whereIn('id', $customerIds)->select()->visible(['id', 'name', 'sex', 'level', 'community_name', 'state', 'square'])->toArray(); foreach ($data as &$cus) { if (isset($customersState[$cus['id']]) && !in_array($cus['state'], Customer::changeState('待确认', 'chaos'), true)) { $cus['stateNum'] = $customersState[$cus['id']]; } else { $cus['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } $cus['last_visit_day'] = isset($lastTime[$cus['id']]) ? get_date_diff(time(), $lastTime[$cus['id']])->days : -1; $cus['check_state'] = $checkCustomerIds[$cus['id']] ?? 0; } return json(['code' => 0, 'msg' => '成功', 'data' => $data, 'count' => $count]); } /** * 申请通过或者驳回 */ public function check($customer_id, $state) { $team_orgs = orgSubIds($this->request->empcrm['org_id']); $customer = Customer::where([['id', '=', $customer_id], ['org_id', 'in', $team_orgs]])->find(); if (!$customer) return json(['code' => 1, 'msg' => '申请失败,客户不存在']); $checkCustomer = CustomerStateCheck::where(['customer_id' => $customer_id, 'root_id' => $this->request->empcrm['root_id']])->find(); if (!$checkCustomer) return json(['code' => 1, 'msg' => '申请失败,客户不存在']); if ($checkCustomer['check_state'] != 0) return json(['code' => 1, 'msg' => '申请失败,该客户已审核']); $checkCustomer->check_state = $state; $checkCustomer->check_employee_id = $this->request->empcrm['id']; $checkCustomer->check_time = date('Y-m-d H:i:s'); $checkCustomer->save(); // 添加跟进记录 $employee = Employee::find($this->request->empcrm['id']); $msg = $state == 1 ? '有效申请已通过' : '有效申请已被拒绝'; CustomerVisitLog::create([ 'customer_id' => $customer_id, 'employee_id' => $this->request->empcrm['id'], 'remark' => $msg, 'user_id' => $employee->uid, 'type' => '' ]); return json(['code' => 0, 'msg' => '审核成功']); } /** * 申请列表 */ public function apply_list($keyword = null, int $page = 1, int $limit = 20) { if (!$this->request->isAjax()) { return View::fetch(); } $param = $this->request->except(['page', 'limit']); $condition = [['root_id', '=', $this->request->empcrm['root_id']]]; $condition[] = ['employee_id', '=', $this->request->empcrm['id']]; if (isset($param['state']) && $param['state'] != '') { $condition[] = ['check_state', '=', $param['state']]; } if (!empty($keyword)) { // 客户姓名 $cid = CustomerStateCheck::hasWhere('customer', function ($query) use ($keyword) { $query->where([['name', 'like', '%' . $keyword . '%']]); })->where(['CustomerStateCheck.root_id' => $this->request->empcrm['root_id'], ['CustomerStateCheck.employee_id', '=', $this->request->empcrm['id']]])->order('CustomerStateCheck.id', 'desc')->column('customer_id'); $condition = [ ['customer_id', 'in', $cid], ]; } $checkCustomerIds = CustomerStateCheck::where($condition)->order('id', 'desc')->page($page, $limit)->column('check_state', 'customer_id'); $count = CustomerStateCheck::where($condition)->count(); $customerIds = array_keys($checkCustomerIds); $lastTime = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('customer_id')->column('max(addtime)', 'customer_id'); // 状态统计获取 $customersStateList = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } $data = Customer::whereIn('id', $customerIds)->select()->visible(['id', 'name', 'sex', 'level', 'community_name', 'state', 'square'])->toArray(); foreach ($data as &$cus) { if (isset($customersState[$cus['id']]) && !in_array($cus['state'], Customer::changeState('待确认', 'chaos'), true)) { $cus['stateNum'] = $customersState[$cus['id']]; } else { $cus['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } $cus['last_visit_day'] = isset($lastTime[$cus['id']]) ? get_date_diff(time(), $lastTime[$cus['id']])->days : -1; $cus['check_state'] = $checkCustomerIds[$cus['id']] ?? 0; } return json(['code' => 0, 'msg' => '成功', 'data' => $data, 'count' => $count]); } }