<?php

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

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

class PlanModelController extends Controller
{
    public function timeList()
    {
      $TimeRange = request('TimeRange');
      if(count($TimeRange)<>2)  return \Yz::echoError1('起止时间格式错误');
      $interval_time = request('interval_time');
      $s=new TimeService();
      $list=$s->TimePointsArr($TimeRange[0],$TimeRange[1],$interval_time);
      return \Yz::Return(true,"查询完成",['list' =>$list]);
    }
    public function GetList()
    {
      $page =request('page');
      $pageSize =request('pageSize');
      $searchInfo=request('searchInfo');
      $list=DB::table('plan_model as a')
        ->leftJoin('plan_type as b','a.plan_type','=','b.id')
        ->select('a.*','b.name as plan_type_name')
        ->where(['a.is_del'=>0]);
      if(isset($searchInfo['name'])){
        $list = $list->where('a.name', 'like','%'.$searchInfo['name'].'%');
      }
      if(isset($searchInfo['status'])){
        $list = $list->where('a.status',$searchInfo['status']);
      }
      $count=$list->count();
      if(isset($page) and isset($pageSize)){
        $list=$list->orderBy('a.id', 'desc')->limit($pageSize)->skip(($page - 1) * $pageSize)->take($pageSize);
      }
      $list=$list ->get();
      foreach ($list as $l){
         $l->y_number=json_decode($l->y_number,true);
      }
      return \Yz::Return(true,'查询完成',['list'=>$list,'count'=>$count]);
    }
    public function Save()
    {
      $Info =request('Info');
      $params = [
        'hospital_id' => isset($Info['hospital_id']) ? $Info['hospital_id'] :null,
        'name' => isset($Info['name']) ? $Info['name'] : null,
        'interval_time' => isset($Info['interval_time']) ? $Info['interval_time'] : null,
        'plan_type' => isset($Info['plan_type']) ? $Info['plan_type'] : null,
        'y_number' => isset($Info['y_number']) ? json_encode($Info['y_number']) : [],
        'count' => isset($Info['count'])  ? $Info['count'] : 0,
        'status'=>isset($Info['status']) ? $Info['status'] : 0,
      ];
      $requiredFields = ['hospital_id'=>'医院','name'=>'名称','interval_time'=>'时间间隔','plan_type'=>'号源类型'];
// 判断是否为空
      foreach ($requiredFields as $key=> $field) {
        if (!isset($params[$key]) || $params[$key] === null) {
          return \Yz::echoError1('参数 ' . $field . ' 不能为空');
        }
      }
      if(!isset($Info['TimeRange']) or count($Info['TimeRange'])<>2){
        return \Yz::echoError1('起止时间格式错误');
      }
      $params['start_time']=$Info['TimeRange'][0];
      $params['end_time']=$Info['TimeRange'][1];
      $do=false;
      $table=DB::table('plan_model');
      if($Info['id']==0){
        $do=$table->insert($params);
      }
      if($Info['id']>0){
        $do=$table->where(['id'=>$Info['id']])->update($params);
      }
      if($do){
        return \Yz::Return(true,'操作成功',[]);
      }else{
        return \Yz::echoError1('操作失败');
      }

    }
    public function GetDetail()
    {
      $id =request('id');
      $info=DB::table('plan_model')->where(['id'=>$id,'is_del'=>0])->first();
      if(!!$info){
        $info->TimeRange=[$info->start_time,$info->end_time];
        $info->y_number=json_decode($info->y_number,true);
        return \Yz::Return(true,'查询完成',$info);
      }else{
        return \Yz::echoError1('查询失败');
      }
    }
  public function Del()
  {
    $id =request('id');
    $d=DB::table('plan_model')->where(['id'=>$id])->update([
      'is_del'=>1
    ]);
    if($d){
      return \Yz::Return(true,'操作完成',[]);
    }else{
      return \Yz::echoError1('操作失败');
    }
  }

}
