package com.example.monitorpatientstatus.service;

import com.example.monitorpatientstatus.entity.Drug;
import com.example.monitorpatientstatus.mapper.DrugMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.monitorpatientstatus.entity.Doctor;
import com.example.monitorpatientstatus.mapper.DoctorMapper;

import java.util.List;

import static java.sql.Types.NULL;
@Service
public class DoctorService {
    @Autowired
    DoctorMapper doctMapper;
    DrugMapper drugMapper;
    /*public String regist(Doctor user) {
        //注册逻辑函数
        try {
            Doctor userExist = doctMapper.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 {
                doctMapper.save(user);
                return "注册成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }

    }
    */
    public int login(String name,String password) {
        //登录逻辑函数
        try {
            Doctor userExistN = doctMapper.findByName(name);
            if (userExistN != null) {
                String userExistP = doctMapper.findPswByName(name);
                if (userExistP.equals(password)) {
                    return 2;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    public List<Doctor> findAll() {
        List<Doctor> list = doctMapper.findAll();
        return list;
    }
    public  Drug searchDrugByName(String name){return drugMapper.findByName(name);}
    public Drug searchDrugByNumber(int number){return drugMapper.findByNumber(number);}

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

}
