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

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class HealthCalendarService
{

//    创建日历
//    参数：['info'=>$info,'group'=>$group,'userid'=>$userid]

    public function CreateCalendar($arr){

      //如果是体检机构为自己创建日历判断权限，是否是为自己创建，否则拒绝
        if($arr['group']==7){
            $cha=DB::table('medical_institution')->where(['link_user_id'=>$arr['userid']])->get();
            if($cha[0]->id <> $arr['info']['orgId']){
                return \Yz::echoError1('无权限为此机构创建体检日历');
            }
        }
        $startDate = Carbon::parse($arr['info']['dateRange'][0]);
        $endDate = Carbon::parse($arr['info']['dateRange'][1]);
        $currentDate = $startDate;
        $time=$arr['info']['timeRange'][0].'--'.$arr['info']['timeRange'][1];
        $j=0;
        while ($currentDate->lte($endDate)) {
            $day= $currentDate->format('Y-m-d') . "\n";
            $weekday=date('N',strtotime($day));
            if (in_array($weekday,$arr['info']['xingqi'] )) { //判断循环到的日期那天的星期是否在勾选的星期内，是则进行添加
                $i=DB::table('institutional_calendar')->insert([
                    'institution_id'=>$arr['info']['orgId'],
                    'date'=>$day,
                    'week'=>$weekday,
                    'time'=>$time,
                    'end_time'=>$day.' '.$arr['info']['endTime'],
                    'count'=>$arr['info']['count'],
                    'status'=>1
                ]);
                $j++;
            }

            $currentDate->addDay(); // 增加一天
        }

        return \Yz::Return(true,'成功创建'.$j.'天体检日历',[]);
    }
    //获取体检机构日历
    //参数['group'=>$group,'userid'=>$userid,'page'=>$page,'pageSize'=>$pageSize,'searchInfo'=>$searchInfo]
    public function getList($arr){
        $sql=' where a.status in(0,1)';
        $canshu=array();



            if($arr['searchInfo']['status']==99){
                $sql=' where a.status in(0,1)';
            }
            if($arr['searchInfo']['status']==0){
                $sql=' where a.status in(0)';
            }
            if($arr['searchInfo']['status']==1){
                $sql=' where a.status in(1)';
            }


        if($arr['searchInfo']['dateRange']){

            $sql=$sql . ' and a.date>=? and a.date<=? ';
           array_push($canshu,$arr['searchInfo']['dateRange'][0],$arr['searchInfo']['dateRange'][1]);
        }
        if($arr['searchInfo']['xingqi']){
            $placeholders = rtrim(str_repeat('?,', count($arr['searchInfo']['xingqi'])), ',');
            $sql=$sql . ' and a.week in('.$placeholders.') ';
            foreach ($arr['searchInfo']['xingqi'] as $item){
                array_push($canshu,$item);
            }

        }

        if($arr['group']==7){
            $cha=DB::table('medical_institution')->where(['link_user_id'=>$arr['userid']])->get();
            $sql=$sql .' and  a.institution_id=?';
            array_push($canshu, $cha[0]->id);
        }else{
            if($arr['searchInfo']['orgId']){

                $sql=$sql .' and  a.institution_id=?';
                array_push($canshu, $arr['searchInfo']['orgId']);
            }

        }
        array_push($canshu,($arr['page']-1)*$arr['pageSize'],$arr['pageSize']);
       // $query=DB::select("select a.*,b.org_name,c.usedcount from institutional_calendar as a left join medical_institution as b on a.institution_id=b.id  LEFT JOIN (select calendar_id,count(*) as usedcount from appointment_record where status=1 group by calendar_id) as c on a.id=c.calendar_id ".$sql." order by a.date limit ?,?",$canshu);
       // $count=DB::select("select count(*) as  c from institutional_calendar as a left join medical_institution as b on a.institution_id=b.id  LEFT JOIN (select calendar_id,count(*) as usedcount from appointment_record where status=1 group by calendar_id) as c on a.id=c.calendar_id ".$sql,$canshu);
       // return \Yz::Return(true,'',['list'=>$query,'count'=>$count[0]->c]);
        return [];
    }

    //参数['list'=>$list,'group'=>$group,'userid'=>$userid]
    public function Del($arr){
        $query = DB::table('institutional_calendar');
        if($arr['group']==7) {
            $cha = DB::table('medical_institution')->select(['id'])->where(['link_user_id' => $arr['userid']])->get();
            $query->where('institution_id',$cha[0]->id);
        }
        $d=$query->whereIn('id',$arr['list'])->delete();
        if($d){
            return \Yz::Return(true,'',[]);
        }else{
            return \Yz::Return(false,'删除失败',[]);
        }
    }


    //参数['changeInfo'=>$changeInfo,'group'=>$group,'userid'=>$userid]
    //修改日历信息
    public function ChangeInfo($arr){
        $u=0;
        $query = DB::table('institutional_calendar');
        if($arr['group']==7) {
            $cha = DB::table('medical_institution')->select(['id'])->where(['link_user_id' => $arr['userid']])->get();
            $query->where('institution_id',$cha[0]->id);
        }
        $query->whereIn('id',$arr['changeInfo']['list']);
        if($arr['changeInfo']['type']=='status'){
            $u=$query->update(['status'=>$arr['changeInfo']['status']]);
        }
        if($arr['changeInfo']['type']=='count'){
            $u=$query->update(['count'=>$arr['changeInfo']['count']]);
        }
        if($u){
            return \Yz::Return(true,'成功更新'.$u.'条记录',[]);
        }else{
            return \Yz::Return(false,'更新失败',[]);
        }
    }
}
