Bläddra i källkod

Accept Merge Request #7: (develop-wxl -> master)

Merge Request: 增加课表导入导出接口

Created By: @万新亮
Accepted By: @万新亮
URL: https://chuanghaikeji.coding.net/p/smarCampus/d/iHotel_student_houtai/git/merge/7?initial=true
万新亮 2 år sedan
förälder
incheckning
94401ca54e

+ 6 - 1
pom.xml

@@ -216,7 +216,12 @@
             <artifactId>jakarta.validation-api</artifactId>
             <version>2.0.2</version>
         </dependency>
-
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>3.1.1</version>
+            <scope>compile</scope>
+        </dependency>
 
     </dependencies>
 

+ 11 - 0
src/main/java/com/template/api/ClassScheduleAPI.java

@@ -4,6 +4,10 @@ import com.template.model.result.CommonResult;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletResponse;
 
 @RequestMapping("/auto/class-schedule")
 public interface ClassScheduleAPI {
@@ -12,4 +16,11 @@ public interface ClassScheduleAPI {
     @ApiOperation(value = "创建课表", notes = "创建课表", httpMethod = "GET")
     CommonResult schedule(String stateTime,String endTime,String teacherName);
 
+    @GetMapping("/downloadSchedule")
+    @ApiOperation(value = "导出课表", notes = "导出课表", httpMethod = "GET")
+    void downloadSchedule(String stateTime, String endTime, String teacherName, HttpServletResponse response);
+
+    @GetMapping("/uploadSchedule")
+    @ApiOperation(value = "导入课表", notes = "导入课表", httpMethod = "GET")
+    CommonResult uploadSchedule(@RequestParam("file") MultipartFile file);
 }

+ 103 - 0
src/main/java/com/template/controller/ClassScheduleController.java

@@ -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);

+ 65 - 0
src/main/java/com/template/model/enumModel/EmunIsWeekType.java

@@ -0,0 +1,65 @@
+package com.template.model.enumModel;
+
+import com.alibaba.excel.converters.Converter;
+import com.alibaba.excel.metadata.GlobalConfiguration;
+import com.alibaba.excel.metadata.data.CellData;
+import com.alibaba.excel.metadata.data.ReadCellData;
+import com.alibaba.excel.metadata.data.WriteCellData;
+import com.alibaba.excel.metadata.property.ExcelContentProperty;
+
+public class EmunIsWeekType implements Converter<String> {
+
+    @Override
+    public WriteCellData convertToExcelData (String string, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
+//    if (string.equals("0")){
+//        return new WriteCellData("否");
+//    }
+//    return new WriteCellData("是");
+    switch (string){
+        case "1":
+            return new WriteCellData("星期一");
+        case "2":
+            return new WriteCellData("星期二");
+        case "3":
+            return new WriteCellData("星期三");
+        case "4":
+            return new WriteCellData("星期四");
+        case "5":
+            return new WriteCellData("星期五");
+        case "6":
+            return new WriteCellData("星期六");
+        case "7":
+            return new WriteCellData("星期天");
+        default:
+            return new WriteCellData("");
+    }
+    }
+    @Override
+    public String convertToJavaData (ReadCellData cellData , ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
+//    if (string.equals("0")){
+//        return new WriteCellData("否");
+//    }
+        String string=cellData.getStringValue();
+//    return new WriteCellData("是");
+        switch (string){
+            case "星期一":
+                return  "1";
+            case "星期二":
+                return  "2";
+            case "星期三":
+                return  "3";
+            case "星期四":
+                return  "4";
+            case "星期五":
+                return  "5";
+            case "星期六":
+                return  "6";
+            case "星期天":
+                return  "7";
+            case "星期日":
+                return  "7";
+            default:
+                return null;
+        }
+    }
+}

+ 70 - 0
src/main/java/com/template/model/vo/ClassScheduleExportVo.java

@@ -0,0 +1,70 @@
+package com.template.model.vo;
+
+import com.alibaba.excel.annotation.ExcelIgnore;
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.baomidou.mybatisplus.annotation.*;
+import com.template.model.enumModel.EmunIsWeekType;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author ceshi
+ * @since 2023-11-06
+ */
+@Data
+public class ClassScheduleExportVo {
+
+    @ExcelIgnore
+    private Integer id;
+
+    @ExcelProperty(value = "节次" , index = 8)
+    private String jc;
+
+    @ExcelProperty(value = "周几", index = 7,converter = EmunIsWeekType.class)
+    private String zj;
+
+    @ExcelProperty(value = "第几周", index = 6)
+    private String djz;
+
+    @ExcelProperty(value = "学期", index = 5)
+    private String xq;
+
+    @ExcelProperty(value = "学年", index = 4)
+    private String xn;
+
+    @ExcelProperty(value = "教师姓名", index = 1)
+    private String jsxm;
+
+    @ExcelProperty(value = "微校卡号", index = 0)
+    private String jsgh;
+
+    @ExcelProperty(value = "上课日期", index = 2)
+    private String dateTime;
+
+    @ExcelProperty(value = "备注", index = 3)
+    private String remark;
+
+    @ExcelIgnore
+    private String createTime;
+
+    @ExcelIgnore
+    private String updateTime;
+
+    @ExcelIgnore
+    private String createUser;
+
+    @ExcelIgnore
+    private String updateUser;
+
+    @ExcelIgnore
+    private Integer deleted;
+}
+