You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
402 lines
15 KiB
402 lines
15 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\api\apiService;
|
|
|
|
use app\model\campus\Campus;
|
|
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_sharing\ResourceSharing;
|
|
use core\base\BaseApiService;
|
|
|
|
/**
|
|
* 资源共享服务层
|
|
* Class MemberService
|
|
* @package app\service\api\member
|
|
*/
|
|
class ResourceSharingService extends BaseApiService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = (new ResourceSharing());
|
|
}
|
|
|
|
//查询资源共享详情
|
|
public function info(array $where, string $field = '*')
|
|
{
|
|
|
|
$res = [
|
|
'code' => 0,
|
|
'msg' => '操作失败',
|
|
'data' => []
|
|
];
|
|
if (!$where) {
|
|
$res['msg'] = '缺少查询条件';
|
|
return $res;
|
|
}
|
|
|
|
$model = $this->model;
|
|
|
|
if (!empty($where['id'])) {
|
|
$model = $model->where('id', $where['id']);
|
|
}
|
|
|
|
$data = $model->with([
|
|
'customerResource' => function ($query) {
|
|
$query->with([
|
|
'sixSpeed' => function ($query_2) {
|
|
$query_2->append([
|
|
'purchase_power_name',//购买力
|
|
'concept_awareness_name',//认知理念
|
|
]);
|
|
},
|
|
'personnel' => function ($query_2) {
|
|
}
|
|
])->append([
|
|
'source_channel_name',//来源渠道
|
|
'source_name',//来源
|
|
'gender_name',//性别
|
|
'purchasing_power_name',//购买力
|
|
'cognitive_idea_name',//认知理念
|
|
'initial_intent_name',//客户初步意向度
|
|
'status_name',//客户状态
|
|
]);
|
|
}
|
|
])->field($field)->find();
|
|
if ($data) {
|
|
$data = $data->toArray();
|
|
}
|
|
if (!empty($data['customerResource'])) {
|
|
$data['customerResource']['cj_count'] = OrderTable::where('resource_id', $data['customerResource']['id'])
|
|
->where('order_status', 'paid')
|
|
->count();//成交次数
|
|
}
|
|
// dd(123123,$data);
|
|
if ($data) {
|
|
$res['code'] = 1;
|
|
$res['msg'] = '操作成功';
|
|
$res['data'] = $data;
|
|
|
|
} else {
|
|
$res['code'] = 0;
|
|
$res['msg'] = '未找到数据';
|
|
$res['data'] = [];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
//更新资源共享表
|
|
public function editData(array $where, array $data)
|
|
{
|
|
$res = [
|
|
'code' => 0,
|
|
'msg' => '操作失败',
|
|
'data' => []
|
|
];
|
|
if (!$where) {
|
|
$res['msg'] = '查询条件不能为空';
|
|
return $res;
|
|
}
|
|
|
|
$model = $this->model;
|
|
if ($where['id']) {
|
|
$model = $model->where('id', $where['id']);
|
|
}
|
|
$data = $model->update($data);
|
|
if ($data) {
|
|
$res = [
|
|
'code' => 1,
|
|
'msg' => '操作成功',
|
|
'data' => []
|
|
];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
public function getList($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]);
|
|
}
|
|
|
|
// 过滤已分配的资源(只显示可再分配的资源)
|
|
// 注意:这里需要用子查询包装OR条件,避免与其他WHERE条件冲突
|
|
$model = $model->when($where['shared_by'] > 0, function ($query) use ($where) {
|
|
$query->where(function ($subQuery) use ($where) {
|
|
$subQuery->where('shared_by', $where['shared_by'])->whereOr('user_id', $where['shared_by']);
|
|
});
|
|
}, function ($query) {
|
|
$query->where(function ($subQuery) {
|
|
$subQuery->where('shared_by', 0)->whereOr('shared_by', null);
|
|
});
|
|
});
|
|
|
|
// 查询数据
|
|
$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');
|
|
$resource_ids = array_unique(array_filter($resource_ids));
|
|
|
|
// 获取分配人员ID列表
|
|
$shared_by_ids = array_column($list['data'], 'shared_by');
|
|
$shared_by_ids = array_unique(array_filter($shared_by_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');
|
|
}
|
|
|
|
// 查询最近沟通记录
|
|
$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)) {
|
|
$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 ($six_speed_records as $record) {
|
|
$order_status[$record['resource_id']] = $record['is_closed'] ? '已开单' : '未开单';
|
|
}
|
|
}
|
|
|
|
// 处理每条数据
|
|
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('SourceChannel', $item['customerResource']['source_channel']);
|
|
$item['customerResource']['campus_name'] = $campus_names[$item['customerResource']['campus']] ?? '';
|
|
|
|
// 修复沟通时间字段
|
|
$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['shared_by_name'] = $shared_by_names[$item['shared_by']] ?? '未分配';
|
|
|
|
// 判断资源是否可再分配(如果已有shared_by且不为0,说明已分配)
|
|
$item['can_reassign'] = empty($item['shared_by']) || $item['shared_by'] == 0;
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
}
|
|
|