|
@@ -1,20 +1,60 @@
|
|
package cn.com.qmth.mps.service.impl;
|
|
package cn.com.qmth.mps.service.impl;
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Set;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.commons.text.StringEscapeUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.qmth.boot.core.collection.PageResult;
|
|
|
|
+import com.qmth.boot.core.exception.StatusException;
|
|
|
|
+import com.qmth.boot.tools.excel.ExcelReader;
|
|
|
|
+import com.qmth.boot.tools.excel.enums.ExcelType;
|
|
|
|
+import com.qmth.boot.tools.excel.model.DataMap;
|
|
|
|
|
|
|
|
+import cn.com.qmth.mps.bean.User;
|
|
import cn.com.qmth.mps.dao.UserDao;
|
|
import cn.com.qmth.mps.dao.UserDao;
|
|
import cn.com.qmth.mps.entity.UserEntity;
|
|
import cn.com.qmth.mps.entity.UserEntity;
|
|
|
|
+import cn.com.qmth.mps.enums.Role;
|
|
|
|
+import cn.com.qmth.mps.service.SchoolService;
|
|
|
|
+import cn.com.qmth.mps.service.UserCourseRelationService;
|
|
import cn.com.qmth.mps.service.UserService;
|
|
import cn.com.qmth.mps.service.UserService;
|
|
|
|
+import cn.com.qmth.mps.util.ByteUtil;
|
|
|
|
+import cn.com.qmth.mps.util.PageUtil;
|
|
|
|
+import cn.com.qmth.mps.util.SHA256;
|
|
|
|
+import cn.com.qmth.mps.vo.user.UserDomain;
|
|
|
|
+import cn.com.qmth.mps.vo.user.UserQuery;
|
|
|
|
+import cn.com.qmth.mps.vo.user.UserVo;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
|
|
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
|
|
|
|
+ private static final String defPassWd = "123456";
|
|
|
|
|
|
- private static final String[] EXCEL_HEADER = new String[] { "姓名", "登录名", "登陆密码", "角色名称", "课程代码", "课程名称", "审核权限" };
|
|
|
|
|
|
+ private static final String[] EXCEL_HEADER = new String[] { "姓名", "登录名", "角色", "科目" };
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserCourseRelationService userCourseRelationService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private SchoolService schoolService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public UserEntity getByLoginName(String phone) {
|
|
public UserEntity getByLoginName(String phone) {
|
|
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
|
|
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
|
|
@@ -23,4 +63,255 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
|
|
return this.getOne(wrapper);
|
|
return this.getOne(wrapper);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void saveUser(UserDomain domain, User user) {
|
|
|
|
+ if (domain.getSchoolId() == null) {
|
|
|
|
+ throw new StatusException("学校不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(domain.getSchoolId())) {
|
|
|
|
+ throw new StatusException("非法操作");
|
|
|
|
+ }
|
|
|
|
+ if (domain.getName() == null) {
|
|
|
|
+ throw new StatusException("姓名不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (domain.getLoginName() == null) {
|
|
|
|
+ throw new StatusException("登录名不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (domain.getRole() == null) {
|
|
|
|
+ throw new StatusException("角色不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (domain.getRole().equals(Role.SUPER_ADMIN)) {
|
|
|
|
+ throw new StatusException("不能新增编辑超管");
|
|
|
|
+ }
|
|
|
|
+ if (!domain.getRole().equals(Role.SECTION_LEADER) && CollectionUtils.isNotEmpty(domain.getCourse())) {
|
|
|
|
+ throw new StatusException("只有科组长可关联科目");
|
|
|
|
+ }
|
|
|
|
+ UserEntity ue = null;
|
|
|
|
+ if (domain.getId() != null) {
|
|
|
|
+ ue = this.getById(domain.getId());
|
|
|
|
+ if (ue == null) {
|
|
|
|
+ throw new StatusException("未找到用户");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (getByLoginName(domain.getLoginName()) != null) {
|
|
|
|
+ throw new StatusException("登录名已存在");
|
|
|
|
+ }
|
|
|
|
+ ue = new UserEntity();
|
|
|
|
+ ue.setPassword(ByteUtil.toHexAscii(SHA256.encode(defPassWd)));
|
|
|
|
+ ue.setSchoolId(user.getSchoolId());
|
|
|
|
+ ue.setEnable(true);
|
|
|
|
+ ue.setLoginName(domain.getLoginName());
|
|
|
|
+ }
|
|
|
|
+ ue.setName(domain.getName());
|
|
|
|
+ ue.setRoleId(domain.getRole().getId());
|
|
|
|
+ this.saveOrUpdate(ue);
|
|
|
|
+ if (CollectionUtils.isNotEmpty(domain.getCourse())) {
|
|
|
|
+ Set<String> set = new HashSet<>();
|
|
|
|
+ for (String s : domain.getCourse()) {
|
|
|
|
+ set.add(s);
|
|
|
|
+ }
|
|
|
|
+ if (set.size() != domain.getCourse().size()) {
|
|
|
|
+ throw new StatusException("科目代码不能重复");
|
|
|
|
+ }
|
|
|
|
+ userCourseRelationService.saveCourse(ue.getSchoolId(), ue.getId(), domain.getCourse());
|
|
|
|
+ } else {
|
|
|
|
+ userCourseRelationService.removeCourse(ue.getId());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> importUser(Long schoolId,User user, MultipartFile file) {
|
|
|
|
+ if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(schoolId)) {
|
|
|
|
+ throw new StatusException("非法操作");
|
|
|
|
+ }
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+ try {
|
|
|
|
+ List<DataMap> lineList = ExcelReader.create(ExcelType.XLSX, inputStream, 4).getDataMapList();
|
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
|
+ throw new StatusException("Excel无内容");
|
|
|
|
+ }
|
|
|
|
+ if (1001 < lineList.size()) {
|
|
|
|
+ throw new StatusException("数据行数不能超过1000");
|
|
|
|
+ }
|
|
|
|
+ List<String> failRecords = new ArrayList<>();
|
|
|
|
+ List<UserDomain> userList = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
|
+ DataMap line = lineList.get(i);
|
|
|
|
+ if (0 == i) {
|
|
|
|
+ if (headerError(line)) {
|
|
|
|
+ throw new StatusException("Excel表头错误");
|
|
|
|
+ }
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ UserDomain impuser = new UserDomain();
|
|
|
|
+ impuser.setSchoolId(schoolId);
|
|
|
|
+ String name = trimAndNullIfBlank(line.getValue(0));
|
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
|
+ msg.append(" 姓名不能为空");
|
|
|
|
+ } else if (name.length() > 20) {
|
|
|
|
+ msg.append(" 姓名不能超过20个字符");
|
|
|
|
+ }
|
|
|
|
+ impuser.setName(name);
|
|
|
|
+
|
|
|
|
+ String loginname = trimAndNullIfBlank(line.getValue(1));
|
|
|
|
+ if (StringUtils.isBlank(loginname)) {
|
|
|
|
+ msg.append(" 登录名不能为空");
|
|
|
|
+ } else if (loginname.length() > 20) {
|
|
|
|
+ msg.append(" 登录名不能超过20个字符");
|
|
|
|
+ }
|
|
|
|
+ impuser.setLoginName(loginname);
|
|
|
|
+
|
|
|
|
+ String role = trimAndNullIfBlank(line.getValue(2));
|
|
|
|
+ if (StringUtils.isBlank(role)) {
|
|
|
|
+ msg.append(" 角色名称不能为空");
|
|
|
|
+ } else if (Role.getByName(role) == null) {
|
|
|
|
+ msg.append(" 角色名称错误");
|
|
|
|
+ } else if (Role.SUPER_ADMIN.equals(Role.getByName(role))) {
|
|
|
|
+ msg.append(" 不能新建超级管理员");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ impuser.setRole(Role.getByName(role));
|
|
|
|
+
|
|
|
|
+ String coursecodes = trimAndNullIfBlank(line.getValue(3));
|
|
|
|
+ if(StringUtils.isNotBlank(coursecodes)) {
|
|
|
|
+ impuser.setCourse(Arrays.asList(coursecodes.split(",")));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (msg.length() > 0) {
|
|
|
|
+ failRecords.add(newError(i + 1, msg.toString()));
|
|
|
|
+ } else {
|
|
|
|
+ userList.add(impuser);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
|
+ return failRecords;
|
|
|
|
+ }
|
|
|
|
+ for (int i = 0; i < userList.size(); i++) {
|
|
|
|
+ UserDomain cur = userList.get(i);
|
|
|
|
+ try {
|
|
|
|
+ saveUser(cur, user);
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ failRecords.add(newError(i + 1, e.getMessage()));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ failRecords.add(newError(i + 1, "系统异常"));
|
|
|
|
+ log.error("用户导入系统异常", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
|
+ }
|
|
|
|
+ return failRecords;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("系统错误",e);
|
|
|
|
+ } finally {
|
|
|
|
+ if(inputStream!=null) {
|
|
|
|
+ try {
|
|
|
|
+ inputStream.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean headerError(DataMap header) {
|
|
|
|
+ if (EXCEL_HEADER.length != header.size()) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ for (int i = 0; i < EXCEL_HEADER.length; i++) {
|
|
|
|
+ if (null == header.getValue(i)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ if (!EXCEL_HEADER[i].equals(header.getValue(i).trim())) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ private String trimAndNullIfBlank(String s) {
|
|
|
|
+ if (StringUtils.isBlank(s)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return s.trim();
|
|
|
|
+ }
|
|
|
|
+ private String newError(int lineNum, String msg) {
|
|
|
|
+ return "第" + lineNum + "行" + msg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public PageResult<UserVo> page(UserQuery query, User user) {
|
|
|
|
+ if (query.getSchoolId() == null) {
|
|
|
|
+ throw new StatusException("学校不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(query.getSchoolId())) {
|
|
|
|
+ throw new StatusException("非法操作");
|
|
|
|
+ }
|
|
|
|
+ IPage<UserVo> iPage = this.baseMapper.page(new Page<UserVo>(query.getPageNumber(), query.getPageSize()), query);
|
|
|
|
+ return PageUtil.of(iPage);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public UserVo info(Long id) {
|
|
|
|
+ UserEntity ue=this.getById(id);
|
|
|
|
+ if (ue == null) {
|
|
|
|
+ throw new StatusException("未找到用户信息");
|
|
|
|
+ }
|
|
|
|
+ UserVo vo=new UserVo();
|
|
|
|
+ vo.setEnable(ue.getEnable());
|
|
|
|
+ vo.setId(ue.getId());
|
|
|
|
+ vo.setRoleId(ue.getRoleId());
|
|
|
|
+ vo.setLoginName(ue.getLoginName());
|
|
|
|
+ vo.setName(ue.getName());
|
|
|
|
+ vo.setSchoolId(ue.getSchoolId());
|
|
|
|
+ vo.setSchoolName(schoolService.getById(ue.getSchoolId()).getName());
|
|
|
|
+ if(vo.getRoleId().equals(Role.SECTION_LEADER.getId())) {
|
|
|
|
+ vo.setCourseCodes(userCourseRelationService.getCourseCodes(vo.getId()));
|
|
|
|
+ }
|
|
|
|
+ return vo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void toggle(List<Long> ids, Boolean enable) {
|
|
|
|
+ UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
+ LambdaUpdateWrapper<UserEntity> lw = wrapper.lambda();
|
|
|
|
+ lw.set(UserEntity::getEnable, enable);
|
|
|
|
+ lw.in(UserEntity::getId, ids);
|
|
|
|
+ this.update(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void updatePass(String password, User accessUser) {
|
|
|
|
+ if (!accessUser.getRole().equals(Role.SUPER_ADMIN)) {
|
|
|
|
+ throw new StatusException("不能修改超级管理员");
|
|
|
|
+ }
|
|
|
|
+ Long userId = accessUser.getId();
|
|
|
|
+ String realPassword = StringEscapeUtils.unescapeJava(password);
|
|
|
|
+ byte[] bytes = SHA256.encode(realPassword);
|
|
|
|
+ String encodePassword = ByteUtil.toHexAscii(bytes);
|
|
|
|
+ UserEntity ue=this.getById(userId);
|
|
|
|
+ if (ue == null) {
|
|
|
|
+ throw new StatusException("未找到用户信息");
|
|
|
|
+ }
|
|
|
|
+ ue.setPassword(encodePassword);
|
|
|
|
+ this.updateById(ue);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ @Override
|
|
|
|
+ public void resetPass(Long schoolId,List<Long> ids, User accessUser) {
|
|
|
|
+ String pw=ByteUtil.toHexAscii(SHA256.encode(defPassWd));
|
|
|
|
+ UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
|
|
|
|
+ LambdaUpdateWrapper<UserEntity> lw = wrapper.lambda();
|
|
|
|
+ lw.set(UserEntity::getPassword, pw);
|
|
|
|
+ lw.in(UserEntity::getId, ids);
|
|
|
|
+ lw.eq(UserEntity::getSchoolId, schoolId);
|
|
|
|
+ this.update(wrapper);
|
|
|
|
+ }
|
|
}
|
|
}
|