package com.template.controller; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.read.listener.PageReadListener; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.template.api.ClassScheduleAPI; import com.template.model.dto.ClassScheduleDto; import com.template.model.pojo.ClassSchedule; import com.template.model.result.CommonResult; import com.template.model.vo.ClassListVo; import com.template.model.vo.ClassScheduleExportVo; import com.template.model.vo.ScheduleVo; import com.template.services.ClassScheduleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.TextStyle; import java.time.temporal.ChronoUnit; import java.util.*; /** *

* 前端控制器 *

* * @author ceshi * @since 2023-11-06 */ @RestController //加密 //@DESRespondSecret public class ClassScheduleController implements ClassScheduleAPI { @Autowired ClassScheduleService classScheduleService; @Override public CommonResult schedule(String stateTime, String endTime, String teacherName) { DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); if (ObjectUtils.isEmpty(stateTime) && ObjectUtils.isEmpty(endTime)) { // 获取当前星期的周一和周日 LocalDate date = LocalDate.now(); String dateStr = date.format(dateTimeFormatter2); Map map = getDjz(dateStr); // 获取周几 String week = map.get("week"); LocalDate localDate = date.minusDays(Integer.valueOf(week) - 1); stateTime = localDate.format(dateTimeFormatter2); LocalDate localEnd = localDate.plusDays(6); endTime = localEnd.format(dateTimeFormatter2); } LocalDate date = LocalDate.parse(stateTime, dateTimeFormatter2); LocalDate end = LocalDate.parse(endTime, dateTimeFormatter2); long until = date.until(end, ChronoUnit.DAYS); ArrayList scheduleVos = new ArrayList<>(); for (int i = 0; i <= until; i++) { LocalDate localDate = date.plusDays(i); ScheduleVo scheduleVo = new ScheduleVo(); ClassListVo classListVo = classScheduleService.classList(localDate,teacherName); scheduleVo.setClassListVo(classListVo); String format = localDate.format(dateTimeFormatter2); Map stringMap = getDjz(format); String week = stringMap.get("week"); if ("1".equals(week)) { scheduleVo.setDateName(format+"/星期一"); } else if ("2".equals(week)) { scheduleVo.setDateName(format+"/星期二"); } else if ("3".equals(week)) { scheduleVo.setDateName(format+"/星期三"); } else if ("4".equals(week)) { scheduleVo.setDateName(format+"/星期四"); } else if ("5".equals(week)) { scheduleVo.setDateName(format+"/星期五"); } else if ("6".equals(week)) { scheduleVo.setDateName(format+"/星期六"); } else { scheduleVo.setDateName(format+"/星期日"); } scheduleVos.add(scheduleVo); } return CommonResult.ok(scheduleVos); } @Override public void downloadSchedule(String stateTime, String endTime, String teacherName, HttpServletResponse response) { DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); if (ObjectUtils.isEmpty(stateTime) && ObjectUtils.isEmpty(endTime)) { // 获取当前星期的周一和周日 LocalDate date = LocalDate.now(); String dateStr = date.format(dateTimeFormatter2); Map map = getDjz(dateStr); // 获取周几 String week = map.get("week"); LocalDate localDate = date.minusDays(Integer.valueOf(week) - 1); stateTime = localDate.format(dateTimeFormatter2); LocalDate localEnd = localDate.plusDays(6); endTime = localEnd.format(dateTimeFormatter2); } LocalDate date = LocalDate.parse(stateTime, dateTimeFormatter2); LocalDate end = LocalDate.parse(endTime, dateTimeFormatter2); QueryWrapper qw = new QueryWrapper<>(); qw.between("date_time",stateTime,endTime); if(teacherName!=null){ qw.like("jsxm",teacherName); } List classSchedulesList= classScheduleService.list(qw); List exportVoList=new ArrayList<>(); for(ClassSchedule classSchedule:classSchedulesList){ ClassScheduleExportVo classScheduleExportVo=JSON.parseObject(JSON.toJSONString(classSchedule), ClassScheduleExportVo.class); exportVoList.add(classScheduleExportVo); } //空数据则为模板增加导入提示 if(exportVoList.size()==0){ ClassScheduleExportVo classScheduleExportVo=new ClassScheduleExportVo(); classScheduleExportVo.setJsgh("示例:12004"); classScheduleExportVo.setRemark("请在编辑导入数据时删除此行"); classScheduleExportVo.setJc("示例:9-10"); classScheduleExportVo.setJsxm("示例:张三"); classScheduleExportVo.setDateTime("示例:2024-06-18"); classScheduleExportVo.setXq("示例:2"); classScheduleExportVo.setXn("示例:2023-2024"); classScheduleExportVo.setZj("示例:星期日"); classScheduleExportVo.setDjz("示例:15"); exportVoList.add(classScheduleExportVo); } ExcelWriter excelWriter = null; try { response.setContentType("application/octet-stream"); //设置前端下载文件名 Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String dateNowStr = sdf.format(d); String fileName = new String(("课表导出-"+dateNowStr).getBytes("utf-8"),"iso-8859-1"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); // response.setCharacterEncoding("utf-8"); // 指定写用哪个class去写 excelWriter = EasyExcel.write(response.getOutputStream(), ClassScheduleExportVo.class).build(); // 同一个sheet只要创建一次 WriteSheet writeSheet = EasyExcel.writerSheet("课表").build(); // 调用写入 excelWriter.write(exportVoList, writeSheet); } catch (Exception e) { System.out.println("导出课表失败"+e.getMessage()); } finally { // 关闭流 if (excelWriter != null) { excelWriter.finish(); } } } @Override public CommonResult uploadSchedule(@RequestParam("file") MultipartFile multipartFile) { if (multipartFile.isEmpty()) { return CommonResult.fail("文件不能为空"); } List classSchedulesList=new ArrayList<>(); // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行 List list=new ArrayList<>(); try { EasyExcel.read(multipartFile.getInputStream(), ClassScheduleExportVo.class, new PageReadListener(dataList -> { for (ClassScheduleExportVo classScheduleExportVo : dataList) { //将导入的数据用mybatisPlus一个个添加进数据库 if(StringUtils.hasText(classScheduleExportVo.getJsgh())&&StringUtils.hasText(classScheduleExportVo.getJsxm()) &&StringUtils.hasText(classScheduleExportVo.getDateTime())){ ClassSchedule classSchedule=JSON.parseObject(JSON.toJSONString(classScheduleExportVo), ClassSchedule.class); classSchedulesList.add(classSchedule); }else{ list.add(classScheduleExportVo.toString()+"姓名,微校卡号,日期必填,请重新导入"); } } })).sheet().doRead(); if (list.size()==0){ classScheduleService.saveOrUpdateBatch(classSchedulesList); }else{ return CommonResult.fail("数据格式异常:"+list.toString()); } } catch (IOException e) { System.out.println(e); return CommonResult.fail("导入异常"+e.getMessage()); } return CommonResult.ok(); } @Override public CommonResult deleteSchedule(List ids) { boolean a =classScheduleService.removeByIds(ids); if(!a){ return CommonResult.fail("删除失败"); } return CommonResult.ok("删除完成"); } @Override public CommonResult insertSchedule(ClassScheduleDto scheduleDto) { ClassSchedule schedule=new ClassSchedule(); schedule.setJsxm(scheduleDto.getJsxm()); schedule.setJsgh(scheduleDto.getJsgh()); schedule.setDateTime(scheduleDto.getDateTime()); boolean save = classScheduleService.save(schedule); if(!save){ return CommonResult.fail("新增失败"); } return CommonResult.ok("新增完成"); } // @Scheduled(cron = "0 44 11 * * ? ") public void update() { // LocalDate localDate = LocalDate.of(2023, 9, 4); LocalDate localDate = LocalDate.of(2024, 2, 26); LambdaQueryWrapper wrapper=new LambdaQueryWrapper<>(); wrapper.eq(ClassSchedule::getXn,"2023-2024") .eq(ClassSchedule::getXq,2); List list = classScheduleService.list(wrapper); DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); ArrayList classSchedules = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { ClassSchedule classSchedule = list.get(i); // 获取第几周 String djz = classSchedule.getDjz(); // 获取周几 String zj = classSchedule.getZj(); // 需要在开学的基础上加多少天 Integer integer = Integer.valueOf(djz); Integer integer1 = Integer.valueOf(zj); int djzDay = (integer - 1) * 7; int zjDay = integer1 - 1; int day = djzDay + zjDay; LocalDate date = localDate.plusDays(day); String dateStr = date.format(dateTimeFormatter2); classSchedule.setDateTime(dateStr); classSchedules.add(classSchedule); } boolean b = classScheduleService.updateBatchById(classSchedules); System.out.println("b = " + b); } /** * 通过一个时间判断是第几周,周几 * * @param dateTime * @return */ public Map getDjz(String dateTime) { // 2023-9-4开始上课 // LocalDate localDate = LocalDate.of(2023, 9, 4); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate date = LocalDate.parse(dateTime, dateTimeFormatter); // 将此日期与指定日期进行比较,传进来的日期不能小于开学日期 /* int i = date.compareTo(localDate); if (i < 0) { return null; }*/ Map map = new HashMap<>(); // 计算2个时间的天数差 // long until = date.until(localDate, ChronoUnit.DAYS); // 需要加1才是第几周 // int weeks = (int) until / 7; // map.put("weeks", (weeks + 1) + ""); // 周几 String chineseDayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA); if ("星期一".equals(chineseDayOfWeek)) { map.put("week", 1 + ""); } else if ("星期二".equals(chineseDayOfWeek)) { map.put("week", 2 + ""); } else if ("星期三".equals(chineseDayOfWeek)) { map.put("week", 3 + ""); } else if ("星期四".equals(chineseDayOfWeek)) { map.put("week", 4 + ""); } else if ("星期五".equals(chineseDayOfWeek)) { map.put("week", 5 + ""); } else if ("星期六".equals(chineseDayOfWeek)) { map.put("week", 6 + ""); } else { map.put("week", 7 + ""); } // 获取月份 int month = date.getMonthValue(); int year = date.getYear(); // 获取学年、学期 String schoolYear, semester; if (month >= 2 && month <= 7) { schoolYear = (year - 1) + "-" + year; semester = "2"; } else { schoolYear = year + "-" + (year + 1); semester = "1"; } map.put("schoolYear", schoolYear); map.put("semester", semester); return map; } public static void main(String[] args) { LocalDate date = LocalDate.now(); String chineseDayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA); String week = ""; if (chineseDayOfWeek.equals("星期三")) { week = "3"; } DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = date.minusDays(Integer.valueOf(week) - 1); String stateTime = localDate.format(dateTimeFormatter2); System.out.println("stateTime = " + stateTime); LocalDate localEnd = localDate.plusDays(6); String endTime = localEnd.format(dateTimeFormatter2); System.out.println("endTime = " + endTime); } }