<?php

namespace app\api\controller\tvuser;

use app\common\controller\Api;
use think\Db;
/**
 * 医生接口，使用自动注册的用户
 */
class Tvdoctor extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

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

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

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

        //科室id
        $keshi_id = input('keshi_id',0);
        if($keshi_id){
            $where['d.keshi_id'] = $keshi_id;
        }

        //关注
        $folllow = input('follow',0);
        if($folllow){
            $my_follow_ids = controller('Userfollow')->my_follow_uids();
            $where['d.id'] = ['IN',$my_follow_ids];
        }

        $field = [
            'd.id','d.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.job_status',
            'keshi.name as keshi_name',
            'level.name as level_name',
            '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.info','d.job_status',
            'info.video_model',
            'keshi.name as keshi_name',
            'level.name as level_name',
        ];
        $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_follow'] = $this->is_follow($this->auth->id,$id);

        //视频说明
        $info['tv_wenzhen_video_serverrule'] = config('site.tv_wenzhen_video_serverrule');

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

    //医生排班
    public function doctor_paiban(){
        $doctor_id = input('doctor_id',0);
        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;
                }
                $newlist[] = date('Y-m-d H:i',$val['activetime']);
            }
        }

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

    //关注某人，取关
    public function follow_one(){
        $follow_uid = input('doctor_id',0);

        if(!$follow_uid){
            $this->error(__('Invalid parameters'));
        }

        $checkuser = Db::name('doctor')->find($follow_uid);
        if(empty($checkuser)){
            $this->error('此医生不存在');
        }

        $map = [
            'uid' => $this->auth->id,
            'follow_uid' => $follow_uid,
        ];

        $check = Db::name('user_follow')->where($map)->find();
        if($check){
            //取关
            $rs = Db::name('user_follow')->where($map)->delete();
            $this->success('操作成功');
        }

        $id = Db::name('user_follow')->insertGetId($map);

        $this->success('操作成功',$id);
    }



}
