package com.example.controller;

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

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

@RestController
@RequestMapping("/worker")
public class WorkerController {
    @Autowired
    private WorkerService workerService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private UserService userService;

    @GetMapping ("/updateEnabled")//更改三种用户的enable属性
    public RespBean updateEnabled(String userid)
    {
        int i = selectSameId(userid);
        int result = 0;

        System.out.println(userid);
        System.out.println(i);

        if(i == 1)//该id为用户
            result = userService.updateUserEnabled(true, userid);
        else if(i == 2)//该id为医生
            result = doctorService.updateDoctorEnabled(true, userid);
        else if(i == 3)//该id为管理员
            result = workerService.updateWorkerEnabled(true, userid);

        System.out.println(result);

        if(result == 1)//表示更新成功
            return new RespBean("success","更新成功");
        else
            return new RespBean("error","更新失败");
    }

    @GetMapping("/delete")
    public RespBean delete(String userid)//根据id删除用户
    {
        int i = selectSameId(userid);
        int result = 0;

        if(i == 1)
            result = userService.deleteUser(userid);
        else if(i == 2)
            result = doctorService.deleteDoctor(userid);
        else if(i == 3)
            result = workerService.deleteWorker(userid);

        if(result == 1)
            return new RespBean("success","删除成功");
        else
            return new RespBean("error","删除失败");
    }

    @PostMapping("/updateWorker")
    public RespBean updateWorker(@RequestBody Worker worker)//更新社区管理员的个人信息
    {
        int result = workerService.updateWorker(worker);
        if(result == 1)
            return new RespBean("success","更新成功");
        else
            return new RespBean("error","更新失败");
    }

    public int selectSameId(String userId)//查找具有相同id的三种用户
    {
        User user = userService.getUserByUserId(userId);
        Doctor doctor = doctorService.getDoctorById(userId);
        Worker worker = workerService.getWorkerById(userId);

        if(user != null)//说明user中有相同的id
            return 1;
        else if(doctor != null)//说明doctor中有相同的id
            return 2;
        else if(worker != null)//说明worker中有相同的id
            return 3;
        else
            return 0;//说明没有相同的id
    }
    @GetMapping("/getAllDoctors")
    public List<Doctor> getAllDoctors()
    {
        return workerService.getAllDoctors();
    }

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

    @GetMapping("/selectNoReport")
    public RespBean selectNoReport()
    {
        List<User> users = userService.selectNoReport();
        return new RespBean(users);
    }

    @GetMapping("/selectTodayError")
    public RespBean selectTodayError()
    {
        List<User> users = userService.selectTodayError();
        return new RespBean(users);
    }

    @GetMapping("/selectAll")
    public RespBean selectAll()
    {
        List<User> users = userService.selectAll();
        return new RespBean(users);
    }

    @GetMapping("/selectUnabled")
    public RespBean selectUnabled()
    {
        List<Doctor> doctors = doctorService.selectUnabled();
        System.out.println(doctors);
        return new RespBean(doctors);
    }

    @GetMapping("/getUserById")
    public RespBean getUserById(String userid)
    {
        User userByUserId = userService.getUserByUserId(userid);
        return new RespBean(userByUserId);
    }
}
