|
|
@ -286,8 +286,8 @@ class ResourceSharingService extends BaseApiService |
|
|
if ($shared_scope === 'all') { |
|
|
if ($shared_scope === 'all') { |
|
|
$latestAssignmentSub = $this->assignmentModel |
|
|
$latestAssignmentSub = $this->assignmentModel |
|
|
->alias('sub') |
|
|
->alias('sub') |
|
|
->field('MAX(id) as latest_id, resource_id') |
|
|
->field('MAX(id) as latest_id, sub.resource_id') |
|
|
->group('resource_id') |
|
|
->group('sub.resource_id') |
|
|
->buildSql(); |
|
|
->buildSql(); |
|
|
|
|
|
|
|
|
$model = $model |
|
|
$model = $model |
|
|
@ -353,7 +353,7 @@ class ResourceSharingService extends BaseApiService |
|
|
'data' => [] |
|
|
'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' => [] |
|
|
'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' => [] |
|
|
'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' => [] |
|
|
'data' => [] |
|
|
]; |
|
|
]; |
|
|
} |
|
|
} |
|
|
$model = $model->whereIn('resource_id', $resource_ids); |
|
|
$model = $model->whereIn('ra.resource_id', $resource_ids); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 过滤已分配的资源(只显示可再分配的资源) |
|
|
// 过滤已分配的资源(只显示可再分配的资源) |
|
|
// 注意:这里需要用子查询包装OR条件,避免与其他WHERE条件冲突 |
|
|
// 注意:这里需要用子查询包装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']) |
|
|
$list = $model->with(['customerResource', 'sixSpeed']) |
|
|
->order('assigned_at', 'desc') |
|
|
->order('assigned_at', 'desc') |
|
|
->paginate([ |
|
|
->paginate([ |
|
|
@ -540,42 +580,72 @@ class ResourceSharingService extends BaseApiService |
|
|
$consultant_names = $personnel->whereIn('id', $consultant_ids)->column('name', 'id'); |
|
|
$consultant_names = $personnel->whereIn('id', $consultant_ids)->column('name', 'id'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 查询销售老师信息(从资源分配表获取) |
|
|
// 查询销售老师信息(从资源分配表获取assigned_by=0的记录) |
|
|
$sales_teachers = []; |
|
|
$sales_teachers = []; |
|
|
if (!empty($resource_ids)) { |
|
|
if (!empty($resource_ids)) { |
|
|
$assignment_model = new \app\model\resource_assignment\ResourceAssignment(); |
|
|
$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 |
|
|
// 查询每个资源assigned_by=0的最新一条记录 |
|
|
$sales_teacher_ids = array_unique(array_column($assignments, 'assignee_id')); |
|
|
$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 = []; |
|
|
$sales_teacher_names = []; |
|
|
if (!empty($sales_teacher_ids)) { |
|
|
if (!empty($sales_teacher_ids)) { |
|
|
$personnel = new \app\model\personnel\Personnel(); |
|
|
$personnel = new \app\model\personnel\Personnel(); |
|
|
$sales_teacher_names = $personnel->whereIn('id', $sales_teacher_ids)->column('name', 'id'); |
|
|
$sales_teacher_names = $personnel->whereIn('id', $sales_teacher_ids)->column('name', 'id'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 按resource_id组织销售老师名称 |
|
|
// 构建销售老师数组 |
|
|
foreach ($assignments as $assignment) { |
|
|
foreach ($sales_assignments as $resource_id => $assignee_id) { |
|
|
$resource_id = $assignment['resource_id']; |
|
|
$sales_teachers[$resource_id] = $sales_teacher_names[$assignee_id] ?? ''; |
|
|
$assignee_id = $assignment['assignee_id']; |
|
|
} |
|
|
$teacher_name = $sales_teacher_names[$assignee_id] ?? ''; |
|
|
} |
|
|
|
|
|
|
|
|
if (!isset($sales_teachers[$resource_id])) { |
|
|
// 查询市场老师信息(从资源分配表获取assigned_source为auto_allocation或api的最新记录) |
|
|
$sales_teachers[$resource_id] = []; |
|
|
$market_teachers = []; |
|
|
} |
|
|
if (!empty($resource_ids)) { |
|
|
if (!empty($teacher_name)) { |
|
|
$assignment_model = new \app\model\resource_assignment\ResourceAssignment(); |
|
|
$sales_teachers[$resource_id][] = $teacher_name; |
|
|
|
|
|
|
|
|
// 查询每个资源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) { |
|
|
$market_teacher_ids = array_values(array_unique(array_filter($market_assignments))); |
|
|
$teachers = implode('、', array_unique($teachers)); |
|
|
$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模型 |
|
|
// 需要先导入PersonCourseSchedule模型 |
|
|
$person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); |
|
|
$person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); |
|
|
$visit_records = $person_course_model |
|
|
$visit_records = $person_course_model |
|
|
->whereIn('person_id', $resource_ids) |
|
|
->whereIn('resources_id', $resource_ids) |
|
|
->where('person_type', 'customer_resource') |
|
|
->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') |
|
|
->order('course_date', 'desc') |
|
|
->select() |
|
|
->select() |
|
|
->toArray(); |
|
|
->toArray(); |
|
|
|
|
|
|
|
|
// 处理到访信息 |
|
|
// 处理到访信息 |
|
|
foreach ($visit_records as $record) { |
|
|
foreach ($visit_records as $record) { |
|
|
$resource_id = $record['person_id']; |
|
|
$resource_id = $record['resources_id']; |
|
|
|
|
|
|
|
|
if (!isset($visit_info[$resource_id])) { |
|
|
if (!isset($visit_info[$resource_id])) { |
|
|
$visit_info[$resource_id] = [ |
|
|
$visit_info[$resource_id] = [ |
|
|
@ -699,13 +769,13 @@ class ResourceSharingService extends BaseApiService |
|
|
$item['customerResource']['consultant_name'] = ''; |
|
|
$item['customerResource']['consultant_name'] = ''; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 添加市场老师字段(与consultant_name相同) |
|
|
// 添加销售老师字段(从资源分配表获取) |
|
|
$item['customerResource']['market_teacher'] = $item['customerResource']['consultant_name']; |
|
|
|
|
|
|
|
|
|
|
|
// 添加销售老师字段(从资源分配表获取的拼接字符串) |
|
|
|
|
|
$resource_id = $item['resource_id']; |
|
|
$resource_id = $item['resource_id']; |
|
|
$item['customerResource']['sales_teacher'] = $sales_teachers[$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']] ?? ''; |
|
|
$item['customerResource']['communication_time'] = $communication_times[$item['resource_id']] ?? ''; |
|
|
|
|
|
|
|
|
$resource_id = $item['resource_id']; |
|
|
$resource_id = $item['resource_id']; |
|
|
@ -884,7 +954,7 @@ class ResourceSharingService extends BaseApiService |
|
|
'data' => [] |
|
|
'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' => [] |
|
|
'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' => [] |
|
|
'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' => [] |
|
|
'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'); |
|
|
$consultant_names = $personnel->whereIn('id', $consultant_ids)->column('name', 'id'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 查询销售老师信息(从资源分配表获取) |
|
|
// 查询销售老师信息(从资源分配表获取assigned_by=0的记录) |
|
|
$sales_teachers = []; |
|
|
$sales_teachers = []; |
|
|
if (!empty($resource_ids)) { |
|
|
if (!empty($resource_ids)) { |
|
|
$assignment_model = new \app\model\resource_assignment\ResourceAssignment(); |
|
|
$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 |
|
|
// 查询每个资源assigned_by=0的最新一条记录 |
|
|
$sales_teacher_ids = array_unique(array_column($assignments, 'assignee_id')); |
|
|
$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 = []; |
|
|
$sales_teacher_names = []; |
|
|
if (!empty($sales_teacher_ids)) { |
|
|
if (!empty($sales_teacher_ids)) { |
|
|
$personnel = new \app\model\personnel\Personnel(); |
|
|
$personnel = new \app\model\personnel\Personnel(); |
|
|
$sales_teacher_names = $personnel->whereIn('id', $sales_teacher_ids)->column('name', 'id'); |
|
|
$sales_teacher_names = $personnel->whereIn('id', $sales_teacher_ids)->column('name', 'id'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 按resource_id组织销售老师名称 |
|
|
// 构建销售老师数组 |
|
|
foreach ($assignments as $assignment) { |
|
|
foreach ($sales_assignments as $resource_id => $assignee_id) { |
|
|
$resource_id = $assignment['resource_id']; |
|
|
$sales_teachers[$resource_id] = $sales_teacher_names[$assignee_id] ?? ''; |
|
|
$assignee_id = $assignment['assignee_id']; |
|
|
} |
|
|
$teacher_name = $sales_teacher_names[$assignee_id] ?? ''; |
|
|
} |
|
|
|
|
|
|
|
|
if (!isset($sales_teachers[$resource_id])) { |
|
|
// 查询市场老师信息(从资源分配表获取assigned_source为auto_allocation或api的最新记录) |
|
|
$sales_teachers[$resource_id] = []; |
|
|
$market_teachers = []; |
|
|
} |
|
|
if (!empty($resource_ids)) { |
|
|
if (!empty($teacher_name)) { |
|
|
$assignment_model = new \app\model\resource_assignment\ResourceAssignment(); |
|
|
$sales_teachers[$resource_id][] = $teacher_name; |
|
|
|
|
|
|
|
|
// 查询每个资源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) { |
|
|
$market_teacher_ids = array_values(array_unique(array_filter($market_assignments))); |
|
|
$teachers = implode('、', array_unique($teachers)); |
|
|
$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模型 |
|
|
// 需要先导入PersonCourseSchedule模型 |
|
|
$person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); |
|
|
$person_course_model = new \app\model\person_course_schedule\PersonCourseSchedule(); |
|
|
$visit_records = $person_course_model |
|
|
$visit_records = $person_course_model |
|
|
->whereIn('person_id', $resource_ids) |
|
|
->whereIn('resources_id', $resource_ids) |
|
|
->where('person_type', 'customer_resource') |
|
|
->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') |
|
|
->order('course_date', 'desc') |
|
|
->select() |
|
|
->select() |
|
|
->toArray(); |
|
|
->toArray(); |
|
|
|
|
|
|
|
|
// 处理到访信息 |
|
|
// 处理到访信息 |
|
|
foreach ($visit_records as $record) { |
|
|
foreach ($visit_records as $record) { |
|
|
$resource_id = $record['person_id']; |
|
|
$resource_id = $record['resources_id']; |
|
|
|
|
|
|
|
|
if (!isset($visit_info[$resource_id])) { |
|
|
if (!isset($visit_info[$resource_id])) { |
|
|
$visit_info[$resource_id] = [ |
|
|
$visit_info[$resource_id] = [ |
|
|
|