StudentManagerController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.xjrsoft.module.student.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import cn.hutool.core.bean.BeanUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.excel.EasyExcel;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.core.metadata.IPage;
  9. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  10. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  11. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  12. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  13. import com.xjrsoft.common.enums.DeleteMark;
  14. import com.xjrsoft.common.enums.GenderDictionaryEnum;
  15. import com.xjrsoft.common.exception.MyException;
  16. import com.xjrsoft.common.model.result.R;
  17. import com.xjrsoft.common.model.result.RT;
  18. import com.xjrsoft.common.page.ConventPage;
  19. import com.xjrsoft.common.page.PageOutput;
  20. import com.xjrsoft.common.utils.FileZipUtil;
  21. import com.xjrsoft.common.utils.TreeUtil;
  22. import com.xjrsoft.common.utils.VoToColumnUtil;
  23. import com.xjrsoft.module.base.entity.BaseClass;
  24. import com.xjrsoft.module.base.entity.BaseGrade;
  25. import com.xjrsoft.module.base.service.IBaseClassService;
  26. import com.xjrsoft.module.base.service.IBaseGradeService;
  27. import com.xjrsoft.module.concat.service.IXjrUserService;
  28. import com.xjrsoft.module.organization.entity.UserDeptRelation;
  29. import com.xjrsoft.module.organization.service.IUserDeptRelationService;
  30. import com.xjrsoft.module.room.service.IRoomBedService;
  31. import com.xjrsoft.module.student.dto.AddBaseStudentUserDto;
  32. import com.xjrsoft.module.student.dto.BaseStudentUserPageDto;
  33. import com.xjrsoft.module.student.dto.UpdateBaseStudentUserDto;
  34. import com.xjrsoft.module.student.entity.BaseStudent;
  35. import com.xjrsoft.module.student.entity.BaseStudentUser;
  36. import com.xjrsoft.module.student.service.IStudentManagerService;
  37. import com.xjrsoft.module.student.vo.BaseStudentSchoolRollVo;
  38. import com.xjrsoft.module.student.vo.BaseStudentTreeVo;
  39. import com.xjrsoft.module.student.vo.BaseStudentUserPageVo;
  40. import com.xjrsoft.module.student.vo.BaseStudentUserVo;
  41. import com.xjrsoft.module.teacher.entity.XjrUser;
  42. import io.swagger.annotations.Api;
  43. import io.swagger.annotations.ApiOperation;
  44. import lombok.AllArgsConstructor;
  45. import org.apache.commons.lang3.StringUtils;
  46. import org.springframework.web.bind.annotation.DeleteMapping;
  47. import org.springframework.web.bind.annotation.GetMapping;
  48. import org.springframework.web.bind.annotation.PostMapping;
  49. import org.springframework.web.bind.annotation.PutMapping;
  50. import org.springframework.web.bind.annotation.RequestBody;
  51. import org.springframework.web.bind.annotation.RequestMapping;
  52. import org.springframework.web.bind.annotation.RequestParam;
  53. import org.springframework.web.bind.annotation.RestController;
  54. import org.springframework.web.multipart.MultipartFile;
  55. import javax.validation.Valid;
  56. import java.io.ByteArrayOutputStream;
  57. import java.io.IOException;
  58. import java.io.InputStream;
  59. import java.text.ParseException;
  60. import java.util.ArrayList;
  61. import java.util.Arrays;
  62. import java.util.Base64;
  63. import java.util.Enumeration;
  64. import java.util.HashMap;
  65. import java.util.HashSet;
  66. import java.util.List;
  67. import java.util.Map;
  68. import java.util.Set;
  69. import java.util.stream.Collectors;
  70. import java.util.zip.ZipEntry;
  71. import java.util.zip.ZipFile;
  72. @RestController
  73. @RequestMapping("/student" + "/studentmanager")
  74. @Api(value = "/student" + "/studentmanager", tags = "学生基本信息管理代码")
  75. @AllArgsConstructor
  76. public class StudentManagerController {
  77. private final IStudentManagerService studentManagerService;
  78. private final IBaseGradeService baseGradeService;
  79. private final IBaseClassService baseClassService;
  80. private final IXjrUserService xjrUserService;
  81. private final IRoomBedService roomBedService;
  82. private final IUserDeptRelationService userDeptRelationService;
  83. @GetMapping(value = "/page")
  84. @ApiOperation(value = "学生列表(分页)")
  85. @SaCheckPermission("studentmanager:detail")
  86. public R page(@Valid BaseStudentUserPageDto dto) {
  87. IPage<BaseStudentUserPageVo> page = studentManagerService.getStudentPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
  88. PageOutput<BaseStudentUserPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentUserPageVo.class);
  89. return R.ok(pageOutput);
  90. }
  91. @GetMapping(value = "/tree")
  92. @ApiOperation(value = "学生年纪班级树")
  93. @SaCheckPermission("studentmanager:detail")
  94. public RT<List<BaseStudentTreeVo>> tree() {
  95. List<String> roleList = StpUtil.getRoleList();
  96. long teacherId = StpUtil.getLoginIdAsLong();
  97. List<BaseClass> classList = baseClassService.list(
  98. new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
  99. );
  100. List<BaseStudentTreeVo> voList = new ArrayList<>();
  101. List<BaseGrade> gradeList = baseGradeService.list(
  102. new QueryWrapper<BaseGrade>().lambda().eq(BaseGrade::getDeleteMark, DeleteMark.NODELETE.getCode())
  103. );
  104. if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
  105. Set<Long> gradeSet = new HashSet<>();
  106. for (BaseClass baseClass : classList) {
  107. if(baseClass.getTeacherId().equals(teacherId)){
  108. gradeSet.add(baseClass.getGradeId());
  109. voList.add(new BaseStudentTreeVo(){{
  110. setId(baseClass.getId());
  111. setName(baseClass.getName());
  112. setParentId(baseClass.getGradeId());
  113. }});
  114. }
  115. }
  116. gradeList.forEach((node) -> {
  117. if(gradeSet.contains(node.getId())){
  118. voList.add(new BaseStudentTreeVo(){{
  119. setId(node.getId());
  120. setName(node.getName());
  121. }});
  122. }
  123. });
  124. List<BaseStudentTreeVo> treeVoList = TreeUtil.build(voList);
  125. return RT.ok(treeVoList);
  126. }
  127. gradeList.forEach((node) -> {
  128. voList.add(new BaseStudentTreeVo(){{
  129. setId(node.getId());
  130. setName(node.getName());
  131. }});
  132. });
  133. classList.forEach((node)->{
  134. voList.add(new BaseStudentTreeVo(){{
  135. setId(node.getId());
  136. setName(node.getName());
  137. setParentId(node.getGradeId());
  138. }});
  139. });
  140. List<BaseStudentTreeVo> treeVoList = TreeUtil.build(voList);
  141. return RT.ok(treeVoList);
  142. }
  143. @GetMapping(value = "/info")
  144. @ApiOperation(value = "根据id查询学生信息")
  145. @SaCheckPermission("studentmanager:detail")
  146. public R info(@RequestParam Long id) {
  147. BaseStudentUser baseStudentUser = studentManagerService.getByIdDeep(id);
  148. if (baseStudentUser == null) {
  149. return R.error("找不到此数据!");
  150. }
  151. BaseStudentUserVo userVo = BeanUtil.toBean(baseStudentUser, BaseStudentUserVo.class);
  152. for (BaseStudentSchoolRollVo roll : userVo.getBaseStudentSchoolRollList()) {
  153. BaseClass aClass = baseClassService.getById(roll.getClassId());
  154. XjrUser xjrUser = xjrUserService.getById(aClass.getTeacherId());
  155. if(xjrUser != null){
  156. roll.setTeacherName(xjrUser.getName());
  157. }
  158. String roomName = roomBedService.getRoomNameByStudentUserId(roll.getUserId());
  159. roll.setRoomName(roomName);
  160. }
  161. List<Long> deptIds = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class)
  162. .eq(UserDeptRelation::getUserId, userVo.getId()))
  163. .stream().map(UserDeptRelation::getDeptId).collect(Collectors.toList());
  164. String allDeptIdStr = StrUtil.join(StringPool.COMMA, deptIds);
  165. userVo.setDepartmentIds(allDeptIdStr);
  166. userVo.setGenderCn(GenderDictionaryEnum.getValue(userVo.getGender()));
  167. return R.ok(userVo);
  168. }
  169. @PostMapping
  170. @ApiOperation(value = "新增学生")
  171. @SaCheckPermission("studentmanager:add")
  172. public R add(@Valid @RequestBody AddBaseStudentUserDto dto){
  173. return R.ok(studentManagerService.add(dto));
  174. }
  175. @PostMapping("upload-image")
  176. @ApiOperation(value = "上传学生学籍照片")
  177. @SaCheckPermission("studentmanager:add")
  178. public R uploadImage(@Valid Long userId, @RequestParam("file") MultipartFile file) throws IOException {
  179. return R.ok(studentManagerService.uploadImage(userId, file));
  180. }
  181. @PutMapping
  182. @ApiOperation(value = "修改学生")
  183. @SaCheckPermission("studentmanager:edit")
  184. public R update(@Valid UpdateBaseStudentUserDto dto){
  185. return R.ok(studentManagerService.update(dto));
  186. }
  187. @DeleteMapping
  188. @ApiOperation(value = "删除")
  189. @SaCheckPermission("studentmanager:delete")
  190. public R delete(@Valid @RequestBody List<Long> ids) {
  191. return R.ok(studentManagerService.delete(ids));
  192. }
  193. @GetMapping(value = "/class")
  194. @ApiOperation(value = "获取当前学生的班主任")
  195. public R studentClass() {
  196. Long userId = StpUtil.getLoginIdAsLong();
  197. return R.ok(studentManagerService.getStudentClass(userId));
  198. }
  199. @PostMapping("/import")
  200. @ApiOperation(value = "导入学生信息")
  201. public RT<Boolean> importData(@RequestParam("file") MultipartFile file) throws IOException, ParseException {
  202. List<Map<Integer, Object>> excelDataList = EasyExcel.read(file.getInputStream()).sheet().headRowNumber(3).doReadSync();
  203. return RT.ok(studentManagerService.importStudentData(excelDataList));
  204. }
  205. @PostMapping(value = "/avatar-import")
  206. @ApiOperation(value = "批量导入学生学学籍照")
  207. @SaCheckPermission("stundentfaceprocess:batch-upload")
  208. public RT<Boolean> batchUpload(@RequestParam("file") MultipartFile file) throws Exception {
  209. List<BaseStudentUser> list = studentManagerService.list(
  210. new MPJLambdaWrapper<BaseStudentUser>().distinct()
  211. .select(BaseStudentUser::getId)
  212. .select(BaseStudentUser.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentUser.class).contains(x.getProperty()))
  213. .leftJoin(BaseStudent.class, BaseStudent::getUserId, BaseStudentUser::getId)
  214. .eq(BaseStudent::getDeleteMark, DeleteMark.NODELETE.getCode())
  215. .eq(BaseStudentUser::getDeleteMark, DeleteMark.NODELETE.getCode())
  216. );
  217. Map<String, BaseStudentUser> studentMap = new HashMap<>();
  218. for (BaseStudentUser baseStudentUser : list) {
  219. studentMap.put(baseStudentUser.getCredentialNumber(), baseStudentUser);
  220. }
  221. ZipFile zipFile = FileZipUtil.convertToZipFile(file);
  222. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  223. Map<String, String> map = new HashMap<String, String>() {
  224. {
  225. put("jpg", "data:image/jpg;base64,");
  226. put("jpeg", "data:image/jpeg;base64,");
  227. put("png", "data:image/png;base64,");
  228. }
  229. };
  230. String[] imgSuffix = new String[]{"png", "jpg", "jpeg"};
  231. while (entries.hasMoreElements()){
  232. ZipEntry entry = entries.nextElement();
  233. String filename = entry.getName();
  234. InputStream inputStream = zipFile.getInputStream(entry); //读取文件内容
  235. String[] split = filename.split("\\.");
  236. String idNumber = split[0].substring(split[0].length() - 18);
  237. String suffix = StringUtils.substringAfterLast(filename, StringPool.DOT);
  238. BaseStudentUser studentUser = studentMap.get(idNumber);
  239. if(studentUser == null){
  240. continue;
  241. }
  242. if (!Arrays.asList(imgSuffix).contains(suffix)) {
  243. throw new MyException("图片格式不正确!");
  244. }
  245. //将照片转换成base64
  246. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  247. byte[] buffer = new byte[4096];
  248. int bytesRead;
  249. while ((bytesRead = inputStream.read(buffer)) != -1) {
  250. outputStream.write(buffer, 0, bytesRead);
  251. }
  252. byte[] imageBytes = outputStream.toByteArray();
  253. String base64String = map.get(suffix) + Base64.getEncoder().encodeToString(imageBytes);
  254. inputStream.close();
  255. outputStream.close();
  256. studentUser.setAvatar(base64String);
  257. studentManagerService.updateById(studentUser);
  258. }
  259. return RT.ok(true);
  260. }
  261. }