package com.luowei.app_sb.controller;

import com.luowei.app_sb.entity.Person_805Entity;
import com.luowei.app_sb.entity.Person_health_805Entity;
import com.luowei.app_sb.entity.User_70Entity;
import com.luowei.app_sb.service.Person_805Service;
import com.luowei.app_sb.service.User_70Service;
import com.luowei.app_sb.vo.PersonHealthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("api")
public class ApiController {
    @RequestMapping("get_1")
    public String get_1(){
        return "spring-boot";
    }
    @RequestMapping("get_2")
    public String get_2(String name){
        return name;
    }
    @Autowired
    User_70Service user_70Service;
    @RequestMapping("register_803_2")
    public String register_803_2(User_70Entity user_70Entity){
        if(user_70Service.select(user_70Entity.getName())==null){
            //md5的处理  md5(aaaa)
            user_70Entity.setPassword(DigestUtils.md5DigestAsHex(user_70Entity.getPassword().getBytes()));
            user_70Service.insert(user_70Entity);
            return "新用户注册成功";
        }else{
            user_70Entity.setPassword(DigestUtils.md5DigestAsHex(user_70Entity.getPassword().getBytes()));
            user_70Service.update(user_70Entity);
            return "用户密码修改成功";
        }
    }
    //java session
    @RequestMapping("login_803_2")
    public String login_803_2(User_70Entity user_70Entity,
                              HttpServletRequest httpServletRequest){
        user_70Entity.setPassword(DigestUtils.md5DigestAsHex(user_70Entity.getPassword().getBytes()));
        HttpSession httpSession=httpServletRequest.getSession();
        if(user_70Service.login(user_70Entity)==1){
            httpSession.setAttribute("name",user_70Entity.getName());// $_SESSION["name"]="mike"
            return "登录成功";

        }else{
            httpSession.setAttribute("name","");// $_SESSION["name"]=""
            return "登录失败";

        }

    }
    @RequestMapping("list_803_2")
    public String list_803_2(HttpServletRequest httpServletRequest){
        HttpSession httpSession=httpServletRequest.getSession();
        String returnValue="";
        try {
            returnValue=httpSession.getAttribute("name").toString();
            if(returnValue==""){
                returnValue="未受权，请先登录";
            }
        }catch (Exception e){
            returnValue="未受权，请先登录";
        }
        return returnValue;
    }

    @RequestMapping("set_803")
    public User_70Entity set_803(@RequestParam String name,@RequestParam String address){
        return user_70Service.setUserInfo(name, address);
    }

    @RequestMapping("get_803")
    public String get_803(@RequestParam String name){
        return user_70Service.getUserAddress(name);
    }

    @RequestMapping("register_803_1")
    public String register_803_1(@RequestParam String name,@RequestParam String password){
        return user_70Service.registerUser(name, password);
    }

    @RequestMapping("login_803_1")
    public String login_803_1(@RequestParam String name,@RequestParam String password){
        return user_70Service.loginUser(name, password);
    }

    @Autowired
    Person_805Entity person_805Entity;
    @Autowired
    Person_health_805Entity person_health_805Entity;
    @Autowired
    Person_805Service person_805Service;

    //提交的表单对应vo层
    @RequestMapping("ehrSet_805")
    public String ehrSet_805(PersonHealthVo personHealthVo){
        String id= UUID.randomUUID().toString().replace("_","").toLowerCase();//生成混杂数字英文的字符串，类似于php中的uniquID
        //时间戳
        long now = new Date().getTime()/1000;
        person_805Entity.setSfz_id(personHealthVo.getSfz_id());
        person_805Entity.setName(personHealthVo.getName());
        person_805Entity.setGender(personHealthVo.getGender());
        person_805Entity.setEmr(personHealthVo.getEmr());
        if (person_805Service.selectPerson_805(personHealthVo.getSfz_id())==null){
            //新增
            person_805Entity.setCreate_time(now);
            person_805Entity.setUpdate_time(now);
            person_805Entity.setId(id);
            person_805Service.insertPerson_805(person_805Entity);
        }else {
            //编辑
            person_805Entity.setUpdate_time(now);
            person_805Service.updatePerson_805(person_805Entity);
        }
        person_health_805Entity.setId(id);
        person_health_805Entity.setSfz_id(personHealthVo.getSfz_id());
        person_health_805Entity.setSbp(personHealthVo.getSbp());
        person_health_805Entity.setDbp(personHealthVo.getDbp());
        person_health_805Entity.setCreate_time(now);
        person_health_805Entity.setUpdate_time(now);
        person_805Service.insertPerson_health_805Entity(person_health_805Entity);

        return person_805Entity.getName();
    }
    @RequestMapping("ehrGet_805")
    public PersonHealthVo ehrGet_805(String sfz_id){
        return person_805Service.selectPersonHealthVo(sfz_id);
    }
}







