<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    //科室列表
    public function keshi_list(){
        $list = Db::name('keshi')->where('is_show',1)->order('weigh','desc')->select();
        $this->success(1,$list);
    }

    //职称列表
    public function level_list(){
        $list = Db::name('doctor_level')->select();
        $this->success(1,$list);
    }

    //医生列表
    //首页推荐，搜索
    public function doctor_list(){

        $where = [
            'd.status' => 1,
            'd.doctor_status' => 1,
        ];

        //科室id
        $keshi_id = input('keshi_id',0);
        if($keshi_id){
            $where['d.keshi_id'] = $keshi_id;
        }
        //职称id
        $level_id = input('level_id',0);
        if($level_id){
            $where['d.level_id'] = $level_id;
        }
        //关注
        $folllow = input('follow',0);
        if($folllow){
            $my_follow_ids = controller('Userfollow')->my_follow_uids();
            $where['d.id'] = ['IN',$my_follow_ids];
        }
        //搜索
        $keyword = input('keyword','');
        if(!empty($keyword)){
            $where['d.nickname|d.goodat|keshi.name'] = ['LIKE','%'.$keyword.'%'];

            if($this->auth->isLogin()){
                Db::name('user_search')->insertGetId(['user_id'=>$this->auth->id,'keyword'=>$keyword]);
            }
        }

        //dump($where);

        $field = [
            'd.id','d.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.ordernum',
            'keshi.name as keshi_name',
            'level.name as level_name','level.name_en as level_name_en',
            'info.typing_switch','info.video_switch','info.typing_price','info.video_price','info.video_model'
        ];
        $list = Db::name('doctor')->alias('d')
            ->field($field)
            ->join('doctor_level level','d.level_id = level.id','LEFT')
            ->join('keshi','d.keshi_id = keshi.id','LEFT')
            ->join('doctor_info info','d.id = info.doctor_id','LEFT')
            ->where($where)->order('d.ordernum desc')->autopage()->select();
        $list = list_domain_image($list,['avatar']);

        $this->success(1,$list);
    }

    //医生详情
    public function doctor_info(){
        $id = input('id',0);

        $field = [
            'd.id','d.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.ordernum','d.info','d.job_status',
            'keshi.name as keshi_name',
            'level.name as level_name',
            'info.typing_switch','info.video_switch','info.typing_price','info.video_price','info.video_model',
        ];
        $info = Db::name('doctor')->alias('d')
            ->field($field)
            ->join('doctor_level level','d.level_id = level.id','LEFT')
            ->join('keshi','d.keshi_id = keshi.id','LEFT')
            ->join('doctor_info info','d.id = info.doctor_id','LEFT')
            ->where('d.id',$id)->find();
        $info = info_domain_image($info,['avatar']);

        //是否关注
        $info['is_follw'] = $this->is_follow($this->auth->id,$id);

        //图文说明
        $info['wenzhen_typing_serverrule'] = config('site.wenzhen_typing_serverrule');
        //视频说明
        $info['wenzhen_video_serverrule'] = config('site.wenzhen_video_serverrule');

        //没上班，文字接诊 停掉
        if($info['typing_switch'] == 1 && $info['job_status'] == 0){
            $info['typing_switch'] = 2;
        }
        //没上班，即时视频 停掉
        if($info['video_switch'] == 1 && $info['video_model'] == 2 && $info['job_status'] == 0){
            $info['video_switch'] = 2;
        }
        //视频问诊，排班模式，如果没排班 停掉
        if($info['video_switch'] == 1 && $info['video_model'] == 1){
            $paiban_list = $this->doctor_paiban($id,true);
            if(empty($paiban_list)){
                $info['video_switch'] = 2;//表示没有排班了，或已经约满
            }
        }

        $this->success(1,$info);
    }

    //医生排班
    public function doctor_paiban($this_doctor_id = 0,$return = false){
        $doctor_id = input('doctor_id',$this_doctor_id);
        if(!$doctor_id){
            $this->error();
        }

        //测试临时屏蔽
//        $list = Db::name('doctor_paiban')->where('doctor_id',$doctor_id)->where('active',1)->where('activetime','gt',time()+7200)->order('activetime asc')->select();
        $list = Db::name('doctor_paiban')->where('doctor_id',$doctor_id)->where('active',1)->where('activetime','gt',time())->order('activetime asc')->select();

        $newlist = [];
        if(!empty($list)){

            //预约到此刻后面的订单
            $need_unset = [];
            $map = [
                'doctor_id' => $doctor_id,
                'ordertype' => 2,
                'status'    => ['IN','10,20,25,30'],//有效订单
                'book_time'=> ['gt',time()],
                'video_model' => 1, //预约模式
            ];
            $order_booked = Db::name('wenzhen_order')->where($map)->group('book_time')->column('book_time,count(id) as count_number');
            if(!empty($order_booked)){
                foreach($order_booked as $bk => $bv){
                    if($bv >= 4){
                        $need_unset[] = $bk;
                    }
                }
            }

            //重组时间数据
            foreach($list as $key => $val){
                //排除接满4单的
                if(in_array($val['activetime'],$need_unset)){
                    continue;
                }

                $date = date('Y-m-d',$val['activetime']);
                if(!isset($newlist[$date])){
                    $newlist[$date] = [];
                }

                $newlist[$date][] = date('H:i',$val['activetime']);
            }
        }

        if($return == true){
            return $newlist;
        }

        $this->success(1,$newlist);
    }


}
