From 8b8d24bcf8187692c2c134c14e2504e8bce4e378 Mon Sep 17 00:00:00 2001 From: pqyt64hnf <2543568107@qq.com> Date: Sun, 30 Apr 2023 01:19:53 +0800 Subject: [PATCH] ADD file via upload --- .../java/com/service/UserInfoService.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/main/java/com/service/UserInfoService.java diff --git a/src/main/java/com/service/UserInfoService.java b/src/main/java/com/service/UserInfoService.java new file mode 100644 index 0000000..fc73c24 --- /dev/null +++ b/src/main/java/com/service/UserInfoService.java @@ -0,0 +1,78 @@ +package com.example.service; + +import com.example.dao.UserInfoDao; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import com.example.entity.UserInfo; +import com.example.exception.CustomException; +import com.example.common.ResultCode; +import com.example.vo.UserInfoVo; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import cn.hutool.crypto.SecureUtil; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.Date; +import java.util.List; + +@Service +public class UserInfoService { + + @Resource + private UserInfoDao userInfoDao; + + public UserInfo add(UserInfo userInfo) { + // 唯一校验 + int count = userInfoDao.checkRepeat("name", userInfo.getName(), null); + if (count > 0) { + throw new CustomException("1001", "用户名\"" + userInfo.getName() + "\"已存在"); + } + if (StringUtils.isEmpty(userInfo.getPassword())) { + // 默认密码123456 + userInfo.setPassword(SecureUtil.md5("123456")); + } else { + userInfo.setPassword(SecureUtil.md5(userInfo.getPassword())); + } + userInfoDao.insertSelective(userInfo); + return userInfo; + } + + public void delete(Long id) { + userInfoDao.deleteByPrimaryKey(id); + } + + public void update(UserInfo userInfo) { + userInfoDao.updateByPrimaryKeySelective(userInfo); + } + + public UserInfo findById(Long id) { + return userInfoDao.selectByPrimaryKey(id); + } + + public List findAll() { + return userInfoDao.findByName("all"); + } + + public PageInfo findPage(String name, Integer pageNum, Integer pageSize, HttpServletRequest request) { + PageHelper.startPage(pageNum, pageSize); + List all = userInfoDao.findByName(name); + return PageInfo.of(all); + } + + public UserInfoVo findByUserName(String name) { + return userInfoDao.findByUsername(name); + } + + public UserInfo login(String username, String password) { + UserInfo userInfo = userInfoDao.findByUsername(username); + if (userInfo == null) { + throw new CustomException(ResultCode.USER_ACCOUNT_ERROR); + } + if (!SecureUtil.md5(password).equalsIgnoreCase(userInfo.getPassword())) { + throw new CustomException(ResultCode.USER_ACCOUNT_ERROR); + } + return userInfo; + } + +}