StudentManagerController.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.ObjectUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.alibaba.excel.EasyExcel;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.core.metadata.IPage;
  10. import com.github.yulichang.toolkit.MPJWrappers;
  11. import com.xjrsoft.common.enums.DeleteMark;
  12. import com.xjrsoft.common.enums.GenderDictionaryEnum;
  13. import com.xjrsoft.common.model.result.R;
  14. import com.xjrsoft.common.model.result.RT;
  15. import com.xjrsoft.common.page.ConventPage;
  16. import com.xjrsoft.common.page.PageOutput;
  17. import com.xjrsoft.common.utils.TreeUtil;
  18. import com.xjrsoft.common.utils.VoToColumnUtil;
  19. import com.xjrsoft.module.base.entity.BaseClass;
  20. import com.xjrsoft.module.base.entity.BaseGrade;
  21. import com.xjrsoft.module.base.service.IBaseClassService;
  22. import com.xjrsoft.module.base.service.IBaseGradeService;
  23. import com.xjrsoft.module.student.dto.AddBaseStudentUserDto;
  24. import com.xjrsoft.module.student.dto.BaseStudentUserPageDto;
  25. import com.xjrsoft.module.student.dto.UpdateBaseStudentUserDto;
  26. import com.xjrsoft.module.student.entity.BaseStudent;
  27. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  28. import com.xjrsoft.module.student.entity.BaseStudentUser;
  29. import com.xjrsoft.module.student.service.IStudentManagerService;
  30. import com.xjrsoft.module.student.vo.BaseStudentTreeVo;
  31. import com.xjrsoft.module.student.vo.BaseStudentUserPageVo;
  32. import com.xjrsoft.module.student.vo.BaseStudentUserVo;
  33. import io.swagger.annotations.Api;
  34. import io.swagger.annotations.ApiOperation;
  35. import lombok.AllArgsConstructor;
  36. import org.springframework.web.bind.annotation.DeleteMapping;
  37. import org.springframework.web.bind.annotation.GetMapping;
  38. import org.springframework.web.bind.annotation.PostMapping;
  39. import org.springframework.web.bind.annotation.PutMapping;
  40. import org.springframework.web.bind.annotation.RequestBody;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestParam;
  43. import org.springframework.web.bind.annotation.RestController;
  44. import org.springframework.web.multipart.MultipartFile;
  45. import javax.validation.Valid;
  46. import java.io.IOException;
  47. import java.text.ParseException;
  48. import java.util.ArrayList;
  49. import java.util.HashSet;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.Set;
  53. @RestController
  54. @RequestMapping("/student" + "/studentmanager")
  55. @Api(value = "/student" + "/studentmanager", tags = "学生基本信息管理代码")
  56. @AllArgsConstructor
  57. public class StudentManagerController {
  58. private final IStudentManagerService studentManagerService;
  59. private final IBaseGradeService baseGradeService;
  60. private final IBaseClassService baseClassService;
  61. @GetMapping(value = "/page")
  62. @ApiOperation(value = "学生列表(分页)")
  63. @SaCheckPermission("studentmanager:detail")
  64. public R page(@Valid BaseStudentUserPageDto dto) {
  65. IPage<BaseStudentUserPageVo> page = studentManagerService.selectJoinListPage(ConventPage.getPage(dto), BaseStudentUserPageVo.class,
  66. MPJWrappers.<BaseStudentUser>lambdaJoin()
  67. .like(StrUtil.isNotBlank(dto.getUserName()), BaseStudentUser::getUserName, dto.getUserName())
  68. .like(StrUtil.isNotBlank(dto.getMobile()), BaseStudentUser::getMobile, dto.getMobile())
  69. .like(StrUtil.isNotBlank(dto.getName()), BaseStudentUser::getName, dto.getName())
  70. .like(StrUtil.isNotBlank(dto.getEmail()), BaseStudentUser::getEmail, dto.getEmail())
  71. .eq(ObjectUtil.isNotNull(dto.getClassId()), BaseClass::getId, dto.getClassId())
  72. .in((ObjectUtil.isNotNull(dto.getTokenType()) && dto.getTokenType() == 1), BaseClass::getTeacherId, StpUtil.getLoginIdAsLong())
  73. .orderByDesc(BaseStudentUser::getId)
  74. .select(BaseStudentUser::getId)
  75. .select(BaseStudent::getStudentId)
  76. .select(BaseStudentSchoolRoll.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentUserPageVo.class).contains(x.getProperty()))
  77. .select(BaseStudentUser.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentUserPageVo.class).contains(x.getProperty()))
  78. .select(BaseStudentSchoolRoll::getStudyYear)
  79. .select("a.`name` as grade_name")
  80. .select("b.`name` as major_set_name")
  81. .selectAs(BaseClass::getName, BaseStudentUserPageVo::getClassName)
  82. .innerJoin(BaseStudent.class, BaseStudent::getUserId, BaseStudentUser::getId)
  83. .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, BaseStudentUser::getId)
  84. .leftJoin("base_grade a on a.id = t2.grade_id")
  85. .leftJoin("base_major_set b on b.id = t2.major_set_id")
  86. .leftJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId)
  87. // .select("c.`mobile` as guardianPhone")
  88. // .select("c.`name` as guardianName")
  89. // .leftJoin("base_student_family_member c on c.user_id = t2.user_id")
  90. );
  91. for (BaseStudentUserPageVo record : page.getRecords()) {
  92. record.setGenderCn(GenderDictionaryEnum.getValue(record.getGender()));
  93. }
  94. PageOutput<BaseStudentUserPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentUserPageVo.class);
  95. return R.ok(pageOutput);
  96. }
  97. @GetMapping(value = "/tree")
  98. @ApiOperation(value = "学生年纪班级树")
  99. @SaCheckPermission("studentmanager:detail")
  100. public RT<List<BaseStudentTreeVo>> tree() {
  101. List<String> roleList = StpUtil.getRoleList();
  102. long teacherId = StpUtil.getLoginIdAsLong();
  103. List<BaseClass> classList = baseClassService.list(
  104. new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
  105. );
  106. List<BaseStudentTreeVo> voList = new ArrayList<>();
  107. List<BaseGrade> gradeList = baseGradeService.list(
  108. new QueryWrapper<BaseGrade>().lambda().eq(BaseGrade::getDeleteMark, DeleteMark.NODELETE.getCode())
  109. );
  110. if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
  111. Set<Long> gradeSet = new HashSet<>();
  112. for (BaseClass baseClass : classList) {
  113. if(baseClass.getTeacherId().equals(teacherId)){
  114. gradeSet.add(baseClass.getGradeId());
  115. voList.add(new BaseStudentTreeVo(){{
  116. setId(baseClass.getId());
  117. setName(baseClass.getName());
  118. setParentId(baseClass.getGradeId());
  119. }});
  120. }
  121. }
  122. gradeList.forEach((node) -> {
  123. if(gradeSet.contains(node.getId())){
  124. voList.add(new BaseStudentTreeVo(){{
  125. setId(node.getId());
  126. setName(node.getName());
  127. }});
  128. }
  129. });
  130. List<BaseStudentTreeVo> treeVoList = TreeUtil.build(voList);
  131. return RT.ok(treeVoList);
  132. }
  133. gradeList.forEach((node) -> {
  134. voList.add(new BaseStudentTreeVo(){{
  135. setId(node.getId());
  136. setName(node.getName());
  137. }});
  138. });
  139. classList.forEach((node)->{
  140. voList.add(new BaseStudentTreeVo(){{
  141. setId(node.getId());
  142. setName(node.getName());
  143. setParentId(node.getGradeId());
  144. }});
  145. });
  146. List<BaseStudentTreeVo> treeVoList = TreeUtil.build(voList);
  147. return RT.ok(treeVoList);
  148. }
  149. @GetMapping(value = "/info")
  150. @ApiOperation(value = "根据id查询学生信息")
  151. @SaCheckPermission("studentmanager:detail")
  152. public R info(@RequestParam Long id) {
  153. BaseStudentUser baseStudentUser = studentManagerService.getByIdDeep(id);
  154. if (baseStudentUser == null) {
  155. return R.error("找不到此数据!");
  156. }
  157. BaseStudentUserVo userVo = BeanUtil.toBean(baseStudentUser, BaseStudentUserVo.class);
  158. userVo.setGenderCn(GenderDictionaryEnum.getValue(userVo.getGender()));
  159. return R.ok(userVo);
  160. }
  161. @PostMapping
  162. @ApiOperation(value = "新增学生")
  163. @SaCheckPermission("studentmanager:add")
  164. public R add(@Valid @RequestBody AddBaseStudentUserDto dto) {
  165. BaseStudentUser baseStudentUser = BeanUtil.toBean(dto, BaseStudentUser.class);
  166. return R.ok(studentManagerService.add(baseStudentUser));
  167. }
  168. @PutMapping
  169. @ApiOperation(value = "修改学生")
  170. @SaCheckPermission("studentmanager:edit")
  171. public R update(@Valid @RequestBody UpdateBaseStudentUserDto dto) {
  172. BaseStudentUser baseStudentUser = BeanUtil.toBean(dto, BaseStudentUser.class);
  173. return R.ok(studentManagerService.update(baseStudentUser));
  174. }
  175. @DeleteMapping
  176. @ApiOperation(value = "删除")
  177. @SaCheckPermission("studentmanager:delete")
  178. public R delete(@Valid @RequestBody List<Long> ids) {
  179. return R.ok(studentManagerService.delete(ids));
  180. }
  181. @GetMapping(value = "/class")
  182. @ApiOperation(value = "获取当前学生的班主任")
  183. public R studentClass() {
  184. Long userId = StpUtil.getLoginIdAsLong();
  185. return R.ok(studentManagerService.getStudentClass(userId));
  186. }
  187. @PostMapping("/import")
  188. @ApiOperation(value = "导入学生信息")
  189. public RT<Boolean> importData(@RequestParam("file") MultipartFile file) throws IOException, ParseException {
  190. List<Map<Integer, Object>> excelDataList = EasyExcel.read(file.getInputStream()).sheet().headRowNumber(3).doReadSync();
  191. return RT.ok(studentManagerService.importStudentData(excelDataList));
  192. }
  193. }