ScheduleController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.QueryWrapper;
  8. import com.google.gson.JsonArray;
  9. import com.google.gson.JsonObject;
  10. import com.google.gson.JsonParser;
  11. import com.xjrsoft.common.enums.DeleteMark;
  12. import com.xjrsoft.common.enums.EnabledMark;
  13. import com.xjrsoft.common.model.result.RT;
  14. import com.xjrsoft.module.base.entity.BaseSemester;
  15. import com.xjrsoft.module.base.service.IBaseSemesterService;
  16. import com.xjrsoft.module.courseTable.entity.ClassTime;
  17. import com.xjrsoft.module.courseTable.entity.CourseTable;
  18. import com.xjrsoft.module.courseTable.service.IClassTimeService;
  19. import com.xjrsoft.module.courseTable.service.ICourseTableService;
  20. import com.xjrsoft.module.schedule.dto.ClassOptionDto;
  21. import com.xjrsoft.module.schedule.dto.CourseTableAdjustDto;
  22. import com.xjrsoft.module.schedule.dto.CourseTableDto;
  23. import com.xjrsoft.module.schedule.dto.CourseTablePreCheckDto;
  24. import com.xjrsoft.module.schedule.dto.ScheduleWeekDto;
  25. import com.xjrsoft.module.schedule.dto.WfCourseAdjustDto;
  26. import com.xjrsoft.module.schedule.entity.CourseReceiveMsg;
  27. import com.xjrsoft.module.schedule.service.ICourseReceiveMsgService;
  28. import com.xjrsoft.module.schedule.service.IWfCourseAdjustService;
  29. import com.xjrsoft.module.schedule.util.ScheduleUtil;
  30. import com.xjrsoft.module.schedule.vo.ClassOptionVo;
  31. import com.xjrsoft.module.schedule.vo.CourseListVo;
  32. import com.xjrsoft.module.schedule.vo.CourseTableVo;
  33. import com.xjrsoft.module.schedule.vo.ScheduleWeekVo;
  34. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  35. import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
  36. import com.xjrsoft.module.teacher.entity.BaseTeacher;
  37. import com.xjrsoft.module.teacher.entity.XjrUser;
  38. import com.xjrsoft.module.teacher.service.IBaseTeacherService;
  39. import com.xjrsoft.module.teacher.service.ITeacherbaseManagerService;
  40. import io.swagger.annotations.Api;
  41. import io.swagger.annotations.ApiOperation;
  42. import lombok.AllArgsConstructor;
  43. import org.springframework.http.ResponseEntity;
  44. import org.springframework.web.bind.annotation.GetMapping;
  45. import org.springframework.web.bind.annotation.PostMapping;
  46. import org.springframework.web.bind.annotation.RequestBody;
  47. import org.springframework.web.bind.annotation.RequestMapping;
  48. import org.springframework.web.bind.annotation.RequestParam;
  49. import org.springframework.web.bind.annotation.RestController;
  50. import javax.validation.Valid;
  51. import java.io.ByteArrayOutputStream;
  52. import java.time.DayOfWeek;
  53. import java.time.Duration;
  54. import java.time.LocalDateTime;
  55. import java.time.ZoneId;
  56. import java.time.format.DateTimeFormatter;
  57. import java.util.ArrayList;
  58. import java.util.Arrays;
  59. import java.util.Date;
  60. import java.util.List;
  61. /**
  62. * @title: 课表
  63. * @Author dzx
  64. * @Date: 2023-12-27
  65. * @Version 1.0
  66. */
  67. @RestController
  68. @RequestMapping("/schedule" + "/schedule")
  69. @Api(value = "/schedule" + "/schedule",tags = "课表代码")
  70. @AllArgsConstructor
  71. public class ScheduleController {
  72. private final ICourseReceiveMsgService courseReceiveMsgService;
  73. // private final IJianyueDataService jianyueDataService;
  74. private final ICourseTableService courseTableService;
  75. private final IBaseStudentSchoolRollService baseStudentSchoolRollService;
  76. private final IBaseTeacherService baseTeacherService;
  77. private final ITeacherbaseManagerService teacherService;
  78. private final IBaseSemesterService semesterService;
  79. private final IWfCourseAdjustService courseAdjustService;
  80. private final IClassTimeService classTimeService;
  81. @GetMapping(value = "/receive-msg")
  82. @ApiOperation(value="接收消息")
  83. @SaCheckPermission("schedule:detail")
  84. public RT<CourseReceiveMsg> receiveMsg(@RequestParam String schoolId, @RequestParam String eduYearSerialNo, @RequestParam String startDate, @RequestParam String endDate) throws Exception {
  85. Date date = new Date();
  86. CourseReceiveMsg courseReceiveMsg = new CourseReceiveMsg();
  87. courseReceiveMsg.setCreateDate(date);
  88. courseReceiveMsg.setSchoolId(schoolId);
  89. courseReceiveMsg.setEduYearSerialNo(eduYearSerialNo);
  90. courseReceiveMsg.setStartDate(startDate);
  91. courseReceiveMsg.setEndDate(endDate);
  92. courseReceiveMsg.setDeleteMark(DeleteMark.NODELETE.getCode());
  93. courseReceiveMsg.setEnabledMark(EnabledMark.ENABLED.getCode());
  94. courseReceiveMsgService.save(courseReceiveMsg);
  95. courseReceiveMsgService.noticeOverseer(eduYearSerialNo, date);
  96. return RT.ok(courseReceiveMsg);
  97. }
  98. @GetMapping(value = "/course-table")
  99. @ApiOperation(value="课表接口(PC端)")
  100. @SaCheckPermission("schedule:detail")
  101. public RT<CourseTableVo> courseInfo(CourseTableDto dto){
  102. if(dto.getSemesterId() == null){
  103. BaseSemester semester = semesterService.getCurrentSemester();
  104. if(semester != null){
  105. dto.setSemesterId(semester.getId());
  106. }
  107. }
  108. CourseTableVo list = courseTableService.getList(dto);
  109. return RT.ok(list);
  110. }
  111. @GetMapping(value = "/course-table-day")
  112. @ApiOperation(value="今日课表接口(手机端)")
  113. @SaCheckPermission("schedule:detail")
  114. public RT<CourseTableVo> courseInfoDay(CourseTableDto dto){
  115. LocalDateTime now = LocalDateTime.now();
  116. DayOfWeek dayOfWeek = now.getDayOfWeek();
  117. dto.setWeekDay(dayOfWeek.getValue());
  118. dto.setToDay(now);
  119. if(ObjectUtil.isNotNull(dto.getStudentId())){
  120. List<BaseStudentSchoolRoll> schoolRolls = baseStudentSchoolRollService.list(
  121. new QueryWrapper<BaseStudentSchoolRoll>().lambda().eq(BaseStudentSchoolRoll::getUserId, dto.getStudentId())
  122. );
  123. if(schoolRolls != null && !schoolRolls.isEmpty()){
  124. dto.setCourseType("class");
  125. dto.setClassId(schoolRolls.get(0).getClassId());
  126. }
  127. }else{
  128. // 查询登录者身份
  129. long loginIdAsLong = StpUtil.getLoginIdAsLong();
  130. List<BaseStudentSchoolRoll> schoolRolls = baseStudentSchoolRollService.list(
  131. new QueryWrapper<BaseStudentSchoolRoll>().lambda().eq(BaseStudentSchoolRoll::getUserId, loginIdAsLong)
  132. );
  133. if(schoolRolls != null && !schoolRolls.isEmpty()){
  134. dto.setCourseType("class");
  135. dto.setClassId(schoolRolls.get(0).getClassId());
  136. }
  137. List<BaseTeacher> teachers = baseTeacherService.list(new QueryWrapper<BaseTeacher>().lambda().eq(BaseTeacher::getUserId, loginIdAsLong));
  138. if(teachers != null && !teachers.isEmpty()){
  139. dto.setCourseType("teacher");
  140. dto.setTeacherId(loginIdAsLong);
  141. }
  142. }
  143. CourseTableVo list = courseTableService.getList(dto);
  144. return RT.ok(list);
  145. }
  146. @GetMapping(value = "/current-week")
  147. @ApiOperation(value="获取当前周次")
  148. @SaCheckPermission("schedule:detail")
  149. public RT<Integer> currentWeek(CourseTableDto dto){
  150. BaseSemester baseSemester = semesterService.getCurrentSemester();
  151. if(baseSemester == null){
  152. return RT.ok(0);
  153. }
  154. LocalDateTime now = LocalDateTime.now();
  155. if(dto.getScheduleDate() != null){
  156. now = dto.getScheduleDate();
  157. }
  158. //计算本周是第几周
  159. LocalDateTime startDateTime = LocalDateTime.ofInstant(baseSemester.getTeachingStart().toInstant(), ZoneId.systemDefault());
  160. LocalDateTime endDateTime = LocalDateTime.ofInstant(baseSemester.getTeachingEnd().toInstant(), ZoneId.systemDefault());
  161. Duration between = Duration.between(startDateTime, endDateTime);
  162. long days = between.toDays();
  163. int weeks = (int) Math.ceil((double) days / 7);
  164. if (dto.getWeek() == null) {
  165. for (int i = 0; i < weeks; i++) {
  166. LocalDateTime startDate = startDateTime.plusDays(i * 7).withHour(0).withMinute(0).withSecond(0).withNano(0);
  167. LocalDateTime endDate = startDate.plusDays(6).withHour(23).withMinute(59).withSecond(59).withNano(9999);
  168. if (now.isAfter(startDate) && now.isBefore(endDate)) {
  169. return RT.ok(i + 1);
  170. }
  171. }
  172. }
  173. return RT.ok(0);
  174. }
  175. @GetMapping(value = "/adjust-list")
  176. @ApiOperation(value = "可以调课的课程")
  177. @SaCheckPermission("schedule:detail")
  178. public RT<List<CourseListVo>> adjustList(CourseTableAdjustDto dto){
  179. if(dto.getAdjustDate() == null || dto.getTeacherId() == null){
  180. return RT.error("请传入调整日期和教师id");
  181. }
  182. List<CourseListVo> list = courseTableService.getAdjustList(dto.getTeacherId().toString(), dto.getAdjustDate(), dto.getClassId(), dto.getAdjustType());
  183. if(list.isEmpty()){
  184. return RT.error("暂无可调课程");
  185. }
  186. return RT.ok();
  187. }
  188. @GetMapping(value = "/pre-check")
  189. @ApiOperation(value = "预检查")
  190. @SaCheckPermission("schedule:detail")
  191. public RT<String> preCheck(CourseTablePreCheckDto dto) throws Exception {
  192. if(dto.getPreCheckType() != null){
  193. if(dto.getPreCheckType() == 1){
  194. for (int i = 0; i < dto.getCourseIds().size(); i ++){
  195. Long courseId = dto.getCourseIds().get(i);
  196. Long swapCourseId = dto.getSwapCourseIds().get(i);
  197. CourseTable courseTable = courseTableService.getById(courseId);
  198. CourseTable swapCourseTable = courseTableService.getById(swapCourseId);
  199. JsonObject preCheck = getExtendPreCheck(dto, courseTable, swapCourseTable);
  200. if(preCheck.get("code").getAsInt() != 0){
  201. return RT.error(preCheck.get("msg").getAsString());
  202. }
  203. }
  204. }else if(dto.getPreCheckType() == 2){
  205. for (Long courseId : dto.getCourseIds()) {
  206. CourseTable courseTable = courseTableService.getById(courseId);
  207. JsonObject jsonObject = substitutePreTestin(dto, courseTable);
  208. if(jsonObject.get("code").getAsInt() != 0){
  209. return RT.error(jsonObject.get("msg").getAsString());
  210. }
  211. }
  212. }
  213. }
  214. return RT.ok("ok");
  215. }
  216. @GetMapping(value = "/getCourseNames")
  217. @ApiOperation(value = "查询课程接口")
  218. @SaCheckPermission("schedule:detail")
  219. public RT<String> getCourseNames(String id){
  220. String[] split = id.split(",");
  221. List<CourseTable> list = courseTableService.list(
  222. new QueryWrapper<CourseTable>().lambda()
  223. .in(CourseTable::getId, Arrays.asList(split))
  224. );
  225. List<ClassTime> classTimeList =classTimeService.list();
  226. String result = "";
  227. for (int i = 0; i< list.size(); i ++){
  228. if(i > 0){
  229. result += ",";
  230. }
  231. CourseTable courseTable = list.get(i);
  232. String courseName = courseTable.getCourseName();
  233. ClassTime classTime = classTimeList.stream().filter(u->u.getNumber().equals(courseTable.getTimeNumber())).findAny().orElse(null);
  234. if(classTime!=null){
  235. courseName +="("+classTime.getShortName()+")";
  236. }
  237. result += courseName;
  238. }
  239. return RT.ok(result);
  240. }
  241. /**
  242. * 顶课预检查
  243. * @param dto
  244. * @param courseTable
  245. * @return 检查结果
  246. */
  247. JsonObject substitutePreTestin(CourseTablePreCheckDto dto, CourseTable courseTable) throws Exception {
  248. JsonParser jsonParser = new JsonParser();
  249. String url = ScheduleUtil.apiUrl + "RescheduleApply/Extend/Substitute/PreTesting";
  250. JsonObject jsonObject = new JsonObject();
  251. jsonObject.addProperty("timetableId", courseTable.getJianyueId());
  252. jsonObject.addProperty("isCycles", Boolean.FALSE);
  253. JsonArray extendIds = new JsonArray();
  254. extendIds.add(dto.getSubTeacherId());
  255. jsonObject.add("extendIds", extendIds);
  256. //获取时间戳
  257. long timestamp = System.currentTimeMillis();
  258. //生成签名
  259. String sign = ScheduleUtil.createSign(timestamp);
  260. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timestamp);
  261. if(StrUtil.isEmpty(result)){
  262. return null;
  263. }
  264. return jsonParser.parse(result).getAsJsonObject();
  265. }
  266. /**
  267. * 调课预检查
  268. * @param courseTable 需要调整的课程
  269. * @param swapCourseTable 对调的课程
  270. * @return 检查结果
  271. */
  272. JsonObject getExtendPreCheck(CourseTablePreCheckDto dto, CourseTable courseTable, CourseTable swapCourseTable) throws Exception {
  273. JsonParser jsonParser = new JsonParser();
  274. String url = ScheduleUtil.apiUrl + "RescheduleApply/Extend/PreTesting";
  275. JsonObject jsonObject = new JsonObject();
  276. jsonObject.addProperty("timetableId", courseTable.getJianyueId());
  277. jsonObject.addProperty("isCycles", Boolean.FALSE);
  278. // jsonObject.addProperty("startDate", "2024-01-01");
  279. // jsonObject.addProperty("endDate", "2024-01-31");
  280. // jsonObject.addProperty("dayOfweek", 5);
  281. jsonObject.addProperty("numberOfday", swapCourseTable.getTimeNumber());
  282. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  283. jsonObject.addProperty("date", dto.getSwapDate().format(formatter));
  284. jsonObject.addProperty("reschduleId", courseTable.getJianyueId());
  285. //获取时间戳
  286. long timestamp = System.currentTimeMillis();
  287. //生成签名
  288. String sign = ScheduleUtil.createSign(timestamp);
  289. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timestamp);
  290. if(StrUtil.isEmpty(result)){
  291. return null;
  292. }
  293. return jsonParser.parse(result).getAsJsonObject();
  294. }
  295. @PostMapping("/schedule-week-export-query")
  296. @ApiOperation(value = "按周导出课表")
  297. public ResponseEntity<byte[]> scheduleWeekExportQuery(@Valid @RequestBody CourseTableDto dto) {
  298. // @GetMapping("/schedule-week-export-query")
  299. // @ApiOperation(value = "按周导出课表")
  300. // public ResponseEntity<byte[]> textbookClaimExportQuery(ScheduleWeekExportQueryDto dto) {
  301. ByteArrayOutputStream bot = courseTableService.scheduleWeekExportQuery(dto);
  302. return RT.fileStream(bot.toByteArray(), "scheduleWeek" + ExcelTypeEnum.XLSX.getValue());
  303. }
  304. @GetMapping(value = "/login-url")
  305. @ApiOperation(value = "获取单点登录地址")
  306. @SaCheckPermission("schedule:detail")
  307. public RT<String> getAccessToken() throws Exception {
  308. XjrUser xjrUser = teacherService.getById(StpUtil.getLoginIdAsLong());
  309. long timeMillis = System.currentTimeMillis();
  310. String sign = ScheduleUtil.createSign(timeMillis);
  311. JsonObject jsonObject = new JsonObject();
  312. jsonObject.addProperty("mobileNo", xjrUser.getMobile());
  313. String url = ScheduleUtil.apiUrl + "auth/user/token";
  314. try {
  315. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timeMillis);
  316. JsonParser parser = new JsonParser();
  317. JsonObject resultJson = parser.parse(result).getAsJsonObject();
  318. if(resultJson.get("code").getAsInt() != 0){
  319. return RT.error("无登录权限");
  320. }
  321. JsonObject dataJson = resultJson.get("data").getAsJsonObject();
  322. String loginUrl = ScheduleUtil.hostUrl + "?access_token=" + dataJson.get("access_token").getAsString();
  323. return RT.ok(loginUrl);
  324. }catch (Exception e){
  325. return RT.error(e.getMessage());
  326. }
  327. }
  328. @GetMapping(value = "/week-list")
  329. @ApiOperation(value="获取周次列表")
  330. @SaCheckPermission("schedule:detail")
  331. public RT<List<ScheduleWeekVo>> weekList(@Valid ScheduleWeekDto dto){
  332. BaseSemester baseSemester;
  333. if(dto.getSemesterId() != null){
  334. baseSemester = semesterService.getById(dto.getSemesterId());
  335. }else{
  336. baseSemester = semesterService.getCurrentSemester();
  337. if(baseSemester == null){
  338. return RT.ok(new ArrayList<>());
  339. }
  340. }
  341. LocalDateTime startDateTime = LocalDateTime.ofInstant(baseSemester.getTeachingStart().toInstant(), ZoneId.systemDefault());
  342. LocalDateTime endDateTime = LocalDateTime.ofInstant(baseSemester.getTeachingEnd().toInstant(), ZoneId.systemDefault());
  343. Duration between = Duration.between(startDateTime, endDateTime);
  344. long days = between.toDays();
  345. int weeks = (int) Math.ceil((double) days / 7);
  346. List<ScheduleWeekVo> result = new ArrayList<>();
  347. for (int i = 0; i < weeks; i++) {
  348. LocalDateTime startDate = startDateTime.plusDays(i * 7).withHour(0).withMinute(0).withSecond(0).withNano(0);
  349. LocalDateTime endDate = startDate.plusDays(6).withHour(23).withMinute(59).withSecond(59).withNano(9999);
  350. int week = i + 1;
  351. result.add(
  352. new ScheduleWeekVo(){{
  353. setWeek(week);
  354. setWeekCn("第" + week + "周");
  355. setStartDate(startDate.toLocalDate());
  356. setEndDate(endDate.toLocalDate());
  357. }}
  358. );
  359. }
  360. return RT.ok(result);
  361. }
  362. @GetMapping(value = "/class-list")
  363. @ApiOperation(value="根据课表获取教师所教班级")
  364. @SaCheckPermission("schedule:detail")
  365. public RT<List<ClassOptionVo>> classList(@Valid ClassOptionDto dto){
  366. if(dto.getUserId() == null){
  367. dto.setUserId(StpUtil.getLoginIdAsLong());
  368. }
  369. List<ClassOptionVo> result = courseTableService.getClassListByTeacherId(dto);
  370. return RT.ok(result);
  371. }
  372. @PostMapping(value = "/cancel")
  373. @ApiOperation(value="调课顶课的作废")
  374. @SaCheckPermission("schedule:detail")
  375. public RT<Boolean> cancel(@Valid @RequestBody WfCourseAdjustDto dto){
  376. return RT.ok(courseAdjustService.cancel(dto));
  377. }
  378. @GetMapping(value = "/is-contains-all")
  379. @ApiOperation(value="判断登录者是否有查看所有课表的权限")
  380. @SaCheckPermission("schedule:detail")
  381. public RT<Boolean> classList(){
  382. if(StpUtil.getRoleList().contains("KeBiao")){
  383. return RT.ok(true);
  384. }
  385. return RT.ok(false);
  386. }
  387. }