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.
312 lines
11 KiB
312 lines
11 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]);
|
|
}
|
|
|
|
// 调试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('SourceChannel', $item['customerResource']['source_channel']);
|
|
$item['customerResource']['campus_name'] = $campus_names[$item['customerResource']['campus']] ?? '';
|
|
$item['customerResource']['communication_time'] = $resultdata[$item['resource_id']] ?? '';
|
|
}
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
}
|
|
|