<?php

namespace App\Http\Controllers\API\Admin\YeWu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class InpatientWardController extends Controller
{
    //获取病区列表
    public function GetList()
    {
        $page = request('page');
        $pageSize = request('pageSize');
        $searchInfo = request('searchInfo');
        $list=DB::table('s_inpatient_ward')->where(['is_del'=>0]);
        if(!empty($searchInfo['name'])){
            $list=$list->where(['name','like','%'.$searchInfo['name'].'%']);
        }
        $count= $list->count();
        $list= $list->skip(($page-1)*$pageSize) // 跳过前9999条记录
        ->take($pageSize)->get();
        foreach ($list as $k=>$v){
            $list[$k]->user_list=DB::table('users')
                ->whereRaw("FIND_IN_SET(?, ward)", [$v->name])
                ->where(['status'=>1])->get();
        }
        return \Yz::Return(true,"查询完成",['list'=>$list,'count'=>$count]);
    }
    public function Save(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $data = request('Info');
        if(empty($data['id'])){
            $id=DB::table('s_inpatient_ward')->insertGetId($data);
            if($id){
                return \Yz::Return(true, '添加成功', $id);
            }else{
                return \Yz::Return(false, '添加失败');
            }
        }else{
            $res=DB::table('s_inpatient_ward')->where('id',$data['id'])->update($data);
            if($res){
                return \Yz::Return(true, '修改成功');
            }else{
                return \Yz::Return(false, '修改失败');
            }
        }
    }

    public function Del()
    {
        $id = request('id');
        $res=DB::table('s_inpatient_ward')->where('id',$id)->update(['is_del'=>1]);
        if($res){
            return \Yz::Return(true, '删除成功');
        }else{
            return \Yz::Return(false, '删除失败');
        }
    }
}
