<?php
namespace App\Services\Admin\YeWu;
use Illuminate\Support\Facades\DB;
class DepartmentResourceService
{
    public function Save($info,$userid)
    {
        $id = $info['id'];
        $department_id = $info['department_id'];
        //查询科室状态是否正常
        $department = DB::table('s_department')
            ->where('id', $department_id)
            ->where('is_del', 0)
            ->where('department_status', 1)
            ->first();
        if(!$department) {
            return \Yz::Return(false, '科室状态异常', []);
        }
        if(isset($id)) {
            $c = DB::table('s_department_resources')
                ->where('id', $id)
                ->update($info);
        }else {
            $info['is_del']=0;
            $info['adduser']=$userid;
            $c = DB::table('s_department_resources')
                ->insert($info);
        }
        if(!$c) {
            return \Yz::Return(false, '保存失败', []);
        }else{
            return \Yz::Return(true, '保存成功', []);
        }
    }
    public function GetList($searchInfo,$page,$pageSize){
        $list=DB::table('s_department_resources as a')->join('s_department','a.department_id','=','s_department.id')
            ->where('a.is_del',0);
        if(isset($searchInfo['department_id'])){
            $list=  $list->where('a.department_id',$searchInfo['department_id']);
        }
        if(isset($searchInfo['status'])){
            $list=  $list->where('a.department_resources_status',$searchInfo['status']);
        }
        if(!empty($searchInfo['name'])){
            $list=  $list->where('a.department_resources_name','like','%'.$searchInfo['name'].'%');
        }
        $c=$list->count();
        $list=$list->select(['a.*','s_department.department_name'])->skip(($page-1)*$pageSize)
            ->take($pageSize)->get() ;
        foreach ($list as $k=>$v){
            $list[$k]->devicesInfo=DB::table('s_department_resources_device')
                ->join('s_devices', 's_department_resources_device.device_id', '=', 's_devices.id')
                ->select('s_devices.*')
                ->where('rsourece_id',$v->id)->get();
        }
        return \Yz::Return(true, '查询成功', ['list'=>$list,'count'=>$c]);
    }
    public function Del($id){
        $c= DB::table('s_department_resources')->where('id',$id)->update(['is_del'=>1]);
        if(!$c){
            return \Yz::Return(false, '删除失败', []);
        }else{
            return \Yz::Return(true, '删除成功', []);
        }
    }
    //绑定设备
    public function BindDevice($department_id,$resource_id,$device_ids){
        $data=[];
        foreach ($device_ids as $k=>$v){
            $data[$k]['department_id']=$department_id;
            $data[$k]['rsourece_id']=$resource_id;
            $data[$k]['device_id']=$v;
        }
        DB::beginTransaction();
        DB::table('s_department_resources_device')->where('rsourece_id',$resource_id)->delete();
        $i= DB::table('s_department_resources_device')->insert($data);
        if($i>0){
            DB::commit();
            return \Yz::Return(true, '绑定成功', []);
        }else{
            DB::rollBack();
            return \Yz::Return(false, '绑定失败');
        }
    }
    public function GetBindDeviceList($resource_id)
    {
        $list =DB::table('s_department_resources_device as a')
            ->join('s_devices as b', 'a.device_id', '=', 'b.id')
            ->select('b.*')
            ->where(['a.rsourece_id'=>$resource_id,'b.status'=>1,'b.is_del'=>0])->get();
        return \Yz::Return(true, '查询成功', $list);
    }
}
