ScheduleController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 cn.hutool.db.Db;
  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.constant.GlobalConstant;
  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.DatasourceUtil;
  16. import com.xjrsoft.module.courseTable.entity.CourseTable;
  17. import com.xjrsoft.module.courseTable.service.ICourseTableService;
  18. import com.xjrsoft.module.schedule.dto.CourseTableAdjustDto;
  19. import com.xjrsoft.module.schedule.dto.CourseTableDto;
  20. import com.xjrsoft.module.schedule.dto.CourseTablePreCheckDto;
  21. import com.xjrsoft.module.schedule.entity.CourseReceiveMsg;
  22. import com.xjrsoft.module.schedule.entity.JianyueData;
  23. import com.xjrsoft.module.schedule.service.ICourseReceiveMsgService;
  24. import com.xjrsoft.module.schedule.service.IJianyueDataService;
  25. import com.xjrsoft.module.schedule.util.DataUtil;
  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.impl.IBaseStudentSchoolRollService;
  31. import com.xjrsoft.module.teacher.entity.BaseTeacher;
  32. import com.xjrsoft.module.teacher.service.IBaseTeacherService;
  33. import io.swagger.annotations.Api;
  34. import io.swagger.annotations.ApiOperation;
  35. import lombok.AllArgsConstructor;
  36. import org.springframework.web.bind.annotation.GetMapping;
  37. import org.springframework.web.bind.annotation.RequestMapping;
  38. import org.springframework.web.bind.annotation.RequestParam;
  39. import org.springframework.web.bind.annotation.RestController;
  40. import javax.sql.DataSource;
  41. import java.time.DayOfWeek;
  42. import java.time.LocalDateTime;
  43. import java.time.format.DateTimeFormatter;
  44. import java.util.Date;
  45. import java.util.HashMap;
  46. import java.util.HashSet;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.Set;
  50. /**
  51. * @title: 课表
  52. * @Author dzx
  53. * @Date: 2023-12-27
  54. * @Version 1.0
  55. */
  56. @RestController
  57. @RequestMapping("/schedule" + "/schedule")
  58. @Api(value = "/schedule" + "/schedule",tags = "课表代码")
  59. @AllArgsConstructor
  60. public class ScheduleController {
  61. private final ICourseReceiveMsgService courseReceiveMsgService;
  62. private final IJianyueDataService jianyueDataService;
  63. private final ICourseTableService courseTableService;
  64. private final IBaseStudentSchoolRollService baseStudentSchoolRollService;
  65. private final IBaseTeacherService baseTeacherService;
  66. @GetMapping(value = "/receive-msg")
  67. @ApiOperation(value="接收消息")
  68. @SaCheckPermission("room:detail")
  69. public RT<CourseReceiveMsg> receiveMsg(@RequestParam String schoolId, @RequestParam String eduYearSerialNo, @RequestParam String startDate, @RequestParam String endDate) throws Exception {
  70. CourseReceiveMsg courseReceiveMsg = new CourseReceiveMsg();
  71. courseReceiveMsg.setCreateDate(new Date());
  72. courseReceiveMsg.setSchoolId(schoolId);
  73. courseReceiveMsg.setEduYearSerialNo(eduYearSerialNo);
  74. courseReceiveMsg.setStartDate(startDate);
  75. courseReceiveMsg.setEndDate(endDate);
  76. courseReceiveMsg.setDeleteMark(DeleteMark.NODELETE.getCode());
  77. courseReceiveMsg.setEnabledMark(EnabledMark.ENABLED.getCode());
  78. courseReceiveMsgService.save(courseReceiveMsg);
  79. DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
  80. Db use = Db.use(datasource);
  81. DataUtil dataUtil = new DataUtil();
  82. JsonArray scheduleInfo = dataUtil.getScheduleInfoByGrade(eduYearSerialNo, startDate, endDate);
  83. String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
  84. List<JianyueData> query = use.query(sql, JianyueData.class);
  85. Set<String> tables = new HashSet<>();
  86. for (JianyueData jianyueData : query) {
  87. tables.add(jianyueData.getTableName());
  88. }
  89. sql = "SELECT * FROM jianyue_data WHERE 0 = 0";
  90. List<JianyueData> list = use.query(sql, JianyueData.class);
  91. Map<String, Map<String, Long>> dataMap = new HashMap<>();
  92. for (String table : tables) {
  93. Map<String, Long> tableData = new HashMap<>();
  94. for (JianyueData jianyueData : list) {
  95. if(!table.equals(jianyueData.getTableName())){
  96. continue;
  97. }
  98. tableData.put(jianyueData.getJianyueId(), jianyueData.getSourceId());
  99. }
  100. dataMap.put(table, tableData);
  101. }
  102. //获取学期
  103. String tableName = "base_semester";
  104. Map<String, Long> semesterMap = dataMap.get(tableName);
  105. //获取课程
  106. tableName = "base_course_subject";
  107. Map<String, Long> courseMap = dataMap.get(tableName);
  108. //获取教职工
  109. tableName = "base_teacher";
  110. Map<String, Long> teacherMap = dataMap.get(tableName);
  111. //获取行政班
  112. tableName = "base_class";
  113. Map<String, Long> classMap = dataMap.get(tableName);
  114. tableName = "base_classroom";
  115. Map<String, Long> classroomMap = dataMap.get(tableName);
  116. dataUtil.insertCourseTable(use, scheduleInfo, classroomMap, courseMap, semesterMap, teacherMap, classMap);
  117. dataUtil.insertClassTime(use, scheduleInfo);
  118. return RT.ok(courseReceiveMsg);
  119. }
  120. @GetMapping(value = "/course-table")
  121. @ApiOperation(value="课表接口(PC端)")
  122. @SaCheckPermission("room:detail")
  123. public RT<CourseTableVo> courseInfo(CourseTableDto dto){
  124. CourseTableVo list = courseTableService.getList(dto);
  125. return RT.ok(list);
  126. }
  127. @GetMapping(value = "/course-table-day")
  128. @ApiOperation(value="今日课表接口(手机端)")
  129. @SaCheckPermission("room:detail")
  130. public RT<CourseTableVo> courseInfoDay(CourseTableDto dto){
  131. DayOfWeek dayOfWeek = LocalDateTime.now().getDayOfWeek();
  132. dto.setWeekDay(dayOfWeek.getValue());
  133. if(ObjectUtil.isNotNull(dto.getStudentId())){
  134. List<BaseStudentSchoolRoll> schoolRolls = baseStudentSchoolRollService.list(
  135. new QueryWrapper<BaseStudentSchoolRoll>().lambda().eq(BaseStudentSchoolRoll::getUserId, dto.getStudentId())
  136. );
  137. if(schoolRolls != null && !schoolRolls.isEmpty()){
  138. dto.setCourseType("class");
  139. dto.setClassId(schoolRolls.get(0).getClassId());
  140. }
  141. }else{
  142. // 查询登录者身份
  143. long loginIdAsLong = StpUtil.getLoginIdAsLong();
  144. List<BaseStudentSchoolRoll> schoolRolls = baseStudentSchoolRollService.list(
  145. new QueryWrapper<BaseStudentSchoolRoll>().lambda().eq(BaseStudentSchoolRoll::getUserId, loginIdAsLong)
  146. );
  147. if(schoolRolls != null && !schoolRolls.isEmpty()){
  148. dto.setCourseType("class");
  149. dto.setClassId(schoolRolls.get(0).getClassId());
  150. }
  151. List<BaseTeacher> teachers = baseTeacherService.list(new QueryWrapper<BaseTeacher>().lambda().eq(BaseTeacher::getUserId, loginIdAsLong));
  152. if(teachers != null && !teachers.isEmpty()){
  153. dto.setCourseType("teacher");
  154. dto.setTeacherId(loginIdAsLong);
  155. }
  156. }
  157. CourseTableVo list = courseTableService.getList(dto);
  158. return RT.ok(list);
  159. }
  160. @GetMapping(value = "/adjust-list")
  161. @ApiOperation(value = "可以调课的课程")
  162. @SaCheckPermission("evaluateobject:detail")
  163. public RT<List<CourseListVo>> adjustList(CourseTableAdjustDto dto){
  164. // if(dto.getAdjustDate() == null || dto.getTeacherId() == null){
  165. // return RT.error("请传入调整日期和教师id");
  166. // }
  167. // List<CourseListVo> list = courseTableService.getAdjustList(dto);
  168. // if(list.isEmpty()){
  169. // return RT.error("暂无可调课程");
  170. // }
  171. return RT.ok();
  172. }
  173. @GetMapping(value = "/pre-check")
  174. @ApiOperation(value = "预检查")
  175. @SaCheckPermission("evaluateobject:detail")
  176. public RT<String> preCheck(CourseTablePreCheckDto dto) throws Exception {
  177. if(dto.getPreCheckType() != null){
  178. if(dto.getPreCheckType() == 1){
  179. CourseTable courseTable = courseTableService.getById(dto.getCourseId());
  180. CourseTable swapCourseTable = courseTableService.getById(dto.getSwapCourseId());
  181. JsonObject preCheck = getExtendPreCheck(dto, courseTable, swapCourseTable);
  182. if(!preCheck.get("data").getAsBoolean()){
  183. return RT.error(preCheck.get("msg").getAsString());
  184. }
  185. }else if(dto.getPreCheckType() == 2){
  186. CourseTable courseTable = courseTableService.getById(dto.getCourseId());
  187. JsonObject jsonObject = substitutePreTestin(dto, courseTable);
  188. if(!jsonObject.get("data").getAsBoolean()){
  189. return RT.error(jsonObject.get("msg").getAsString());
  190. }
  191. }
  192. }
  193. return RT.ok("ok");
  194. }
  195. /**
  196. * 顶课预检查
  197. * @param dto
  198. * @param courseTable
  199. * @return 检查结果
  200. */
  201. JsonObject substitutePreTestin(CourseTablePreCheckDto dto, CourseTable courseTable) throws Exception {
  202. JsonParser jsonParser = new JsonParser();
  203. String url = ScheduleUtil.apiUrl + "RescheduleApply/Extend/Substitute/PreTesting";
  204. JsonObject jsonObject = new JsonObject();
  205. jsonObject.addProperty("timetableId", courseTable.getJianyueId());
  206. jsonObject.addProperty("isCycles", Boolean.FALSE);
  207. JsonArray extendIds = new JsonArray();
  208. extendIds.add(dto.getSubTeacherId());
  209. jsonObject.add("extendIds", extendIds);
  210. //获取时间戳
  211. long timestamp = System.currentTimeMillis();
  212. //生成签名
  213. String sign = ScheduleUtil.createSign(timestamp);
  214. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timestamp);
  215. if(StrUtil.isEmpty(result)){
  216. return null;
  217. }
  218. return jsonParser.parse(result).getAsJsonObject();
  219. }
  220. /**
  221. * 调课预检查
  222. * @param courseTable 需要调整的课程
  223. * @param swapCourseTable 对调的课程
  224. * @return 检查结果
  225. */
  226. JsonObject getExtendPreCheck(CourseTablePreCheckDto dto, CourseTable courseTable, CourseTable swapCourseTable) throws Exception {
  227. JsonParser jsonParser = new JsonParser();
  228. String url = ScheduleUtil.apiUrl + "RescheduleApply/Extend/PreTesting";
  229. JsonObject jsonObject = new JsonObject();
  230. jsonObject.addProperty("timetableId", courseTable.getJianyueId());
  231. jsonObject.addProperty("isCycles", Boolean.FALSE);
  232. // jsonObject.addProperty("startDate", "2024-01-01");
  233. // jsonObject.addProperty("endDate", "2024-01-31");
  234. // jsonObject.addProperty("dayOfweek", 5);
  235. jsonObject.addProperty("numberOfday", swapCourseTable.getTimeNumber());
  236. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  237. jsonObject.addProperty("date", dto.getSwapDate().format(formatter));
  238. jsonObject.addProperty("reschduleId", courseTable.getJianyueId());
  239. //获取时间戳
  240. long timestamp = System.currentTimeMillis();
  241. //生成签名
  242. String sign = ScheduleUtil.createSign(timestamp);
  243. String result = ScheduleUtil.doPost(url, jsonObject.toString(), sign, timestamp);
  244. if(StrUtil.isEmpty(result)){
  245. return null;
  246. }
  247. return jsonParser.parse(result).getAsJsonObject();
  248. }
  249. }