ClassScheduleController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package com.template.controller;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.ExcelWriter;
  4. import com.alibaba.excel.read.listener.PageReadListener;
  5. import com.alibaba.excel.write.metadata.WriteSheet;
  6. import com.alibaba.fastjson.JSON;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  10. import com.template.api.ClassScheduleAPI;
  11. import com.template.model.dto.ClassScheduleDto;
  12. import com.template.model.pojo.ClassSchedule;
  13. import com.template.model.result.CommonResult;
  14. import com.template.model.vo.ClassListVo;
  15. import com.template.model.vo.ClassScheduleExportVo;
  16. import com.template.model.vo.ScheduleVo;
  17. import com.template.services.ClassScheduleService;
  18. import com.template.services.OrganizationService;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.util.StringUtils;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.IOException;
  26. import java.text.SimpleDateFormat;
  27. import java.time.LocalDate;
  28. import java.time.format.DateTimeFormatter;
  29. import java.time.format.TextStyle;
  30. import java.time.temporal.ChronoUnit;
  31. import java.util.*;
  32. /**
  33. * <p>
  34. * 前端控制器
  35. * </p>
  36. *
  37. * @author ceshi
  38. * @since 2023-11-06
  39. */
  40. @RestController
  41. //加密
  42. //@DESRespondSecret
  43. public class ClassScheduleController implements ClassScheduleAPI {
  44. @Autowired
  45. ClassScheduleService classScheduleService;
  46. @Override
  47. public CommonResult schedule(String stateTime, String endTime, String teacherName,String jsgh) {
  48. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  49. if (ObjectUtils.isEmpty(stateTime) && ObjectUtils.isEmpty(endTime)) {
  50. // 获取当前星期的周一和周日
  51. LocalDate date = LocalDate.now();
  52. String dateStr = date.format(dateTimeFormatter2);
  53. Map<String, String> map = getDjz(dateStr);
  54. // 获取周几
  55. String week = map.get("week");
  56. LocalDate localDate = date.minusDays(Integer.valueOf(week) - 1);
  57. stateTime = localDate.format(dateTimeFormatter2);
  58. LocalDate localEnd = localDate.plusDays(6);
  59. endTime = localEnd.format(dateTimeFormatter2);
  60. }
  61. LocalDate date = LocalDate.parse(stateTime, dateTimeFormatter2);
  62. LocalDate end = LocalDate.parse(endTime, dateTimeFormatter2);
  63. long until = date.until(end, ChronoUnit.DAYS);
  64. ArrayList<ScheduleVo> scheduleVos = new ArrayList<>();
  65. for (int i = 0; i <= until; i++) {
  66. LocalDate localDate = date.plusDays(i);
  67. ScheduleVo scheduleVo = new ScheduleVo();
  68. ClassListVo classListVo = classScheduleService.classList(localDate,teacherName,jsgh);
  69. scheduleVo.setClassListVo(classListVo);
  70. String format = localDate.format(dateTimeFormatter2);
  71. Map<String, String> stringMap = getDjz(format);
  72. String week = stringMap.get("week");
  73. if ("1".equals(week)) {
  74. scheduleVo.setDateName(format+"/星期一");
  75. } else if ("2".equals(week)) {
  76. scheduleVo.setDateName(format+"/星期二");
  77. } else if ("3".equals(week)) {
  78. scheduleVo.setDateName(format+"/星期三");
  79. } else if ("4".equals(week)) {
  80. scheduleVo.setDateName(format+"/星期四");
  81. } else if ("5".equals(week)) {
  82. scheduleVo.setDateName(format+"/星期五");
  83. } else if ("6".equals(week)) {
  84. scheduleVo.setDateName(format+"/星期六");
  85. } else {
  86. scheduleVo.setDateName(format+"/星期日");
  87. }
  88. scheduleVos.add(scheduleVo);
  89. }
  90. return CommonResult.ok(scheduleVos);
  91. }
  92. @Override
  93. public void downloadSchedule(String stateTime, String endTime, String teacherName,String jsgh, HttpServletResponse response) {
  94. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  95. if (ObjectUtils.isEmpty(stateTime) && ObjectUtils.isEmpty(endTime)) {
  96. // 获取当前星期的周一和周日
  97. LocalDate date = LocalDate.now();
  98. String dateStr = date.format(dateTimeFormatter2);
  99. Map<String, String> map = getDjz(dateStr);
  100. // 获取周几
  101. String week = map.get("week");
  102. LocalDate localDate = date.minusDays(Integer.valueOf(week) - 1);
  103. stateTime = localDate.format(dateTimeFormatter2);
  104. LocalDate localEnd = localDate.plusDays(6);
  105. endTime = localEnd.format(dateTimeFormatter2);
  106. }
  107. List<ClassSchedule> classSchedulesList= classScheduleService.listByTime(stateTime,endTime,teacherName,jsgh);
  108. List<ClassScheduleExportVo> exportVoList=new ArrayList<>();
  109. for(ClassSchedule classSchedule:classSchedulesList){
  110. ClassScheduleExportVo classScheduleExportVo=JSON.parseObject(JSON.toJSONString(classSchedule), ClassScheduleExportVo.class);
  111. exportVoList.add(classScheduleExportVo);
  112. }
  113. //空数据则为模板增加导入提示
  114. if(exportVoList.size()==0){
  115. ClassScheduleExportVo classScheduleExportVo=new ClassScheduleExportVo();
  116. classScheduleExportVo.setJsgh("示例:12004");
  117. classScheduleExportVo.setRemark("请在编辑导入数据时删除此行");
  118. classScheduleExportVo.setJc("示例:9-10");
  119. classScheduleExportVo.setJsxm("示例:张三");
  120. classScheduleExportVo.setDateTime("示例:2024-06-18");
  121. classScheduleExportVo.setXq("示例:2");
  122. classScheduleExportVo.setXn("示例:2023-2024");
  123. classScheduleExportVo.setZj("示例:星期日");
  124. classScheduleExportVo.setDjz("示例:15");
  125. exportVoList.add(classScheduleExportVo);
  126. }
  127. ExcelWriter excelWriter = null;
  128. try {
  129. response.setContentType("application/octet-stream");
  130. //设置前端下载文件名
  131. Date d = new Date();
  132. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  133. String dateNowStr = sdf.format(d);
  134. String fileName = new String(("课表导出-"+dateNowStr).getBytes("utf-8"),"iso-8859-1");
  135. response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
  136. // response.setCharacterEncoding("utf-8");
  137. // 指定写用哪个class去写
  138. excelWriter = EasyExcel.write(response.getOutputStream(), ClassScheduleExportVo.class).build();
  139. // 同一个sheet只要创建一次
  140. WriteSheet writeSheet = EasyExcel.writerSheet("课表").build();
  141. // 调用写入
  142. excelWriter.write(exportVoList, writeSheet);
  143. } catch (Exception e) {
  144. System.out.println("导出课表失败"+e.getMessage());
  145. } finally {
  146. // 关闭流
  147. if (excelWriter != null) {
  148. excelWriter.finish();
  149. }
  150. }
  151. }
  152. @Override
  153. public CommonResult uploadSchedule(@RequestParam("file") MultipartFile multipartFile) {
  154. if (multipartFile.isEmpty()) {
  155. return CommonResult.fail("文件不能为空");
  156. }
  157. List<ClassSchedule> classSchedulesList=new ArrayList<>();
  158. // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
  159. // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
  160. List<String> list=new ArrayList<>();
  161. try {
  162. EasyExcel.read(multipartFile.getInputStream(), ClassScheduleExportVo.class, new PageReadListener<ClassScheduleExportVo>(dataList -> {
  163. for (ClassScheduleExportVo classScheduleExportVo : dataList) {
  164. //将导入的数据用mybatisPlus一个个添加进数据库
  165. if(StringUtils.hasText(classScheduleExportVo.getJsgh())&&StringUtils.hasText(classScheduleExportVo.getJsxm())
  166. &&StringUtils.hasText(classScheduleExportVo.getDateTime())){
  167. ClassSchedule classSchedule=JSON.parseObject(JSON.toJSONString(classScheduleExportVo), ClassSchedule.class);
  168. classSchedulesList.add(classSchedule);
  169. }else{
  170. list.add(classScheduleExportVo.toString()+"姓名,微校卡号,日期必填,请重新导入");
  171. }
  172. }
  173. })).sheet().doRead();
  174. if (list.size()==0){
  175. classScheduleService.saveOrUpdateBatch(classSchedulesList);
  176. }else{
  177. return CommonResult.fail("数据格式异常:"+list.toString());
  178. }
  179. } catch (IOException e) {
  180. System.out.println(e);
  181. return CommonResult.fail("导入异常"+e.getMessage());
  182. }
  183. return CommonResult.ok();
  184. }
  185. @Override
  186. public CommonResult deleteSchedule(List<Integer> ids) {
  187. boolean a =classScheduleService.removeByIds(ids);
  188. if(!a){
  189. return CommonResult.fail("删除失败");
  190. }
  191. return CommonResult.ok("删除完成");
  192. }
  193. @Override
  194. public CommonResult insertSchedule(ClassScheduleDto scheduleDto) {
  195. ClassSchedule schedule=new ClassSchedule();
  196. schedule.setJsxm(scheduleDto.getJsxm());
  197. schedule.setJsgh(scheduleDto.getJsgh());
  198. schedule.setDateTime(scheduleDto.getDateTime());
  199. boolean save = classScheduleService.save(schedule);
  200. if(!save){
  201. return CommonResult.fail("新增失败");
  202. }
  203. return CommonResult.ok("新增完成");
  204. }
  205. // @Scheduled(cron = "0 44 11 * * ? ")
  206. public void update() {
  207. // LocalDate localDate = LocalDate.of(2023, 9, 4);
  208. LocalDate localDate = LocalDate.of(2024, 2, 26);
  209. LambdaQueryWrapper<ClassSchedule> wrapper=new LambdaQueryWrapper<>();
  210. wrapper.eq(ClassSchedule::getXn,"2023-2024")
  211. .eq(ClassSchedule::getXq,2);
  212. List<ClassSchedule> list = classScheduleService.list(wrapper);
  213. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  214. ArrayList<ClassSchedule> classSchedules = new ArrayList<>();
  215. for (int i = 0; i < list.size(); i++) {
  216. ClassSchedule classSchedule = list.get(i);
  217. // 获取第几周
  218. String djz = classSchedule.getDjz();
  219. // 获取周几
  220. String zj = classSchedule.getZj();
  221. // 需要在开学的基础上加多少天
  222. Integer integer = Integer.valueOf(djz);
  223. Integer integer1 = Integer.valueOf(zj);
  224. int djzDay = (integer - 1) * 7;
  225. int zjDay = integer1 - 1;
  226. int day = djzDay + zjDay;
  227. LocalDate date = localDate.plusDays(day);
  228. String dateStr = date.format(dateTimeFormatter2);
  229. classSchedule.setDateTime(dateStr);
  230. classSchedules.add(classSchedule);
  231. }
  232. boolean b = classScheduleService.updateBatchById(classSchedules);
  233. System.out.println("b = " + b);
  234. }
  235. /**
  236. * 通过一个时间判断是第几周,周几
  237. *
  238. * @param dateTime
  239. * @return
  240. */
  241. public Map<String, String> getDjz(String dateTime) {
  242. // 2023-9-4开始上课
  243. // LocalDate localDate = LocalDate.of(2023, 9, 4);
  244. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  245. LocalDate date = LocalDate.parse(dateTime, dateTimeFormatter);
  246. // 将此日期与指定日期进行比较,传进来的日期不能小于开学日期
  247. /* int i = date.compareTo(localDate);
  248. if (i < 0) {
  249. return null;
  250. }*/
  251. Map<String, String> map = new HashMap<>();
  252. // 计算2个时间的天数差
  253. // long until = date.until(localDate, ChronoUnit.DAYS);
  254. // 需要加1才是第几周
  255. // int weeks = (int) until / 7;
  256. // map.put("weeks", (weeks + 1) + "");
  257. // 周几
  258. String chineseDayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
  259. if ("星期一".equals(chineseDayOfWeek)) {
  260. map.put("week", 1 + "");
  261. } else if ("星期二".equals(chineseDayOfWeek)) {
  262. map.put("week", 2 + "");
  263. } else if ("星期三".equals(chineseDayOfWeek)) {
  264. map.put("week", 3 + "");
  265. } else if ("星期四".equals(chineseDayOfWeek)) {
  266. map.put("week", 4 + "");
  267. } else if ("星期五".equals(chineseDayOfWeek)) {
  268. map.put("week", 5 + "");
  269. } else if ("星期六".equals(chineseDayOfWeek)) {
  270. map.put("week", 6 + "");
  271. } else {
  272. map.put("week", 7 + "");
  273. }
  274. // 获取月份
  275. int month = date.getMonthValue();
  276. int year = date.getYear();
  277. // 获取学年、学期
  278. String schoolYear, semester;
  279. if (month >= 2 && month <= 7) {
  280. schoolYear = (year - 1) + "-" + year;
  281. semester = "2";
  282. } else {
  283. schoolYear = year + "-" + (year + 1);
  284. semester = "1";
  285. }
  286. map.put("schoolYear", schoolYear);
  287. map.put("semester", semester);
  288. return map;
  289. }
  290. public static void main(String[] args) {
  291. LocalDate date = LocalDate.now();
  292. String chineseDayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
  293. String week = "";
  294. if (chineseDayOfWeek.equals("星期三")) {
  295. week = "3";
  296. }
  297. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  298. LocalDate localDate = date.minusDays(Integer.valueOf(week) - 1);
  299. String stateTime = localDate.format(dateTimeFormatter2);
  300. System.out.println("stateTime = " + stateTime);
  301. LocalDate localEnd = localDate.plusDays(6);
  302. String endTime = localEnd.format(dateTimeFormatter2);
  303. System.out.println("endTime = " + endTime);
  304. }
  305. }