|
|
@@ -1,21 +1,34 @@
|
|
|
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.Wrapper;
|
|
|
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.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.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
+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.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
@@ -92,6 +105,96 @@ public class ClassScheduleController implements ClassScheduleAPI {
|
|
|
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<String, String> 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<ClassSchedule> qw = new QueryWrapper<>();
|
|
|
+ qw.between("date_time",stateTime,endTime);
|
|
|
+ qw.like("jsxm",teacherName);
|
|
|
+ List<ClassSchedule> classSchedulesList= classScheduleService.list(qw);
|
|
|
+ List<ClassScheduleExportVo> exportVoList=new ArrayList<>();
|
|
|
+ for(ClassSchedule classSchedule:classSchedulesList){
|
|
|
+ ClassScheduleExportVo classScheduleExportVo=JSON.parseObject(JSON.toJSONString(classSchedule), ClassScheduleExportVo.class);
|
|
|
+ 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<ClassSchedule> classSchedulesList=new ArrayList<>();
|
|
|
+ // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
|
|
|
+ // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
|
|
|
+ List<String> list=new ArrayList<>();
|
|
|
+ try {
|
|
|
+ EasyExcel.read(multipartFile.getInputStream(), ClassScheduleExportVo.class, new PageReadListener<ClassScheduleExportVo>(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();
|
|
|
+ }
|
|
|
+
|
|
|
// @Scheduled(cron = "0 44 11 * * ? ")
|
|
|
public void update() {
|
|
|
LocalDate localDate = LocalDate.of(2023, 9, 4);
|