package com.example.monitorpatientstatus.service;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
import com.example.monitorpatientstatus.entity.Patient;
import com.example.monitorpatientstatus.mapper.PatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientService {

    //该注解可以对类成员变量、方法以及构造函数进行标注，完成自动装配工作
    @Autowired
   PatientMapper patientMapper;

    public int login(String name, String password){
        //登录逻辑函数
        try{
            Patient patientExistN = patientMapper.findByName(name);
            if(patientExistN!=null){
                String patientExistP = patientMapper.findPswByName(name);
                if(patientExistP.equals(password)){
                    return 3;
                }else {
                    return 0;
                }
            }else {
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    public String regist(Patient patient){
        //注册逻辑函数
        try {
            Patient patientExist = patientMapper.findByName(patient.getUsername());
            if (patient.getUsername().equals("")){
                return "用户名不能为空";
            }else if (patient.getPassword().equals("")){
                return "密码不能为空";
            }else if (patientExist!=null){
                return "账户已经存在";
            }else{
                patientMapper.save(patient);
                return "注册成功";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }

    }

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

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

    public List<Patient> findAll(){
        List<Patient> list = patientMapper.findAll();
        return list;
    }

}