UserServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package cn.com.qmth.mps.service.impl;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Set;
  9. import java.util.stream.Collectors;
  10. import org.apache.commons.collections4.CollectionUtils;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.commons.text.StringEscapeUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  19. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  20. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  21. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  22. import com.baomidou.mybatisplus.core.metadata.IPage;
  23. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  24. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  25. import com.qmth.boot.core.collection.PageResult;
  26. import com.qmth.boot.core.exception.StatusException;
  27. import com.qmth.boot.tools.excel.ExcelReader;
  28. import com.qmth.boot.tools.excel.enums.ExcelType;
  29. import com.qmth.boot.tools.excel.model.DataMap;
  30. import cn.com.qmth.mps.bean.CourseInfo;
  31. import cn.com.qmth.mps.bean.User;
  32. import cn.com.qmth.mps.dao.UserDao;
  33. import cn.com.qmth.mps.entity.UserEntity;
  34. import cn.com.qmth.mps.enums.Role;
  35. import cn.com.qmth.mps.service.SchoolService;
  36. import cn.com.qmth.mps.service.UserCourseRelationService;
  37. import cn.com.qmth.mps.service.UserService;
  38. import cn.com.qmth.mps.util.ByteUtil;
  39. import cn.com.qmth.mps.util.PageUtil;
  40. import cn.com.qmth.mps.util.SHA256;
  41. import cn.com.qmth.mps.vo.user.UserDomain;
  42. import cn.com.qmth.mps.vo.user.UserQuery;
  43. import cn.com.qmth.mps.vo.user.UserVo;
  44. @Service
  45. public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
  46. private static final String DEFAULT_PASSWD = "123456";
  47. @Autowired
  48. private UserCourseRelationService userCourseRelationService;
  49. @Autowired
  50. private SchoolService schoolService;
  51. @Override
  52. public UserEntity getByLoginName(String phone) {
  53. QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
  54. LambdaQueryWrapper<UserEntity> lw = wrapper.lambda();
  55. lw.eq(UserEntity::getLoginName, phone);
  56. return this.getOne(wrapper);
  57. }
  58. @Transactional
  59. @Override
  60. public void saveUser(UserDomain domain, User user) {
  61. if (domain.getSchoolId() == null) {
  62. throw new StatusException("学校不能为空");
  63. }
  64. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(domain.getSchoolId())) {
  65. throw new StatusException("没有权限");
  66. }
  67. if (StringUtils.isBlank(domain.getName())) {
  68. throw new StatusException("姓名不能为空");
  69. }
  70. if (StringUtils.isBlank(domain.getLoginName())) {
  71. throw new StatusException("登录名不能为空");
  72. }
  73. if (domain.getId() == null && StringUtils.isBlank(domain.getPasswd())) {
  74. throw new StatusException("密码不能为空");
  75. }
  76. if (domain.getRole() == null) {
  77. throw new StatusException("角色不能为空");
  78. }
  79. if (domain.getRole().equals(Role.SUPER_ADMIN)) {
  80. throw new StatusException("不能新增超管");
  81. }
  82. if (!domain.getRole().equals(Role.SECTION_LEADER) && CollectionUtils.isNotEmpty(domain.getCourse())) {
  83. throw new StatusException("只有科组长可关联科目");
  84. }
  85. UserEntity ue = null;
  86. if (domain.getId() != null) {
  87. ue = this.getById(domain.getId());
  88. if (ue == null) {
  89. throw new StatusException("未找到用户");
  90. }
  91. if (ue.getRoleId().equals(Role.SUPER_ADMIN.getId())) {
  92. throw new StatusException("不能编辑超管");
  93. }
  94. } else {
  95. if (getByLoginName(domain.getLoginName()) != null) {
  96. throw new StatusException("登录名已存在");
  97. }
  98. ue = new UserEntity();
  99. ue.setPassword(ByteUtil.toHexAscii(SHA256.encode(domain.getPasswd())));
  100. ue.setSchoolId(user.getSchoolId());
  101. ue.setEnable(true);
  102. ue.setLoginName(domain.getLoginName());
  103. }
  104. ue.setName(domain.getName());
  105. ue.setRoleId(domain.getRole().getId());
  106. this.saveOrUpdate(ue);
  107. if (CollectionUtils.isNotEmpty(domain.getCourse())) {
  108. Set<String> set = new HashSet<>();
  109. for (String s : domain.getCourse()) {
  110. set.add(s);
  111. }
  112. if (set.size() != domain.getCourse().size()) {
  113. throw new StatusException("科目代码不能重复");
  114. }
  115. userCourseRelationService.saveCourse(ue.getSchoolId(), ue.getId(), domain.getCourse());
  116. } else {
  117. userCourseRelationService.removeCourse(ue.getId());
  118. }
  119. }
  120. @Transactional
  121. @Override
  122. public List<String> importUser(Long schoolId, User user, MultipartFile file) {
  123. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(schoolId)) {
  124. throw new StatusException("没有权限");
  125. }
  126. InputStream inputStream = null;
  127. try {
  128. inputStream = file.getInputStream();
  129. List<DataMap> lineList = ExcelReader.create(ExcelType.XLSX, inputStream, 0).getDataMapList();
  130. if (CollectionUtils.isEmpty(lineList)) {
  131. throw new StatusException("Excel无内容");
  132. }
  133. if (1001 < lineList.size()) {
  134. throw new StatusException("数据行数不能超过1000");
  135. }
  136. List<String> failRecords = new ArrayList<>();
  137. List<UserDomain> userList = new ArrayList<>();
  138. for (int i = 0; i < lineList.size(); i++) {
  139. DataMap line = lineList.get(i);
  140. StringBuilder msg = new StringBuilder();
  141. UserDomain impuser = new UserDomain();
  142. impuser.setSchoolId(schoolId);
  143. String name = trimAndNullIfBlank(line.getValue(0));
  144. if (StringUtils.isBlank(name)) {
  145. msg.append(" 姓名不能为空");
  146. } else if (name.length() > 20) {
  147. msg.append(" 姓名不能超过20个字符");
  148. }
  149. impuser.setName(name);
  150. String loginname = trimAndNullIfBlank(line.getValue(1));
  151. if (StringUtils.isBlank(loginname)) {
  152. msg.append(" 登录名不能为空");
  153. } else if (loginname.length() > 20) {
  154. msg.append(" 登录名不能超过20个字符");
  155. }
  156. impuser.setLoginName(loginname);
  157. String role = trimAndNullIfBlank(line.getValue(2));
  158. if (StringUtils.isBlank(role)) {
  159. msg.append(" 角色名称不能为空");
  160. } else if (Role.getByName(role) == null) {
  161. msg.append(" 角色名称错误");
  162. } else if (Role.SUPER_ADMIN.equals(Role.getByName(role))) {
  163. msg.append(" 不能新建超级管理员");
  164. }
  165. impuser.setRole(Role.getByName(role));
  166. String coursecodes = trimAndNullIfBlank(line.getValue(3));
  167. if (StringUtils.isNotBlank(coursecodes)) {
  168. impuser.setCourse(Arrays.asList(coursecodes.split(",")));
  169. }
  170. if (msg.length() > 0) {
  171. failRecords.add(newError(i + 1, msg.toString()));
  172. } else {
  173. userList.add(impuser);
  174. }
  175. }
  176. if (CollectionUtils.isNotEmpty(failRecords)) {
  177. return failRecords;
  178. }
  179. for (int i = 0; i < userList.size(); i++) {
  180. UserDomain cur = userList.get(i);
  181. cur.setPasswd(DEFAULT_PASSWD);
  182. try {
  183. saveUser(cur, user);
  184. } catch (StatusException e) {
  185. failRecords.add(newError(i + 1, e.getMessage()));
  186. } catch (Exception e) {
  187. failRecords.add(newError(i + 1, "系统异常"));
  188. log.error("用户导入系统异常", e);
  189. }
  190. }
  191. if (CollectionUtils.isNotEmpty(failRecords)) {
  192. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  193. }
  194. return failRecords;
  195. } catch (StatusException e) {
  196. throw e;
  197. } catch (Exception e) {
  198. throw new StatusException("系统错误", e);
  199. } finally {
  200. if (inputStream != null) {
  201. try {
  202. inputStream.close();
  203. } catch (IOException e) {
  204. }
  205. }
  206. }
  207. }
  208. private String trimAndNullIfBlank(String s) {
  209. if (StringUtils.isBlank(s)) {
  210. return null;
  211. }
  212. return s.trim();
  213. }
  214. private String newError(int lineNum, String msg) {
  215. return "第" + lineNum + "行" + msg;
  216. }
  217. @Override
  218. public PageResult<UserVo> page(UserQuery query, User user) {
  219. if (query.getSchoolId() == null) {
  220. throw new StatusException("学校不能为空");
  221. }
  222. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(query.getSchoolId())) {
  223. throw new StatusException("没有权限");
  224. }
  225. IPage<UserVo> iPage = this.baseMapper.page(new Page<UserVo>(query.getPageNumber(), query.getPageSize()), query);
  226. if (CollectionUtils.isNotEmpty(iPage.getRecords())) {
  227. for (UserVo vo : iPage.getRecords()) {
  228. vo.setRole(Role.getById(vo.getRoleId()));
  229. if (vo.getRoleId().equals(Role.SECTION_LEADER.getId())) {
  230. List<CourseInfo> cs = userCourseRelationService.getCourses(vo.getId());
  231. vo.setCourseCodes(cs.stream().map(m -> m.getCode()).collect(Collectors.toList()));
  232. }
  233. }
  234. }
  235. return PageUtil.of(iPage);
  236. }
  237. @Override
  238. public UserVo info(Long id) {
  239. UserEntity ue = this.getById(id);
  240. if (ue == null) {
  241. throw new StatusException("未找到用户信息");
  242. }
  243. UserVo vo = new UserVo();
  244. vo.setEnable(ue.getEnable());
  245. vo.setId(ue.getId());
  246. vo.setRoleId(ue.getRoleId());
  247. vo.setRole(Role.getById(ue.getRoleId()));
  248. vo.setLoginName(ue.getLoginName());
  249. vo.setName(ue.getName());
  250. vo.setSchoolId(ue.getSchoolId());
  251. vo.setSchoolName(schoolService.getById(ue.getSchoolId()).getName());
  252. if (vo.getRoleId().equals(Role.SECTION_LEADER.getId())) {
  253. List<CourseInfo> cs = userCourseRelationService.getCourses(vo.getId());
  254. vo.setCourseCodes(cs.stream().map(m -> m.getCode()).collect(Collectors.toList()));
  255. }
  256. return vo;
  257. }
  258. @Transactional
  259. @Override
  260. public void toggle(List<Long> ids, Boolean enable, User user) {
  261. UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
  262. LambdaUpdateWrapper<UserEntity> lw = wrapper.lambda();
  263. lw.set(UserEntity::getEnable, enable);
  264. lw.in(UserEntity::getId, ids);
  265. if (!user.getRole().equals(Role.SUPER_ADMIN)) {
  266. lw.eq(UserEntity::getSchoolId, user.getSchoolId());
  267. }
  268. this.update(wrapper);
  269. }
  270. @Transactional
  271. @Override
  272. public void updatePass(String password, User accessUser) {
  273. if (!accessUser.getRole().equals(Role.SUPER_ADMIN)) {
  274. throw new StatusException("不能修改超级管理员");
  275. }
  276. Long userId = accessUser.getId();
  277. String realPassword = StringEscapeUtils.unescapeJava(password);
  278. byte[] bytes = SHA256.encode(realPassword);
  279. String encodePassword = ByteUtil.toHexAscii(bytes);
  280. UserEntity ue = this.getById(userId);
  281. if (ue == null) {
  282. throw new StatusException("未找到用户信息");
  283. }
  284. ue.setPassword(encodePassword);
  285. this.updateById(ue);
  286. }
  287. @Transactional
  288. @Override
  289. public void resetPass(Long userId, String passwd, User user) {
  290. UserEntity ue = this.getById(userId);
  291. if (ue == null) {
  292. throw new StatusException("未找到用户信息");
  293. }
  294. if (ue.getRoleId().equals(Role.SUPER_ADMIN.getId())) {
  295. throw new StatusException("不能修改超级管理员");
  296. }
  297. if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(ue.getSchoolId())) {
  298. throw new StatusException("没有权限");
  299. }
  300. String pw = ByteUtil.toHexAscii(SHA256.encode(passwd));
  301. UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
  302. LambdaUpdateWrapper<UserEntity> lw = wrapper.lambda();
  303. lw.set(UserEntity::getPassword, pw);
  304. lw.eq(UserEntity::getId, userId);
  305. this.update(wrapper);
  306. }
  307. @Override
  308. public UserVo myInfo(User user) {
  309. UserEntity ue = this.getById(user.getId());
  310. if (ue == null) {
  311. throw new StatusException("未找到用户信息");
  312. }
  313. UserVo vo = new UserVo();
  314. vo.setEnable(ue.getEnable());
  315. vo.setId(ue.getId());
  316. vo.setRoleId(ue.getRoleId());
  317. vo.setLoginName(ue.getLoginName());
  318. vo.setName(ue.getName());
  319. vo.setSchoolId(ue.getSchoolId());
  320. vo.setSchoolName(schoolService.getById(ue.getSchoolId()).getName());
  321. if (vo.getRoleId().equals(Role.SECTION_LEADER.getId())) {
  322. List<CourseInfo> cs = userCourseRelationService.getCourses(vo.getId());
  323. if (CollectionUtils.isNotEmpty(cs)) {
  324. vo.setCourseCodes(cs.stream().map(m -> m.getCode()).collect(Collectors.toList()));
  325. vo.setCourseNames(cs.stream().map(m -> m.getCode() + "-" + m.getName()).collect(Collectors.toList()));
  326. }
  327. }
  328. return vo;
  329. }
  330. }