<?php

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

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

class EntrustController extends Controller
{
     public function GetList(){
         $searchInfo = request('searchInfo');
         $page = request('page');
         $pageSize = request('pageSize');
         $list=DB::table('s_list')
             ->leftJoin('s_period','s_list.reservation_time','=','s_period.id')
             ->leftJoin('s_department_resources','s_list.reservation_sources','=','s_department_resources.id')
             ->select('s_list.*','s_period.period_begin_time','s_period.period_end_time','s_department_resources.department_resources_name');
         if ($searchInfo['dateRange']!=null and count($searchInfo['dateRange']) == 2) {
             $list = $list->where(function ($q) use($searchInfo) {
                 $q->whereBetween('s_list.entrust_date', $searchInfo['dateRange'])
                     ->orWhereBetween('s_list.reservation_date', $searchInfo['dateRange']);
             });
         }
         if(isset($searchInfo['department_id'])){
             $department = DB::table('s_department')->where(['id' => $searchInfo['department_id'],'is_del'=>0])->first();
             $list=$list->where(['RISRAcceptDeptCode'=>$department->department_number]);
         }
         if (isset($searchInfo['list_status'])) {
             $list = $list->where('s_list.list_status', $searchInfo['list_status']);
         }
         if (isset($searchInfo['patient_type'])) {
             $list = $list->where('s_list.patient_type', $searchInfo['patient_type']);
         }
         if (!empty($searchInfo['resources'])) {
             $list = $list->whereIn('s_list.reservation_sources', $searchInfo['resources']);
         }
         if (isset($searchInfo['services_group'])) {
             $list = $list->whereRaw("FIND_IN_SET({$searchInfo['services_group']}, s_list.services_group)");
         }
         if (isset($searchInfo['reg_num'])) {
             $list = $list->where('s_list.reg_num', $searchInfo['reg_num']);
         }
         if (isset($searchInfo['user_name'])) {
             $list = $list->where('s_list.user_name', 'like','%'.$searchInfo['user_name'].'%');
         }
         if (isset($searchInfo['doctor'])) {
             $list = $list->where('s_list.docotr', 'like','%'.$searchInfo['doctor'].'%');
         }
         if (isset($searchInfo['apply_department'])) {
             $list = $list->where('s_list.reservation_department', 'like','%'.$searchInfo['apply_department'].'%');
         }

         $count = $list;
         $count = $count->count();
         $list=$list->where(['s_list.is_del'=>0,'s_list.is_nullify'=>0])->orderBy('id', 'desc')->limit($pageSize)->skip(($page - 1) * $pageSize) // 跳过前9999条记录
         ->take($pageSize)->get();

         //匹配设备（服务组）
         $devices = DB::table('s_devices')->get();
         foreach ($list as $key => $value) {
             $list[$key]->age = Tools::calculateAgeText($value->user_brithday);
             $list[$key]->devices = [];
             $array_device_id = explode(",", $value->services_group);
             foreach ($devices as $k => $v) {
                 if (in_array($v->id, $array_device_id)) {
                     $list[$key]->devices[] = $v;
                 }
             }
         }
         return \Yz::Return(true,'查询完成',['list'=>$list,'count'=>$count]);
     }
     //获取医嘱信息 支持多人
    public function getMainDetail_duoren()
    {
        date_default_timezone_set('PRC');
        $entrustInfos = request('entrustInfo');
        $entrustids = request('entrustid');
        $episodeid = request('episodeid');
        $appointment_type=request('appointment_type');
        if(empty($entrustInfos)) return \Yz::echoError1("entrustInfo不能为空");
        $allInfo=[];
        foreach ($entrustInfos as $key => $entrustInfo) {
            $info = DB::table('s_list as a')->where(['a.reg_num' => $entrustInfo['reg_num'], 'a.episodeid'=>$entrustInfo['episodeid'],'a.is_nullify'=>0])
                ->select('a.*','c.period_begin_time','c.period_end_time')
                ->leftJoin('s_period as c','a.reservation_time','=','c.id')
                ->whereIn('a.entrust_id',$entrustInfo['entrustid'])
                ->get();
            if (count($info)===0) return \Yz::echoError1('登记号'.$entrustInfo['reg_num'].'没有找到对应医嘱信息');

            $mainInfo=$info[0];
            $itemInfo=[];
            foreach ($info as $value){
                $ii = DB::table('s_check_item as a')
                    ->leftJoin('s_check_item_device as b','a.id','=','b.item_id')
                    ->where(['a.item_code' => $value->entrust_code,'a.status'=>1,"a.is_del"=>0])->first();

                $msg='';
                if(!$ii){
                    $msg="检查项目不可用";
                }
                if(!!$ii){
                    if(empty($ii->device_id)){
                        $msg="检查项目未关联服务组";
                    }

                    $qudaos = explode(',', $ii->reservation_method);
                    if (!in_array($appointment_type, $qudaos)){
                        $msg=$msg.',该项目不支持在此渠道预约';
                    }

                }
                $itemInfo[]=[
                    'entrust'=>$value,
                    'item'=>$ii,
                    'msg'=>$msg,
                    'selected'=>false
                ];
            }
            $allInfo[]=['mainInfo'=>$mainInfo,'itemInfo'=>$itemInfo];
        }





        return \Yz::Return(true,'查询完成',['today_date'=>date("Y-m-d"),'info'=>$allInfo]);
    }
}
