package com.example.controller;

import com.example.bean.Doctor;
import com.example.bean.RespBean;
import com.example.bean.User;
import com.example.service.DoctorService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doctor")
public class DoctorController
{
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private UserService userService;

    @PostMapping("/updateDoctor")
    public RespBean updateDoctor(@RequestBody Doctor doctor)
    {
        int result = doctorService.updateDoctor(doctor);
        if(result == 1)
            return new RespBean("success","更新成功");
        else
            return new RespBean("error","更新失败");
    }

    @GetMapping("/get")
    public RespBean get(String userid)
    {
        Doctor doctorById = doctorService.getDoctorById(userid);
        RespBean respBean = new RespBean(doctorById);
        return respBean;
    }

    @GetMapping("/getUserAndDoctor")
    public RespBean getUserAndDoctor(String userid)
    {
        System.out.println(userid);
        User userByUserId = userService.getUserByUserId(userid);
        System.out.println(userByUserId);
        Doctor doctorById = doctorService.getDoctorById(userByUserId.getDid());
        List<Object> objectList = new ArrayList<>();
        objectList.add(userByUserId);
        objectList.add(doctorById);
        return new RespBean(objectList);
    }

    @GetMapping("/getAllMyUsers")
    public RespBean getAllMyUsers(String userid)
    {
        List<User> allMyUsers = doctorService.getAllMyUsers(userid);
        System.out.println(allMyUsers);
        String length = allMyUsers.size() + "";
        return new RespBean("",length,allMyUsers);
    }

    @PostMapping ("/getDoctor")
    public RespBean getDoctorById(@RequestBody Map<String,String> user)
    {
        String userid = user.get("userid");
        Doctor doctor = doctorService.getDoctorById(userid);
        List<User> allMyUsers = doctorService.getAllMyUsers(userid);
        int length = allMyUsers.size();
        if(length >= 5)
            return new RespBean("已满",length+"",doctor);
        else
            return new RespBean("未满",length+"",doctor);
    }

    @GetMapping ("/getMyDoctor")
    public RespBean getMyDoctor(String userid)//返回自己选择医生的全部信息，参数是患者的id
    {
        User userByUserId = userService.getUserByUserId(userid);
        String did = userByUserId.getDid();
        Doctor doctor = doctorService.getDoctorById(did);
        System.out.println(doctor);
        String length = doctorService.getAllMyUsers(did).size() + "";
        System.out.println(length);
        return new RespBean("success",length,doctor);
    }

}
