Prechádzať zdrojové kódy

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

gorden 1 týždeň pred
rodič
commit
5f87858da6

+ 74 - 63
app/admin/service/consultant/CustomService.php

@@ -4,15 +4,11 @@
 namespace app\admin\service\consultant;
 
 
-use app\admin\service\member\MemberService;
 use app\model\Consultant;
 use app\model\MarketCustomer;
 use app\model\MarketCustomerFollow;
 use app\model\MarketCustomerLogs;
-use app\model\Member;
 use app\model\SysDept;
-use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
-use PhpOffice\PhpSpreadsheet\IOFactory;
 use PhpOffice\PhpSpreadsheet\Reader\Csv;
 use PhpOffice\PhpSpreadsheet\Reader\Xls;
 use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
@@ -42,6 +38,7 @@ class CustomService
         $currentTime = time();
         $diffNums = self::DIFF_TIME;
         $deptIds = TeamService::getIdsByUser();
+        $belongStatus = $params['belong_status'] ?? 1;
         if (false === $deptIds) {
             //无权限
             $where[] = ['dept_id', '=', 0];
@@ -54,6 +51,13 @@ class CustomService
             $where[] = [function($query) use ($deptIds) {
                 $query->whereIn('dept_id', $deptIds);
             }];
+            $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 (visit_time + ({$diffNums} * 4)) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2 AND belong_status = 1)")->whereOr('belong_status','<>', 1);
+            }];
+
         } else {
             if (!empty($params['dept_id'])) {
                 $deptId = end($params['dept_id']);
@@ -76,35 +80,20 @@ class CustomService
                 $query->where('mobile', 'like', "%{$mobile}%");
             }];
         }
-        if (!empty($params['report_status'])) {
-            $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');
+            $consultantIds = Consultant::where('name', 'like', "%{$params['consultant_name']}%")
+                ->where('mobile', 'like', "%{$params['consultant_name']}%")
+                ->pluck('id')->toArray();
+            $where[] = [function($query) use ($consultantIds) {
+                $query->whereIn('consultant_id', $consultantIds);
+            }];
+        }
+        if (!empty($params['report_name'])) {
+            $consultantIds = Consultant::where('name', 'like', "%{$params['report_name']}%")
+                ->where('mobile', 'like', "%{$params['report_name']}%")
+                ->pluck('id')->toArray();
             $where[] = [function($query) use ($consultantIds) {
-                $query->orWhereIn('consultant_id', $consultantIds)->orWhereIn('report_consultant_id', $consultantIds);
+                $query->whereIn('report_consultant_id', $consultantIds);
             }];
         }
         if (!empty($params['type'])) {
@@ -137,11 +126,17 @@ class CustomService
 
         if (!empty($params['current_status'])) {
             $currentStatus = $params['current_status'];
-            if ($currentStatus == 1) {
-                $where[] = ['current_status', '=', $currentStatus];
+            if ($currentStatus == -2) {
+                //检索公池状态
+                $where[] = [function($query) use ($diffNums, $currentTime){
+                    $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND (visit_time + ({$diffNums} * 4)) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2 AND belong_status = 1)");
+                }];
             } else {
-                $where[] = [function($query) use ($currentStatus){
-                    $query->where('current_status', '=', $currentStatus)->where('check_status', '<>', 1);
+                $where[] = ['current_status', '=', $currentStatus];
+                $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)");
                 }];
             }
         }
@@ -494,13 +489,20 @@ class CustomService
         if (!is_array($ids)) {
             $ids = [$ids];
         }
+        Db::beginTransaction();
         try {
             if (is_array($ids)) {
                 MarketCustomer::whereIn('id', $ids)->delete();
+                MarketCustomerFollow::whereIn('market_customer_id', $ids)->delete();//删除跟进记录
+                MarketCustomerLogs::whereIn('market_customer_id', $ids)->delete();//删除移交记录
             } else {
                 MarketCustomer::where('id', $ids)->delete();
+                MarketCustomerFollow::where('market_customer_id', $ids)->delete();//删除跟进记录
+                MarketCustomerLogs::where('market_customer_id', $ids)->delete();//删除移交记录
             }
+            Db::commit();
         } catch (\Exception $e) {
+            Db::rollBack();
             return json_fail('删除失败');
         }
 
@@ -645,7 +647,7 @@ class CustomService
                 $query->whereIn('consultant_id', $consultantIds);
             }];
         }
-        $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id','visit_time'])->get();
+        $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[] = [
@@ -674,10 +676,12 @@ class CustomService
                 if ($currentConsultantId != 0) {
                     //移交至指定顾问
                     //判断一下是否到了到访的过期时间,如果过期了就更新到访时间,没过期时间就不变
-                    $visitTime = $item->visit_time;
-                    $diff = strtotime($visitTime) + $diffTime - time();
-                    if ($diff <= 0) {
-                        $item->visit_time = time();
+                    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();
+                        }
                     }
                 } else {
                     //移交至其他团队
@@ -1222,22 +1226,27 @@ class CustomService
                         $msg = '';
                         if (empty($val[7]) && empty($val[8])) {
                             $msg = $line.'报备人和顾问必须存在其中之一';
-                            throw new \Exception($line.'报备人和顾问必须存在其中之一');
+                            $val[10] = '报备人和顾问必须存在其中之一';
+//                            throw new \Exception($line.'报备人和顾问必须存在其中之一');
                         }
                         if (empty($val[9])) {
                             $msg = $line.'无团队信息';
-                            throw new \Exception($line.'无团队信息');
+                            $val[10] = '无团队信息';
+//                            throw new \Exception($line.'无团队信息');
                         }
                         if ($val[0] == '1970-01-01 08:00:00') {
                             $msg = $line.'日期格式错误';
+                            $val[10] = '日期格式错误';
 //                            throw new \Exception($line.'日期格式错误');
                         }
                         if (empty($mobile)) {
                             $msg = $line.'手机号不能为空';
+                            $val[10] = '手机号不能为空';
 //                            throw new \Exception($line.'手机号不能为空');
                         }
                         if (!preg_match($validate, $mobile)) {
                             $msg = $line.'手机号格式非法';
+                            $val[10] = '手机号格式非法';
 //                            throw new \Exception($line.'手机号格式非法');
                         }
                         $consultantName = $val[7];
@@ -1249,6 +1258,7 @@ class CustomService
                         $reportConsultantId = $teamKeys[$reportKey]['consultant_id'] ?? 0;
                         if ($consultantId <= 0 && $reportConsultantId <= 0) {
                             $msg = $line.'健康顾问或报备人都不存在';
+                            $val[10] = '健康顾问或报备人都不存在';
 //                            throw new \Exception($line.'健康顾问或报备人都不存在');
                         }
                         if ($consultantId > 0) {
@@ -1263,37 +1273,38 @@ class CustomService
                         }
                         if (empty($deptId)) {
                             $msg =  $line.'健康顾问或报备人都不存在';
+                            $val[10] = '健康顾问或报备人都不存在';
 //                            throw new \Exception($line.'团队不存在');
                         }
                         if (MarketCustomer::checkCustomExists($val[2])) {
                             $msg =  $line.'客户已转到访,无法二次录入';
+                            $val[10] = '客户已转到访,无法二次录入';
 //                            throw new \Exception($line.'客户已转到访,无法二次录入');
                         }
-                        //查看手机号是否存在
-                        $insertData = [
-                            'name' => $val[1],
-                            'mobile' => $mobile,
-                            'level' => $level ?? null,
-                            'consultant_id' => $consultantId,
-                            'report_consultant_id' => $reportConsultantId,
-                            'create_consultant_id' => $reportConsultantId,
-                            'dept_id' => $deptId,
-                            'belong_status' => 1,
-                            'current_status' => 2,
-                            'check_status' => 2,
-                            'check_note' => '导入的客户信息,无审核人员',
-                            'type' => 1,
-                            'visit_time' => strtotime('2024-08-31 00:00:00'),
-                            'created_at' => strtotime('2024-08-31 00:00:00')
-                        ];
+
                         $result = false;
                         if (empty($msg)) {
+                            //查看手机号是否存在
+                            $insertData = [
+                                'name' => $val[1],
+                                'mobile' => $mobile,
+                                'level' => $level ?? null,
+                                'consultant_id' => $consultantId,
+                                'report_consultant_id' => $reportConsultantId,
+                                'create_consultant_id' => $reportConsultantId,
+                                'dept_id' => $deptId ?? 0,
+                                'belong_status' => 1,
+                                'current_status' => 2,
+                                'check_status' => 2,
+                                'check_note' => '导入的客户信息,无审核人员',
+                                'type' => 1,
+                                'visit_time' => strtotime($val[0]),
+                                'created_at' => strtotime($val[0])
+                            ];
                             $result = Db::table('market_customer')->insert($insertData);
                         } else {
-                            $errorData[] = [
-                                'msg' => $msg,
-                                'val' => $val
-                            ];
+                            $val[11] = $line;
+                            $errorData[] = $val;
                         }
                         if ($result) {
                             $sucNums++;

+ 55 - 13
app/wechat/service/CustomService.php

@@ -58,14 +58,20 @@ class CustomService
         $type = $consultantInfo->type;
         $currentTime = time();
         $diffNums = self::DIFF_TIME;
-        $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)");
-        }]
-        ];
+        $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 {
+            $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);
@@ -95,11 +101,39 @@ class CustomService
             //所有团队的
 
         }
-        if (!empty($params['custom'])) {
-            $keywords = $params['custom'];
-            $where[] = [function($query) use ($keywords) {
-                $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
-            }];
+        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) {
@@ -587,7 +621,7 @@ class CustomService
         if (!empty($consultantId)) {
             $whereCustom[] = ['consultant_id', '=', $consultantId];
         }
-        $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id'])->get();
+        $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[] = [
@@ -609,10 +643,18 @@ class CustomService
         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();

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

@@ -374,7 +374,8 @@ class UserService
         $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)")
+                ->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);
         }];
         //客户总量
         $customNums = MarketCustomer::where($where)->count();