<?php
namespace App\Services\Admin\YeWu;

use Illuminate\Support\Facades\DB;

class DepartmentService
{
  public function GetList($searchInfo,$page,$pageSize)
  {
      $list=DB::table('s_department')->where('is_del',0);
      if(isset($searchInfo['status'])){
          $list=  $list->where('department_status',$searchInfo['status']);

      }
      if(!empty($searchInfo['name'])){
          $list=  $list->where('department_name','like','%'.$searchInfo['name'].'%');
      }
      $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();
        }
      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']])->get();
          $department_id=$userInfo[0]->department_id;
          $list= $list->where(['id'=>$department_id]);
      }
      $list= $list->where(['is_del'=>0,'department_status'=>1])->get();
      return \Yz::Return(true, '查询成功', ['list'=>$list]);
  }
  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)
  {
      $c= DB::table('s_department')->where('id',$id)->update(['is_del'=>1]);
      if(!$c){
          return \Yz::Return(false, '删除失败', []);
      }else{
          return \Yz::Return(true, '删除成功', []);
      }
  }
}
