You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
8.6 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services\Admin\YeWu;
use Illuminate\Support\Facades\DB;
class DepartmentService
{
public function GetList($searchInfo,$page,$pageSize,$userid=null,$group=null)
{
$list=DB::table('s_department')->where('is_del',0);
if(!in_array($group,[1,5]) && $userid){
$userInfo = DB::table('users')->where(['id'=>$userid])->first();
if($userInfo){
$deptIds = [];
if(!empty($userInfo->department_ids)){
$deptIds = explode(",", $userInfo->department_ids);
} elseif($userInfo->department_id){
$deptIds = [$userInfo->department_id];
}
if(count($deptIds) > 0){
$list = $list->whereIn('id', $deptIds);
}
}
}
if(isset($searchInfo['status'])){
$list= $list->where('department_status',$searchInfo['status']);
}
if(!empty($searchInfo['name'])){
$list= $list->where('department_name','like','%'.$searchInfo['name'].'%');
}
//只显示执行科室(有科室资源的科室)
if(!empty($searchInfo['only_execute'])){
$hasResourceDeptIds = DB::table('s_department_resources')
->where('is_del', 0)
->distinct()
->pluck('department_id');
$list = $list->whereIn('id', $hasResourceDeptIds);
}
//只显示一级科室pid=0的顶级科室
if(!empty($searchInfo['only_first_level'])){
$list = $list->where('pid', 0);
}
//只显示参与预约的科室
if(!empty($searchInfo['appointment_enabled'])){
$list = $list->where('appointment_enabled', 1);
}
$c=$list->count();
$l=$list ->orderBy('id','desc')->skip(($page-1)*$pageSize)
->take($pageSize)->get() ;
//查询科室下的资源总数和子科室数量
foreach ($l as $k=>$v){
$l[$k]->user_list=DB::table('users')->where(['department_id'=>$v->id,'status'=>1])->get();
$l[$k]->resource_count=DB::table('s_department_resources')->where(['department_id'=>$v->id,'is_del'=>0])->count();
$l[$k]->children_count=DB::table('s_department')->where(['pid'=>$v->id,'is_del'=>0])->count();
}
return \Yz::Return(true, '查询成功', ['list'=>$l,'count'=>$c]);
}
public function GetEnableList($arr,$is_all=false)
{
$list=DB::table('s_department');
if(!in_array($arr['group'],[1,5]) and $is_all===false){
$userInfo = DB::table('users')->where(['id'=>$arr['userid']])->first();
if($userInfo){
$deptIds = [];
if(!empty($userInfo->department_ids)){
$deptIds = explode(",", $userInfo->department_ids);
} elseif($userInfo->department_id){
$deptIds = [$userInfo->department_id];
}
if(count($deptIds) > 0){
$list = $list->whereIn('id', $deptIds);
}
}
}
$list= $list->where(['is_del'=>0,'department_status'=>1])->get();
return \Yz::Return(true, '查询成功', ['list'=>$list]);
}
// 获取筛选后的可用科室列表(仅显示存在执行诊室的科室)
public function GetFilteredEnableList($arr, $is_all = false)
{
$list = DB::table('s_department');
if (!in_array($arr['group'], [1, 5]) && $is_all === false) {
$userInfo = DB::table('users')->where(['id' => $arr['userid']])->first();
if ($userInfo) {
$deptIds = [];
if (!empty($userInfo->department_ids)) {
$deptIds = explode(",", $userInfo->department_ids);
} elseif ($userInfo->department_id) {
$deptIds = [$userInfo->department_id];
}
if (count($deptIds) > 0) {
$list = $list->whereIn('id', $deptIds);
}
}
}
// 基础过滤:启用、未删除
$list = $list->where(['is_del' => 0, 'department_status' => 1]);
// 只显示执行科室(在 s_department_resources 表中有记录的科室)
$hasResourceDeptIds = DB::table('s_department_resources')
->where('is_del', 0)
->distinct()
->pluck('department_id');
$list = $list->whereIn('id', $hasResourceDeptIds);
return \Yz::Return(true, '查询成功', ['list' => $list->get()]);
}
public function Save($info)
{
if(isset($info['id'])){
$id=$info['id'];
unset($info['id']);
$c= DB::table('s_department')->where('id',$id)->update($info);
}else{
$info['is_del']=0;
$c= DB::table('s_department')->insert($info);
}
if(!$c){
return \Yz::Return(false, '保存失败', []);
}else{
return \Yz::Return(true, '保存成功', []);
}
}
public function Del($id)
{
//将子科室的pid置0
DB::table('s_department')->where('pid',$id)->where('is_del',0)->update(['pid'=>0]);
$c= DB::table('s_department')->where('id',$id)->update(['is_del'=>1]);
if(!$c){
return \Yz::Return(false, '删除失败', []);
}else{
return \Yz::Return(true, '删除成功', []);
}
}
//获取科室已关联的子科室ID列表
public function GetChildren($department_id)
{
$ids = DB::table('s_department')
->where('pid', $department_id)
->where('is_del', 0)
->pluck('id');
return \Yz::Return(true, '查询成功', ['ids' => $ids]);
}
//获取可作为诊室的叶子节点科室列表空闲的叶子节点pid=0且无子科室
public function GetLeafList($department_id)
{
$subIds = DB::table('s_department')
->where('pid', '>', 0)
->where('is_del', 0)
->pluck('pid');
//已关联的子科室IDs供右侧显示
$childrenIds = DB::table('s_department')
->where('pid', $department_id)
->where('is_del', 0)
->pluck('id');
$list = DB::table('s_department')
->where('is_del', 0)
->where('id', '!=', $department_id)
->where(function($query) use ($subIds, $childrenIds) {
$query->where(function($q) use ($subIds) {
$q->where('pid', 0)->whereNotIn('id', $subIds);
})->orWhereIn('id', $childrenIds);
})
->select('id', 'department_name')
->get();
return \Yz::Return(true, '查询成功', ['list' => $list]);
}
//保存科室关联诊室关系
public function SaveChildren($department_id, $children_ids)
{
//先清空旧关联
DB::table('s_department')
->where('pid', $department_id)
->where('is_del', 0)
->update(['pid' => 0]);
//设置新关联
if (!empty($children_ids)) {
DB::table('s_department')
->whereIn('id', $children_ids)
->update(['pid' => $department_id]);
}
return \Yz::Return(true, '保存成功', []);
}
//获取有排班资源的科室列表(排除指定科室自身)
public function GetHasScheduleTopDepartments($exclude_id = null)
{
$hasResourceDeptIds = DB::table('s_department_resources')
->where('is_del', 0)
->distinct()
->pluck('department_id');
$list = DB::table('s_department')
->where('is_del', 0)
->where('department_status', 1)
->whereIn('id', $hasResourceDeptIds)
->when($exclude_id, function($q) use ($exclude_id) {
$q->where('id', '!=', $exclude_id);
})
->select('id', 'department_name')
->get();
return \Yz::Return(true, '查询成功', ['list' => $list]);
}
//按名称或编号模糊搜索科室
public function Search($keyword)
{
$list = DB::table('s_department')
->where('is_del', 0)
->where('department_status', 1)
->where(function($q) use ($keyword) {
$q->where('department_name', 'like', "%{$keyword}%")
->orWhere('department_number', 'like', "%{$keyword}%");
})
->select('id', 'department_name', 'department_number')
->orderBy('department_name')
->limit(20)
->get();
return \Yz::Return(true, '查询成功', ['list' => $list]);
}
//获取指定科室的下级科室列表(执行诊室)
public function GetChildrenDepartments($department_id)
{
$list = DB::table('s_department')
->where('pid', $department_id)
->where('is_del', 0)
->where('department_status', 1)
->select('id', 'department_name')
->get();
return \Yz::Return(true, '查询成功', ['list' => $list]);
}
}