diff --git a/admin/src/app/lang/zh-cn/customer_resources.customer_resources.json b/admin/src/app/lang/zh-cn/customer_resources.customer_resources.json index e4737f12..c0017596 100644 --- a/admin/src/app/lang/zh-cn/customer_resources.customer_resources.json +++ b/admin/src/app/lang/zh-cn/customer_resources.customer_resources.json @@ -3,7 +3,7 @@ "sourcePlaceholder": "请输入来源", "sourceChannel": "来源渠道", "sourceChannelPlaceholder": "请输入来源渠道", - "consultant": "顾问", + "consultant": "首次录入", "name": "姓名", "namePlaceholder": "请输入姓名", "age": "年龄", diff --git a/admin/src/app/views/xsyj/xsyj.vue b/admin/src/app/views/xsyj/xsyj.vue index 266e291b..d60e28aa 100644 --- a/admin/src/app/views/xsyj/xsyj.vue +++ b/admin/src/app/views/xsyj/xsyj.vue @@ -1,275 +1,316 @@ \ No newline at end of file diff --git a/niucloud/app/api/controller/apiController/ResourceSharing.php b/niucloud/app/api/controller/apiController/ResourceSharing.php index 7ea7084c..a35f400a 100644 --- a/niucloud/app/api/controller/apiController/ResourceSharing.php +++ b/niucloud/app/api/controller/apiController/ResourceSharing.php @@ -51,7 +51,32 @@ class ResourceSharing extends BaseApiService $res= (new ResourceSharingService())->getList($where); return success($res); } + public function index1(Request $request) + { + $shared_by = $request->param('shared_by','');//共享人ID + $name = $request->param('name','');////客户资源表-姓名 + $phone_number = $request->param('phone_number','');//客户资源表-手机号 + $campus_name = $request->param('campus_name','');//客户资源表-校区 + $shared_at_str = $request->param('shared_at_str','');//共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)] + $shared_at_arr = []; + if(!empty($shared_at_str)){ + $shared_at_arr = explode(' ~ ',$shared_at_str); + $shared_at_arr[0] = "{$shared_at_arr[0]} 00:00:00"; + $shared_at_arr[1] = "{$shared_at_arr[1]} 23:59:59"; + } + + $where = [ + 'shared_by'=>$shared_by, + 'shared_at_arr'=>$shared_at_arr, + 'name'=>$name, + 'phone_number'=>$phone_number, + 'campus_name' => $campus_name + ]; + + $res= (new ResourceSharingService())->getList1($where); + return success($res); + } //资源共享-详情 public function info(Request $request) { diff --git a/niucloud/app/api/route/route.php b/niucloud/app/api/route/route.php index d056f990..5b1da52e 100644 --- a/niucloud/app/api/route/route.php +++ b/niucloud/app/api/route/route.php @@ -242,6 +242,7 @@ Route::group(function () { //资源共享-列表 Route::get('resourceSharing/index', 'apiController.ResourceSharing/index'); + Route::get('resourceSharing/index1', 'apiController.ResourceSharing/index1'); //资源共享-详情(客户详情) Route::get('resourceSharing/info', 'apiController.ResourceSharing/info'); //资源共享-分配员工 diff --git a/niucloud/app/service/api/apiService/ResourceSharingService.php b/niucloud/app/service/api/apiService/ResourceSharingService.php index 23a655e1..1ae53d80 100644 --- a/niucloud/app/service/api/apiService/ResourceSharingService.php +++ b/niucloud/app/service/api/apiService/ResourceSharingService.php @@ -41,10 +41,10 @@ class ResourceSharingService extends BaseApiService $person_id = $this->member_id;//当前登录的员工id $campus_where = []; - $campus_where[] = ['person_id','=',$person_id]; + $campus_where[] = ['person_id', '=', $person_id]; if (!empty($where['campus_name'])) { - $campus_where[] = ['campus_name','like','%'.$where['campus_name'].'%']; + $campus_where[] = ['campus_name', 'like', '%' . $where['campus_name'] . '%']; } @@ -91,15 +91,15 @@ class ResourceSharingService extends BaseApiService $model = $model->whereOr('shared_by', $where['shared_by']); } //分页查询 - $res = $model->with(['customerResource','sixSpeed']) - ->withJoin(['customerResource','sixSpeed']) + $res = $model->with(['customerResource', 'sixSpeed']) + ->withJoin(['customerResource', 'sixSpeed']) ->order('customerResource.updated_at', 'desc') - ->paginate([ - 'list_rows' => $limit, - 'page' => $page, - ])->toArray(); + ->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ])->toArray(); // 获取列表中的campus_ids - $campus_ids = array_unique(array_column(array_column($res['data'],'customerResource'), 'campus')); + $campus_ids = array_unique(array_column(array_column($res['data'], 'customerResource'), 'campus')); $campus = new Campus(); $campus_name = $campus->whereIn('id', $campus_ids)->column('campus_name', 'id'); // 获取客户资源沟通记录 @@ -116,10 +116,10 @@ class ResourceSharingService extends BaseApiService $resultdata[$item['resource_id']] = $item['max_time']; } - foreach ($res['data'] as &$item){ - $item['customerResource']['source'] = get_dict_value('source',$item['customerResource']['source']); - $item['customerResource']['source_channel'] = get_dict_value('source',$item['customerResource']['source_channel']); - $item['customerResource']['campus_name'] = $campus_name[$item['customerResource']['campus']] ?? ''; + foreach ($res['data'] as &$item) { + $item['customerResource']['source'] = get_dict_value('source', $item['customerResource']['source']); + $item['customerResource']['source_channel'] = get_dict_value('source', $item['customerResource']['source_channel']); + $item['customerResource']['campus_name'] = $campus_name[$item['customerResource']['campus']] ?? ''; $item['customerResource']['communication_time'] = $resultdata[$item['resource_id']] ?? ''; } @@ -219,4 +219,189 @@ class ResourceSharingService extends BaseApiService return $res; } + public function getList1($where) + { + $page_params = $this->getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + + $person_id = $this->member_id;//当前登录的员工id + + // 查询当前用户在CampusPersonRole中的角色和校区 + $campus_person_roles = CampusPersonRole::where('person_id', $person_id)->select()->toArray(); + + // 初始化查询条件 + $model = $this->model; + + // 判断用户角色和校区权限 + $is_admin = false; + $is_campus_admin = false; + $campus_ids = []; + + foreach ($campus_person_roles as $item) { + // 记录用户所属的校区ID + if ($item['campus_id'] > 0) { + $campus_ids[] = $item['campus_id']; + } + + // 判断是否为管理员角色(1,4,7) + if (in_array($item['role_id'], [1, 4, 7])) { + // 如果没有校区,则为全局管理员 + if ($item['campus_id'] == 0) { + $is_admin = true; + } else { + // 有校区的管理员 + $is_campus_admin = true; + } + } + } + // 共享人查询 - 如果指定了shared_by,优先处理这个条件 + if (isset($where['shared_by']) && $where['shared_by']) { + $model = $model->where(function ($query) use ($where) { + $query->where('user_id', $where['shared_by']) + ->whereOr('shared_by', $where['shared_by']); + }); + } else { + // 根据角色权限设置查询条件 + if ($is_admin) { + // 全局管理员可以查看所有数据,不需要额外条件 + } else if ($is_campus_admin && !empty($campus_ids)) { + // 校区管理员可以查看自己校区所有人的数据 + // 先获取校区内所有人员ID + $campus_person_ids = CampusPersonRole::whereIn('campus_id', $campus_ids) + ->distinct(true) + ->column('person_id'); + + if (!empty($campus_person_ids)) { + $model = $model->where(function ($query) use ($campus_person_ids) { + $query->whereIn('user_id', $campus_person_ids) + ->whereOr('shared_by', 'in', $campus_person_ids); + }); + } + } else { + // 普通角色只能查看user_id是自己或shared_by是自己的数据 + $model = $model->where(function ($query) use ($person_id) { + $query->where('user_id', $person_id) + ->whereOr('shared_by', $person_id); + }); + } + } + // 处理查询条件 - CustomerResources模型的字段 + $resource_conditions = []; + + // 校区名称查询 + if (!empty($where['campus_name'])) { + // 先查询匹配的校区ID + $campus = new Campus(); + $matched_campus_ids = $campus->where('campus_name', 'like', '%' . $where['campus_name'] . '%')->column('id'); + + if (!empty($matched_campus_ids)) { + $resource_conditions[] = ['campus', 'in', $matched_campus_ids]; + } else { + // 没有匹配的校区,返回空结果 + return [ + 'count' => 0, + 'total' => 0, + 'current_page' => $page, + 'last_page' => 0, + 'data' => [] + ]; + } + } + + // 资源名称查询 + if (!empty($where['name'])) { + $resource_conditions[] = ['name', 'like', '%' . $where['name'] . '%']; + } + + // 手机号查询 + if (!empty($where['phone_number'])) { + $resource_conditions[] = ['phone_number', 'like', '%' . $where['phone_number'] . '%']; + } + + // 查询符合条件的资源ID + $resource_ids = []; + if (!empty($resource_conditions)) { + $resource_ids = (new CustomerResources())->where($resource_conditions)->column('id'); + if (empty($resource_ids)) { + // 没有匹配的资源,返回空结果 + return [ + 'count' => 0, + 'total' => 0, + 'current_page' => $page, + 'last_page' => 0, + 'data' => [] + ]; + } + $model = $model->whereIn('resource_id', $resource_ids); + } + + // 共享时间查询 + if (!empty($where['shared_at_arr'])) { + $model = $model->where('shared_at', '>=', $where['shared_at_arr'][0]) + ->where('shared_at', '<=', $where['shared_at_arr'][1]); + } + + // 调试SQL语句 + // $sql = $model->with(['customerResource', 'sixSpeed'])->fetchSql(true)->select(); + // var_dump($sql); + + // 查询数据 + $list = $model->with(['customerResource', 'sixSpeed']) + ->order('shared_at', 'desc') + ->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ])->toArray(); + + // 处理结果数据 + if (!empty($list['data'])) { + // 获取校区信息 + $campus_ids = []; + foreach ($list['data'] as $item) { + if (!empty($item['customerResource']) && !empty($item['customerResource']['campus'])) { + $campus_ids[] = $item['customerResource']['campus']; + } + } + + $campus_ids = array_unique($campus_ids); + $campus_names = []; + + if (!empty($campus_ids)) { + $campus = new Campus(); + $campus_names = $campus->whereIn('id', $campus_ids)->column('campus_name', 'id'); + } + + // 获取资源ID列表 + $resource_ids = array_column($list['data'], 'resource_id'); + + // 查询最近沟通记录 + $communication_times = []; + if (!empty($resource_ids)) { + $resource_ids = array_map('intval', $resource_ids); + $subQuery = CommunicationRecords::whereIn('resource_id', $resource_ids) + ->field('resource_id, max(communication_time) as max_time') + ->group('resource_id') + ->select() + ->toArray(); + + foreach ($subQuery as $item) { + $communication_times[$item['resource_id']] = $item['max_time']; + } + } + + // 处理每条数据 + foreach ($list['data'] as &$item) { + if (!empty($item['customerResource'])) { + // 设置来源和渠道名称 + $item['customerResource']['source'] = get_dict_value('source', $item['customerResource']['source']); + $item['customerResource']['source_channel'] = get_dict_value('source', $item['customerResource']['source_channel']); + $item['customerResource']['campus_name'] = $campus_name[$item['customerResource']['campus']] ?? ''; + $item['customerResource']['communication_time'] = $resultdata[$item['resource_id']] ?? ''; + } + } + } + + return $list; + } }