<?php

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

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

class ComboController extends Controller
{
    //获取套餐
    public function UpdateCombo()
    {
        $hospital_id =request('hospital');
        date_default_timezone_set('PRC');
        $data=[
            "价格下限"=>"0","价格上限"=>"999999","性别"=>null,"妇检"=>false,"请求来源"=>"小程序"
        ];
        $peis=new PEISApiController();
        $info= $peis::Post('套餐查询',$hospital_id,$data,false);
        $successCount=0;//成功的数量
        if(count($info['data'])>0){
            $combos=$info['data'];
            //查询医院下所有缓存的套餐
            $db_combo_all=DB::table('combos')->where(['hospital_id'=>$hospital_id])->get();
            foreach($db_combo_all as $key=>$db_combo){
                $cunzai=false;
                foreach ($combos as $c_key=>$combo){
                   if($db_combo->combo_id==$combo['Id']){
                       $cunzai=true;
                   }
                }
                if(!$cunzai){
                    DB::table('combos')->where(['id'=>$db_combo->id])->update([
                        'status'=>0
                    ]);
                }
            }
            foreach ($combos as $c_key=>$combo){

                $item=[];
                $comboDetail= $peis::Post('套餐详情查询',$hospital_id,['套餐Id'=>$combo['Id']],false);
                if($comboDetail['code']!=0) return \Yz::echoError1("套餐详情查询失败");

                $comboDetail_list=$comboDetail['data'][0]['包含项目'];
                foreach ($comboDetail_list as $key=>$v){
                    $item[]=[
                        'id'=>$v['Id'],
                        'name'=>$v['名称'],
                        'desc'=>$v['简介'],
                    ];
                }
                $item=json_encode($item,JSON_UNESCAPED_UNICODE);
                $sex=null;
                if(isset($combo['性别限制'])){
                    $sex_array=['全部'=>0,'男'=>1,'女'=>2];
                    $sex=$sex_array[$combo['性别限制']]>=0?$sex_array[$combo['性别限制']]:null;
                }

                $db_combo=DB::table('combos')->where(['hospital_id'=>$hospital_id,'combo_id'=>$combo['Id']])->first();
                $comboData=[
                    'hospital_id'=>$hospital_id,
                    'combo_id'=>$combo['Id'],
                    'sex'=>$sex,
                    'name'=>$combo['名称'],
                    'original_price'=>$combo['原价'],
                    'price'=>$combo['价格'],
                    'items'=>$item,
                    'status'=>1,
                    'updated_at'=>date('Y-m-d H:i:s'),
                ];
                if(!!$db_combo){
                    //如果存在套餐
                    $u=DB::table('combos')->where(['hospital_id'=>$hospital_id,'combo_id'=>$combo['Id']])->update($comboData);
                }else{
                    //如果不存在套餐
                    $comboData['tags']=json_encode([]);
                    $u= DB::table('combos')->insert($comboData);
                }
                if($u){
                    $successCount++;
                }else{
                    return \Yz::echoError1('更新失败');
                }

            }
              $hospital=DB::table('hospital')->where(['id'=>$hospital_id])->first();
                if(!!$hospital->frequency){
                    $date = new DateTime();
                    $date->modify('+'.$hospital->frequency.' minutes');
                    $formatted_time = $date->format('Y-m-d H:i:s');
                    DB::table("hospital")->where(['id'=>$hospital_id])->update([
                        'next_time'=>$formatted_time
                    ]);
                }
              return \Yz::Return(true,'操作完成',['success_count'=>$successCount,'combos_count'=>count($combos)]);

        }else{
            return \Yz::echoError1('未查询到套餐');
        }
    }
    public function GetList()
    {
        $page = request('page');
        $pageSize = request('pageSize');
        $searchInfo = request('searchInfo');
        $list=DB::table('combos')
            ->select('combos.*','hospital.name as hospital_name')
            ->leftJoin('hospital','hospital.id','=','combos.hospital_id');
        if(isset($searchInfo['name'])){

            $list = $list->where('combos.name', 'like', '%' . $searchInfo['name'] . '%');
        }
        $count=$list->count();
        $list=$list
            ->skip(($page-1)*$pageSize) // 跳过前9999条记录
            ->take($pageSize)->get();

        return \Yz::Return(true,"查询完成",['list'=>$list,'count'=>$count]);
    }
    public function GetDetail()
    {
        $id = request('id');
        $query=DB::table('combos')->where(['id'=>$id])->first();
        if(!!$query){
            $query->tags=implode(", ", json_decode($query->tags,true));
            $query->items=json_decode($query->items);
            return \Yz::Return(true,"查询完成",$query);
        }else{
            return \Yz::echoError1("获取详情失败");
        }
    }
    public function Save()
    {
        $Info = request('Info');
        $u=DB::table('combos')->where(['id'=>$Info['id']])->update([
            'tags'=>json_encode($Info['tags'],JSON_UNESCAPED_UNICODE),
            'cover'=>$Info['cover'],
            'intro'=>$Info['intro'],
            'sub_intro'=>$Info['sub_intro'],
            'desc'=>$Info['desc']
        ]);
        if($u){
            return \Yz::Return(true,"更新完成",[]);
        }else{
            return \Yz::echoError1("没有数据更新");
        }
    }
}
