Browse Source

去除来访方式、新增报备人

yb 6 months ago
parent
commit
9bf4101400

+ 5 - 0
app/model/MarketCustomer.php

@@ -169,4 +169,9 @@ class MarketCustomer extends Model
     {
         return $this->belongsTo(Consultant::class, 'consultant_id');
     }
+
+    public function report()
+    {
+        return $this->belongsTo(Consultant::class, 'report_consultant_id');
+    }
 }

+ 12 - 0
app/wechat/controller/CustomController.php

@@ -178,4 +178,16 @@ class CustomController extends IndexController
         $params = $request->post();
         return CustomService::checkPhone($params);
     }
+
+    /**
+     * Notes: 指定顾问
+     * User: yb
+     * Date: 2024/9/5
+     * Time: 12:51
+     */
+    public function appointConsultant(Request $request)
+    {
+        $params = $request->post();
+        return CustomService::appoint($params);
+    }
 }

+ 86 - 9
app/wechat/service/CustomService.php

@@ -59,7 +59,6 @@ class CustomService
         $currentTime = time();
         $diffNums = self::DIFF_TIME;
         $where = [
-            ['consultant_id', '<>', 0],
             [function($query) use ($diffNums, $currentTime){
             $query->orWhereRaw("check_status IN (-1, 1)")
                 ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
@@ -74,7 +73,10 @@ class CustomService
                 $query->whereIn('dept_id', $deptIds);
             }];
             if (!empty($params['consultant_id'])) {
-                $where[] = ['consultant_id', '=', $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);
@@ -85,7 +87,9 @@ class CustomService
 
         } else if ($type == 2){
             //个人的
-            $where[] = ['consultant_id', '=', $consultantId];
+            $where[] = [function($query) use ($consultantId){
+                $query->orWhere('consultant_id', $consultantId)->orWhere('report_consultant_id', $consultantId);
+            }];
             $whereFollow[] = ['consultant_id', '=', $consultantId];
         } else {
             //所有团队的
@@ -156,7 +160,7 @@ class CustomService
         $fn = function ($query) {
             $query->select('name', 'mobile', 'id');
         };
-        $paginator = MarketCustomer::with(['consultant' => $fn])->where($where)->orderBy('visit_time', 'desc')->paginate($size, '*', 'page', $page);
+        $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)) {
@@ -259,7 +263,9 @@ class CustomService
         $insertData = [
             'name' => $params['name'],
             'mobile' => $mobile,
-            'consultant_id' => $consultantId,
+            'consultant_id' => 0,
+            'report_consultant_id' => $consultantId,
+            'create_consultant_id' => $consultantId,
             'dept_id' => $deptId,
             'gender' => $params['gender'] ?? null,
             'visit_type' => $params['visit_type'] ?? null,
@@ -381,15 +387,15 @@ class CustomService
      */
     public static function info($id)
     {
-        $customInfo = MarketCustomer::firstWhere(['id' => $id]);
+        $fn = function ($query) {
+            $query->select('name', 'mobile', 'id');
+        };
+        $customInfo = MarketCustomer::with(['consultant' => $fn, 'report' => $fn])->firstWhere(['id' => $id]);
         if (empty($customInfo)) {
             return json_fail('客户不存在');
         }
         //手机号加密处理
         $customInfo->mask_mobile = self::handlePhone($customInfo->mobile);
-        //查询负责顾问
-        $consultantName = Consultant::where('id', $customInfo->consultant_id)->value('name');
-        $customInfo->consultant_name = $consultantName;
         //查询最新的跟进记录
         $follow = MarketCustomerFollow::where('market_customer_id', $id)->orderBy('created_at', 'desc')->first();
         $customInfo->follow = $follow;
@@ -459,6 +465,75 @@ class CustomService
         }
     }
 
+    /**
+     * 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
@@ -603,6 +678,7 @@ class CustomService
                 return json_fail('客户已经存在');
             }
         }
+        $consultantId = $params['consultant_id'];
         $result = false;
         Db::beginTransaction();
         try {
@@ -611,6 +687,7 @@ class CustomService
                 //拒绝录入拒绝理由
                 $info->check_note = $params['note'] ?? '无拒绝理由';
             }
+            $info->consultant_id = $consultantId;
             $info->current_status = $checkStatus;
             $info->check_time = time();
             $info->check_admin_id = $adminId;

+ 4 - 2
app/wechat/service/FollowService.php

@@ -24,7 +24,7 @@ class FollowService
         $consultantId = JwtToken::getCurrentId();
         //查询客户是否存在
         $where = [
-            ['consultant_id', '=', $consultantId],
+//            ['consultant_id', '=', $consultantId],
             ['id', '=', $params['market_customer_id']],
             [function($query){
             $diffNums = CustomService::DIFF_TIME;
@@ -86,7 +86,9 @@ class FollowService
         //查询顾问下的客户信息
         $customIds = [];
         if (!empty($ids)) {
-            $customIds = MarketCustomer::whereIn('consultant_id', $ids)->pluck('id')->toArray();
+            $customIds = MarketCustomer::where(function ($query) use ($ids) {
+                $query->orWhereIn('consultant_id', $ids)->orWhereIn('report_consultant_id', $ids);
+            })->pluck('id')->toArray();
         }
         $where = [
             [function($query) use ($customIds) {

+ 6 - 2
app/wechat/service/UserService.php

@@ -355,11 +355,15 @@ class UserService
         $currentTime = time();
         $diffNums = CustomService::DIFF_TIME;
 
-        $whereFollow = $where = [
+        $whereFollow = [
             [function($query) use ($userIds) {
-            $query->whereIn('consultant_id', $userIds);
+                $query->whereIn('consultant_id', $userIds);
             }]
         ];
+        $where = [];
+        $where[] = [function($query) use ($userIds) {
+            $query->orWhereIn('consultant_id', $userIds)->orWhereIn('report_consultant_id', $userIds);
+        }];
         $where[] = [function($query) use ($diffNums, $currentTime){
             $query->orWhereRaw("check_status IN (-1, 1)")
                 ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")

+ 2 - 1
app/wechat/validate/CustomValidate.php

@@ -15,7 +15,8 @@ class CustomValidate extends Validate
         'gender|客户性别' => 'require',
         'type|来访方式' => 'require',
         'current_status|当前状态' => 'require',
-        'check_status|报备状态' => 'require'
+        'check_status|报备状态' => 'require',
+        'consultant_id|指定顾问' => 'require'
     ];
 
     protected $message = [];

+ 1 - 0
route/wechat.php

@@ -29,6 +29,7 @@ Route::group('/wechat',function (){
         Route::post('/logs', [\app\wechat\controller\CustomController::class, 'moveLogs']);
         Route::post('/check', [\app\wechat\controller\CustomController::class, 'checkCustom']);
         Route::post('/mobile', [\app\wechat\controller\CustomController::class, 'checkMobile']);
+        Route::post('/appoint', [\app\wechat\controller\CustomController::class, 'appointConsultant']);
     });
     Route::group('/follow', function () {
         Route::post('/add', [\app\wechat\controller\FollowController::class, 'addFollow']);