ScheduleController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.xjrsoft.module.schedule.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.excel.support.ExcelTypeEnum;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.google.gson.JsonArray;
  10. import com.google.gson.JsonObject;
  11. import com.google.gson.JsonParser;
  12. import com.xjrsoft.common.enums.DeleteMark;
  13. import com.xjrsoft.common.enums.EnabledMark;
  14. import com.xjrsoft.common.model.result.RT;
  15. import com.xjrsoft.common.utils.VoToColumnUtil;
  16. import com.xjrsoft.module.base.entity.BaseSemester;
  17. import com.xjrsoft.module.base.service.IBaseSemesterService;
  18. import com.xjrsoft.module.courseTable.entity.CourseTable;
  19. import com.xjrsoft.module.courseTable.service.ICourseTableService;
  20. import com.xjrsoft.module.schedule.dto.CourseTableAdjustDto;
  21. import com.xjrsoft.module.schedule.dto.CourseTableDto;
  22. import com.xjrsoft.module.schedule.dto.CourseTablePreCheckDto;
  23. import com.xjrsoft.module.schedule.dto.ScheduleWeekExportQueryDto;
  24. import com.xjrsoft.module.schedule.entity.CourseReceiveMsg;
  25. import com.xjrsoft.module.schedule.service.ICourseReceiveMsgService;
  26. import com.xjrsoft.module.schedule.util.ScheduleUtil;
  27. import com.xjrsoft.module.schedule.vo.CourseListVo;
  28. import com.xjrsoft.module.schedule.vo.CourseTableVo;
  29. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  30. import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
  31. import com.xjrsoft.module.teacher.entity.BaseTeacher;
  32. import com.xjrsoft.module.teacher.entity.XjrUser;
  33. import com.xjrsoft.module.teacher.service.IBaseTeacherService;
  34. import com.xjrsoft.module.teacher.service.ITeacherbaseManagerService;
  35. import io.swagger.annotations.Api;
  36. import io.swagger.annotations.ApiOperation;
  37. import lombok.AllArgsConstructor;
  38. import org.springframework.http.ResponseEntity;
  39. import org.springframework.web.bind.annotation.GetMapping;
  40. import org.springframework.web.bind.annotation.PostMapping;
  41. import org.springframework.web.bind.annotation.RequestBody;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RequestParam;
  44. import org.springframework.web.bind.annotation.RestController;
  45. import javax.validation.Valid;
  46. import java.io.ByteArrayOutputStream;
  47. import java.time.DayOfWeek;
  48. import java.time.Duration;
  49. import java.time.LocalDateTime;
  50. import java.time.ZoneId;
  51. import java.time.format.DateTimeFormatter;
  52. import java.util.Date;
  53. import java.util.List;
  54. /**
  55. * @title: 课表
  56. * @Author dzx
  57. * @Date: 2023-12-27
  58. * @Version 1.0
  59. */
  60. @RestController
  61. @RequestMapping("/schedule" + "/schedule")
  62. @Api(value = "/schedule" + "/schedule",tags = "课表代码")
  63. @AllArgsConstructor
  64. public class ScheduleController {
  65. private final ICourseReceiveMsgService courseReceiveMsgService;
  66. // private final IJianyueDataService jianyueDataService;
  67. private final ICourseTableService courseTableService;
  68. private final IBaseStudentSchoolRollService baseStudentSchoolRollService;
  69. private final IBaseTeacherService baseTeacherService;
  70. private final ITeacherbaseManagerService teacherService;
  71. private final IBaseSemesterService semesterService;
  72. @GetMapping(value = "/receive-msg")
  73. @ApiOperation(value="接收消息")
  74. @SaCheckPermission("room:detail")
  75. public RT<CourseReceiveMsg> receiveMsg(@RequestParam String schoolId, @RequestParam String eduYearSerialNo, @RequestParam String startDate, @RequestParam String endDate) throws Exception {
  76. CourseReceiveMsg courseReceiveMsg = new CourseReceiveMsg();
  77. courseReceiveMsg.setCreateDate(new Date());
  78. courseReceiveMsg.setSchoolId(schoolId);
  79. courseReceiveMsg.setEduYearSerialNo(eduYearSerialNo);
  80. courseReceiveMsg.setStartDate(startDate);
  81. courseReceiveMsg.setEndDate(endDate);
  82. courseReceiveMsg.setDeleteMark(DeleteMark.NODELETE.getCode());
  83. courseReceiveMsg.setEnabledMark(EnabledMark.ENABLED.getCode());
  84. courseReceiveMsgService.save(courseReceiveMsg);
  85. return RT.ok(courseReceiveMsg);
  86. }
  87. @GetMapping(value = "/course-table")
  88. @ApiOperation(value="课表接口(PC端)")
  89. @SaCheckPermission("room:detail")
  90. public RT<CourseTableVo> courseInfo(CourseTableDto dto){
  91. if(dto.getSemesterId() == null){
  92. LambdaQueryWrapper<BaseSemester> queryWrapper = new LambdaQueryWrapper<>();
  93. queryWrapper
  94. .orderByDesc(BaseSemester::getStartDate)
  95. .select(BaseSemester.class,x -> VoToColumnUtil.fieldsToColumns(BaseSemester.class).contains(x.getProperty()));
  96. List<BaseSemester> semesterList = semesterService.list(queryWrapper);
  97. if(!semesterList.isEmpty()){
  98. dto.setSemesterId(semesterList.get(0).getId());
  99. }
  100. }
  101. CourseTableVo list = courseTableService.getList(dto);
  102. return RT.ok(list);
  103. }
  104. @GetMapping(value = "/course-table-day")
  105. @ApiOperation(value="今日课表接口(手机端)")
  106. @SaCheckPermission("room:detail")
  107. public RT<CourseTableVo> courseInfoDay(CourseTableDto dto){
  108. LocalDateTime now = LocalDateTime.now();
  109. DayOfWeek dayOfWeek = now.getDayOfWeek();
  110. dto.setWeekDay(dayOfWeek.getValue());
  111. dto.setToDay(now);
  112. if(ObjectUtil.isNotNull(dto.getStudentId())){
  113. List<BaseStudentSchoolRoll> schoolRolls = baseStudentSchoolRollService.list(
  114. new QueryWrapper<BaseStudentSchoolRoll>().lambda().eq(BaseStudentSchoolRoll::getUserId, dto.getStudentId())
  115. );
  116. if(schoolRolls != null && !schoolRolls.isEmpty()){
  117. dto.setCourseType("class");
  118. dto.setClassId(schoolRolls.get(0).getClassId());
  119. }
  120. }else{
  121. // 查询登录者身份
  122. long loginIdAsLong = StpUtil.getLoginIdAsLong();
  123. List<BaseStudentSchoolRoll> schoolRolls = baseStudentSchoolRollService.list(
  124. new QueryWrapper<BaseStudentSchoolRoll>().lambda().eq(BaseStudentSchoolRoll::getUserId, loginIdAsLong)
  125. );
  126. if(schoolRolls != null && !schoolRolls.isEmpty()){
  127. dto.setCourseType("class");
  128. dto.setClassId(schoolRolls.get(0).getClassId());
  129. }
  130. List<BaseTeacher> teachers = baseTeacherService.list(new QueryWrapper<BaseTeacher>().lambda().eq(BaseTeacher::getUserId, loginIdAsLong));
  131. if(teachers != null && !teachers.isEmpty()){
  132. dto.setCourseType("teacher");
  133. dto.setTeacherId(loginIdAsLong);
  134. }
  135. }
  136. CourseTableVo list = courseTableService.getList(dto);
  137. return RT.ok(list);
  138. }
  139. @GetMapping(value = "/current-week")
  140. @ApiOperation(value="获取当前周次")
  141. @SaCheckPermission("room:detail")
  142. public RT<Integer> currentWeek(CourseTableDto dto){
  143. List<BaseSemester> semesterList = semesterService.list(new QueryWrapper<BaseSemester>().lambda().orderByDesc(BaseSemester::getStartDate));
  144. BaseSemester baseSemester = semesterList.get(0);
  145. LocalDateTime now = LocalDateTime.now();
  146. //计算本周是第几周
  147. LocalDateTime startDateTime = LocalDateTime.ofInstant(baseSemester.getStartDate().toInstant(), ZoneId.systemDefault());
  148. LocalDateTime endDateTime = LocalDateTime.ofInstant(baseSemester.getEndDate().toInstant(), ZoneId.systemDefault());
  149. Duration between = Duration.between(startDateTime, endDateTime);
  150. long days = between.toDays();
  151. int weeks = (int) Math.ceil((double) days / 7);
  152. if (dto.getWeek() == null) {
  153. for (int i = 0; i < weeks; i++) {
  154. LocalDateTime startDate = startDateTime.plusDays(i * 6).withHour(0).withMinute(0).withSecond(0).withNano(0);
  155. LocalDateTime endDate = startDateTime.plusDays((i + 1) * 6).withHour(23).withMinute(59).withSecond(59).withNano(9999);
  156. if (now.isAfter(startDate) && now.isBefore(endDate)) {
  157. return RT.ok(i + 1);
  158. }
  159. }
  160. }
  161. return RT.error("未能查询到当前周次");
  162. }
  163. @GetMapping(value = "/adjust-list")
  164. @ApiOperation(value = "可以调课的课程")
  165. @SaCheckPermission("evaluateobject:detail")
  166. public RT<List<CourseListVo>> adjustList(CourseTableAdjustDto dto){
  167. // if(dto.getAdjustDate() == null || dto.getTeacherId() == null){
  168. // return RT.error("请传入调整日期和教师id");
  169. // }
  170. // List<CourseListVo> list = courseTableService.getAdjustList(dto);
  171. // if(list.isEmpty()){
  172. // return RT.error("暂无可调课程");
  173. // }
  174. return RT.ok();
  175. }
  176. @GetMapping(value = "/pre-check")
  177. @ApiOperation(value = "预检查")
  178. @SaCheckPermission("evaluateobject:detail")
  179. public RT<String> preCheck(CourseTablePreCheckDto dto) throws Exception {
  180. if(dto.getPreCheckType() != null){
  181. if(dto.getPreCheckType() == 1){
  182. CourseTable courseTable = courseTableService.getById(dto.getCourseId());
  183. CourseTable swapCourseTable = courseTableService.getById(dto.getSwapCourseId());
  184. JsonObject preCheck = getExtendPreCheck(dto, courseTable, swapCourseTable);
  185. if(preCheck.get("code").getAsInt() != 0){
  186. return RT.error(preCheck.get("msg").getAsString());
  187. }
  188. }else if(dto.getPreCheckType() == 2){
  189. CourseTable courseTable = courseTableService.getById(dto.getCourseId());
  190. JsonObject jsonObject = substitutePreTestin(dto, courseTable);
  191. if(jsonObject.get("code").getAsInt() != 0){
  192. return RT.error(jsonObject.get("msg").getAsString());
  193. }
  194. }
  195. }
  196. return RT.ok("ok");
  197. }
  198. /**
  199. * 顶课预检查
  200. * @param dto
  201. * @param courseTable
  202. * @return 检查结果
  203. */
  204. JsonObject substitutePreTestin(CourseTablePreCheckDto dto, CourseTable courseTable) throws Exception {
  205. JsonParser jsonParser = new JsonParser();
  206. String url = ScheduleUtil.apiUrl + "RescheduleApply/Extend/Substitute/PreTesting";
  207. JsonObject jsonObject = new JsonObject();
  208. jsonObject.addProperty("timetableId", courseTable.getJianyueId());
  209. jsonObject.addProperty("isCycles", Boolean.FALSE);
  210. JsonArray extendIds = new JsonArray();
  211. extendIds.add(dto.getSubTeacherId());
  212. jsonObject.add("extendIds", extendIds);
  213. //获取时间戳
  214. long timestamp = System.currentTimeMillis();
  215. //生成签名
  216. String sign = ScheduleUtil.createSign(timestamp);
  217. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timestamp);
  218. if(StrUtil.isEmpty(result)){
  219. return null;
  220. }
  221. return jsonParser.parse(result).getAsJsonObject();
  222. }
  223. /**
  224. * 调课预检查
  225. * @param courseTable 需要调整的课程
  226. * @param swapCourseTable 对调的课程
  227. * @return 检查结果
  228. */
  229. JsonObject getExtendPreCheck(CourseTablePreCheckDto dto, CourseTable courseTable, CourseTable swapCourseTable) throws Exception {
  230. JsonParser jsonParser = new JsonParser();
  231. String url = ScheduleUtil.apiUrl + "RescheduleApply/Extend/PreTesting";
  232. JsonObject jsonObject = new JsonObject();
  233. jsonObject.addProperty("timetableId", courseTable.getJianyueId());
  234. jsonObject.addProperty("isCycles", Boolean.FALSE);
  235. // jsonObject.addProperty("startDate", "2024-01-01");
  236. // jsonObject.addProperty("endDate", "2024-01-31");
  237. // jsonObject.addProperty("dayOfweek", 5);
  238. jsonObject.addProperty("numberOfday", swapCourseTable.getTimeNumber());
  239. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  240. jsonObject.addProperty("date", dto.getSwapDate().format(formatter));
  241. jsonObject.addProperty("reschduleId", courseTable.getJianyueId());
  242. //获取时间戳
  243. long timestamp = System.currentTimeMillis();
  244. //生成签名
  245. String sign = ScheduleUtil.createSign(timestamp);
  246. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timestamp);
  247. if(StrUtil.isEmpty(result)){
  248. return null;
  249. }
  250. return jsonParser.parse(result).getAsJsonObject();
  251. }
  252. @PostMapping("/schedule-week-export-query")
  253. @ApiOperation(value = "按周导出课表")
  254. public ResponseEntity<byte[]> scheduleWeekExportQuery(@Valid @RequestBody ScheduleWeekExportQueryDto dto) {
  255. // @GetMapping("/schedule-week-export-query")
  256. // @ApiOperation(value = "按周导出课表")
  257. // public ResponseEntity<byte[]> textbookClaimExportQuery(@Valid ScheduleWeekExportQueryDto dto) {
  258. ByteArrayOutputStream bot = courseTableService.listScheduleWeekExportQuery(dto);
  259. return RT.fileStream(bot.toByteArray(), "scheduleWeek" + ExcelTypeEnum.XLSX.getValue());
  260. }
  261. @GetMapping(value = "/login-url")
  262. @ApiOperation(value = "获取单点登录地址")
  263. @SaCheckPermission("evaluateobject:detail")
  264. public RT<String> getAccessToken() throws Exception {
  265. XjrUser xjrUser = teacherService.getById(StpUtil.getLoginIdAsLong());
  266. long timeMillis = System.currentTimeMillis();
  267. String sign = ScheduleUtil.createSign(timeMillis);
  268. JsonObject jsonObject = new JsonObject();
  269. jsonObject.addProperty("mobileNo", xjrUser.getMobile());
  270. String url = ScheduleUtil.apiUrl + "auth/user/token";
  271. try {
  272. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timeMillis);
  273. JsonParser parser = new JsonParser();
  274. JsonObject resultJson = parser.parse(result).getAsJsonObject();
  275. if(resultJson.get("code").getAsInt() != 0){
  276. return RT.error("无登录权限");
  277. }
  278. JsonObject dataJson = resultJson.get("data").getAsJsonObject();
  279. String loginUrl = ScheduleUtil.hostUrl + "?access_token=" + dataJson.get("access_token").getAsString();
  280. return RT.ok(loginUrl);
  281. }catch (Exception e){
  282. return RT.error(e.getMessage());
  283. }
  284. }
  285. }