123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860 |
- <?php
- namespace app\wechat\service;
- use app\model\Consultant;
- use app\model\MarketCustomer;
- use app\model\MarketCustomerFollow;
- use app\model\MarketCustomerLogs;
- use app\wechat\validate\CustomValidate;
- use support\Db;
- use support\exception\BusinessException;
- use Tinywan\Jwt\JwtToken;
- class CustomService
- {
- const DIFF_TIME = (60 * 60 * 24 * 30);
- const CHECK_NOTE = '该客户已在其他顾问那到访';
- /**
- * Notes: 选项配置
- * User: yb
- * Date: 2024/8/8
- * Time: 15:18
- * @return array
- */
- public static function config()
- {
- $options = MarketCustomer::config();
- if (isset($options['visit_type'])) {
- //如果存在拜访方式,直接在后台处理完成后返回前台
- foreach ($options['visit_type'] as $key => $val) {
- $data = [];
- foreach ($val as $index => $item) {
- $data[] = ['text' => $item, 'value' => $index];
- }
- $options['visit_type'][$key] = $data;
- }
- }
- return $options;
- }
- /**
- * Notes: 客户列表
- * User: yb
- * Date: 2024/8/12
- * Time: 11:08
- * @param $params
- */
- public static function index($params)
- {
- $page = $params['page'] ?? 1;
- $size = $params['size'] ?? 10;
- //查询顾问信息
- $consultantId = JwtToken::getCurrentId();
- $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
- $deptId = $consultantInfo->dept_id;
- $type = $consultantInfo->type;
- $currentTime = time();
- $diffNums = self::DIFF_TIME;
- $where = [];
- $whereFollow = [];
- if (!empty($params['belong_status'])) {
- //团队权限
- $where[] = [function($query) use ($diffNums, $currentTime){
- $query->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND (visit_time + ({$diffNums} * 4)) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2 AND belong_status = 1)")->whereOr('belong_status','<>', 1);
- }];
- } else {
- if ($type == 4) {
- $where[] = [function($query) use ($diffNums, $currentTime){
- $query->orWhereRaw("check_status IN (-1, 1)")
- ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
- ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)")
- ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 2 AND check_status = 2)");
- }];
- } else {
- $where[] = [function($query) use ($diffNums, $currentTime){
- $query->orWhereRaw("check_status IN (-1, 1)")
- ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
- ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
- }];
- }
- }
- if ($type == 1) {
- //团队下所有的
- $deptIds = TeamService::getIds($deptId);
- $where[] = [function($query) use ($deptIds) {
- $query->whereIn('dept_id', $deptIds);
- }];
- if (!empty($params['consultant_id'])) {
- $consultantId = $params['consultant_id'];
- $where[] = [function($query) use ($consultantId) {
- $query->orWhere('consultant_id', $consultantId)->orWhere('report_consultant_id', $consultantId);
- }];
- $whereFollow[] = ['consultant_id', '=', $params['consultant_id']];
- } else {
- $consultantIds = UserService::getIds(1);
- $whereFollow[] = [function($query) use ($consultantIds) {
- $query->whereIn('consultant_id', $consultantIds);
- }];
- }
- } else if ($type == 2){
- //个人的
- $where[] = [function($query) use ($consultantId){
- $query->orWhere('consultant_id', $consultantId)->orWhere('report_consultant_id', $consultantId);
- }];
- $whereFollow[] = ['consultant_id', '=', $consultantId];
- } else if ($type == 3){
- //所有团队的
- } else if ($type == 4) {
- //查询渠道人员
- $where[] = [function($query) use ($consultantId) {
- $query->orWhere('consultant_id', $consultantId)->orWhere('report_consultant_id', $consultantId);
- }];
- }
- if (!empty($params['search_type'])) {
- $searchType = $params['search_type'];
- // 1 = 仅搜索客户
- if ($searchType == 1) {
- if (!empty($params['custom'])) {
- $keywords = $params['custom'];
- $where[] = [function($query) use ($keywords) {
- $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
- }];
- }
- } else {
- if (!empty($params['custom'])) {
- $keywords = $params['custom'];
- $consultantIds = Consultant::orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%")->pluck('id')->toArray();
- if ($searchType == 2) {
- $searchKey = 'consultant_id';
- } else {
- $searchKey = 'report_consultant_id';
- }
- $where[] = [function($query) use ($consultantIds, $searchKey) {
- $query->orWhereIn($searchKey, $consultantIds);
- }];
- }
- }
- } else {
- if (!empty($params['custom'])) {
- $keywords = $params['custom'];
- $consultantIds = Consultant::orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%")->pluck('id')->toArray();
- $where[] = [function($query) use ($keywords, $consultantIds) {
- $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%")
- ->orWhereIn('consultant_id', $consultantIds)->orWhereIn('report_consultant_id', $consultantIds);
- }];
- }
- }
- if (!empty($params['report_status'])) {
- if ($params['report_status'] == 1) {
- //已报备
- $where[] = [function($query) use ($diffNums, $currentTime) {
- $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1)->where('check_status', '=', 2);
- }];
- } else if ($params['report_status'] == 3) {
- //已锁定
- $where[] = [function($query) {
- $query->whereIn('current_status', [2,3,4])->where('check_status', '=', 2);
- }];
- } else if ($params['report_status'] == 2) {
- //已失效
- $where[] = [function($query) use ($diffNums, $currentTime){
- $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1)");
- }];
- $where[] = ['check_status', '=', 2];
- } else if ($params['report_status'] == '-1') {
- //待审核
- $where[] = ['check_status', '=', 1];
- }
- }
- if (!empty($params['check_status'])) {
- $where[] = ['check_status', '=', $params['check_status']];
- }
- if (!empty($params['type'])) {
- $where[] = ['type', '=', $params['type']];
- }
- if (!empty($params['visit_time'])) {
- $datetime = $params['visit_time'];
- $startTime = strtotime($datetime['start'].' 00:00:00');
- $endTime = strtotime($datetime['end'].' 23:59:59');
- $where[] = [function($query) use ($startTime, $endTime) {
- $query->whereBetween('visit_time', [$startTime, $endTime]);
- }];
- }
- if (!empty($params['current_status'])) {
- $currentStatus = $params['current_status'];
- $where[] = [function($query) use ($currentStatus) {
- $query->where('current_status', $currentStatus);
- }];
- }
- if (!empty($params['created_at'])) {
- $createdAt = $params['created_at'];
- if (is_array($createdAt)) {
- $createdAtStart = strtotime($createdAt[0]);
- $createdAtEnd = strtotime($createdAt[1]);
- } else {
- //本月开始和结束
- $createdAtStart = strtotime(date('Y-m-01 00:00:00'));
- $createdAtEnd = strtotime(date('Y-m-t 23:59:59'));
- }
- $where[] = [function($query) use ($createdAtStart,$createdAtEnd) {
- $query->whereBetween('created_at', [$createdAtStart, $createdAtEnd]);
- }];
- }
- $fn = function ($query) {
- $query->select('name', 'mobile', 'id', 'dept_id');
- };
- $paginator = MarketCustomer::with(['consultant' => $fn, 'report' => $fn])->where($where)->orderBy('visit_time', 'desc')->paginate($size, '*', 'page', $page);
- $total = $paginator->total();
- $items = $paginator->items();
- if (!empty($items)) {
- //查询所有团队信息
- $teamKeys = TeamService::getTeams()->pluck('dept_name', 'dept_id');
- $now = time();
- foreach ($items as &$item) {
- $item->mask_mobile = self::handlePhone($item->mobile);
- $visitTimeInt = strtotime($item->visit_time);
- $endTime = $visitTimeInt + 60 * 60 * 24 * 30;
- $visitTimeInt = $endTime - $now;
- if ($visitTimeInt <= 0) {
- $visitTimeInt = 0;
- }
- if (!empty($item->consultant)) {
- $item->consultant->dept_name = $teamKeys[$item->consultant->dept_id] ?? '';
- }
- if (!empty($item->report)) {
- $item->report->dept_name = $teamKeys[$item->report->dept_id] ?? '';
- }
- $item->visit_time_int = $visitTimeInt;
- }
- }
- //本日新增
- // $date = date('Y-m-d');
- // $start = strtotime($date.' 00:00:00');
- // $end = strtotime($date.' 23:59:59');
- // $newNums = MarketCustomer::where($where)->whereBetween('created_at', [$start, $end])->count();
- // $followNums = MarketCustomerFollow::where($whereFollow)->whereBetween('created_at', [$start, $end])->count();
- $data = [
- 'new_follow_num' => $followNums ?? 0,
- 'new_custom_num' => $newNums ?? 0,
- 'total' => $total,
- 'rows' => $items
- ];
- return json_success('success', $data);
- }
- /**
- * Notes: 我的客户
- * User: yb
- * Date: 2024/8/13
- * Time: 15:14
- * @param $params
- */
- public static function myCustomList($params)
- {
- $currentTime = time();
- $diffNums = self::DIFF_TIME;
- $page = $params['page'] ?? 1;
- $size = $params['size'] ?? 10;
- //查询有效顾问信息
- $consultantId = JwtToken::getCurrentId();
- $where = [
- ['consultant_id', '=', $consultantId],
- ['current_status', '<>', '-1'],
- ['check_status', '<>', '-1']
- ];
- $where[] = [function($query) use ($diffNums, $currentTime){
- $query->orWhereRaw("check_status IN (-1, 1)")
- ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
- ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
- }];
- if (!empty($params['custom'])) {
- $keywords = $params['custom'];
- $where[] = [function($query) use ($keywords) {
- $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
- }];
- }
- $paginator = MarketCustomer::where($where)->orderBy('created_at', 'desc')->paginate($size, ['id','name','mobile','gender'], 'page', $page);
- $total = $paginator->total();
- $items = $paginator->items();
- if (!empty($items)) {
- foreach ($items as &$item) {
- $item->mask_mobile = self::handlePhone($item->mobile);
- }
- }
- $data = [
- 'total' => $total,
- 'rows' => $items
- ];
- return json_success('success', $data);
- }
- /**
- * Notes: 添加客户
- * User: yb
- * Date: 2024/8/6
- * Time: 11:20
- */
- public static function add($params)
- {
- $params = MarketCustomer::handleNumParams($params);
- $mobile = $params['mobile'];
- //查询客户手机号是否已经存在
- if (MarketCustomer::checkCustomExists($mobile)) {
- return json_fail('客户已经到访');
- }
- //查询顾问信息
- $consultantId = JwtToken::getCurrentId();
- $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
- if (empty($consultantInfo)) {
- return json_fail('顾问信息不存在');
- }
- $deptId = $consultantInfo->dept_id;
- $insertData = [
- 'name' => $params['name'],
- 'mobile' => $mobile,
- 'consultant_id' => 0,
- 'report_consultant_id' => $consultantId,
- 'create_consultant_id' => $consultantId,
- 'dept_id' => $deptId,
- 'gender' => $params['gender'] ?? null,
- 'visit_type' => $params['visit_type'] ?? null,
- 'require_area' => $params['require_area'] ?? null,
- 'area' => $params['area'] ?? null,
- 'requirement' => $params['requirement'] ?? null,
- 'age_range' => $params['age_range'] ?? null,
- 'focus' => $params['focus'] ?? null,
- 'region' => $params['region'] ?? null,
- 'purpose' => $params['purpose'] ?? null,
- 'level' => $params['level'] ?? null,
- 'type' => $params['type'] ?? null,
- 'visit_time' => time(),
- 'note' => $params['note'] ?? '',
- 'check_status' => 1,
- 'current_status' => 1,
- 'belong_status' => 1,
- 'created_at' => time()
- ];
- Db::beginTransaction();
- try {
- $customId = MarketCustomer::insertGetId($insertData);
- Db::commit();
- }catch (BusinessException|\Exception $e){
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- if ($customId) {
- return json_success('添加成功');
- } else {
- return json_fail('添加失败');
- }
- }
- /**
- * Notes: 编辑客户
- * User: yb
- * Date: 2024/8/12
- * Time: 10:23
- * @param $params
- * @return \support\Response
- */
- public static function edit($params)
- {
- $consultantId = JwtToken::getCurrentId();
- $params = MarketCustomer::handleNumParams($params);
- if (empty($params['id'])) {
- return json_fail('客户id不能为空');
- }
- $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
- if (empty($consultantInfo)) {
- return json_fail('顾问信息不存在');
- }
- $info = MarketCustomer::firstWhere(['id' => $params['id'], 'consultant_id' => $consultantId]);
- if (empty($info)) {
- return json_fail('客户信息不存在');
- }
- $updateData = [
- 'name' => $params['name'],
- 'gender' => $params['gender'] ?? null,
- 'visit_type' => $params['visit_type'] ?? null,
- 'require_area' => $params['require_area'] ?? null,
- 'area' => $params['area'] ?? null,
- 'requirement' => $params['requirement'] ?? null,
- 'age_range' => $params['age_range'] ?? null,
- 'focus' => $params['focus'] ?? null,
- 'region' => $params['region'] ?? null,
- 'purpose' => $params['purpose'] ?? null,
- 'level' => $params['level'] ?? null,
- 'type' => $params['type'] ?? null,
- 'note' => $params['note'] ?? '',
- 'updated_at' => time()
- ];
- if (empty($params['mobile'])) {
- return json_fail('手机号不能为空');
- }
- $mobile = $params['mobile'];
- if (false === strpos($mobile,'****')) {
- //校验手机号格式
- $validate = new CustomValidate();
- if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
- return json_fail($validate->getError());
- }
- //修改手机号操作
- if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
- return json_fail('客户已经登记过了');
- }
- $updateData['mobile'] = $mobile;
- }
- $currentStatus = $info->current_status;
- if ($currentStatus == 1) {
- //已来电改为已到访
- if ($params['type'] == 2) {
- $updateData['current_status'] = 2;
- }
- }
- $result = false;
- Db::beginTransaction();
- try {
- $result = MarketCustomer::where('id', $params['id'])->update($updateData);
- Db::commit();
- }catch (BusinessException|\Exception $e){
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- if ($result) {
- return json_success('编辑成功');
- } else {
- return json_fail('编辑失败');
- }
- }
- /**
- * Notes: 客户详情
- * User: yb
- * Date: 2024/8/8
- * Time: 15:36
- * @param $params
- */
- public static function info($id)
- {
- $fn = function ($query) {
- $query->select('name', 'mobile', 'id', 'dept_id');
- };
- $customInfo = MarketCustomer::with(['consultant' => $fn, 'report' => $fn])->firstWhere(['id' => $id]);
- if (empty($customInfo)) {
- return json_fail('客户不存在');
- }
- $teamKeys = TeamService::getTeams()->pluck('dept_name', 'dept_id');
- if (!empty($customInfo->consultant)) {
- $customInfo->consultant->dept_name = $teamKeys[$customInfo->consultant->dept_id] ?? '';
- }
- if (!empty($customInfo->report)) {
- $customInfo->report->dept_name = $teamKeys[$customInfo->report->dept_id] ?? '';
- }
- //手机号加密处理
- $customInfo->mask_mobile = self::handlePhone($customInfo->mobile);
- //查询最新的跟进记录
- $follow = MarketCustomerFollow::where('market_customer_id', $id)->orderBy('created_at', 'desc')->first();
- $customInfo->follow = $follow;
- $focus = $customInfo->focus;
- if (!empty($focus)) {
- $customInfo->focus = explode(',', $focus);
- }
- return json_success('', $customInfo);
- }
- /**
- * Notes: 更新客户状态
- * User: yb
- * Date: 2024/8/14
- * Time: 13:23
- * @param $params
- */
- public static function updateStatus($params)
- {
- $customId = $params['id'];
- $currentStatus = $params['current_status'];
- if (!in_array($currentStatus, [-1, 2, 3, 4])) {
- return json_fail('状态值非法');
- }
- $info = MarketCustomer::firstWhere(['id' => $customId]);
- if (empty($info)) {
- return json_fail('客户不存在');
- }
- if ($info->current_status != 1) {
- return json_fail('客户非已报备状态');
- }
- if ($currentStatus >= 2) {
- //验证是否已经存在其他到访客户
- if (MarketCustomer::checkCustomExists($info->mobile)) {
- return json_fail('操作失败,客户已存在');
- }
- }
- Db::beginTransaction();
- $result = false;
- try {
- if ($currentStatus == 2) {
- $info->visit_time = time();
- }
- $info->current_status = $currentStatus;
- $result = $info->save();
- if ($result) {
- if ($currentStatus >= 2) {
- $where = [
- ['id', '<>', $customId]
- ];
- //将其他的改为无效客户
- MarketCustomer::where($where)->where('check_status', 2)->update(['current_status' => -1]);
- //将待审核的改为拒绝
- MarketCustomer::where($where)->where('check_status', 1)->update(['check_status' => -1, 'check_note' => self::CHECK_NOTE]);
- }
- }
- Db::commit();
- }catch (BusinessException|\Exception $e){
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- if ($result) {
- return json_success('操作成功');
- } else {
- return json_fail('操作失败');
- }
- }
- /**
- * Notes: 指定顾问
- * User: yb
- * Date: 2024/9/5
- * Time: 12:52
- * @param $params
- */
- public static function appoint($params)
- {
- if (empty($params['id'])) {
- return json_fail('请选择客户');
- }
- if (empty($params['report_consultant_id'])) {
- return json_fail('请选择指定顾问');
- }
- $userId = JwtToken::getCurrentId();
- //获取绑定的管理信息
- $userInfo = Consultant::firstWhere(['id' => $userId]);
- if (empty($userInfo)) {
- return json_fail('管理员信息不存在');
- }
- if (!in_array($userInfo->type, [1,3])) {
- return json_fail('操作权限不足');
- }
- $relationUserId = $userInfo->relation_user_id;
- $customId = $params['id'];
- $reportConsultantId = $params['report_consultant_id'];
- $customInfo = MarketCustomer::firstWhere(['id' => $customId]);
- if (empty($customInfo)) {
- return json_fail('客户信息不存在');
- }
- $consultantInfo = Consultant::firstWhere(['id' => $reportConsultantId]);
- if (empty($consultantInfo)) {
- return json_fail('顾问信息不存在');
- }
- $currentDeptId = $consultantInfo->dept_id; //当前部门
- $now = time();
- $logData = [
- 'market_customer_id' => $customId,
- 'consultant_id' => $reportConsultantId,
- 'dept_id' => $currentDeptId,
- 'before_consultant_id' => $customInfo->consultant_id,
- 'before_dept_id' => $customInfo->dept_id,
- 'note' => $params['note'] ?? '判客指定报备顾问',
- 'change_user_id' => $relationUserId,
- 'change_consultant_id' => $userId,
- 'created_at' => $now
- ];
- Db::beginTransaction();
- $result = false;
- try {
- $customInfo->report_consultant_id = $reportConsultantId;
- $customInfo->dept_id = $currentDeptId;
- $result = $customInfo->save();
- MarketCustomerLogs::insert($logData);
- Db::commit();
- }catch (BusinessException|\Exception $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- if ($result) {
- return json_success('指定成功');
- } else {
- return json_fail('指定失败');
- }
- }
- /**
- * Notes: 转移客户
- * User: yb
- * Date: 2024/8/7
- * Time: 14:27
- * @param $params
- */
- public static function moveCustom($params)
- {
- $userId = JwtToken::getCurrentId();
- //获取绑定的管理信息
- $userInfo = Consultant::firstWhere(['id' => $userId]);
- if (empty($userInfo)) {
- return json_fail('管理员信息不存在');
- }
- if (!in_array($userInfo->type, [1,3])) {
- return json_fail('操作权限不足');
- }
- $relationUserId = $userInfo->relation_user_id;
- $currentConsultantId = $params['consultant_id']; //当前顾问
- $consultantId = $params['move_consultant_id'] ?? 0;
- $customId = $params['move_market_customer_id'] ?? 0;
- $note = $params['note'] ?? '';
- $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
- if (empty($consultantInfo)) {
- return json_fail('接收成员不存在');
- }
- $currentDeptId = $consultantInfo->dept_id; //当前部门
- $logsInertData = [];
- $now = time();
- $whereCustom = [];
- if (!empty($customId)) {
- $whereCustom[] = ['id', '=', $customId];
- }
- if (!empty($consultantId)) {
- $whereCustom[] = ['consultant_id', '=', $consultantId];
- }
- $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id','visit_time','check_status','current_status'])->get();
- if (!$customList->isEmpty()) {
- foreach ($customList as$item) {
- $logsInertData[] = [
- 'market_customer_id' => $item->id,
- 'consultant_id' => $currentConsultantId,
- 'dept_id' => $currentDeptId,
- 'before_consultant_id' => $item->consultant_id,
- 'before_dept_id' => $item->dept_id,
- 'note' => $note,
- 'change_user_id' => $relationUserId,
- 'change_consultant_id' => $userId,
- 'created_at' => $now
- ];
- }
- }
- if ($customList->isEmpty()) {
- return json_fail('成员没有客户记录');
- }
- if (empty($logsInertData)) {
- return json_fail('移交记录不能为空');
- }
- $diffTime = self::DIFF_TIME;
- Db::beginTransaction();
- $result = false;
- try {
- foreach ($customList as $item) {
- if ($item->check_status == 2 && $item->current_status == 2) {
- $visitTime = $item->visit_time;
- $diff = strtotime($visitTime) + $diffTime - time();
- if ($diff <= 0) {
- $item->visit_time = time();
- }
- }
- $item->consultant_id = $currentConsultantId;
- $item->dept_id = $currentDeptId;
- $item->save();
- }
- $result = MarketCustomerLogs::insert($logsInertData);
- Db::commit();
- }catch (BusinessException|\Exception $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- if ($result) {
- return json_success('移交成功');
- } else {
- return json_fail('移交失败');
- }
- }
- /**
- * Notes: 转移记录
- * User: yb
- * Date: 2024/8/7
- * Time: 15:33
- * @param $params
- */
- public static function moveLogs($params)
- {
- $page = $params['page'] ?? 1;
- $size = $params['size'] ?? 10;
- $where = [];
- if (!empty($params['custom_id'])) {
- $where[] = ['market_customer_id', '=', $params['custom_id']];
- }
- $selectFn = function ($query){
- $query->select('name', 'mobile', 'id');
- };
- $paginator = MarketCustomerLogs::with(['custom' => $selectFn, 'beforeMan' => $selectFn, 'currentMan' => $selectFn])
- ->where($where)
- ->orderBy('created_at', 'desc')
- ->paginate($size, '*', 'page', $page);
- $total = $paginator->total();
- $items = $paginator->items();
- $data = [
- 'total' => $total,
- 'rows' => $items
- ];
- return json_success('success', $data);
- }
- /**
- * Notes: 审核客户
- * User: yb
- * Date: 2024/8/16
- * Time: 11:21
- * @param $params
- */
- public static function checkCustom($params)
- {
- $userId = JwtToken::getCurrentId();
- $adminId = Consultant::where('id', '=', $userId)->value('relation_user_id');
- //查询客户是否已经存在
- $customId = $params['id'];
- $checkStatus = $params['check_status'];
- if (!in_array($checkStatus, [-1,2])) {
- return json_fail('状态值非法');
- }
- $info = MarketCustomer::firstWhere(['id' => $customId]);
- if (empty($info)) {
- return json_fail('客户不存在');
- }
- $status = $info->check_status;
- if ($status != 1) {
- return json_fail('客户不是待转状态');
- }
- $mobile = $info->mobile;
- if ($checkStatus == 2) {
- if (MarketCustomer::checkCustomExists($mobile)) {
- return json_fail('客户已经存在');
- }
- }
- $consultantId = $params['consultant_id'];
- $result = false;
- Db::beginTransaction();
- try {
- $info->check_status = $checkStatus;
- if ($checkStatus == -1) {
- //拒绝录入拒绝理由
- $info->check_note = $params['note'] ?? '无拒绝理由';
- }
- $info->consultant_id = $consultantId;
- $info->current_status = $checkStatus;
- $info->check_time = time();
- $info->check_admin_id = $adminId;
- $info->check_consultant_id = $userId;
- $result = $info->save();
- if ($checkStatus == 2) {
- //将其他待审的相同手机号的改拒绝
- $where = [
- ['mobile' , '=', $mobile],
- ['check_status', '=', 1],
- ['id', '<>', $customId]
- ];
- MarketCustomer::where($where)->update(['current_status' => -1, 'check_time' => time(),'check_status' => -1,'check_note' => self::CHECK_NOTE]);
- }
- Db::commit();
- }catch (BusinessException|\Exception $e){
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- if ($result) {
- return json_success('审核成功');
- } else {
- return json_fail('审核失败');
- }
- }
- /**
- * Notes: 检查客户是否已经登记
- * User: yb
- * Date: 2024/8/21
- * Time: 9:40
- * @param $params
- * @return \support\Response
- */
- public static function checkPhone($params)
- {
- if (empty($params['mobile'])) {
- return json_fail('请输入联系电话');
- }
- $mobile = $params['mobile'];
- //添加和编辑
- if (!empty($params['id'])) {
- //编辑的情况
- if (false === strpos($mobile,'****')) {
- //校验手机号格式
- $validate = new CustomValidate();
- if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
- return json_fail($validate->getError());
- }
- //修改手机号操作
- if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
- return json_fail('客户已经登记过了');
- }
- }
- return json_success('请求成功',['is_ex' => 2]);
- } else {
- $validate = new CustomValidate();
- if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
- return json_fail($validate->getError());
- }
- //添加
- $result = MarketCustomer::checkCustomExists($mobile);
- if ($result) {
- $isEx = 1;
- } else {
- $isEx = 2;
- }
- return json_success('请求成功',['is_ex' => $isEx]);
- }
- }
- /**
- * Notes: 对手机号加密处理
- * User: yb
- * Date: 2024/8/8
- * Time: 15:41
- * @param $val
- * @return string|string[]
- */
- public static function handlePhone($val)
- {
- return substr_replace($val, '****', 3, 4);
- }
- /**
- * Notes: 处理拜访时间
- * User: yb
- * Date: 2024/8/8
- * Time: 16:06
- * @param $val
- * @return float|int
- */
- protected static function handleVisitTime($val) {
- return (strtotime($val) * 1000);
- }
- }
|