diff --git a/admin/src/app/views/campus_person_role/components/campus-person-role-edit.vue b/admin/src/app/views/campus_person_role/components/campus-person-role-edit.vue index 93617a07..ec742ded 100644 --- a/admin/src/app/views/campus_person_role/components/campus-person-role-edit.vue +++ b/admin/src/app/views/campus_person_role/components/campus-person-role-edit.vue @@ -171,9 +171,6 @@ const formRules = computed(() => { return { person_id: [ { required: true, message: "请选择人员", trigger: 'blur' }, - ], - role_id: [ - { required: true, message: "请选择角色", trigger: 'blur' }, ] } }) diff --git a/niucloud/app/api/controller/apiController/ResourceSharing.php b/niucloud/app/api/controller/apiController/ResourceSharing.php index 0f53a752..057dacda 100644 --- a/niucloud/app/api/controller/apiController/ResourceSharing.php +++ b/niucloud/app/api/controller/apiController/ResourceSharing.php @@ -41,6 +41,14 @@ class ResourceSharing extends BaseApiService $valid_type = $request->param('valid_type','');//资源有效类型 $communication_status = $request->param('communication_status','');//沟通情况 $course_search = $request->param('course_search','');//课程检索 + $shared_scope = $request->param('shared_scope', ''); + $assignee_role_ids = $request->param('assignee_role_ids', []); + + if (is_string($assignee_role_ids) && $assignee_role_ids !== '') { + $assignee_role_ids = array_filter(array_map('intval', explode(',', $assignee_role_ids))); + } elseif (!is_array($assignee_role_ids)) { + $assignee_role_ids = []; + } $shared_at_str = $request->param('shared_at_str','');//共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)] $shared_at_arr = []; @@ -62,7 +70,9 @@ class ResourceSharing extends BaseApiService 'deal_type' => $deal_type, 'valid_type' => $valid_type, 'communication_status' => $communication_status, - 'course_search' => $course_search + 'course_search' => $course_search, + 'shared_scope' => $shared_scope, + 'assignee_role_ids' => $assignee_role_ids ]; $res = (new ResourceSharingService())->getList($where); return success($res); @@ -87,44 +97,90 @@ class ResourceSharing extends BaseApiService return success($res['data']); } - //把资源分配给指定员工 + //把资源分配给指定员工(兼容旧接口) public function assign(Request $request) { - $id = $request->param('resource_sharing_id', '');//资源共享表 - $shared_by = $request->param('shared_by', '');//共享人ID + $id = $request->param('resource_sharing_id', '');//资源共享表ID + $shared_by = $request->param('shared_by', '');//分配给的用户ID if (empty($id) || empty($shared_by)) { return fail('缺少必要参数'); } - $where = [ - 'id' => $id, - ]; + $service = new ResourceSharingService(); - $data = [ - 'shared_by' => $shared_by - ]; + // 使用新的分配方法(兼容旧接口) + $res = $service->assignResourceLegacy($id, $shared_by); + + if (!$res['code']) { + return fail($res['msg']); + } + return success($res['msg']); + } + + //新的资源分配接口 - 支持用户和角色分配 + public function assignNew(Request $request) + { + $resource_id = $request->param('resource_id', '');//资源ID + $assignee_type = $request->param('assignee_type', 'user');//分配对象类型:user/role + $assignee_id = $request->param('assignee_id', '');//分配对象ID + $is_primary = $request->param('is_primary', false);//是否为主责分配 + $campus_id = $request->param('campus_id', null);//校区ID + + if (empty($resource_id) || empty($assignee_id)) { + return fail('缺少必要参数'); + } + + if (!in_array($assignee_type, ['user', 'role'])) { + return fail('分配对象类型错误'); + } + + $service = new ResourceSharingService(); + $res = $service->assignResource($resource_id, $assignee_type, $assignee_id, $is_primary, $campus_id); - $info = (new ResourceSharingService())->info($where);//获取详情 - - if (!$info['code']) { - return fail($info['msg']); - } else { - if ($info['data']['shared_by'] > 0) { - if ($info['data']['shared_by'] == $shared_by) { - return success('当前资源已分享给该用户'); - } else { - return fail('该资源已被分享给其他用户'); - } - } + if (!$res['code']) { + return fail($res['msg']); + } + return success($res['msg'], $res['data']); + } + + //移除资源分配 + public function removeAssignment(Request $request) + { + $resource_id = $request->param('resource_id', '');//资源ID + $assignee_type = $request->param('assignee_type', 'user');//分配对象类型:user/role + $assignee_id = $request->param('assignee_id', '');//分配对象ID + + if (empty($resource_id) || empty($assignee_id)) { + return fail('缺少必要参数'); + } + + if (!in_array($assignee_type, ['user', 'role'])) { + return fail('分配对象类型错误'); } + $service = new ResourceSharingService(); + $res = $service->removeAssignment($resource_id, $assignee_type, $assignee_id); - $res = (new ResourceSharingService())->editData($where, $data);//更新 if (!$res['code']) { - return fail('操作失败'); + return fail($res['msg']); } - return success('操作成功'); + return success($res['msg']); + } + + //获取资源的所有分配信息 + public function assignments(Request $request) + { + $resource_id = $request->param('resource_id', '');//资源ID + + if (empty($resource_id)) { + return fail('缺少资源ID参数'); + } + + $service = new ResourceSharingService(); + $assignments = $service->getResourceAssignments($resource_id); + + return success('获取成功', $assignments); } } diff --git a/niucloud/app/common.php b/niucloud/app/common.php index ae1c0d5d..41d14dd9 100644 --- a/niucloud/app/common.php +++ b/niucloud/app/common.php @@ -1255,13 +1255,25 @@ function get_dict_value($key, $value) $info = $dict->field($field)->where([['key', '=', $key]])->findOrEmpty()->toArray(); + if ($info['dictionary'] == null) { $info['dictionary'] = []; } // 如果dictionary是字符串,尝试解析为数组 if (is_string($info['dictionary'])) { - $info['dictionary'] = json_decode($info['dictionary'], true) ?: []; + // 处理双重编码的JSON字符串 + $dictionary_str = $info['dictionary']; + + // 先尝试直接解析 + $dictionary_data = json_decode($dictionary_str, true); + + // 如果解析结果仍然是字符串,说明是双重编码,需要再解析一次 + if (is_string($dictionary_data)) { + $dictionary_data = json_decode($dictionary_data, true); + } + + $info['dictionary'] = is_array($dictionary_data) ? $dictionary_data : []; } $map = []; @@ -1271,6 +1283,7 @@ function get_dict_value($key, $value) } } + return $map[$value] ?? ''; } diff --git a/niucloud/app/model/resource_assignment/ResourceAssignment.php b/niucloud/app/model/resource_assignment/ResourceAssignment.php new file mode 100644 index 00000000..0617ae98 --- /dev/null +++ b/niucloud/app/model/resource_assignment/ResourceAssignment.php @@ -0,0 +1,51 @@ +hasOne(CustomerResources::class, 'id', 'resource_id'); + } + + /** + * 关联六速表 + */ + public function sixSpeed(): HasOne + { + return $this->hasOne(SixSpeed::class, 'resource_id', 'resource_id'); + } +} diff --git a/niucloud/app/service/admin/campus_person_role/CampusPersonRoleService.php b/niucloud/app/service/admin/campus_person_role/CampusPersonRoleService.php index b0502e17..15c861f2 100644 --- a/niucloud/app/service/admin/campus_person_role/CampusPersonRoleService.php +++ b/niucloud/app/service/admin/campus_person_role/CampusPersonRoleService.php @@ -126,9 +126,21 @@ class CampusPersonRoleService extends BaseAdminService $personnel_summary->where(['id' => $tasks['id']])->update(['task_num' => $tasks['task_num']]); } - $data['dept_id'] = Db::table('school_sys_role')->where('role_id', $data['role_id'])->value('dept_id'); + // 如果角色ID为空,删除该用户的所有角色关系 + if (empty($data['role_id']) || $data['role_id'] == 0) { + // 获取当前记录的person_id + $currentRecord = $this->model->where([['id', '=', $id]])->find(); + if ($currentRecord) { + // 删除该人员的所有角色关系 + $this->model->where([['person_id', '=', $currentRecord->person_id]])->delete(); + return success("已移除该人员的所有角色关系"); + } + } else { + // 有角色ID时,正常更新 + $data['dept_id'] = Db::table('school_sys_role')->where('role_id', $data['role_id'])->value('dept_id'); + $this->model->where([['id', '=', $id]])->update($data); + } - $this->model->where([['id', '=', $id]])->update($data); return success("操作成功"); } diff --git a/niucloud/app/service/api/apiService/ResourceSharingService.php b/niucloud/app/service/api/apiService/ResourceSharingService.php index 6d57bc2c..d5b7ab87 100644 --- a/niucloud/app/service/api/apiService/ResourceSharingService.php +++ b/niucloud/app/service/api/apiService/ResourceSharingService.php @@ -16,7 +16,9 @@ use app\model\campus_person_role\CampusPersonRole; use app\model\communication_records\CommunicationRecords; use app\model\customer_resources\CustomerResources; use app\model\order_table\OrderTable; +use app\model\resource_assignment\ResourceAssignment; use app\model\resource_sharing\ResourceSharing; +use app\model\sys\SysRole; use core\base\BaseApiService; /** @@ -26,10 +28,131 @@ use core\base\BaseApiService; */ class ResourceSharingService extends BaseApiService { + /** + * 新表模型实例 + */ + protected ResourceAssignment $assignmentModel; + public function __construct() { parent::__construct(); - $this->model = (new ResourceSharing()); + $this->model = new ResourceSharing(); + $this->assignmentModel = new ResourceAssignment(); + } + + /** + * 抽象查询权限逻辑 - 计算用户可见的校区ID和权限范围 + * @param int $personId 用户ID + * @return array ['is_admin' => bool, 'is_campus_admin' => bool, 'campus_ids' => array, 'role_keys' => array] + */ + protected function calculateUserPermissions(int $personId): array + { + // 查询当前用户在CampusPersonRole中的角色和校区 + $campus_person_roles = CampusPersonRole::where('person_id', $personId)->select()->toArray(); + + $role_ids = array_unique(array_filter(array_column($campus_person_roles, 'role_id'))); + $role_keys = []; + if (!empty($role_ids)) { + $role_keys = SysRole::whereIn('role_id', $role_ids)->column('role_key', 'role_id'); + } + + // 判断用户角色和校区权限 + $is_admin = false; + $is_campus_admin = false; + $campus_ids = []; + + foreach ($campus_person_roles as $item) { + // 记录用户所属的校区ID + if (!empty($item['campus_id']) && $item['campus_id'] > 0) { + $campus_ids[] = $item['campus_id']; + } + + // 判断是否为管理员角色(1,4,7) + if (in_array($item['role_id'], [1, 4, 7])) { + if ((int) $item['campus_id'] === 0) { + $is_admin = true; + } else { + $is_campus_admin = true; + } + } + + // 特殊处理市场经理角色 + $role_key = $role_keys[$item['role_id']] ?? null; + if ($role_key === 'market_manager') { + if ((int) $item['campus_id'] === 0) { + $is_admin = true; + } else { + $is_campus_admin = true; + $campus_ids[] = $item['campus_id']; + } + } + } + + $campus_ids = array_values(array_unique(array_filter($campus_ids))); + + return [ + 'is_admin' => $is_admin, + 'is_campus_admin' => $is_campus_admin, + 'campus_ids' => $campus_ids, + 'role_keys' => $role_keys, + 'campus_person_roles' => $campus_person_roles + ]; + } + + /** + * 应用权限过滤到查询模型 + * @param mixed $model 查询模型 + * @param array $permissions 权限信息 + * @param int $personId 当前用户ID + * @param string $shared_scope 查询范围 + * @param int|null $shared_by 指定的分配人ID + * @param array $assignee_role_ids 指定的角色ID数组 + * @return mixed + */ + protected function applyPermissionFilter($model, array $permissions, int $personId, string $shared_scope = '', ?int $shared_by = null, array $assignee_role_ids = []) + { + if ($shared_scope === 'user' && $shared_by) { + $model = $model->where(function ($query) use ($shared_by) { + $query->where(function ($subQuery) use ($shared_by) { + $subQuery->where('assignee_type', 'user')->where('assignee_id', $shared_by); + })->whereOr('assigned_by', $shared_by); + }); + } elseif ($shared_scope === 'role' && !empty($assignee_role_ids)) { + $model = $model->where('assignee_type', 'role') + ->whereIn('assignee_id', (array) $assignee_role_ids); + } else { + if ($shared_by) { + $model = $model->where(function ($query) use ($shared_by) { + $query->where(function ($subQuery) use ($shared_by) { + $subQuery->where('assignee_type', 'user')->where('assignee_id', $shared_by); + })->whereOr('assigned_by', $shared_by); + }); + } elseif ($permissions['is_admin']) { + // 全局管理员无需额外限制 + } elseif ($permissions['is_campus_admin'] && !empty($permissions['campus_ids'])) { + $campus_person_ids = CampusPersonRole::whereIn('campus_id', array_unique($permissions['campus_ids'])) + ->distinct(true) + ->column('person_id'); + + if (!empty($campus_person_ids)) { + $model = $model->where(function ($query) use ($campus_person_ids) { + $query->where(function ($subQuery) use ($campus_person_ids) { + $subQuery->where('assignee_type', 'user')->whereIn('assignee_id', $campus_person_ids); + })->whereOr(function ($subQuery) use ($campus_person_ids) { + $subQuery->whereIn('assigned_by', $campus_person_ids); + }); + }); + } + } else { + $model = $model->where(function ($query) use ($personId) { + $query->where(function ($subQuery) use ($personId) { + $subQuery->where('assignee_type', 'user')->where('assignee_id', $personId); + })->whereOr('assigned_by', $personId); + }); + } + } + + return $model; } //查询资源共享详情 @@ -82,7 +205,7 @@ class ResourceSharingService extends BaseApiService ->where('order_status', 'paid') ->count();//成交次数 } -// dd(123123,$data); + if ($data) { $res['code'] = 1; $res['msg'] = '操作成功'; @@ -130,6 +253,427 @@ class ResourceSharingService extends BaseApiService $page = $page_params['page']; $limit = $page_params['limit']; + $shared_scope = $where['shared_scope'] ?? ''; + $assignee_role_ids = $where['assignee_role_ids'] ?? []; + $shared_by_param = $where['shared_by'] ?? null; + + // 兼容旧的shared_by=0参数,转换为新的shared_scope参数 + if ($shared_scope === '' && $shared_by_param !== null && $shared_by_param !== '') { + $shared_scope = ((int) $shared_by_param === 0) ? 'unassigned' : 'user'; + } + + unset($where['shared_scope'], $where['assignee_role_ids']); + + $person_id = $this->member_id;//当前登录的员工id + + // 使用抽象的权限计算方法 + $permissions = $this->calculateUserPermissions($person_id); + + // 对于未分配资源的查询,暂时使用旧逻辑 + if ($shared_scope === 'unassigned') { + return $this->getUnassignedList($where, $page, $limit, $person_id, $permissions['campus_ids'], $permissions['is_admin'], $permissions['is_campus_admin']); + } + + // 初始化查询条件 - 使用新表 + $model = (new ResourceAssignment())->alias('ra'); + + $shared_by = ($shared_by_param !== null && $shared_by_param !== '') ? (int) $shared_by_param : null; + + // 应用权限过滤 + $model = $this->applyPermissionFilter($model, $permissions, $person_id, $shared_scope, $shared_by, $assignee_role_ids); + + // 处理查询条件 - 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'] . '%']; + } + + // 来源查询 + if (!empty($where['source'])) { + $resource_conditions[] = ['source', 'like', '%' . $where['source'] . '%']; + } + + // 来源渠道查询 + if (!empty($where['source_channel'])) { + $resource_conditions[] = ['source_channel', 'like', '%' . $where['source_channel'] . '%']; + } + + // 查询符合条件的资源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('assigned_at', '>=', $where['shared_at_arr'][0]) + ->where('assigned_at', '<=', $where['shared_at_arr'][1]); + } + + // 处理有效性查询 - 需要通过six_speed表关联查询 + if (!empty($where['valid_type'])) { + $six_speed_model = new \app\model\six_speed\SixSpeed(); + if ($where['valid_type'] === '有效') { + $valid_resource_ids = $six_speed_model->where('efficacious', 1)->column('resource_id'); + } else if ($where['valid_type'] === '无效') { + $valid_resource_ids = $six_speed_model->where('efficacious', 0)->column('resource_id'); + } + + if (isset($valid_resource_ids)) { + if (empty($resource_ids)) { + $resource_ids = $valid_resource_ids; + } else { + $resource_ids = array_intersect($resource_ids, $valid_resource_ids); + } + + 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['deal_type'])) { + $order_model = new OrderTable(); + if ($where['deal_type'] === '已成交') { + $deal_resource_ids = $order_model->where('order_status', 'paid')->column('resource_id'); + } else if ($where['deal_type'] === '未成交') { + // 没有付费订单的资源 + $paid_resource_ids = $order_model->where('order_status', 'paid')->column('resource_id'); + $all_resource_ids = (new CustomerResources())->column('id'); + $deal_resource_ids = array_diff($all_resource_ids, $paid_resource_ids); + } else if ($where['deal_type'] === '定金') { + // 没有付费订单的资源 + $paid_resource_ids = $order_model->where('payment_type', 'deposit')->column('resource_id'); + $all_resource_ids = (new CustomerResources())->column('id'); + $deal_resource_ids = array_diff($all_resource_ids, $paid_resource_ids); + } + + if (isset($deal_resource_ids)) { + if (empty($resource_ids)) { + $resource_ids = $deal_resource_ids; + } else { + $resource_ids = array_intersect($resource_ids, $deal_resource_ids); + } + + 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['communication_status'])) { + if ($where['communication_status'] === '已沟通') { + $comm_resource_ids = CommunicationRecords::distinct(true)->column('resource_id'); + } else if ($where['communication_status'] === '未沟通') { + // 没有沟通记录的资源 + $comm_resource_ids_with_records = CommunicationRecords::distinct(true)->column('resource_id'); + $all_resource_ids = (new CustomerResources())->column('id'); + $comm_resource_ids = array_diff($all_resource_ids, $comm_resource_ids_with_records); + } + + if (isset($comm_resource_ids)) { + if (empty($resource_ids)) { + $resource_ids = $comm_resource_ids; + } else { + $resource_ids = array_intersect($resource_ids, $comm_resource_ids); + } + + if (empty($resource_ids)) { + return [ + 'count' => 0, + 'total' => 0, + 'current_page' => $page, + 'last_page' => 0, + 'data' => [] + ]; + } + $model = $model->whereIn('resource_id', $resource_ids); + } + } + + // 过滤已分配的资源(只显示可再分配的资源) + // 注意:这里需要用子查询包装OR条件,避免与其他WHERE条件冲突 + // 查询数据 + $list = $model->with(['customerResource', 'sixSpeed']) + ->order('assigned_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'); + $resource_ids = array_unique(array_filter($resource_ids)); + + // 获取分配人员ID列表 + $shared_by_ids = array_column($list['data'], 'assigned_by'); + $shared_by_ids = array_unique(array_filter($shared_by_ids)); + + // 获取市场老师ID列表 + $consultant_ids = []; + foreach ($list['data'] as $item) { + if (!empty($item['customerResource']) && !empty($item['customerResource']['consultant'])) { + $consultant_ids[] = $item['customerResource']['consultant']; + } + } + $consultant_ids = array_unique(array_filter($consultant_ids)); + + // 查询分配人员信息 + $shared_by_names = []; + if (!empty($shared_by_ids)) { + $personnel = new \app\model\personnel\Personnel(); + $shared_by_names = $personnel->whereIn('id', $shared_by_ids)->column('name', 'id'); + } + + // 查询市场老师信息 + $consultant_names = []; + if (!empty($consultant_ids)) { + $personnel = new \app\model\personnel\Personnel(); + $consultant_names = $personnel->whereIn('id', $consultant_ids)->column('name', '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']; + } + } + + // 查询到访信息 + $visit_info = []; + if (!empty($resource_ids)) { + // 需要先导入PersonCourseSchedule模型 + $person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); + $visit_records = $person_course_model + ->whereIn('person_id', $resource_ids) + ->where('person_type', 'customer_resource') + ->field('person_id, course_date, time_slot, status') + ->order('course_date', 'desc') + ->select() + ->toArray(); + + // 处理到访信息 + foreach ($visit_records as $record) { + $resource_id = $record['person_id']; + + if (!isset($visit_info[$resource_id])) { + $visit_info[$resource_id] = [ + 'first_visit_status' => '未到', + 'second_visit_status' => '未到', + 'visit_count' => 0 + ]; + } + + if ($record['status'] == 1) { // 假设status=1表示已到 + $visit_info[$resource_id]['visit_count']++; + + if ($visit_info[$resource_id]['visit_count'] == 1) { + $visit_info[$resource_id]['first_visit_status'] = '已到'; + } elseif ($visit_info[$resource_id]['visit_count'] == 2) { + $visit_info[$resource_id]['second_visit_status'] = '已到'; + } + } + } + } + + // 查询开单状态 + $order_status = []; + if (!empty($resource_ids)) { + // 首先查询是否有付费订单 + $order_model = new OrderTable(); + $paid_orders = $order_model + ->whereIn('resource_id', $resource_ids) + ->where('order_status', 'paid') + ->field('resource_id') + ->select() + ->toArray(); + + $paid_resource_ids = array_column($paid_orders, 'resource_id'); + + // 查询六速表的开单状态 + $six_speed_model = new \app\model\six_speed\SixSpeed(); + $six_speed_records = $six_speed_model + ->whereIn('resource_id', $resource_ids) + ->field('resource_id, is_closed') + ->select() + ->toArray(); + + // 设置每个资源的订单状态 + foreach ($resource_ids as $resource_id) { + if (in_array($resource_id, $paid_resource_ids)) { + $order_status[$resource_id] = '已报名'; + } else { + // 查找对应的六速记录 + $six_speed_record = array_filter($six_speed_records, function($record) use ($resource_id) { + return $record['resource_id'] == $resource_id; + }); + + if (!empty($six_speed_record)) { + $six_speed_record = array_shift($six_speed_record); + $order_status[$resource_id] = $six_speed_record['is_closed'] ? '已开单' : '未开单'; + } else { + $order_status[$resource_id] = '未报名'; + } + } + } + } + + // 处理每条数据 + foreach ($list['data'] as &$item) { + if (!empty($item['customerResource'])) { + // 确保数据类型转换:数据库中的数值需要转换为字符串 + $source_value = (string) $item['customerResource']['source']; + $source_channel_value = (string) $item['customerResource']['source_channel']; + + + $item['customerResource']['source'] = get_dict_value('source', $source_value); + $item['customerResource']['source_channel'] = get_dict_value('SourceChannel', $source_channel_value); + $item['customerResource']['campus_name'] = $campus_names[$item['customerResource']['campus']] ?? ''; + + if (!empty($item['customerResource']['consultant'])) { + $item['customerResource']['consultant_name'] = $consultant_names[$item['customerResource']['consultant']] ?? ''; + } else { + $item['customerResource']['consultant_name'] = ''; + } + + $item['customerResource']['communication_time'] = $communication_times[$item['resource_id']] ?? ''; + + $resource_id = $item['resource_id']; + $item['customerResource']['first_visit_status'] = $visit_info[$resource_id]['first_visit_status'] ?? '未到'; + $item['customerResource']['second_visit_status'] = $visit_info[$resource_id]['second_visit_status'] ?? '未到'; + + $item['customerResource']['order_status'] = $order_status[$resource_id] ?? '未报名'; + } + + $item['user_id'] = ($item['assignee_type'] ?? '') === 'user' ? (int) $item['assignee_id'] : 0; + $item['role_id'] = ($item['assignee_type'] ?? '') === 'role' ? (int) $item['assignee_id'] : 0; + $item['shared_by'] = (int) ($item['assigned_by'] ?? 0); + $item['shared_at'] = $item['assigned_at'] ?? null; + $item['shared_by_name'] = $shared_by_names[$item['shared_by']] ?? '未分配'; + $item['can_reassign'] = empty($item['assignee_type']) || (int) ($item['assigned_by'] ?? 0) === 0; + } + } + + return $list; + } + + protected function getUnassignedList(array $where, int $page, int $limit, int $personId, array $campusIds, bool $isAdmin, bool $isCampusAdmin) + { + // 暂时复用旧逻辑,后续迁移完成后改为基于 resource_assignment 的查询 + $legacyResult = $this->getListLegacy($where); + + if (empty($legacyResult['data'])) { + return $legacyResult; + } + + foreach ($legacyResult['data'] as &$item) { + $userId = (int)($item['user_id'] ?? 0); + $roleId = (int)($item['role_id'] ?? 0); + if ($userId > 0) { + $item['assignee_type'] = 'user'; + $item['assignee_id'] = $userId; + } elseif ($roleId > 0) { + $item['assignee_type'] = 'role'; + $item['assignee_id'] = $roleId; + } else { + $item['assignee_type'] = null; + $item['assignee_id'] = 0; + } + + $item['assigned_by'] = (int)($item['shared_by'] ?? 0); + $item['assigned_at'] = $item['shared_at'] ?? null; + } + + return $legacyResult; + } + + public function getListLegacy($where) + { + $page_params = $this->getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + $person_id = $this->member_id;//当前登录的员工id // 查询当前用户在CampusPersonRole中的角色和校区 @@ -523,9 +1067,13 @@ class ResourceSharingService extends BaseApiService // 处理每条数据 foreach ($list['data'] as &$item) { if (!empty($item['customerResource'])) { + // 确保数据类型转换:数据库中的数值需要转换为字符串 + $source_value = (string) $item['customerResource']['source']; + $source_channel_value = (string) $item['customerResource']['source_channel']; + // 设置来源和渠道名称 - $item['customerResource']['source'] = get_dict_value('source', $item['customerResource']['source']); - $item['customerResource']['source_channel'] = get_dict_value('SourceChannel', $item['customerResource']['source_channel']); + $item['customerResource']['source'] = get_dict_value('source', $source_value); + $item['customerResource']['source_channel'] = get_dict_value('SourceChannel', $source_channel_value); $item['customerResource']['campus_name'] = $campus_names[$item['customerResource']['campus']] ?? ''; // 设置市场老师名称 @@ -557,4 +1105,173 @@ class ResourceSharingService extends BaseApiService return $list; } + + /** + * 新的资源分配方法 - 支持用户和角色双重分配 + * @param int $resource_id 资源ID + * @param string $assignee_type 分配对象类型:user/role + * @param int $assignee_id 分配对象ID + * @param bool $is_primary 是否为主责分配 + * @param int|null $campus_id 校区ID + * @return array + */ + public function assignResource(int $resource_id, string $assignee_type, int $assignee_id, bool $is_primary = false, ?int $campus_id = null): array + { + $result = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + + if (!in_array($assignee_type, ['user', 'role'])) { + $result['msg'] = '分配对象类型错误'; + return $result; + } + + if (empty($resource_id) || empty($assignee_id)) { + $result['msg'] = '缺少必要参数'; + return $result; + } + + $assigned_by = $this->member_id; // 当前操作用户ID + + try { + // 检查是否已存在相同的分配 + $existing = $this->assignmentModel + ->where('resource_id', $resource_id) + ->where('assignee_type', $assignee_type) + ->where('assignee_id', $assignee_id) + ->find(); + + if ($existing) { + $result['code'] = 1; + $result['msg'] = '该资源已分配给指定对象'; + return $result; + } + + // 创建新的分配记录 + $data = [ + 'resource_id' => $resource_id, + 'assignee_type' => $assignee_type, + 'assignee_id' => $assignee_id, + 'is_primary' => $is_primary ? 1 : 0, + 'assigned_by' => $assigned_by, + 'assigned_at' => date('Y-m-d H:i:s'), + 'campus_id' => $campus_id, + 'assigned_source' => 'api' + ]; + + $assignment = $this->assignmentModel->create($data); + + if ($assignment) { + $result['code'] = 1; + $result['msg'] = '分配成功'; + $result['data'] = $assignment->toArray(); + } + + } catch (\Exception $e) { + $result['msg'] = '操作失败:' . $e->getMessage(); + } + + return $result; + } + + /** + * 移除资源分配 + * @param int $resource_id 资源ID + * @param string $assignee_type 分配对象类型:user/role + * @param int $assignee_id 分配对象ID + * @return array + */ + public function removeAssignment(int $resource_id, string $assignee_type, int $assignee_id): array + { + $result = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + + try { + $deleted = $this->assignmentModel + ->where('resource_id', $resource_id) + ->where('assignee_type', $assignee_type) + ->where('assignee_id', $assignee_id) + ->delete(); + + if ($deleted) { + $result['code'] = 1; + $result['msg'] = '移除成功'; + } else { + $result['msg'] = '未找到对应的分配记录'; + } + + } catch (\Exception $e) { + $result['msg'] = '操作失败:' . $e->getMessage(); + } + + return $result; + } + + /** + * 兼容旧接口的资源分配方法 + * @param int $resource_sharing_id 旧表记录ID(实际为resource_id) + * @param int $shared_by 分配给的用户ID + * @return array + */ + public function assignResourceLegacy(int $resource_sharing_id, int $shared_by): array + { + // 首先检查旧表中的记录,获取resource_id + $old_record = $this->model->where('id', $resource_sharing_id)->find(); + + if (!$old_record) { + return [ + 'code' => 0, + 'msg' => '资源记录不存在', + 'data' => [] + ]; + } + + $resource_id = $old_record['resource_id']; + + // 使用新的分配方法 + return $this->assignResource($resource_id, 'user', $shared_by, true, $old_record['campus_id']); + } + + /** + * 获取资源的所有分配信息 + * @param int $resource_id 资源ID + * @return array + */ + public function getResourceAssignments(int $resource_id): array + { + $assignments = $this->assignmentModel + ->where('resource_id', $resource_id) + ->order('assigned_at', 'desc') + ->select() + ->toArray(); + + // 补充分配人员和分配对象的名称信息 + foreach ($assignments as &$assignment) { + // 获取分配人员名称 + if ($assignment['assigned_by'] > 0) { + $personnel = new \app\model\personnel\Personnel(); + $assigned_by_name = $personnel->where('id', $assignment['assigned_by'])->value('name'); + $assignment['assigned_by_name'] = $assigned_by_name ?: '未知'; + } else { + $assignment['assigned_by_name'] = '系统'; + } + + // 获取分配对象名称 + if ($assignment['assignee_type'] === 'user') { + $personnel = new \app\model\personnel\Personnel(); + $assignee_name = $personnel->where('id', $assignment['assignee_id'])->value('name'); + $assignment['assignee_name'] = $assignee_name ?: '未知用户'; + } elseif ($assignment['assignee_type'] === 'role') { + $role_name = SysRole::where('role_id', $assignment['assignee_id'])->value('role_name'); + $assignment['assignee_name'] = $role_name ?: '未知角色'; + } + } + + return $assignments; + } } diff --git a/uniapp/common/config.js b/uniapp/common/config.js index 52e2d4bb..a157a2c5 100644 --- a/uniapp/common/config.js +++ b/uniapp/common/config.js @@ -1,10 +1,10 @@ // 环境变量配置 // const env = 'development' -const env = 'prod' +const env = 'development' const isMockEnabled = false // 默认禁用Mock优先模式,仅作为回退 const isDebug = false // 默认启用调试模式 -const devurl = 'https://api.hnhbty.cn/api' -const devimgurl = 'https://api.hnhbty.cn' +const devurl = 'http://localhost:20080/api' +const devimgurl = 'http://localhost:20080' const produrl = 'https://api.hnhbty.cn/api' const prodimgurl = 'https://api.hnhbty.cn' // API配置 - 支持环境变量 @@ -26,4 +26,4 @@ export { isMockEnabled, isDebug, env -} \ No newline at end of file +}