| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- 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 com.template.services.OrganizationService;
- 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.*;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @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,String jsgh) {
- 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);
- long until = date.until(end, ChronoUnit.DAYS);
- ArrayList<ScheduleVo> 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,jsgh);
- scheduleVo.setClassListVo(classListVo);
- String format = localDate.format(dateTimeFormatter2);
- Map<String, String> 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,String jsgh, 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);
- }
- List<ClassSchedule> classSchedulesList= classScheduleService.listByTime(stateTime,endTime,teacherName,jsgh);
- List<ClassScheduleExportVo> 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<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();
- }
- @Override
- public CommonResult deleteSchedule(List<Integer> 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<ClassSchedule> wrapper=new LambdaQueryWrapper<>();
- wrapper.eq(ClassSchedule::getXn,"2023-2024")
- .eq(ClassSchedule::getXq,2);
- List<ClassSchedule> list = classScheduleService.list(wrapper);
- DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- ArrayList<ClassSchedule> 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<String, String> 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<String, String> 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);
- }
- }
|