Browse Source

Merge branch 'master' of http://39.98.194.76:3000/txct/wanyue_app

gorden 6 months ago
parent
commit
f2858b4705

+ 10 - 3
app/admin/controller/consultant/CustomController.php

@@ -121,12 +121,19 @@ class CustomController extends Curd
     public function moveCustom(Request $request)
     {
         $params = $request->post();
-        if (empty($params['consultant_id'])) {
-            return json_fail('请选择目标顾问');
+        if (empty($params['move_type'])) {
+            if (empty($params['consultant_id'])) {
+                return json_fail('请选择目标顾问');
+            }
+        } else {
+            if (empty($params['dept_id'])) {
+                return json_fail('请选择目标团队');
+            }
         }
         if (empty($params['move_market_customer_id']) && empty($params['move_consultant_id'])) {
             return json_fail('请选择需要转移的客户或顾问');
         }
+
         return CustomService::moveCustom($params);
     }
 
@@ -144,7 +151,7 @@ class CustomController extends Curd
     }
 
     /**
-     * Notes: 报备审核
+     * Notes: 转到访审核
      * User: yb
      * Date: 2024/8/16
      * Time: 15:55

+ 12 - 4
app/admin/service/consultant/ConsultantService.php

@@ -112,10 +112,14 @@ class ConsultantService
         }
         $type = 2;
         if (!empty($params['type'])) {
-            if (in_array($params['type'], [1,2])) {
+            if (in_array($params['type'], [1,2,3])) {
                 $type = $params['type'];
-                if ($type == 2) {
+                if ($type != 1) {
                     $params['relation_user_id'] = null;
+                } else {
+                    if (empty($params['relation_user_id'])) {
+                        return json_fail('请绑定后台账号');
+                    }
                 }
             }
         }
@@ -175,10 +179,14 @@ class ConsultantService
         }
         $type = 2;
         if (!empty($params['type'])) {
-            if (in_array($params['type'], [1,2])) {
+            if (in_array($params['type'], [1,2,3])) {
                 $type = $params['type'];
-                if ($type == 2) {
+                if ($type != 1) {
                     $params['relation_user_id'] = null;
+                } else {
+                    if (empty($params['relation_user_id'])) {
+                        return json_fail('请绑定后台账号');
+                    }
                 }
             }
         }

+ 118 - 58
app/admin/service/consultant/CustomService.php

@@ -72,28 +72,29 @@ class CustomService
             }];
         }
         if (!empty($params['report_status'])) {
-            if ($params['report_status'] == -1) {
-                //待审核
-                $where[] = ['check_status', '=', 1];
-            } else {
-                $where[] = ['check_status', '=', 2];
-            }
-            if ($params['report_status'] == 1) {
-                //已报备
-                $where[] = [function($query) use ($diffNums, $currentTime) {
-                    $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
-                }];
-            } else if ($params['report_status'] == 3) {
-                //已锁定
-                $where[] = [function($query) {
-                    $query->whereIn('current_status', [2,3,4]);
-                }];
-            } 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 AND check_status = 2)");
-                }];
-            }
+            $where[] = ['check_status', '=', $params['report_status']];
+//            if ($params['report_status'] == -1) {
+//                //待审核
+//                $where[] = ['check_status', '=', 1];
+//            } else {
+//                $where[] = ['check_status', '=', 2];
+//            }
+//            if ($params['report_status'] == 1) {
+//                //已报备
+//                $where[] = [function($query) use ($diffNums, $currentTime) {
+//                    $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
+//                }];
+//            } else if ($params['report_status'] == 3) {
+//                //已锁定
+//                $where[] = [function($query) {
+//                    $query->whereIn('current_status', [2,3,4]);
+//                }];
+//            } 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 AND check_status = 2)");
+//                }];
+//            }
         }
         if (!empty($params['consultant_name'])) {
             $consultantIds = Consultant::where('name', 'like', "%{$params['consultant_name']}%")->pluck('id');
@@ -131,9 +132,13 @@ class CustomService
 
         if (!empty($params['current_status'])) {
             $currentStatus = $params['current_status'];
-            $where[] = [function($query) use ($currentStatus){
-                $query->where('current_status', '=', $currentStatus)->where('check_status', '=', 2);
-            }];
+            if ($currentStatus == 1) {
+                $where[] = ['current_status', '=', $currentStatus];
+            } else {
+                $where[] = [function($query) use ($currentStatus){
+                    $query->where('current_status', '=', $currentStatus)->where('check_status', '<>', 1);
+                }];
+            }
         }
         if (!empty($params['consultant_id'])) {
             $where[] = ['consultant_id', '=', $params['consultant_id']];
@@ -203,6 +208,24 @@ class CustomService
                 $where[] = ['level', '=', ($params['level'] + 1)];
             }
         }
+        if (!empty($params['belong_status'])) {
+            $belongStatus = $params['belong_status'];
+            if ($belongStatus == 1) {
+                //未流入公池
+                $where[] = ['belong_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)");
+                }];
+            } else {
+                //已经流入公池
+                $where[] = [function($query) use ($diffNums, $currentTime){
+                    $query->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 2 AND check_status = 2 AND belong_status = 1)")->whereOr('belong_status','<>', 1);
+                }];
+            }
+        }
+
 
         return $where;
     }
@@ -303,6 +326,7 @@ class CustomService
             'check_status' => 1,
             'visit_time' => $params['visit_time'] ?? null,
             'note' => $params['note'] ?? '',
+            'belong_status' => 1,
             'current_status' => $params['current_status'] ?? null,
             'created_at' => time()
         ];
@@ -533,15 +557,21 @@ class CustomService
     public static function moveCustom($params)
     {
         $userId = JwtToken::getCurrentId();
-        $currentConsultantId = $params['consultant_id']; //当前顾问
+        $moveType = $params['move_type'] ?? 0;
+        if ($moveType == 1) {
+            $currentDeptId = $params['dept_id'];
+            $currentConsultantId = 0;
+        } else {
+            $currentConsultantId = $params['consultant_id'] ?? 0; //当前顾问
+            $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
+            if (empty($consultantInfo)) {
+                return json_fail('顾问不存在');
+            }
+            $currentDeptId = $consultantInfo->dept_id; //当前部门
+        }
         $consultantIds = $params['move_consultant_id'] ?? [];
         $customIds = $params['move_market_customer_id'] ?? [];
         $note = $params['note'] ?? '';
-        $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
-        if (empty($consultantInfo)) {
-            return json_fail('顾问不存在');
-        }
-        $currentDeptId = $consultantInfo->dept_id; //当前部门
         $logsInertData  = [];
         $now = time();
         $whereCustom = [];
@@ -580,6 +610,13 @@ class CustomService
         $result = false;
         try {
             foreach ($customList as $item) {
+                if ($currentConsultantId != 0) {
+                    //移交至指定顾问
+                    $item->visit_time = time();
+                } else {
+                    //移交至其他团队
+                    $item->visit_time = time() - self::DIFF_TIME;
+                }
                 $item->consultant_id = $currentConsultantId;
                 $item->dept_id = $currentDeptId;
                 $item->save();
@@ -682,16 +719,26 @@ class CustomService
                 $query->whereBetween('created_at', $createdDate);
             }];
         }
-        $paginator = MarketCustomerLogs::with(['custom', 'beforeMan', 'currentMan', 'opBackPerson', 'opFrontPerson'])
-            ->whereHas('custom', function ($query) use ($whereCustom) {
-                $query->where($whereCustom);
-            })
-            ->whereHas('currentMan', function ($query) use ($whereCurrent) {
-                $query->where($whereCurrent);
-            })
-            ->whereHas('beforeMan', function ($query) use ($whereBefore) {
-                $query->where($whereBefore);
-            })
+        $paginator = MarketCustomerLogs::with(['custom' => function($query) {
+            $query->select('id', 'name', 'mobile');
+        }, 'beforeMan' => function($query) {
+            $query->select('id', 'name', 'mobile');
+        }, 'currentMan' => function($query) {
+            $query->select('id', 'name', 'mobile');
+        }, 'beforeDept' => function($query) {
+            $query->select('dept_id', 'dept_name');
+        }, 'currentDept' => function($query) {
+            $query->select('dept_id', 'dept_name');
+        }])
+//            ->whereHas('custom', function ($query) use ($whereCustom) {
+//                $query->where($whereCustom);
+//            })
+//            ->whereHas('currentMan', function ($query) use ($whereCurrent) {
+//                $query->where($whereCurrent);
+//            })
+//            ->whereHas('beforeMan', function ($query) use ($whereBefore) {
+//                $query->where($whereBefore);
+//            })
             ->where($where)
             ->orderBy('created_at', 'desc')
             ->paginate($limit, '*', 'page', $page);
@@ -731,7 +778,7 @@ class CustomService
         }
         $status = $info->check_status;
         if ($status != 1) {
-            return json_fail('客户不是待审核状态');
+            return json_fail('客户不是待转到访状态');
         }
         $mobile = $info->mobile;
         if ($checkStatus == 2) {
@@ -743,10 +790,15 @@ class CustomService
         Db::beginTransaction();
         try {
             $info->check_status = $checkStatus;
+            $info->current_status = $checkStatus;
             if ($checkStatus == -1) {
                 //拒绝录入拒绝理由
                 $info->check_note = $params['note'] ?? '无拒绝理由';
             }
+            if ($checkStatus == 2) {
+                //更新到访保护期时间
+                $info->visit_time = time();
+            }
             $info->check_time = time();
             $info->check_admin_id = $adminId;
             $result = $info->save();
@@ -757,7 +809,7 @@ class CustomService
                     ['check_status', '=', 1],
                     ['id', '<>', $customId]
                 ];
-                MarketCustomer::where($where)->update(['check_time' => time(),'check_status' => -1,'check_note' => '客户已经被其他顾问报备']);
+                MarketCustomer::where($where)->update(['check_time' => time(),'check_status' => -1, 'current_status' => -1,'check_note' => '客户已经被其他登记']);
             }
             Db::commit();
         }catch (BusinessException|\Exception $e){
@@ -765,9 +817,9 @@ class CustomService
             return json_fail($e->getMessage());
         }
         if ($result) {
-            return json_success('审核成功');
+            return json_success('操作成功');
         } else {
-            return json_fail('审核失败');
+            return json_fail('操作失败');
         }
 
 
@@ -786,14 +838,18 @@ class CustomService
         $where = self::commonSearch($params);
         //客户总数
         $total = MarketCustomer::where($where)->count();
-        //待审核
-        $checkTotal = MarketCustomer::where($where)->where('check_status', 1)->count();
+        //公池客户
+        $pubTotal = MarketCustomer::where($where)->where([[function($query){
+            $diffNums = self::DIFF_TIME;
+            $query->orWhereRaw("((visit_time + {$diffNums}) - UNIX_TIMESTAMP() <= 0 AND current_status = 2 AND check_status = 2 AND belong_status = 1)")->whereOr('belong_status','<>', 1);
+        }]])->count();
         //已报备
-        $reportTotal = MarketCustomer::where($where)->where(
+        $checkTotal = MarketCustomer::where($where)->where('check_status', 1)->count();
+        //已到访
+        $visitTotal = MarketCustomer::where($where)->where(
             [[function($query) {
-                $currentTime = time();
                 $diffNums = self::DIFF_TIME;
-                $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0) AND current_status = 1 AND check_status = 2");
+                $query->whereRaw("((visit_time + {$diffNums}) - UNIX_TIMESTAMP() > 0) AND current_status = 2 AND check_status = 2")->where('belong_status', 1);
             }]]
         )->count();
         //已来电
@@ -801,31 +857,35 @@ class CustomService
             $query->where('check_status', '=', 2)->where('current_status', '=', 1);
         }]])->count();
         //已到访
-        $visitTotal = MarketCustomer::where($where)->where([[function($query) {
-            $query->where('check_status', '=', 2)->where('current_status', '=', 2);
-        }]])->count();
+//        $visitTotal = MarketCustomer::where($where)->where([[function($query) {
+//            $query->where('check_status', '=', 2)->where('current_status', '=', 2);
+//        }]])->count();
         //已缴费
         $payTotal = MarketCustomer::where($where)->where([[function($query) {
-            $query->where('check_status', '=', 2)->where('current_status', '=', 3);
+            $query->where('check_status', '=', 2)->where('current_status', '=', 3)->where('belong_status', 1);
         }]])->count();
         //已成交
         $dealTotal = MarketCustomer::where($where)->where([[function($query) {
-            $query->where('check_status', '=', 2)->where('current_status', '=', 4);
+            $query->where('check_status', '=', 2)->where('current_status', '=', 4)->where('belong_status', 1);
         }]])->count();
-        //已到访 + 已缴费 + 已成交 / 客户总数
+        //已缴费 + 已成交 / 客户总数
+        //报备失败
+        $failTotal = MarketCustomer::where($where)->where('current_status', -1)->count();
         $rant = 0;
         if ($total > 0) {
-            $rant = $payTotal + $dealTotal / $total;
+            $rant = ($payTotal + $dealTotal) / $total;
         }
         $rant = sprintf('%.2f', $rant);
         $data = [
             'total' => $total,
+            'pub_total' => $pubTotal,
             'check_total' => $checkTotal,
-            'report_total' => $reportTotal,
+            'report_total' => 0,
             'mobile_total' => $mobileTotal,
             'visit_total' => $visitTotal,
             'pay_total' => $payTotal,
             'deal_total' => $dealTotal,
+            'fail_total' => $failTotal,
             'rant' => $rant,
         ];
         return json_success('请求成功', $data);

+ 13 - 7
app/model/MarketCustomer.php

@@ -144,14 +144,20 @@ class MarketCustomer extends Model
     {
         $where = [
             ['mobile', '=', $mobile],
+            ['check_status', '=', 2],
             [function($query) {
-            $query->orWhere(function ($query){
-                $diffNums = (60 * 60 * 24 * 30);
-                $currentTime = time();
-                $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
-            })->orWhereIn('current_status', [2,3,4]);
-            }],
-            ['check_status', '=', 2]
+            $query->whereIn('current_status', [2,3,4]);
+            }]
+//            [function($query) {
+//            $query->orWhere(function ($query){
+//                $diffNums = (60 * 60 * 24 * 30);
+//                $currentTime = time();
+//                $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 2);
+//            })->orWhereIn('current_status', [2,3,4]);
+//            }],
+
+
+
         ];
         if (!empty($id)) {
             $where[] = ['id', '<>', $id];

+ 7 - 0
app/model/MarketCustomerLogs.php

@@ -42,4 +42,11 @@ class MarketCustomerLogs extends Model
         return $this->belongsTo(Consultant::class, 'change_consultant_id');
     }
 
+    public function beforeDept(){
+        return $this->belongsTo(SysDept::class, 'before_dept_id');
+    }
+
+    public function currentDept(){
+        return $this->belongsTo(SysDept::class, 'dept_id');
+    }
 }

+ 76 - 46
app/wechat/service/CustomService.php

@@ -16,6 +16,7 @@ use Tinywan\Jwt\JwtToken;
 class CustomService
 {
     const DIFF_TIME = (60 * 60 * 24 * 30);
+    const CHECK_NOTE = '该客户已在其他顾问那到访';
     /**
      * Notes: 选项配置
      * User: yb
@@ -57,7 +58,14 @@ class CustomService
         $type = $consultantInfo->type;
         $currentTime = time();
         $diffNums = self::DIFF_TIME;
-        $where = [];
+        $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)")
+                ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
+        }]
+        ];
         $whereFollow = [];
         if ($type == 1) {
             //团队下所有的
@@ -75,10 +83,13 @@ class CustomService
                 }];
             }
 
-        } else {
+        } else if ($type == 2){
             //个人的
             $where[] = ['consultant_id', '=', $consultantId];
             $whereFollow[] = ['consultant_id', '=', $consultantId];
+        } else {
+            //所有团队的
+
         }
         if (!empty($params['custom'])) {
             $keywords = $params['custom'];
@@ -125,7 +136,7 @@ class CustomService
         if (!empty($params['current_status'])) {
             $currentStatus = $params['current_status'];
             $where[] = [function($query) use ($currentStatus) {
-                $query->where('current_status', $currentStatus)->where('check_status', 2);
+                $query->where('current_status', $currentStatus);
             }];
         }
         if (!empty($params['created_at'])) {
@@ -162,14 +173,14 @@ class CustomService
             }
         }
         //本日新增
-        $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();
+//        $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,
-            'new_custom_num' => $newNums,
+            'new_follow_num' => $followNums ?? 0,
+            'new_custom_num' => $newNums ?? 0,
             'total' => $total,
             'rows' => $items
         ];
@@ -187,31 +198,21 @@ class CustomService
      */
     public static function myCustomList($params)
     {
+        $currentTime = time();
+        $diffNums = self::DIFF_TIME;
         $page = $params['page'] ?? 1;
         $size = $params['size'] ?? 10;
         //查询有效顾问信息
         $consultantId = JwtToken::getCurrentId();
-        $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
-        $deptId = $consultantInfo->dept_id;
-        $type = $consultantInfo->type;
-        $where = [];
-        if ($type == 1) {
-            //团队下所有的
-            $deptIds = TeamService::getIds($deptId);
-            $where[] = [function($query) use ($deptIds) {
-                $query->whereIn('dept_id', $deptIds);
-            }];
-        } else {
-            //个人的
-            $where[] = ['consultant_id', '=', $consultantId];
-        }
-        $where[] = ['check_status', '=', 2];
-        $where[] = [function($query) {
-            $query->orWhere(function ($query){
-                $diffNums = (60 * 60 * 24 * 30);
-                $currentTime = time();
-                $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
-            })->orWhereIn('current_status', [2,3,4]);
+        $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'];
@@ -246,7 +247,7 @@ class CustomService
         $mobile = $params['mobile'];
         //查询客户手机号是否已经存在
         if (MarketCustomer::checkCustomExists($mobile)) {
-            return json_fail('客户已经登记过了');
+            return json_fail('客户已经到访');
         }
         //查询顾问信息
         $consultantId = JwtToken::getCurrentId();
@@ -274,14 +275,10 @@ class CustomService
             'visit_time' => time(),
             'note' => $params['note'] ?? '',
             'check_status' => 1,
-            'current_status' => $params['current_status'] ?? null,
+            'current_status' => 1,
+            'belong_status' => 1,
             'created_at' => time()
         ];
-        if ($insertData['type'] == 1) {
-            $insertData['current_status'] = 1;
-        } else if ($insertData['type'] == 2) {
-            $insertData['current_status'] = 2;
-        }
         Db::beginTransaction();
         try {
             $customId = MarketCustomer::insertGetId($insertData);
@@ -414,19 +411,51 @@ class CustomService
     {
         $customId = $params['id'];
         $currentStatus = $params['current_status'];
-        if (!in_array($currentStatus, [-1, 3, 4])) {
+        if (!in_array($currentStatus, [-1, 2, 3, 4])) {
             return json_fail('状态值非法');
         }
         $info = MarketCustomer::firstWhere(['id' => $customId]);
         if (empty($info)) {
             return json_fail('客户不存在');
         }
-        $info->current_status = $currentStatus;
-        $result = $info->save();
+        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('更新成功');
+            return json_success('操作成功');
         } else {
-            return json_fail('更新失败');
+            return json_fail('操作失败');
         }
     }
 
@@ -445,7 +474,7 @@ class CustomService
         if (empty($userInfo)) {
             return json_fail('管理员信息不存在');
         }
-        if ($userInfo->type != 1) {
+        if (!in_array($userInfo->type, [1,3])) {
             return json_fail('操作权限不足');
         }
         $relationUserId = $userInfo->relation_user_id;
@@ -566,7 +595,7 @@ class CustomService
         }
         $status = $info->check_status;
         if ($status != 1) {
-            return json_fail('客户不是待审核状态');
+            return json_fail('客户不是待状态');
         }
         $mobile = $info->mobile;
         if ($checkStatus == 2) {
@@ -582,6 +611,7 @@ class CustomService
                 //拒绝录入拒绝理由
                 $info->check_note = $params['note'] ?? '无拒绝理由';
             }
+            $info->current_status = $checkStatus;
             $info->check_time = time();
             $info->check_admin_id = $adminId;
             $info->check_consultant_id = $userId;
@@ -593,7 +623,7 @@ class CustomService
                     ['check_status', '=', 1],
                     ['id', '<>', $customId]
                 ];
-                MarketCustomer::where($where)->update(['check_time' => time(),'check_status' => -1,'check_note' => '客户已经被其他顾问报备']);
+                MarketCustomer::where($where)->update(['current_status' => -1, 'check_time' => time(),'check_status' => -1,'check_note' => self::CHECK_NOTE]);
             }
             Db::commit();
         }catch (BusinessException|\Exception $e){

+ 9 - 17
app/wechat/service/FollowService.php

@@ -25,24 +25,21 @@ class FollowService
         //查询客户是否存在
         $where = [
             ['consultant_id', '=', $consultantId],
-            ['id', '=', $params['market_customer_id']]
+            ['id', '=', $params['market_customer_id']],
+            [function($query){
+            $diffNums = CustomService::DIFF_TIME;
+                $query->orWhereRaw("check_status IN (-1, 1)")
+                    ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
+                    ->orWhereRaw("((visit_time + {$diffNums}) - UNIX_TIMESTAMP() > 0 AND current_status = 2 AND check_status = 2)");
+            }],
         ];
         $customInfo = MarketCustomer::where($where)->first();
         if (empty($customInfo)) {
             return json_fail('客户不存在');
         }
         $currentStatus = $customInfo->current_status;
-        if ($currentStatus == -1) {
-            return json_fail('无效客户,无法跟进');
-        }
-        if ($currentStatus == 1) {
-            $visitTime = $customInfo->visit_time;
-            $now = time();
-            $start = strtotime($visitTime);
-            $end = $start + CustomService::DIFF_TIME;
-            if ($end < $now) {
-                return json_fail('已失效,请重新添加客户信息');
-            }
+        if ($currentStatus == -1 || $customInfo->check_status == -1) {
+            return json_fail('报备失败客户,无法跟进');
         }
         $insertData = [
             'market_customer_id' => $params['market_customer_id'],
@@ -57,11 +54,6 @@ class FollowService
         $result = false;
         Db::beginTransaction();
         try {
-            if ($currentStatus == 1) {
-                if ($insertData['visit_type'] == 2) {
-                    $customInfo->current_status = 2;
-                }
-            }
             $customInfo->follow_num = $customInfo->follow_num + 1;
             $customInfo->level = $insertData['intention_type'];
             $customInfo->visit_time = $insertData['follow_time'];

+ 26 - 34
app/wechat/service/UserService.php

@@ -332,9 +332,12 @@ class UserService
                 $where[] = ['id', '<>', $userId];
             }
             $ids = Consultant::where($where)->pluck('id')->toArray();
-        } else {
+        } else if ($userInfo->type == 2){
             //普通账号
             $ids[] = $userId;
+        } else if ($userInfo->type == 3) {
+            //判客账户
+            $ids = Consultant::pluck('id')->toArray();
         }
         return $ids;
     }
@@ -352,11 +355,16 @@ class UserService
         $currentTime = time();
         $diffNums = CustomService::DIFF_TIME;
 
-        $where = [
+        $whereFollow = $where = [
             [function($query) use ($userIds) {
             $query->whereIn('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)")
+                ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
+        }];
         //客户总量
         $customNums = MarketCustomer::where($where)->count();
         if (!empty($params['time'])) {
@@ -372,18 +380,15 @@ class UserService
         $where[] = [function($query) use ($start, $end) {
             $query->whereBetween('created_at', [$start, $end]);
         }];
-        //待审核客户数量
-        $checkCustomNums = MarketCustomer::where($where)->where('check_status', '=', 1)->count();
+        $whereFollow[] = [function($query) use ($start, $end) {
+            $query->whereBetween('created_at', [$start, $end]);
+        }];
+        //待转到访客户数量
+        $checkCustomNums = MarketCustomer::where($where)->where('check_status', 1)->where('current_status', 1)->count();
         //新增客户
         $newCustomNums = MarketCustomer::where($where)->count();
         //已报备客户数
-        $whereReport = [
-            [function($query) use ($diffNums, $currentTime) {
-                $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
-            }],
-            ['check_status', '=', 2]
-        ];
-        $reportCustomNums = MarketCustomer::where($where)->where($whereReport)->count();
+        $reportCustomNums = MarketCustomer::where($where)->where('current_status', 1)->count();
         //已到访客户数量
         $visitCustomNums = MarketCustomer::where($where)->where('current_status', 2)->where('check_status', 2)->count();
         //已缴费客户数量
@@ -391,25 +396,13 @@ class UserService
         //已成交客户数量
         $dealCustomNums = MarketCustomer::where($where)->where('current_status', 4)->where('check_status', 2)->count();
         //新增跟进记录数量
-        $newFollowNums = MarketCustomerFollow::where($where)->count();
-        //男女性别占比
-        $whereEffective = [
-            [function($query) {
-                $query->orWhere(function ($query){
-                    $diffNums = (60 * 60 * 24 * 30);
-                    $currentTime = time();
-                    $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
-                })->orWhereIn('current_status', [2,3,4]);
-            }],
-            ['check_status', '=', 2]
-        ];
-
+        $newFollowNums = MarketCustomerFollow::where($whereFollow)->count();
         /* 男女数量 */
         $genderDataNums = [
             ['label' => '男', 'value' => 1, 'nums' => 0],
             ['label' => '女', 'value' => 2, 'nums' => 0],
         ];
-        $genderData = MarketCustomer::where($where)->select(Db::raw('gender, count(*) as nums'))->where($whereEffective)->groupBy('gender')->get();
+        $genderData = MarketCustomer::where($where)->select(Db::raw('gender, count(*) as nums'))->groupBy('gender')->get();
         if (!$genderData->isEmpty()) {
             $genderData = $genderData->toArray();
             foreach ($genderDataNums as $genderKey => $genderVal) {
@@ -428,7 +421,7 @@ class UserService
             ['label' => 'D类', 'value' => 4, 'nums' => 0],
             ['label' => '其他', 'value' => "", 'nums' => 0],
         ];
-        $levelData = MarketCustomer::where($where)->select(Db::raw('level, count(*) as nums'))->where($whereEffective)->groupBy('level')->get();
+        $levelData = MarketCustomer::where($where)->select(Db::raw('level, count(*) as nums'))->groupBy('level')->get();
         if (!$levelData->isEmpty()) {
             $levelData = $levelData->toArray();
             $levelData = array_column($levelData, 'nums', 'level');
@@ -461,20 +454,19 @@ class UserService
      */
     public static function customTrend($params)
     {
+        $currentTime = time();
+        $diffNums = CustomService::DIFF_TIME;
         //统计客户总数
         $userIds = self::getIds();
         $where = [
             [function($query) use ($userIds) {
                 $query->whereIn('consultant_id', $userIds);
             }],
-            [function($query) {
-                $query->orWhere(function ($query){
-                    $diffNums = (60 * 60 * 24 * 30);
-                    $currentTime = time();
-                    $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
-                })->orWhereIn('current_status', [2,3,4]);
-            }],
-            ['check_status', '=', 2]
+            [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)");
+            }]
         ];
         $whereTrend = [];
         if (!empty($params['type'])) {