package com.qmth.distributed.print.api; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.qmth.boot.api.annotation.Aac; import com.qmth.boot.api.annotation.BOOL; import com.qmth.boot.api.constant.ApiConstant; import com.qmth.boot.api.exception.ApiException; import com.qmth.distributed.print.business.bean.params.LoginParam; import com.qmth.distributed.print.business.bean.result.DictionaryResult; import com.qmth.distributed.print.business.bean.result.EditResult; import com.qmth.distributed.print.business.entity.ExamPrintPlan; import com.qmth.distributed.print.business.entity.TBSyncTask; import com.qmth.distributed.print.business.enums.DictionaryEnum; import com.qmth.distributed.print.business.service.BasicVerifyCodeService; import com.qmth.distributed.print.business.service.ExamPrintPlanService; import com.qmth.distributed.print.business.service.PrintCommonService; import com.qmth.distributed.print.business.service.TBSyncTaskService; import com.qmth.teachcloud.common.bean.auth.AuthBean; import com.qmth.teachcloud.common.bean.result.LoginResult; import com.qmth.teachcloud.common.bean.result.UserLoginCheckResult; import com.qmth.teachcloud.common.config.DictionaryConfig; import com.qmth.teachcloud.common.contant.SystemConstant; import com.qmth.teachcloud.common.entity.*; import com.qmth.teachcloud.common.enums.*; import com.qmth.teachcloud.common.enums.userPush.SpecialPrivilegeEnum; import com.qmth.teachcloud.common.service.*; import com.qmth.teachcloud.common.util.Result; import com.qmth.teachcloud.common.util.ResultUtil; import com.qmth.teachcloud.common.util.ServletUtil; import io.swagger.annotations.*; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.validation.Valid; import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.stream.Collectors; /** * @Date: 2021/3/30. */ @Api(tags = "系统Controller") @RestController @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.common}") public class SysController { private final static Logger log = LoggerFactory.getLogger(SysController.class); @Autowired private SysUserService sysUserService; @Autowired private BasicVerifyCodeService basicVerifyCodeService; @Autowired private DictionaryConfig dictionaryConfig; @Resource CommonCacheService commonCacheService; @Resource PrintCommonService printCommonService; @Resource TBTaskService tbTaskService; @Resource BasicAttachmentService basicAttachmentService; @Autowired private SysUserRoleService sysUserRoleService; @Resource TeachcloudCommonService teachcloudCommonService; @Resource SysOrgService sysOrgService; @Resource BasicMajorService basicMajorService; @Resource BasicClazzService basicClazzService; @Resource BasicStudentService basicStudentService; @Resource BasicSemesterService basicSemesterService; @Resource ExamPrintPlanService examPrintPlanService; @Resource TBSyncTaskService tbSyncTaskService; /** * 登录 * * @param login * @return */ @ApiOperation(value = "登录") @RequestMapping(value = "/login", method = RequestMethod.POST) @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = LoginResult.class)}) @Aac(auth = BOOL.FALSE) public Result login(@ApiParam(value = "用户信息", required = true) @Valid @RequestBody LoginParam login, BindingResult bindingResult) throws NoSuchAlgorithmException { if (bindingResult.hasErrors()) { return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage()); } BasicSchool basicSchool = null; if (!login.getSchoolCode().equalsIgnoreCase(SystemConstant.ADMIN_CODE)) { basicSchool = commonCacheService.schoolCache(login.getSchoolCode()); if (Objects.isNull(basicSchool)) { throw ExceptionResultEnum.SCHOOL_NO_DATA.exception(); } if (Objects.nonNull(basicSchool.getEnable()) && !basicSchool.getEnable()) { throw ExceptionResultEnum.SCHOOL_ENABLE.exception(); } } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(SysUser::getLoginName, login.getLoginName()); if (!login.getSchoolCode().equalsIgnoreCase(SystemConstant.ADMIN_CODE)) { wrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId()); } List userList = sysUserService.list(wrapper); //用户不存在 if (Objects.isNull(userList) || userList.size() == 0) { throw ExceptionResultEnum.USER_NO_DATA.exception(); } if (login.getSchoolCode().equalsIgnoreCase(SystemConstant.ADMIN_CODE)) { userList.forEach(o -> { AuthBean authBean = teachcloudCommonService.getUserAuth(o.getId()); if (Objects.nonNull(authBean) && Objects.nonNull(authBean.getRoleList()) && authBean.getRoleList().size() > 0) { if (Objects.nonNull(authBean.getSchool())) { throw ExceptionResultEnum.ERROR.exception("用户无法通过当前页面登录"); } } }); } if (userList.size() > 1) { throw ExceptionResultEnum.ERROR.exception("查询的用户有多条"); } SysUser sysUser = userList.get(0); if (Objects.nonNull(sysUser.getSchoolId()) && sysUser.getSchoolId().longValue() != basicSchool.getId().longValue()) { throw ExceptionResultEnum.ERROR.exception("用户学校不匹配"); } // 校验验证码 sysUserService.checkSmsCode(sysUser.getId(), sysUser.getMobileNumber(), login.getCode()); LoginResult loginResult = teachcloudCommonService.login(login.getPassword(), sysUser, AppSourceEnum.SYSTEM); // 如果不是共用验证码再过期 if (!dictionaryConfig.smsDomain().getSmsNormalCode().equals(login.getCode())) { sysUserService.expiredVerifyCode(sysUser.getId(), sysUser.getMobileNumber()); } return ResultUtil.ok(loginResult); } /** * 登出 * * @return */ @ApiOperation(value = "登出") @RequestMapping(value = "/logout", method = RequestMethod.POST) @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)}) public Result logout() throws NoSuchAlgorithmException { SysUser sysUser = (SysUser) ServletUtil.getRequestUser(); teachcloudCommonService.removeUserInfo(sysUser.getId(), false); return ResultUtil.ok(); } /** * 发送验证码 * * @param loginParam * @return */ @ApiOperation(value = "发送验证码") @RequestMapping(value = "/get_verify_code", method = RequestMethod.POST) @Aac(auth = BOOL.FALSE) public Result getVerifyCode(@RequestBody LoginParam loginParam) { String loginName = loginParam.getLoginName(); String password = loginParam.getPassword(); QueryWrapper wrapper = new QueryWrapper<>(); if (StringUtils.isNotBlank(loginParam.getSchoolCode())) { BasicSchool basicSchool = commonCacheService.schoolCache(loginParam.getSchoolCode()); if (Objects.nonNull(basicSchool)) { wrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId()); } } wrapper.lambda().eq(SysUser::getLoginName, loginName); SysUser user = sysUserService.getOne(wrapper); //用户不存在 if (Objects.isNull(user)) { throw ExceptionResultEnum.ERROR.exception("用户不存在"); } if (!password.equals(user.getPassword())) { throw ExceptionResultEnum.ERROR.exception("密码错误"); } String mobileNumber = loginParam.getMobileNumber(); if (!SystemConstant.strNotNull(mobileNumber)) { mobileNumber = user.getMobileNumber(); } if (SystemConstant.strNotNull(mobileNumber)) { basicVerifyCodeService.sendVeirfyCode(mobileNumber, user.getId()); } int pwdCount = user.getPwdCount(); List sysRoleList = sysUserRoleService.listRoleByUserId(user.getId()); if (sysRoleList.stream().map(SysRole::getType).collect(Collectors.toSet()).contains(RoleTypeEnum.ADMIN)) { mobileNumber = user.getLoginName() + "(特权用户)"; pwdCount = 1; } UserLoginCheckResult userLoginCheckResult = new UserLoginCheckResult(user.getId(), mobileNumber, pwdCount); return ResultUtil.ok(userLoginCheckResult, ""); } @ApiOperation(value = "根据机构代码查询机构信息接口") @RequestMapping(value = "/school/query_by_school_code", method = RequestMethod.POST) @ApiResponses({@ApiResponse(code = 200, message = "学校信息", response = EditResult.class)}) @Aac(auth = BOOL.FALSE) public Result queryBySchoolCode(@ApiParam(value = "机构code", required = true) @RequestParam String code) { if (!code.equalsIgnoreCase(SystemConstant.ADMIN_CODE)) { BasicSchool basicSchool = commonCacheService.schoolCache(code); if (Objects.isNull(basicSchool)) { throw ExceptionResultEnum.SCHOOL_NO_DATA.exception(); } Map map = new HashMap<>(); map.put(SystemConstant.LOGO, basicSchool.getLogo()); map.put("name", basicSchool.getName()); return ResultUtil.ok(map); } else { Map map = new HashMap<>(); map.put(SystemConstant.LOGO, dictionaryConfig.sysDomain().getAdminLogoUrl()); map.put("name", null); return ResultUtil.ok(map); } } @ApiOperation(value = "文件上传接口") @RequestMapping(value = "/file/upload", method = RequestMethod.POST) @Transactional @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)}) public Result fileUpload(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file, @ApiParam(value = "上传文件类型", required = true) @RequestParam UploadFileEnum type) { BasicAttachment basicAttachment = null; try { basicAttachment = printCommonService.saveAttachment(file, ServletUtil.getRequestMd5(), type); if (Objects.isNull(basicAttachment)) { throw ExceptionResultEnum.ATTACHMENT_ERROR.exception(); } } catch (Exception e) { log.error("请求出错", e); if (Objects.nonNull(basicAttachment)) { basicAttachmentService.deleteAttachment(basicAttachment); } if (e instanceof ApiException) { ResultUtil.error((ApiException) e, e.getMessage()); } else { ResultUtil.error(e.getMessage()); } } return ResultUtil.ok(new EditResult(basicAttachment.getId(), teachcloudCommonService.filePreview(basicAttachment.getPath()), basicAttachment.getPages())); } @ApiOperation(value = "文件下载接口") @RequestMapping(value = "/file/download", method = RequestMethod.POST) @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)}) public Result fileDownload(@ApiParam(value = "任务id", required = true) @RequestParam String id, @ApiParam(value = "下载文件类型", required = true) @RequestParam DownloadFileEnum type) { String path = null; if (DownloadFileEnum.SYNC_REPORT.equals(type)){ TBSyncTask tbSyncTask = tbSyncTaskService.getById(SystemConstant.convertIdToLong(id)); if (Objects.isNull(tbSyncTask)){ throw ExceptionResultEnum.SYNC_TASK_NO_DATA.exception(); } path = tbSyncTask.getReportFilePath(); }else { TBTask tbTask = tbTaskService.getById(Long.parseLong(id)); if (Objects.isNull(tbTask)) { throw ExceptionResultEnum.TASK_NO_DATA.exception(); } switch (type) { case IMPORT_FILE: path = tbTask.getImportFilePath(); break; case TASK_REPORT: path = tbTask.getReportFilePath(); break; case RESULT: path = tbTask.getResultFilePath(); break; } } if (Objects.isNull(path)) { throw ExceptionResultEnum.PATH_NO_DATA.exception(); } return ResultUtil.ok(new EditResult(teachcloudCommonService.filePreview(path))); } @ApiOperation(value = "文件预览接口") @RequestMapping(value = "/file/preview", method = RequestMethod.POST) @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)}) public Result filePreview(@ApiParam(value = "附件id", required = false, defaultValue = "") @RequestParam(value = "id", required = false, defaultValue = "") String id) { if (StringUtils.isBlank(id)) { throw ExceptionResultEnum.ERROR.exception("没有附件"); } else { BasicAttachment basicAttachment = basicAttachmentService.getById(Long.parseLong(id)); return ResultUtil.ok(new EditResult(teachcloudCommonService.filePreview(basicAttachment.getPath()))); } } @ApiOperation(value = "查询用户权限") @RequestMapping(value = "/get_menu", method = RequestMethod.POST) public Result getMenu() { return ResultUtil.ok(sysUserRoleService.listByUserId()); } @ApiOperation(value = "获取服务器时间") @RequestMapping(value = "/get_system_time", method = RequestMethod.POST) @Aac(auth = BOOL.FALSE) public Result getSystemTime() { return ResultUtil.ok(System.currentTimeMillis()); } @ApiOperation(value = "获取系统编码") @RequestMapping(value = "/get_code", method = RequestMethod.POST) // @Aac(auth = BOOL.FALSE) public Result getCode(@ApiParam(value = "编码类型", required = true) @RequestParam SystemCodeEnum type) { SysUser sysUser = (SysUser) ServletUtil.getRequestUser(); String number = teachcloudCommonService.getSysIncrCode(type, sysUser); return ResultUtil.ok((Object) number); } @ApiOperation(value = "共用接口-查询字典") @RequestMapping(value = "/get_dictionary", method = RequestMethod.POST) @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)}) public Result findDictionaryList(@ApiParam(value = "学期id") @RequestParam(required = false) String semesterId, @ApiParam(value = "学院id") @RequestParam(required = false) String collegeId, @ApiParam(value = "专业id") @RequestParam(required = false) String majorId, @ApiParam(value = "班级id") @RequestParam(required = false) String clazzId, @ApiParam(value = "学生id") @RequestParam(required = false) String studentId, @ApiParam(value = "查询字典对象") @RequestParam(required = true) DictionaryEnum dictionaryEnum) { Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString()); List dictionaryResultList = new ArrayList<>(); switch (dictionaryEnum) { case SEMESTER: List basicSemesterList = basicSemesterService.list(new QueryWrapper().lambda().eq(BasicSemester::getSchoolId, schoolId).eq(BasicSemester::getEnable, true)); dictionaryResultList = basicSemesterList.stream().map(e -> { DictionaryResult dictionaryResult = new DictionaryResult(); dictionaryResult.setId(e.getId()); dictionaryResult.setCode(e.getCode()); dictionaryResult.setName(e.getName()); return dictionaryResult; }).collect(Collectors.toList()); break; case COLLEGE: if (Objects.nonNull(semesterId)) { QueryWrapper examPrintPlanQueryWrapper = new QueryWrapper<>(); examPrintPlanQueryWrapper.select(" DISTINCT org_id as orgId ") .eq("semester_id", SystemConstant.convertIdToLong(semesterId)); List examPrintPlanList = examPrintPlanService.list(examPrintPlanQueryWrapper); Set orgIdSet = examPrintPlanList.stream().map(s -> s.getOrgId()).collect(Collectors.toSet()); QueryWrapper sysOrgQueryWrapper = new QueryWrapper<>(); sysOrgQueryWrapper.lambda().in(SysOrg::getId, orgIdSet); List sysOrgList = sysOrgService.list(sysOrgQueryWrapper); dictionaryResultList = sysOrgList.stream().map(e -> { DictionaryResult dictionaryResult = new DictionaryResult(); dictionaryResult.setId(e.getId()); dictionaryResult.setCode(e.getCode()); dictionaryResult.setName(e.getName()); return dictionaryResult; }).collect(Collectors.toList()); } else { List sysOrgList = sysOrgService.list(new QueryWrapper().lambda().eq(SysOrg::getSchoolId, schoolId).eq(SysOrg::getEnable, true)); dictionaryResultList = sysOrgList.stream().map(e -> { DictionaryResult dictionaryResult = new DictionaryResult(); dictionaryResult.setId(e.getId()); dictionaryResult.setCode(e.getCode()); dictionaryResult.setName(e.getName()); return dictionaryResult; }).collect(Collectors.toList()); } break; case MAJOR: QueryWrapper majorQueryWrapper = new QueryWrapper<>(); majorQueryWrapper.lambda() .eq(BasicMajor::getSchoolId, schoolId) .eq(BasicMajor::getEnable, true); if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(collegeId))) { majorQueryWrapper.lambda().eq(BasicMajor::getBelongOrgId, collegeId); } List basicMajorList = basicMajorService.list(majorQueryWrapper); dictionaryResultList = basicMajorList.stream().map(e -> { DictionaryResult dictionaryResult = new DictionaryResult(); dictionaryResult.setId(e.getId()); dictionaryResult.setCode(e.getCode()); dictionaryResult.setName(e.getName()); return dictionaryResult; }).collect(Collectors.toList()); break; case CLAZZ: QueryWrapper clazzQueryWrapper = new QueryWrapper<>(); clazzQueryWrapper.lambda().eq(BasicClazz::getSchoolId, schoolId).eq(BasicClazz::getEnable, true); if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(majorId))) { clazzQueryWrapper.lambda().eq(BasicClazz::getMajorId, majorId); } List basicClazzList = basicClazzService.list(clazzQueryWrapper); dictionaryResultList = basicClazzList.stream().map(e -> { DictionaryResult dictionaryResult = new DictionaryResult(); dictionaryResult.setId(e.getId()); dictionaryResult.setCode(e.getClazzCode()); dictionaryResult.setName(e.getClazzName()); return dictionaryResult; }).collect(Collectors.toList()); break; case STUDENT: QueryWrapper studentQueryWrapper = new QueryWrapper<>(); studentQueryWrapper.lambda().eq(BasicStudent::getSchoolId, schoolId).eq(BasicStudent::getEnable, true); if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(collegeId))) { studentQueryWrapper.lambda().eq(BasicStudent::getBelongOrgId, collegeId); } if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(majorId))) { studentQueryWrapper.lambda().eq(BasicStudent::getMajorId, majorId); } if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(clazzId))) { studentQueryWrapper.lambda().eq(BasicStudent::getClazzId, clazzId); } if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(studentId))) { studentQueryWrapper.lambda().eq(BasicStudent::getId, studentId); } List basicStudentList = basicStudentService.list(studentQueryWrapper); dictionaryResultList = basicStudentList.stream().map(e -> { DictionaryResult dictionaryResult = new DictionaryResult(); dictionaryResult.setId(e.getId()); dictionaryResult.setCode(e.getStudentCode()); dictionaryResult.setName(e.getStudentName()); return dictionaryResult; }).collect(Collectors.toList()); break; } return ResultUtil.ok(dictionaryResultList); } /** * 获取用户阅卷角色 * * @return */ @ApiOperation(value = "获取用户阅卷角色") @RequestMapping(value = "/get_open_role", method = RequestMethod.POST) public Result getOpenRole() { SysUser sysUser = (SysUser) ServletUtil.getRequestUser(); SpecialPrivilegeEnum userSpecialPrivilege = sysUserService.findUserSpecialPrivilegeByUserId(sysUser.getId()); List list = new ArrayList<>(); if (!SpecialPrivilegeEnum.UNIDENTIFIED.equals(userSpecialPrivilege)) { if (SpecialPrivilegeEnum.COMPOSITE.equals(userSpecialPrivilege)) { list.add(SpecialPrivilegeEnum.MARKER); list.add(SpecialPrivilegeEnum.SUBJECT_HEADER); } else { list.add(userSpecialPrivilege); } } return ResultUtil.ok(list); } }