package com.ruoyi.generator.service.front;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.front.FrontUser;
import com.ruoyi.generator.domain.front.dto.FrontLoginBody;
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
import com.ruoyi.generator.mapper.front.FrontUserMapper;

@Service
public class FrontUserServiceImpl implements IFrontUserService
{
    @Autowired
    private FrontUserMapper frontUserMapper;

    @Override
    public FrontUser login(FrontLoginBody loginBody)
    {
        if (loginBody == null || StringUtils.isEmpty(loginBody.getUsername()) || StringUtils.isEmpty(loginBody.getPassword()))
        {
            throw new ServiceException("用户名或密码不能为空");
        }
        FrontUser user = frontUserMapper.selectFrontUserByUsername(loginBody.getUsername());
        if (user == null || !StringUtils.equals(user.getPassword(), loginBody.getPassword()))
        {
            throw new ServiceException("用户名或密码错误");
        }
        user.setLastLoginTime(new Date());
        frontUserMapper.updateFrontUser(user);
        return user;
    }

    @Override
    public FrontUser register(FrontRegisterBody registerBody)
    {
        if (registerBody == null || StringUtils.isEmpty(registerBody.getUsername()) || StringUtils.isEmpty(registerBody.getPassword()))
        {
            throw new ServiceException("用户名或密码不能为空");
        }
        if (frontUserMapper.selectFrontUserByUsername(registerBody.getUsername()) != null)
        {
            throw new ServiceException("用户名已存在");
        }
        FrontUser user = new FrontUser();
        user.setUsername(registerBody.getUsername());
        user.setPassword(registerBody.getPassword());
        user.setNickname(registerBody.getNickname());
        user.setEmail(registerBody.getEmail());
        user.setPhone(registerBody.getPhone());
        user.setStatus("0");
        user.setDelFlag("0");
        frontUserMapper.insertFrontUser(user);
        return user;
    }

    @Override
    public FrontUser selectFrontUserById(Long userId)
    {
        return frontUserMapper.selectFrontUserById(userId);
    }

    @Override
    public FrontUser selectFrontUserByUsername(String username)
    {
        return frontUserMapper.selectFrontUserByUsername(username);
    }

    @Override
    public List<FrontUser> selectFrontUserList(FrontUser frontUser)
    {
        return frontUserMapper.selectFrontUserList(frontUser);
    }
}
