要求:
前端给定医生id,要求后端获取该医生后五天的预约情况。
刚开始拿来确实无从下手,死磨硬泡了半天,决定改一下表结构
见五天放在一个字段里,每天的早中晚预约情况,放五个字段里。
既然是后五天,肯定周末要去点,当天也没有:
1、初始化开启项目的时间,每次查询时,判断本地时间是否已经过了当天,因为过了一天,就得更新表
2、创建一个dayTade类(存放月份、天数、早中晚的预约数),用来给前端返回数据,总不能返回一个字符串回去吧。
package com.quxiao.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quxiao.mapper.ConventionMapper;
import com.quxiao.pojo.Convention;
import com.quxiao.pojo.dayDate;
import com.quxiao.util.ResultDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.Arrays;
/**
* @program: yy
* @author: quxiao
* @create: 2023-04-19 11:25
**/
@Service
public class ConventionService {
@Autowired
protected ConventionMapper mapper;
//记录第一次加载项目的时间,
private int refreshDay = LocalDate.now().getDayOfMonth();
//标记每天只用刷新一次
private boolean TimeIsNew = false;
private LocalDate date = LocalDate.now();
private QueryWrapper<Convention> queryWrapper = new QueryWrapper<>();
public ResultDate selectDoctorConvention(int dctorId) {
//一旦过了一天,就代表要刷新数据库了
if (LocalDate.now().getDayOfMonth() > refreshDay) {
TimeIsNew = false;
refreshDay = LocalDate.now().getDayOfMonth();
}
if (!TimeIsNew) {
//每天都要更新一遍表中的数据,就时,今天是20日,就要把20日的删掉,显示的是:21 22 23 24 25 (不能为周末哦)
dailyUpdateTable();
}
queryWrapper.eq("doctorid", dctorId);
Convention convention = mapper.selectOne(queryWrapper);
if (convention != null) {
String[] timeArr = convention.getTime().split(",");
System.out.println(Arrays.toString(timeArr));
dayDate[] ans = new dayDate[5];
for (int i = 0; i < timeArr.length; i++) {
fill(ans, timeArr, i, convention);
}
return new ResultDate(1, "1", ans);
}
return new ResultDate(0, "无此医生", null);
}
private void fill(dayDate[] ans, String[] timeArr, int i, Convention convention) {
int month = Integer.parseInt(timeArr[i].substring(0, 1));
int day = Integer.parseInt(timeArr[i].substring(2));
int morning = 0;
int middle = 0;
int evening = 0;
String[] split = null;
switch (i) {
case 0:
split = convention.getFirstDay().split(",");
morning = Integer.parseInt(split[0]);
middle = Integer.parseInt(split[1]);
evening = Integer.parseInt(split[2]);
break;
case 1:
split = convention.getNextDay().split(",");
morning = Integer.parseInt(split[0]);
middle = Integer.parseInt(split[1]);
evening = Integer.parseInt(split[2]);
break;
case 2:
split = convention.getThirdDay().split(",");
morning = Integer.parseInt(split[0]);
middle = Integer.parseInt(split[1]);
evening = Integer.parseInt(split[2]);
break;
case 3:
split = convention.getFourthDay().split(",");
morning = Integer.parseInt(split[0]);
middle = Integer.parseInt(split[1]);
evening = Integer.parseInt(split[2]);
break;
case 4:
split = convention.getFifthDay().split(",");
morning = Integer.parseInt(split[0]);
middle = Integer.parseInt(split[1]);
evening = Integer.parseInt(split[2]);
break;
}
ans[i] = new dayDate(month, day, morning, middle, evening);
}
private void dailyUpdateTable() {
queryWrapper.eq("doctorid", 1);
Convention convention = mapper.selectOne(queryWrapper);
String[] split = convention.getTime().split(",");
// System.out.println(date.getDayOfMonth() >= Integer.parseInt(split[0].substring(2)));//true
if (date.getDayOfMonth() >= Integer.parseInt(split[0].substring(2))) {
//说明已经过了一天,要删掉一天,重新赋值,
//0 1 2 3 4
//4:19,4:20,4:21,4:22,4:23
int cont = 0;
date = LocalDate.of(date.getYear(), Integer.parseInt(split[1].substring(0, 1)), Integer.parseInt(split[1].substring(2)));
// System.out.println(date);//ok
String str = "";
while (cont != 5) {
String s = "" + date.getDayOfWeek();
if (!(s.equals("SATURDAY") || s.equals("SUNDAY"))) {
str += date.getMonthValue() + ":" + date.getDayOfMonth() + ",";
cont++;
date = date.plusDays(1);
} else {
date = date.plusDays(1);
}
}
str = str.substring(0, str.length() - 1);
// //数据更新完毕,插入数据库
mapper.updateTime(str);
TimeIsNew = true;
}
}
}