package com.luowei.app_sb.service;

import com.luowei.app_sb.entity.User_70Entity;
import com.luowei.app_sb.mapper.User_70Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class User_70Service {
    @Autowired
    User_70Mapper user_70Mapper;
    public User_70Entity select(String name){
        return user_70Mapper.select(name);
    }
    public int insert(User_70Entity user_70Entity){
        return user_70Mapper.insert(user_70Entity);
    }
    public int update(User_70Entity user_70Entity){
        return user_70Mapper.update(user_70Entity);
    }
    public int login(User_70Entity user_70Entity){
        return user_70Mapper.login(user_70Entity);
    }
    public User_70Entity setUserInfo(String name,String address){
        User_70Entity user = select(name);
        if (user!=null){
            user.setAddress(address);
            update(user);
        }else {
            user = new User_70Entity();
            user.setName(name);
            user.setAddress(address);
            insert(user);
        }
        return user;
    }
    public String getUserAddress(String name){
        User_70Entity user = select(name);
        if (user!=null){
            return user.getAddress();
        }
        return null;
    }
    //注册用户
    public String registerUser(String name, String password){
        User_70Entity user = select(name);
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        if (user == null){
            User_70Entity newUser = new User_70Entity();
            newUser.setName(name);
            newUser.setPassword(encryptedPassword);
            insert(newUser);
            return "新用户注册成功";
        }else {
            user.setPassword(encryptedPassword);
            update(user);
            return "用户密码修改成功";
        }
    }
    //登陆用户
    public String loginUser(String name,String password){
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes());;
        User_70Entity user = select(name);
        if (user!=null && user.getPassword().equals(encryptedPassword)){
            return "登录成功";
        }
        return "登录失败";
    }
}
















