|
@@ -0,0 +1,169 @@
|
|
|
|
+package com.sky.ioc.service.user.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
|
+import com.sky.ioc.entity.domain.system.Users;
|
|
|
|
+import com.sky.ioc.entity.dto.LoginUserVo;
|
|
|
|
+import com.sky.ioc.mapper.user.UserMapper;
|
|
|
|
+import com.sky.ioc.service.user.UserService;
|
|
|
|
+import com.sky.ioc.tool.JwtUtil;
|
|
|
|
+import com.sky.ioc.tool.Pbkdf2Sha256Digest;
|
|
|
|
+import com.sky.ioc.tool.RedisUtil;
|
|
|
|
+import com.sky.ioc.tool.ReturnMsg;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class UserServiceImpl implements UserService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserMapper userMapper;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONObject login(String username, String password) {
|
|
|
|
+ Users users = userMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUserName, username));
|
|
|
|
+ if (Objects.isNull(users)){
|
|
|
|
+ throw new RuntimeException("用户名或密码错误");
|
|
|
|
+ }
|
|
|
|
+ boolean verification = Pbkdf2Sha256Digest.verification(password, users.getPassword());
|
|
|
|
+ if (!verification){
|
|
|
|
+ throw new RuntimeException("用户名或密码错误");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 认证成功
|
|
|
|
+ LoginUserVo loginUser = new LoginUserVo();
|
|
|
|
+ BeanUtils.copyProperties(users, loginUser);
|
|
|
|
+
|
|
|
|
+ String token = JwtUtil.genJwtToken(users);
|
|
|
|
+
|
|
|
|
+ // 将用户信息存入redis
|
|
|
|
+ redisUtil.put("login:"+loginUser.getUserName(), loginUser);
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("code", 0);
|
|
|
|
+ jsonObject.put("data", token);
|
|
|
|
+ jsonObject.put("message", loginUser);
|
|
|
|
+ return jsonObject;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg addUser(Users users) {
|
|
|
|
+ Users u = userMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUserName, users.getUserName()));
|
|
|
|
+ if (!Objects.isNull(u)){
|
|
|
|
+ throw new RuntimeException("用户名已存在");
|
|
|
|
+ }
|
|
|
|
+ int insert = userMapper.insert(users);
|
|
|
|
+ if (insert<1) {
|
|
|
|
+ throw new RuntimeException("添加失败");
|
|
|
|
+ }
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg getUserDetail(String id) {
|
|
|
|
+ Users users = userMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getId, id));
|
|
|
|
+ return ReturnMsg.ok(users);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg updUser(Users users) {
|
|
|
|
+ int i = userMapper.updateById(users);
|
|
|
|
+ if (i<1) {
|
|
|
|
+ throw new RuntimeException("修改失败");
|
|
|
|
+ }
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg deleteUserById(String id) {
|
|
|
|
+ int i = userMapper.deleteById(id);
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg batchDeleteUserById(String ids) {
|
|
|
|
+ userMapper.deleteBatchIds(Arrays.asList(StringUtils.split(ids, ",")));
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg getUserList(String status, String filter, String page, String pageSize) {
|
|
|
|
+ LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ if (StringUtils.isNotBlank(status)) {
|
|
|
|
+ queryWrapper.eq(Users::getAccountStatus, status);
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isNotBlank(filter)) {
|
|
|
|
+ queryWrapper.like(Users::getUserName, "%"+filter+"%")
|
|
|
|
+ .or().like(Users::getCompany, "%"+filter+"%")
|
|
|
|
+ .or().like(Users::getDepartment, "%"+filter+"%");
|
|
|
|
+ }
|
|
|
|
+ List<Users> users = new ArrayList<>();
|
|
|
|
+ long total = 0;
|
|
|
|
+ if (StringUtils.isNoneBlank(page, pageSize)) {
|
|
|
|
+ PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
|
|
|
|
+ PageInfo<Users> pageInfo = PageInfo.of(userMapper.selectList(queryWrapper));
|
|
|
|
+ users = pageInfo.getList();
|
|
|
|
+ total = pageInfo.getTotal();
|
|
|
|
+ } else {
|
|
|
|
+ users = userMapper.selectList(queryWrapper);
|
|
|
|
+ total = users.size();
|
|
|
|
+ }
|
|
|
|
+ ReturnMsg<List<Users>> ok = ReturnMsg.ok(users);
|
|
|
|
+ ok.setTotal(total);
|
|
|
|
+ return ok;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg updateUserStatus(String id, String status) {
|
|
|
|
+ Users users = userMapper.selectById(id);
|
|
|
|
+ if (users==null) {
|
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
|
+ }
|
|
|
|
+ users.setAccountStatus(status);
|
|
|
|
+ int i = userMapper.updateById(users);
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg batchModifyStatus(String ids, String status) {
|
|
|
|
+ String[] split = StringUtils.split(ids, ",");
|
|
|
|
+ userMapper.batchUpdateStatus(split, status);
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReturnMsg batchModifyUserRole(String usersId, String roleId) {
|
|
|
|
+ String[] userIds = StringUtils.split(usersId, ",");
|
|
|
|
+ userMapper.batchModifyUserRole(userIds, roleId);
|
|
|
|
+ return ReturnMsg.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void exportData(HttpServletResponse response) {
|
|
|
|
+ List<Users> users = userMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
|
+ try {
|
|
|
|
+ ServletOutputStream outputStream = response.getOutputStream();
|
|
|
|
+ EasyExcel.write(outputStream, Users.class).autoCloseStream(true).sheet().doWrite(users);
|
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
|
+ response.setCharacterEncoding("utf8");
|
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + "用户信息—"+System.currentTimeMillis()+".xlsx" );
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|