<?php

namespace App\Http\Controllers\API\H5;

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

class PlanController extends Controller
{
    //获取本月每日剩余号源数量
    public function GetMonthPlanCount()
    {
      $hospital_id =request('hospital');
      $openid =request('openid');
      $person_id=request('person_id');
      $month=request('month');
      $use_type=request('use_type');//使用类型 1个检 2团检
      $checkup_type_id=request('checkup_type_id');//体检类型表对应id
      $amount=request('amount');//总金额

      $first_day_timestamp = strtotime($month . '-01');
      $first_day = date('Y-m-d', $first_day_timestamp);  //当月第一天
      $last_day_timestamp = strtotime($month . '-01 +1 month -1 day');
      $last_day = date('Y-m-d', $last_day_timestamp);   //当月最后一天
      $list=DB::table('plans')
        ->whereBetween('date',[$first_day,$last_day])->whereIn('status',[1])
        ->whereRaw('JSON_CONTAINS(checkup_type_id, ?, "$")', [$checkup_type_id])
        ->where(['hospital_id'=>$hospital_id,'type'=>1])
        ->whereIn('use_type',[0,$use_type]);
      if($use_type==1){
        $list=$list->where(['amount_limit1'=>0])->orWhere('amount_limit1','>=',$amount);
      }
      if($use_type==2){
        $list=$list->where(['amount_limit2'=>0])->orWhere('amount_limit2','>=',$amount);
      }
      $list=$list->select('date', DB::raw('COUNT(*) as count'));
      $list=$list->groupBy('date')->get();
      return \Yz::Return(true,"查询完成",['list'=>$list]);
    }
    //获取某日号源列表
    public function GetDayPlanList()
    {
      $hospital_id =request('hospital');
      $openid =request('openid');
      $person_id=request('person_id');
      $date=request('date');
      $use_type=request('use_type');//使用类型 1个检 2团检
      $checkup_type_id=request('checkup_type_id');//体检类型表对应id
      $amount=request('amount');//总金额

      $list=DB::table('plans')
        ->where('date',$date)->whereIn('status',[1,2])
        ->whereRaw('JSON_CONTAINS(checkup_type_id, ?, "$")', [$checkup_type_id])
        ->where(['hospital_id'=>$hospital_id,'type'=>1])
        ->whereIn('use_type',[0,$use_type]);
      if($use_type==1){
        $list=$list->where(['amount_limit1'=>0])->orWhere('amount_limit1','>=',$amount);
      }
      if($use_type==2){
        $list=$list->where(['amount_limit2'=>0])->orWhere('amount_limit2','>=',$amount);
      }
      $list=$list->get();
      return \Yz::Return(true,"查询完成",['list'=>$list]);
    }
}
