<?php

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

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

class TimePeriodController extends Controller
{
    public function Save(Request $request)
    {
        //判断id是否存在，存在则更新，不存在则插入
        $PeriodInfo =request('PeriodInfo');
        if(isset($PeriodInfo['timeRange'])){
            unset($PeriodInfo['timeRange']);
        }
        if(isset($PeriodInfo['department_name'])){
            unset($PeriodInfo['department_name']);
        }
        if($PeriodInfo['period_deadline']>$PeriodInfo['period_end_time']) return \Yz::echoError1('截止时间不能超过结束时间');
        if($PeriodInfo['id']==0){
            $userid = $request->get('userid');//中间件产生的参数
            $userInfo = DB::table('users')->where(['id'=>$userid])->get();
            if(count($userInfo)>0 && $userInfo[0]->department_id>0){
                $PeriodInfo['department_id']=$userInfo[0]->department_id;
                $query=DB::table('s_period')->insert($PeriodInfo);
            }else{
                return \Yz::echoError1('用户未关联科室，操作失败');
            }

        }else{
            $query=DB::table('s_period')->where(['id'=>$PeriodInfo['id']])->update($PeriodInfo);
        }
        if ($query) {
            return \Yz::Return(true, '操作成功',[]);
        } else {
            return \Yz::echoError1('操作失败');
        }

    }
    //获取时间段详情
    public function GetDetail()
    {
        $id =request('id');
        $info=DB::table('s_period')->where('id',$id)->first();
        if(!!$info){
            return \Yz::Return(true, '操作成功',$info);
        }else{
            return \Yz::echoError1('获取时间段信息失败');
        }
    }
    public function GetList(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $searchInfo =request('searchInfo');
        $page =request('page');
        $pageSize =request('pageSize');
        $searchInfo['date_type']=$searchInfo['date_type']?$searchInfo['date_type']:1;
        $department_id=0;
        $list=DB::table('s_period')
        ->leftJoin('s_department', 's_period.department_id', '=', 's_department.id')
        ->select('s_period.*','s_department.department_name')
        ->where(['s_period.is_del'=>0,'s_period.date_type'=>$searchInfo['date_type']]);

        if($group==1){//如果是管理员
            if(!empty($searchInfo['department_id'])){
                $list=  $list->where('department_id', $searchInfo['department_id']);
            }

        }else{
            $userInfo = DB::table('users')->where(['id'=>$userid])->get();
            $department_id=$userInfo[0]->department_id;
            $list=$list->where(['department_id'=>$department_id]);
        }
        $count=$list;
        $count=$count->count();
        $list=$list->limit($pageSize)->skip(($page-1)*$pageSize) // 跳过前9999条记录
        ->take($pageSize)->get();
        return \Yz::Return(true, '操作成功',['list'=>$list,'count'=>$count]);
    }
    public function GetEnableList(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $date_type =request('date_type');
        $userInfo = DB::table('users')->where(['id'=>$userid])->get();
        $department_id=$userInfo[0]->department_id;

        $list=DB::table('s_period');
        if(isset($date_type)){
            $list=$list->where('date_type',$date_type);
        }
        $list=$list->where(['department_id'=>$department_id,'period_status'=>1,'is_del'=>0])->get();
        if(count($list)>0){
            return \Yz::Return(true, '操作成功',$list);
        }else{
            return \Yz::echoError1('无可用时间段');
        }

    }
    public function Del(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $id =request('id');
        $table=DB::table('s_period')->where('id',$id);
        if($group!=1){
            $userInfo = DB::table('users')->where(['id'=>$userid])->get();
            $department_id=$userInfo[0]->department_id;
            $table=$table->where(['department_id'=>$department_id]);
        }
        $table=$table->update([
            'is_del'=>1
        ]);
        if ($table) {
            return \Yz::Return(true, '操作成功',[]);
        } else {
            return \Yz::echoError1('操作失败');
        }
    }
}
