| | |
| | | package com.ycl.jxkg.service.impl; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.read.listener.PageReadListener; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ycl.jxkg.domain.entity.UserEventLog; |
| | | import com.ycl.jxkg.enums.RoleEnum; |
| | | import com.ycl.jxkg.enums.UserStatusEnum; |
| | | import com.ycl.jxkg.domain.form.RegisterForm; |
| | | import com.ycl.jxkg.domain.other.KeyValue; |
| | | import com.ycl.jxkg.domain.vo.StudentVO; |
| | | import com.ycl.jxkg.event.UserEvent; |
| | | import com.ycl.jxkg.exception.BusinessException; |
| | | import com.ycl.jxkg.domain.entity.User; |
| | | import com.ycl.jxkg.event.OnRegistrationCompleteEvent; |
| | | import com.ycl.jxkg.mapper.UserMapper; |
| | | import com.ycl.jxkg.service.UserService; |
| | | import com.ycl.jxkg.domain.vo.admin.user.UserPageRequestVO; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.ycl.jxkg.domain.entity.User; |
| | | import com.ycl.jxkg.domain.entity.UserEventLog; |
| | | import com.ycl.jxkg.domain.form.RegisterForm; |
| | | import com.ycl.jxkg.domain.form.StudentUpdatePwdForm; |
| | | import com.ycl.jxkg.domain.form.UpdatePwdForm; |
| | | import com.ycl.jxkg.domain.other.KeyValue; |
| | | import com.ycl.jxkg.domain.vo.StudentVO; |
| | | import com.ycl.jxkg.domain.vo.admin.user.UserExcelVo; |
| | | import com.ycl.jxkg.domain.vo.admin.user.UserPageRequestVO; |
| | | import com.ycl.jxkg.enums.RoleEnum; |
| | | import com.ycl.jxkg.enums.UserStatusEnum; |
| | | import com.ycl.jxkg.event.OnRegistrationCompleteEvent; |
| | | import com.ycl.jxkg.event.UserEvent; |
| | | import com.ycl.jxkg.exception.BusinessException; |
| | | import com.ycl.jxkg.mapper.UserMapper; |
| | | import com.ycl.jxkg.service.AuthenticationService; |
| | | import com.ycl.jxkg.service.UserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.context.ApplicationEventPublisher; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | |
| | |
| | | |
| | | private final UserMapper userMapper; |
| | | private final ApplicationEventPublisher eventPublisher; |
| | | private final AuthenticationService authenticationService; |
| | | |
| | | |
| | | public List<User> getUsers() { |
| | |
| | | |
| | | @Override |
| | | public void insertUser(User user) { |
| | | user.setLastUpdatePasswordTime(new Date()); |
| | | userMapper.insert(user); |
| | | eventPublisher.publishEvent(new OnRegistrationCompleteEvent(user)); |
| | | } |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = BusinessException.class) |
| | | public void insertUsers(List<User> users) { |
| | | users.stream().forEach(item -> { |
| | | item.setLastUpdatePasswordTime(new Date()); |
| | | }); |
| | | userMapper.insertUsers(users); |
| | | throw new BusinessException("test BusinessException roll back"); |
| | | } |
| | |
| | | public List<StudentVO> classesStudent(Integer classesId) { |
| | | return userMapper.classesStudent(classesId); |
| | | } |
| | | |
| | | @Override |
| | | public void updatePassword(UpdatePwdForm form) { |
| | | new LambdaUpdateChainWrapper<>(baseMapper) |
| | | .eq(User::getId, form.getUserId()) |
| | | .set(User::getPassword, authenticationService.pwdEncode(form.getNewPassword())) |
| | | .set(User::getLastUpdatePasswordTime, new Date()) |
| | | .update(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateStudentPassword(StudentUpdatePwdForm form) { |
| | | User user = baseMapper.selectById(form.getUserId()); |
| | | if(user ==null){ |
| | | throw new RuntimeException("用户不存在"); |
| | | } |
| | | if(authenticationService.authUser(user,user.getUserName(),form.getOldPassword())){ |
| | | //密码正确 |
| | | new LambdaUpdateChainWrapper<>(baseMapper) |
| | | .eq(User::getId, form.getUserId()) |
| | | .set(User::getPassword, authenticationService.pwdEncode(form.getNewPassword())) |
| | | .set(User::getLastUpdatePasswordTime, new Date()) |
| | | .update(); |
| | | }else { |
| | | //密码错误 |
| | | throw new RuntimeException("旧密码错误,请确认后重新输入"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | | public void importTemplate(HttpServletResponse response) { |
| | | List<UserExcelVo> list = new ArrayList<>(); |
| | | UserExcelVo userExcelVo = new UserExcelVo(); |
| | | userExcelVo.setUserName("zhangSan"); |
| | | userExcelVo.setRealName("张三"); |
| | | userExcelVo.setSex(1); |
| | | userExcelVo.setRole(1); |
| | | userExcelVo.setPhone("12345678901"); |
| | | userExcelVo.setAge(24); |
| | | userExcelVo.setBirthDay("2000-01-01"); |
| | | list.add(userExcelVo); |
| | | ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), UserExcelVo.class).build(); |
| | | WriteSheet writeSheet = EasyExcel.writerSheet(0, "用户信息").build(); |
| | | excelWriter.write(list, writeSheet); |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | | @Transactional |
| | | public String importExcel(MultipartFile file) { |
| | | List<User> userList = new ArrayList<>(); |
| | | List<String> errorList = new ArrayList<>(); |
| | | EasyExcel.read(file.getInputStream(), UserExcelVo.class, new PageReadListener<UserExcelVo>(dataList -> dataList.forEach(item -> { |
| | | User user = new User(); |
| | | user.setUserName(item.getUserName()); |
| | | user.setPassword(authenticationService.pwdEncode("!Password123456789")); |
| | | user.setRealName(item.getRealName()); |
| | | user.setSex(item.getSex()); |
| | | user.setPhone(item.getPhone()); |
| | | try { |
| | | user.setBirthDay(new SimpleDateFormat("yyyy-MM-dd").parse(item.getBirthDay())); |
| | | } catch (ParseException e) { |
| | | errorList.add(item.getRealName()); |
| | | } |
| | | user.setAge(item.getAge()); |
| | | user.setRole(item.getRole()); |
| | | user.setStatus(UserStatusEnum.Enable.getCode()); |
| | | user.setLastUpdatePasswordTime(new Date()); |
| | | userList.add(user); |
| | | }))).sheet().doRead(); |
| | | if (!errorList.isEmpty()) { |
| | | return "以下数据日期格式错误:" + String.join(",", errorList); |
| | | } else { |
| | | saveBatch(userList); |
| | | } |
| | | return "成功导入" + userList.size() + "条数据"; |
| | | } |
| | | } |