From 825c4f30b729b448d2598fa13ebfca00e64e2b8c Mon Sep 17 00:00:00 2001 From: zeyan <258785420@qq.com> Date: Mon, 13 Oct 2025 16:06:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../schedule/ResourceAutoAllocation.php | 302 ++++++++++++------ .../CustomerResourcesService.php | 29 +- .../apiService/CustomerResourcesService.php | 24 +- .../api/apiService/ResourceSharingService.php | 230 +++++++++---- 4 files changed, 404 insertions(+), 181 deletions(-) diff --git a/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php b/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php index b38f77b0..a82cf8b7 100644 --- a/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php +++ b/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php @@ -3,7 +3,9 @@ namespace app\job\transfer\schedule; use app\model\campus_person_role\CampusPersonRole; -use app\model\resource_sharing\ResourceSharing; +use app\model\customer_resources\CustomerResources; +use app\model\resource_assignment\ResourceAssignment; +use app\model\personnel\Personnel; use core\base\BaseScheduleJob; use think\facade\Db; use think\facade\Log; @@ -64,175 +66,275 @@ class ResourceAutoAllocation extends BaseScheduleJob } /** - * 获取销售人员(角色ID为6或7的人员) + * 获取销售人员(按角色ID为6或7的人员,支持校区优先和主管兜底) + * @param int|null $campus_id 校区ID,用于优先分配 * @return array 销售人员列表 */ - public function getSalesman() + public function getSalesman(?int $campus_id = null) { - Log::write('获取销售人员'); - - // 获取角色ID为6或7的人员ID - $salesmen = CampusPersonRole::where('role_id', 'in', [6, 7]) - ->where('deleted_at', 0) // 未删除的记录 - ->field('person_id, role_id') + Log::write('获取销售人员' . ($campus_id ? ',优先校区ID:' . $campus_id : '')); + + // 获取所有角色ID为6或7的人员 + $all_salesmen = CampusPersonRole::where('role_id', 'in', [6, 7]) + ->where('deleted_at', 0) + ->field('person_id, role_id, campus_id') ->select() ->toArray(); - - if (empty($salesmen)) { + + if (empty($all_salesmen)) { Log::write('未找到销售人员'); return []; } - - // 获取每个销售人员当前拥有的资源数量 - foreach ($salesmen as &$salesman) { - $resourceCount = ResourceSharing::where('shared_by', $salesman['person_id']) + + // 获取人员详细信息 + $person_ids = array_column($all_salesmen, 'person_id'); + $personnel_info = Personnel::whereIn('id', $person_ids) + ->field('id, name, status') + ->where('status', 1) // 只获取在职人员 + ->select() + ->toArray(); + + $personnel_map = array_column($personnel_info, null, 'id'); + + // 过滤在职人员并整理数据 + $valid_salesmen = []; + foreach ($all_salesmen as $salesman) { + if (isset($personnel_map[$salesman['person_id']])) { + $salesman['name'] = $personnel_map[$salesman['person_id']]['name']; + $valid_salesmen[] = $salesman; + } + } + + if (empty($valid_salesmen)) { + Log::write('未找到在职销售人员'); + return []; + } + + // 获取每个销售人员当前拥有的资源数量(使用新表) + foreach ($valid_salesmen as &$salesman) { + $resourceCount = ResourceAssignment::where('assignee_type', 'user') + ->where('assignee_id', $salesman['person_id']) ->count(); $salesman['resource_count'] = $resourceCount; } - - // 按资源数量升序排序,资源少的排在前面 - array_multisort(array_column($salesmen, 'resource_count'), SORT_ASC, $salesmen); - - Log::write('找到' . count($salesmen) . '个销售人员'); - return $salesmen; + + // 如果指定了校区,优先分配给该校区的销售人员 + if ($campus_id) { + $campus_salesmen = array_filter($valid_salesmen, function($salesman) use ($campus_id) { + return $salesman['campus_id'] == $campus_id; + }); + + if (!empty($campus_salesmen)) { + // 校区内销售人员按资源数量排序 + array_multisort(array_column($campus_salesmen, 'resource_count'), SORT_ASC, $campus_salesmen); + + // 其他销售人员(主管兜底)按资源数量排序 + $other_salesmen = array_filter($valid_salesmen, function($salesman) use ($campus_id) { + return $salesman['campus_id'] != $campus_id; + }); + + if (!empty($other_salesmen)) { + array_multisort(array_column($other_salesmen, 'resource_count'), SORT_ASC, $other_salesmen); + + // 主管优先(角色ID=7) + usort($other_salesmen, function($a, $b) { + if ($a['role_id'] == 7 && $b['role_id'] != 7) return -1; + if ($a['role_id'] != 7 && $b['role_id'] == 7) return 1; + return $a['resource_count'] - $b['resource_count']; + }); + } + + Log::write('找到' . count($campus_salesmen) . '个校区销售人员,' . count($other_salesmen) . '个其他销售人员'); + + // 返回校区优先,主管兜底的列表 + return array_merge($campus_salesmen, $other_salesmen); + } + } + + // 没有指定校区或没有校区销售人员,按主管优先排序 + usort($valid_salesmen, function($a, $b) { + // 主管优先(角色ID=7) + if ($a['role_id'] == 7 && $b['role_id'] != 7) return -1; + if ($a['role_id'] != 7 && $b['role_id'] == 7) return 1; + // 相同角色按资源数量排序 + return $a['resource_count'] - $b['resource_count']; + }); + + Log::write('找到' . count($valid_salesmen) . '个销售人员,主管优先排序'); + return $valid_salesmen; } /** - * 获取待分配的资源 + * 获取待分配的资源(使用新表结构) * @return array 待分配资源列表 */ public function getResource() { Log::write('获取待分配资源'); - - // 获取role_id不是6,7的,shared_by是0的资源ID - $resources = ResourceSharing::where(function ($query) { - $query->where('role_id', 'not in', [6, 7]) - ->whereOr('role_id', 'null'); - }) - ->where('shared_by', 0) - ->field('id, resource_id') + + // 方法1:获取新表中没有分配记录的资源 + $customer_resources = CustomerResources::alias('cr') + ->leftJoin('school_resource_assignment ra', 'cr.id = ra.resource_id') + ->whereNull('ra.resource_id') + ->field('cr.id as resource_id, cr.campus_id, cr.name, cr.phone_number') ->select() ->toArray(); - - Log::write('找到' . count($resources) . '个待分配资源'); - return $resources; + + if (empty($customer_resources)) { + Log::write('未找到待分配资源'); + return []; + } + + Log::write('找到' . count($customer_resources) . '个待分配资源'); + return $customer_resources; } /** - * 按照销售人员的资源拥有情况分配资源 + * 按照销售人员的资源拥有情况分配资源(使用新表结构) * @param array $resources 待分配的资源列表 * @param array $salesmen 销售人员列表 */ public function allocateResource($resources, $salesmen) { Log::write('按照销售人员的资源拥有情况分配资源'); - + if (empty($resources) || empty($salesmen)) { Log::write('没有资源或销售人员,无法分配'); - return; + return [ + 'allocated' => 0, + 'updated' => 0, + 'created' => 0 + ]; } - + // 记录分配结果 $allocations = []; - + $createdCount = 0; + // 开始分配 foreach ($resources as $resource) { // 重新获取销售人员的资源数量排序,确保每次分配都是给最少资源的人 - $currentSalesmen = $this->refreshSalesmenResourceCount($salesmen); - + $currentSalesmen = $this->refreshSalesmenResourceCount($salesmen, $resource['campus_id']); + if (empty($currentSalesmen)) { Log::write('没有可用的销售人员'); break; } - - // 选择资源最少的销售人员 + + // 选择最优销售人员 $targetSalesman = $currentSalesmen[0]; - - // 更新现有资源记录,而不是插入新记录 + + // 使用新表创建分配记录 try { Db::startTrans(); - - // 检查是否存在该资源的分配记录 - $existingRecord = ResourceSharing::where('id', $resource['id'])->find(); - - if ($existingRecord) { - // 更新现有记录 - $updateData = [ - 'user_id' => $targetSalesman['person_id'], - 'role_id' => $targetSalesman['role_id'], - 'shared_by' => $targetSalesman['person_id'], - 'shared_at' => date('Y-m-d H:i:s'), - 'updated_at' => time() - ]; - - ResourceSharing::where('id', $resource['id'])->update($updateData); - - Log::write('更新资源分配记录,资源ID:' . $resource['resource_id'] . ' 分配给销售人员ID:' . $targetSalesman['person_id']); - } else { - // 如果记录不存在,创建新记录 - $insertData = [ - 'resource_id' => $resource['resource_id'], - 'user_id' => $targetSalesman['person_id'], - 'role_id' => $targetSalesman['role_id'], - 'shared_by' => $targetSalesman['person_id'], - 'shared_at' => date('Y-m-d H:i:s'), - 'created_at' => time(), - 'updated_at' => time() - ]; - - ResourceSharing::create($insertData); - - Log::write('创建新资源分配记录,资源ID:' . $resource['resource_id'] . ' 分配给销售人员ID:' . $targetSalesman['person_id']); + + // 检查是否已存在分配记录 + $existingAssignment = ResourceAssignment::where('resource_id', $resource['resource_id']) + ->where('assignee_type', 'user') + ->where('assignee_id', $targetSalesman['person_id']) + ->find(); + + if ($existingAssignment) { + Log::write('资源ID ' . $resource['resource_id'] . ' 已分配给销售人员 ' . $targetSalesman['name'] . ',跳过'); + Db::commit(); + continue; } - - // 记录分配结果 - $allocations[] = [ + + // 创建新的分配记录 + $assignmentData = [ 'resource_id' => $resource['resource_id'], - 'salesman_id' => $targetSalesman['person_id'], - 'action' => $existingRecord ? 'updated' : 'created' + 'assignee_type' => 'user', // 精确分配到人 + 'assignee_id' => $targetSalesman['person_id'], + 'is_primary' => 0, // 非主责分配 + 'assigned_by' => 0, // 系统自动分配 + 'assigned_at' => date('Y-m-d H:i:s'), + 'campus_id' => $resource['campus_id'], // 使用资源的校区 + 'assigned_source' => 'auto_allocation' // 标记为自动分配 ]; - + + $assignment = ResourceAssignment::create($assignmentData); + + if ($assignment) { + $createdCount++; + Log::write('成功分配资源ID:' . $resource['resource_id'] . ' 给销售人员:' . $targetSalesman['name'] . ' (ID:' . $targetSalesman['person_id'] . ')'); + } + Db::commit(); - + } catch (\Exception $e) { Db::rollback(); - Log::write('资源分配失败:' . $e->getMessage()); + Log::write('资源分配失败,资源ID:' . $resource['resource_id'] . ',错误:' . $e->getMessage()); } } - - $updatedCount = count(array_filter($allocations, function($a) { return $a['action'] == 'updated'; })); - $createdCount = count(array_filter($allocations, function($a) { return $a['action'] == 'created'; })); - - Log::write('成功分配' . count($allocations) . '个资源,其中更新:' . $updatedCount . '个,新建:' . $createdCount . '个'); - + + Log::write('成功分配 ' . $createdCount . ' 个资源'); + return [ - 'allocated' => count($allocations), - 'updated' => $updatedCount, + 'allocated' => $createdCount, + 'updated' => 0, 'created' => $createdCount ]; } /** - * 刷新销售人员的资源数量并重新排序 + * 刷新销售人员的资源数量并重新排序(使用新表结构) * @param array $salesmen 销售人员列表 + * @param int|null $campus_id 资源所属校区ID * @return array 更新后的销售人员列表 */ - private function refreshSalesmenResourceCount($salesmen) + private function refreshSalesmenResourceCount($salesmen, ?int $campus_id = null) { if (empty($salesmen)) { return []; } - + foreach ($salesmen as &$salesman) { - $resourceCount = ResourceSharing::where('shared_by', $salesman['person_id']) + // 使用新表查询资源数量 + $resourceCount = ResourceAssignment::where('assignee_type', 'user') + ->where('assignee_id', $salesman['person_id']) ->count(); $salesman['resource_count'] = $resourceCount; } - - // 按资源数量升序排序 - array_multisort(array_column($salesmen, 'resource_count'), SORT_ASC, $salesmen); - + + // 如果指定了校区,按校区优先和资源数量重新排序 + if ($campus_id) { + $campus_salesmen = array_filter($salesmen, function($salesman) use ($campus_id) { + return $salesman['campus_id'] == $campus_id; + }); + + if (!empty($campus_salesmen)) { + // 校区内销售人员按资源数量排序 + array_multisort(array_column($campus_salesmen, 'resource_count'), SORT_ASC, $campus_salesmen); + + // 其他销售人员按主管优先和资源数量排序 + $other_salesmen = array_filter($salesmen, function($salesman) use ($campus_id) { + return $salesman['campus_id'] != $campus_id; + }); + + if (!empty($other_salesmen)) { + usort($other_salesmen, function($a, $b) { + // 主管优先(角色ID=7) + if ($a['role_id'] == 7 && $b['role_id'] != 7) return -1; + if ($a['role_id'] != 7 && $b['role_id'] == 7) return 1; + // 相同角色按资源数量排序 + return $a['resource_count'] - $b['resource_count']; + }); + } + + // 返回校区优先,主管兜底的列表 + return array_merge($campus_salesmen, $other_salesmen); + } + } + + // 没有校区或没有校区销售人员,按主管优先和资源数量排序 + usort($salesmen, function($a, $b) { + // 主管优先(角色ID=7) + if ($a['role_id'] == 7 && $b['role_id'] != 7) return -1; + if ($a['role_id'] != 7 && $b['role_id'] == 7) return 1; + // 相同角色按资源数量排序 + return $a['resource_count'] - $b['resource_count']; + }); + return $salesmen; } } \ No newline at end of file diff --git a/niucloud/app/service/admin/customer_resources/CustomerResourcesService.php b/niucloud/app/service/admin/customer_resources/CustomerResourcesService.php index a84dc765..0fef21af 100644 --- a/niucloud/app/service/admin/customer_resources/CustomerResourcesService.php +++ b/niucloud/app/service/admin/customer_resources/CustomerResourcesService.php @@ -29,6 +29,8 @@ use app\service\admin\member\MemberLabelService; use core\base\BaseAdminService; use PhpOffice\PhpSpreadsheet\IOFactory; use think\facade\Event; +use think\facade\Db; +use think\facade\Log; /** @@ -154,10 +156,8 @@ class CustomerResourcesService extends BaseAdminService */ public function add(array $data) { - $resourceSharing = new ResourceSharing(); $personnel = new Personnel(); $data['consultant'] = $personnel->where(['sys_user_id' => $this->uid])->value("id"); -// $data['consultant'] = 3; if (!$data['consultant']) { return fail("超级管理员不允许添加资源"); } @@ -179,19 +179,30 @@ class CustomerResourcesService extends BaseAdminService $data['trial_class_count'] = 2; $res = $this->model->create($data); + // 写入资源分配表 + $assignee_id = $data['consultant'] ?: 56; // 如果没有查到人员记录则使用默认值56 + try { + Db::table('school_resource_assignment')->insert([ + 'resource_id' => $res->id, + 'assignee_type' => 'user', + 'assignee_id' => $assignee_id, + 'is_primary' => 1, + 'assigned_by' => 0, + 'assigned_at' => date("Y-m-d H:i:s"), + 'campus_id' => $data['campus'], + 'assigned_source' => 'adminapi' + ]); + } catch (\Exception $e) { + // 如果插入失败,记录日志但不影响主流程 + Log::error("资源分配记录写入失败:" . $e->getMessage()); + } + $event_data = [ 'customer_resources_id' => $res->id,//客户资源表id 'event_type' => 'add'//事件类型"add=添加,edit=修改 ];//事件类型"add=添加,edit=修改 Event::trigger('CalculatePerformance', $event_data, true); - - $resourceSharing->insert([ - 'resource_id' => $res->id, - 'user_id' => $data['consultant'], - 'role_id' => $role_id - ]); - if ($data['purchase_power']) { $six_id = $sixSpeed->where(['resource_id' => $res->id])->value("id"); diff --git a/niucloud/app/service/api/apiService/CustomerResourcesService.php b/niucloud/app/service/api/apiService/CustomerResourcesService.php index ca95574d..ac05971f 100644 --- a/niucloud/app/service/api/apiService/CustomerResourcesService.php +++ b/niucloud/app/service/api/apiService/CustomerResourcesService.php @@ -137,13 +137,23 @@ class CustomerResourcesService extends BaseApiService Db::rollback(); return $res; } - // 资源共享表新增记录 - 使用传入的staff_id和role_id - $resourceSharing = new ResourceSharing(); - $resourceSharing->insert([ - 'resource_id' => $resource_id, - 'user_id' => $staff_id, // 使用传入的登录人staff_id - 'role_id' => $role_id // 使用传入的登录人role_id - ]); + // 写入资源分配表 + $assignee_id = $staff_id ?: 56; // 使用传入的staff_id,如果没有则使用默认值56 + try { + Db::table('school_resource_assignment')->insert([ + 'resource_id' => $resource_id, + 'assignee_type' => 'user', + 'assignee_id' => $assignee_id, + 'is_primary' => 1, + 'assigned_by' => 0, + 'assigned_at' => date("Y-m-d H:i:s"), + 'campus_id' => $customer_resources_data['campus'], + 'assigned_source' => 'api' + ]); + } catch (\Exception $e) { + // 如果插入失败,记录日志但不影响主流程 + Log::error("资源分配记录写入失败:" . $e->getMessage()); + } // 转介绍奖励逻辑:当source=3且有referral_resource_id时发放奖励 if ($customer_resources_data['source'] == '3' && !empty($customer_resources_data['referral_resource_id'])) { $this->grantReferralReward($customer_resources_data['referral_resource_id'], $resource_id); diff --git a/niucloud/app/service/api/apiService/ResourceSharingService.php b/niucloud/app/service/api/apiService/ResourceSharingService.php index fbdc2f50..007f5f9b 100644 --- a/niucloud/app/service/api/apiService/ResourceSharingService.php +++ b/niucloud/app/service/api/apiService/ResourceSharingService.php @@ -286,8 +286,8 @@ class ResourceSharingService extends BaseApiService if ($shared_scope === 'all') { $latestAssignmentSub = $this->assignmentModel ->alias('sub') - ->field('MAX(id) as latest_id, resource_id') - ->group('resource_id') + ->field('MAX(id) as latest_id, sub.resource_id') + ->group('sub.resource_id') ->buildSql(); $model = $model @@ -353,7 +353,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } // 共享时间查询 @@ -387,7 +387,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } } @@ -424,7 +424,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } } @@ -455,13 +455,53 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } } // 过滤已分配的资源(只显示可再分配的资源) // 注意:这里需要用子查询包装OR条件,避免与其他WHERE条件冲突 - // 查询数据 + // 处理到课类型查询 - 通过PersonCourseSchedule表关联查询 + if (!empty($where['attendance_type'])) { + if ($where['attendance_type'] === '一访已到') { + // 查询一访已到的资源ID - 通过至少有一次已完成课程来判断 + $attendance_resource_ids = \app\model\person_course_schedule\PersonCourseSchedule::where('person_type', 'customer_resource') + ->where('status', 1) // status=1表示已上课 + ->distinct(true) + ->column('resources_id'); + } else if ($where['attendance_type'] === '二访已到') { + // 查询二访已到的资源ID - 通过至少有两次已完成课程来判断 + $attendance_resource_ids = \app\model\person_course_schedule\PersonCourseSchedule::where('person_type', 'customer_resource') + ->where('status', 1) // status=1表示已上课 + ->group('resources_id') + ->having('count(*) >= 2') // 至少有两次考勤记录 + ->column('resources_id'); + } else { + // 其他到课类型的处理 + $attendance_resource_ids = []; + } + + if (isset($attendance_resource_ids)) { + if (empty($resource_ids)) { + $resource_ids = $attendance_resource_ids; + } else { + $resource_ids = array_intersect($resource_ids, $attendance_resource_ids); + } + + if (empty($resource_ids)) { + return [ + 'count' => 0, + 'total' => 0, + 'current_page' => $page, + 'last_page' => 0, + 'data' => [] + ]; + } + $model = $model->whereIn('ra.resource_id', $resource_ids); + } + } + + // 查询数据 $list = $model->with(['customerResource', 'sixSpeed']) ->order('assigned_at', 'desc') ->paginate([ @@ -540,42 +580,72 @@ class ResourceSharingService extends BaseApiService $consultant_names = $personnel->whereIn('id', $consultant_ids)->column('name', 'id'); } - // 查询销售老师信息(从资源分配表获取) + // 查询销售老师信息(从资源分配表获取assigned_by=0的记录) $sales_teachers = []; if (!empty($resource_ids)) { $assignment_model = new \app\model\resource_assignment\ResourceAssignment(); - $assignments = $assignment_model - ->whereIn('resource_id', $resource_ids) - ->where('assignee_type', 'user') - ->field('resource_id, assignee_id') - ->select() - ->toArray(); - // 获取所有销售人员ID - $sales_teacher_ids = array_unique(array_column($assignments, 'assignee_id')); + // 查询每个资源assigned_by=0的最新一条记录 + $sales_assignments = []; + foreach ($resource_ids as $resource_id) { + $latest_assignment = $assignment_model + ->where('resource_id', $resource_id) + ->where('assignee_type', 'user') + ->where('assigned_by', 0) + ->order('assigned_at', 'desc') + ->field('assignee_id') + ->find(); + + if ($latest_assignment) { + $sales_assignments[$resource_id] = $latest_assignment['assignee_id']; + } + } + + // 获取销售人员姓名 + $sales_teacher_ids = array_values(array_unique(array_filter($sales_assignments))); $sales_teacher_names = []; if (!empty($sales_teacher_ids)) { $personnel = new \app\model\personnel\Personnel(); $sales_teacher_names = $personnel->whereIn('id', $sales_teacher_ids)->column('name', 'id'); } - // 按resource_id组织销售老师名称 - foreach ($assignments as $assignment) { - $resource_id = $assignment['resource_id']; - $assignee_id = $assignment['assignee_id']; - $teacher_name = $sales_teacher_names[$assignee_id] ?? ''; + // 构建销售老师数组 + foreach ($sales_assignments as $resource_id => $assignee_id) { + $sales_teachers[$resource_id] = $sales_teacher_names[$assignee_id] ?? ''; + } + } - if (!isset($sales_teachers[$resource_id])) { - $sales_teachers[$resource_id] = []; - } - if (!empty($teacher_name)) { - $sales_teachers[$resource_id][] = $teacher_name; + // 查询市场老师信息(从资源分配表获取assigned_source为auto_allocation或api的最新记录) + $market_teachers = []; + if (!empty($resource_ids)) { + $assignment_model = new \app\model\resource_assignment\ResourceAssignment(); + + // 查询每个资源assigned_source为auto_allocation或api的最新一条记录 + $market_assignments = []; + foreach ($resource_ids as $resource_id) { + $latest_assignment = $assignment_model + ->where('resource_id', $resource_id) + ->whereIn('assigned_source', ['auto_allocation', 'api']) + ->order('assigned_at', 'desc') + ->field('assignee_id') + ->find(); + + if ($latest_assignment) { + $market_assignments[$resource_id] = $latest_assignment['assignee_id']; } } - // 将数组转为字符串 - foreach ($sales_teachers as $resource_id => &$teachers) { - $teachers = implode('、', array_unique($teachers)); + // 获取市场老师姓名 + $market_teacher_ids = array_values(array_unique(array_filter($market_assignments))); + $market_teacher_names = []; + if (!empty($market_teacher_ids)) { + $personnel = new \app\model\personnel\Personnel(); + $market_teacher_names = $personnel->whereIn('id', $market_teacher_ids)->column('name', 'id'); + } + + // 构建市场老师数组 + foreach ($market_assignments as $resource_id => $assignee_id) { + $market_teachers[$resource_id] = $market_teacher_names[$assignee_id] ?? ''; } } @@ -600,16 +670,16 @@ class ResourceSharingService extends BaseApiService // 需要先导入PersonCourseSchedule模型 $person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); $visit_records = $person_course_model - ->whereIn('person_id', $resource_ids) + ->whereIn('resources_id', $resource_ids) ->where('person_type', 'customer_resource') - ->field('person_id, course_date, time_slot, status') + ->field('resources_id, course_date, time_slot, status') ->order('course_date', 'desc') ->select() ->toArray(); // 处理到访信息 foreach ($visit_records as $record) { - $resource_id = $record['person_id']; + $resource_id = $record['resources_id']; if (!isset($visit_info[$resource_id])) { $visit_info[$resource_id] = [ @@ -699,13 +769,13 @@ class ResourceSharingService extends BaseApiService $item['customerResource']['consultant_name'] = ''; } - // 添加市场老师字段(与consultant_name相同) - $item['customerResource']['market_teacher'] = $item['customerResource']['consultant_name']; - - // 添加销售老师字段(从资源分配表获取的拼接字符串) + // 添加销售老师字段(从资源分配表获取) $resource_id = $item['resource_id']; $item['customerResource']['sales_teacher'] = $sales_teachers[$resource_id] ?? ''; + // 添加市场老师字段(从资源分配表获取的最新分配记录) + $item['customerResource']['market_teacher'] = $market_teachers[$resource_id] ?? ''; + $item['customerResource']['communication_time'] = $communication_times[$item['resource_id']] ?? ''; $resource_id = $item['resource_id']; @@ -884,7 +954,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } // 共享时间查询 @@ -918,7 +988,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } } @@ -955,7 +1025,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } } @@ -986,7 +1056,7 @@ class ResourceSharingService extends BaseApiService 'data' => [] ]; } - $model = $model->whereIn('resource_id', $resource_ids); + $model = $model->whereIn('ra.resource_id', $resource_ids); } } @@ -1059,42 +1129,72 @@ class ResourceSharingService extends BaseApiService $consultant_names = $personnel->whereIn('id', $consultant_ids)->column('name', 'id'); } - // 查询销售老师信息(从资源分配表获取) + // 查询销售老师信息(从资源分配表获取assigned_by=0的记录) $sales_teachers = []; if (!empty($resource_ids)) { $assignment_model = new \app\model\resource_assignment\ResourceAssignment(); - $assignments = $assignment_model - ->whereIn('resource_id', $resource_ids) - ->where('assignee_type', 'user') - ->field('resource_id, assignee_id') - ->select() - ->toArray(); - // 获取所有销售人员ID - $sales_teacher_ids = array_unique(array_column($assignments, 'assignee_id')); + // 查询每个资源assigned_by=0的最新一条记录 + $sales_assignments = []; + foreach ($resource_ids as $resource_id) { + $latest_assignment = $assignment_model + ->where('resource_id', $resource_id) + ->where('assignee_type', 'user') + ->where('assigned_by', 0) + ->order('assigned_at', 'desc') + ->field('assignee_id') + ->find(); + + if ($latest_assignment) { + $sales_assignments[$resource_id] = $latest_assignment['assignee_id']; + } + } + + // 获取销售人员姓名 + $sales_teacher_ids = array_values(array_unique(array_filter($sales_assignments))); $sales_teacher_names = []; if (!empty($sales_teacher_ids)) { $personnel = new \app\model\personnel\Personnel(); $sales_teacher_names = $personnel->whereIn('id', $sales_teacher_ids)->column('name', 'id'); } - // 按resource_id组织销售老师名称 - foreach ($assignments as $assignment) { - $resource_id = $assignment['resource_id']; - $assignee_id = $assignment['assignee_id']; - $teacher_name = $sales_teacher_names[$assignee_id] ?? ''; + // 构建销售老师数组 + foreach ($sales_assignments as $resource_id => $assignee_id) { + $sales_teachers[$resource_id] = $sales_teacher_names[$assignee_id] ?? ''; + } + } - if (!isset($sales_teachers[$resource_id])) { - $sales_teachers[$resource_id] = []; - } - if (!empty($teacher_name)) { - $sales_teachers[$resource_id][] = $teacher_name; + // 查询市场老师信息(从资源分配表获取assigned_source为auto_allocation或api的最新记录) + $market_teachers = []; + if (!empty($resource_ids)) { + $assignment_model = new \app\model\resource_assignment\ResourceAssignment(); + + // 查询每个资源assigned_source为auto_allocation或api的最新一条记录 + $market_assignments = []; + foreach ($resource_ids as $resource_id) { + $latest_assignment = $assignment_model + ->where('resource_id', $resource_id) + ->whereIn('assigned_source', ['auto_allocation', 'api']) + ->order('assigned_at', 'desc') + ->field('assignee_id') + ->find(); + + if ($latest_assignment) { + $market_assignments[$resource_id] = $latest_assignment['assignee_id']; } } - // 将数组转为字符串 - foreach ($sales_teachers as $resource_id => &$teachers) { - $teachers = implode('、', array_unique($teachers)); + // 获取市场老师姓名 + $market_teacher_ids = array_values(array_unique(array_filter($market_assignments))); + $market_teacher_names = []; + if (!empty($market_teacher_ids)) { + $personnel = new \app\model\personnel\Personnel(); + $market_teacher_names = $personnel->whereIn('id', $market_teacher_ids)->column('name', 'id'); + } + + // 构建市场老师数组 + foreach ($market_assignments as $resource_id => $assignee_id) { + $market_teachers[$resource_id] = $market_teacher_names[$assignee_id] ?? ''; } } @@ -1119,16 +1219,16 @@ class ResourceSharingService extends BaseApiService // 需要先导入PersonCourseSchedule模型 $person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); $visit_records = $person_course_model - ->whereIn('person_id', $resource_ids) + ->whereIn('resources_id', $resource_ids) ->where('person_type', 'customer_resource') - ->field('person_id, course_date, time_slot, status') + ->field('resources_id, course_date, time_slot, status') ->order('course_date', 'desc') ->select() ->toArray(); // 处理到访信息 foreach ($visit_records as $record) { - $resource_id = $record['person_id']; + $resource_id = $record['resources_id']; if (!isset($visit_info[$resource_id])) { $visit_info[$resource_id] = [