package com.xjrsoft.module.student.controller; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.github.yulichang.toolkit.MPJWrappers; import com.xjrsoft.common.enums.DeleteMark; import com.xjrsoft.common.enums.GenderDictionaryEnum; import com.xjrsoft.common.model.result.R; import com.xjrsoft.common.model.result.RT; import com.xjrsoft.common.page.ConventPage; import com.xjrsoft.common.page.PageOutput; import com.xjrsoft.common.utils.TreeUtil; import com.xjrsoft.common.utils.VoToColumnUtil; import com.xjrsoft.module.base.entity.BaseClass; import com.xjrsoft.module.base.entity.BaseGrade; import com.xjrsoft.module.base.service.IBaseClassService; import com.xjrsoft.module.base.service.IBaseGradeService; import com.xjrsoft.module.student.dto.AddBaseStudentUserDto; import com.xjrsoft.module.student.dto.BaseStudentUserPageDto; import com.xjrsoft.module.student.dto.UpdateBaseStudentUserDto; import com.xjrsoft.module.student.entity.BaseStudent; import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll; import com.xjrsoft.module.student.entity.BaseStudentUser; import com.xjrsoft.module.student.service.IStudentManagerService; import com.xjrsoft.module.student.vo.BaseStudentTreeVo; import com.xjrsoft.module.student.vo.BaseStudentUserPageVo; import com.xjrsoft.module.student.vo.BaseStudentUserVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @RestController @RequestMapping("/student" + "/studentmanager") @Api(value = "/student" + "/studentmanager", tags = "学生基本信息管理代码") @AllArgsConstructor public class StudentManagerController { private final IStudentManagerService studentManagerService; private final IBaseGradeService baseGradeService; private final IBaseClassService baseClassService; @GetMapping(value = "/page") @ApiOperation(value = "学生列表(分页)") @SaCheckPermission("studentmanager:detail") public R page(@Valid BaseStudentUserPageDto dto) { IPage page = studentManagerService.selectJoinListPage(ConventPage.getPage(dto), BaseStudentUserPageVo.class, MPJWrappers.lambdaJoin() .like(StrUtil.isNotBlank(dto.getUserName()), BaseStudentUser::getUserName, dto.getUserName()) .like(StrUtil.isNotBlank(dto.getMobile()), BaseStudentUser::getMobile, dto.getMobile()) .like(StrUtil.isNotBlank(dto.getName()), BaseStudentUser::getName, dto.getName()) .like(StrUtil.isNotBlank(dto.getEmail()), BaseStudentUser::getEmail, dto.getEmail()) .eq(ObjectUtil.isNotNull(dto.getClassId()), BaseClass::getId, dto.getClassId()) .in((ObjectUtil.isNotNull(dto.getTokenType()) && dto.getTokenType() == 1), BaseClass::getTeacherId, StpUtil.getLoginIdAsLong()) .orderByDesc(BaseStudentUser::getId) .select(BaseStudentUser::getId) .select(BaseStudent::getStudentId) .select(BaseStudentSchoolRoll.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentUserPageVo.class).contains(x.getProperty())) .select(BaseStudentUser.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentUserPageVo.class).contains(x.getProperty())) .select(BaseStudentSchoolRoll::getStudyYear) .select("a.`name` as grade_name") .select("b.`name` as major_set_name") .selectAs(BaseClass::getName, BaseStudentUserPageVo::getClassName) .innerJoin(BaseStudent.class, BaseStudent::getUserId, BaseStudentUser::getId) .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, BaseStudentUser::getId) .leftJoin("base_grade a on a.id = t2.grade_id") .leftJoin("base_major_set b on b.id = t2.major_set_id") .leftJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId) // .select("c.`mobile` as guardianPhone") // .select("c.`name` as guardianName") // .leftJoin("base_student_family_member c on c.user_id = t2.user_id") ); for (BaseStudentUserPageVo record : page.getRecords()) { record.setGenderCn(GenderDictionaryEnum.getValue(record.getGender())); } PageOutput pageOutput = ConventPage.getPageOutput(page, BaseStudentUserPageVo.class); return R.ok(pageOutput); } @GetMapping(value = "/tree") @ApiOperation(value = "学生年纪班级树") @SaCheckPermission("studentmanager:detail") public RT> tree() { List roleList = StpUtil.getRoleList(); long teacherId = StpUtil.getLoginIdAsLong(); List classList = baseClassService.list( new QueryWrapper().lambda().eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode()) ); List voList = new ArrayList<>(); List gradeList = baseGradeService.list( new QueryWrapper().lambda().eq(BaseGrade::getDeleteMark, DeleteMark.NODELETE.getCode()) ); if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){ Set gradeSet = new HashSet<>(); for (BaseClass baseClass : classList) { if(baseClass.getTeacherId().equals(teacherId)){ gradeSet.add(baseClass.getGradeId()); voList.add(new BaseStudentTreeVo(){{ setId(baseClass.getId()); setName(baseClass.getName()); setParentId(baseClass.getGradeId()); }}); } } gradeList.forEach((node) -> { if(gradeSet.contains(node.getId())){ voList.add(new BaseStudentTreeVo(){{ setId(node.getId()); setName(node.getName()); }}); } }); List treeVoList = TreeUtil.build(voList); return RT.ok(treeVoList); } gradeList.forEach((node) -> { voList.add(new BaseStudentTreeVo(){{ setId(node.getId()); setName(node.getName()); }}); }); classList.forEach((node)->{ voList.add(new BaseStudentTreeVo(){{ setId(node.getId()); setName(node.getName()); setParentId(node.getGradeId()); }}); }); List treeVoList = TreeUtil.build(voList); return RT.ok(treeVoList); } @GetMapping(value = "/info") @ApiOperation(value = "根据id查询学生信息") @SaCheckPermission("studentmanager:detail") public R info(@RequestParam Long id) { BaseStudentUser baseStudentUser = studentManagerService.getByIdDeep(id); if (baseStudentUser == null) { return R.error("找不到此数据!"); } BaseStudentUserVo userVo = BeanUtil.toBean(baseStudentUser, BaseStudentUserVo.class); userVo.setGenderCn(GenderDictionaryEnum.getValue(userVo.getGender())); return R.ok(userVo); } @PostMapping @ApiOperation(value = "新增学生") @SaCheckPermission("studentmanager:add") public R add(@Valid @RequestBody AddBaseStudentUserDto dto) { BaseStudentUser baseStudentUser = BeanUtil.toBean(dto, BaseStudentUser.class); return R.ok(studentManagerService.add(baseStudentUser)); } @PutMapping @ApiOperation(value = "修改学生") @SaCheckPermission("studentmanager:edit") public R update(@Valid @RequestBody UpdateBaseStudentUserDto dto) { BaseStudentUser baseStudentUser = BeanUtil.toBean(dto, BaseStudentUser.class); return R.ok(studentManagerService.update(baseStudentUser)); } @DeleteMapping @ApiOperation(value = "删除") @SaCheckPermission("studentmanager:delete") public R delete(@Valid @RequestBody List ids) { return R.ok(studentManagerService.delete(ids)); } @GetMapping(value = "/class") @ApiOperation(value = "获取当前学生的班主任") public R studentClass() { Long userId = StpUtil.getLoginIdAsLong(); return R.ok(studentManagerService.getStudentClass(userId)); } @PostMapping("/import") @ApiOperation(value = "导入学生信息") public RT importData(@RequestParam("file") MultipartFile file) throws IOException, ParseException { List> excelDataList = EasyExcel.read(file.getInputStream()).sheet().headRowNumber(3).doReadSync(); return RT.ok(studentManagerService.importStudentData(excelDataList)); } }