package com.example.monitorpatientstatus.service;

import com.example.monitorpatientstatus.entity.Manager;
import com.example.monitorpatientstatus.mapper.ManagerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.example.monitorpatientstatus.mapper.PatientMapper;
import  com.example.monitorpatientstatus.entity.Patient;
import  com.example.monitorpatientstatus.entity.Doctor;

import java.util.List;

import static java.sql.Types.NULL;

@Service
public class ManagerService {
    @Autowired
    ManagerMapper managerMapper;
    PatientMapper patientMapper;

    /*public String regist(Manager user) {
        //注册逻辑函数
        try {
            Manager userExist = managerMapper.findByName(user.getUsername());
            if (user.getUsername().equals("")) {
                return "用户名不能为空";
            } else if (user.getPassword().equals("")) {
                return "密码不能为空";
            } else if(user.getPhoneNumber()==NULL)
            {
                return"请填写手机号";
            }
            else if (userExist != null) {
                return "账户已经存在";
            } else {
                managerMapper.save(user);
                return "注册成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }

    }
    */
    public int login(String name, String password) {
        //登录逻辑函数
        try {
            Manager userExistN = managerMapper.findByName(name);
            if (userExistN != null) {
                String userExistP = managerMapper.findPswByName(name);
                if (userExistP.equals(password)) {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }



    public String showPatientInfo(Patient patient){
        //查询个人信息逻辑函数
        try{
            Patient patientExistN = patientMapper.findByName(patient.getUsername());
            if(patientExistN!=null) {
                managerMapper.showPatientInfo(patient);
                return  "查询成功";
            }
            else {
                return "用户不存在!";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String modifyPatient(Patient patient){
        //修改患者信息逻辑函数
        try{
            Patient patientExistN = patientMapper.findByName(patient.getUsername());
            if(patientExistN!=null) {
                managerMapper.modifyPatient(patient);
                return  "修改成功";
            }
            else {
                return "用户不存在!";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String modifyDoctor(Doctor doctor){
        //修改患者信息逻辑函数
        try{
            Patient patientExistN = patientMapper.findByName(doctor.getUsername());
            if(patientExistN!=null) {
                managerMapper.modifyDoctor(doctor);
                return  "修改成功";
            }
            else {
                return "用户不存在!";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public List<Manager> findAll() {
        List<Manager> list = managerMapper.findAll();
        return list;
    }

}
