ClassScheduleController.java 14 KB

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