查询忙闲信息

This commit is contained in:
俞扬
2023-06-17 18:15:25 +08:00
parent a5ae55675a
commit dcb3d1097c
12 changed files with 520 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
package com.cool.store.service;
import com.cool.store.exception.ApiException;
import com.cool.store.http.ISVHttpRequest;
import com.cool.store.request.GetFreeBusyListReq;
import com.cool.store.vo.interview.GetFreeBusyListVO;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
/**
* @Author: young.yu
* @Date: 2023-06-16 13:13
* @Description:
*/
public interface FeiShuService {
GetFreeBusyListVO getFreeBusyList(GetFreeBusyListReq request) throws ApiException;
}

View File

@@ -0,0 +1,120 @@
package com.cool.store.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cool.store.dto.calendar.UserFreeBusyInfoDTO;
import com.cool.store.entity.HyPartnerInterviewPlanDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.exception.ServiceException;
import com.cool.store.http.ISVHttpRequest;
import com.cool.store.mapper.HyPartnerInterviewPlanMapper;
import com.cool.store.request.GetFreeBusyListReq;
import com.cool.store.service.FeiShuService;
import com.cool.store.vo.interview.CalendarInfo;
import com.cool.store.vo.interview.FreeBusyInfo;
import com.cool.store.vo.interview.GetFreeBusyListVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: young.yu
* @Date: 2023-06-16 13:13
* @Description:
*/
@Service
public class FeiShuServiceImpl implements FeiShuService {
@Autowired
private ISVHttpRequest isvHttpRequest;
@Autowired
private HyPartnerInterviewPlanMapper hyPartnerInterviewPlanMapper;
@Override
public GetFreeBusyListVO getFreeBusyList(GetFreeBusyListReq request) throws ApiException {
GetFreeBusyListVO freeBusyListVO = new GetFreeBusyListVO();
String queryDate = request.getQueryDate();//查询日期
String partnerId = request.getPartnerId();//加盟商用户ID
String interviewerId = request.getInterviewerId();//面试官ID
//如果加盟商用户ID为空且面试官ID为空返回错误信息
if (StringUtils.isEmpty(partnerId) && StringUtils.isEmpty(interviewerId)) {
throw new ServiceException(ErrorCodeEnum.INTERVIEW_AND_PARTNER_ID_IS_NULL);
}
//如果加盟商用户ID不为空且面试官ID为空
if (StringUtils.isNotEmpty(partnerId) && StringUtils.isEmpty(interviewerId)) {
//根据加盟商用户ID查询面试官ID
HyPartnerInterviewPlanDO hyPartnerInterviewPlanDO = new HyPartnerInterviewPlanDO();
hyPartnerInterviewPlanDO.setPartnerId(partnerId);
List<HyPartnerInterviewPlanDO> hyPartnerInterviewPlanDOS = hyPartnerInterviewPlanMapper.selectBySelective(hyPartnerInterviewPlanDO);
//如果查询结果为空,返回错误信息
if (CollectionUtils.isEmpty(hyPartnerInterviewPlanDOS)) {
throw new ServiceException(ErrorCodeEnum.INTERVIEW_PLAN_IS_NULL);
}
interviewerId = hyPartnerInterviewPlanDOS.get(0).getInterviewer();
}
long startTime = 0L;
long endTime = 0L;
DateTime startDateTime = null;
DateTime endDateTime = null;
//如果查询时间为空,取明天
if (StringUtils.isEmpty(queryDate)) {
startDateTime = DateUtil.beginOfDay(DateUtil.tomorrow());
endDateTime = DateUtil.endOfDay(DateUtil.tomorrow());
} else {
startDateTime = DateUtil.beginOfDay(DateUtil.parse(queryDate));
endDateTime = DateUtil.endOfDay(DateUtil.parse(queryDate));
}
if (startDateTime.isAfter(endDateTime)) {
throw new ServiceException(ErrorCodeEnum.DATE_PARAMS_IS_ERROR);
}
startTime = startDateTime.getTime();
endTime = endDateTime.getTime();
List<UserFreeBusyInfoDTO> UserFreeBusyInfoList = isvHttpRequest.getFreeBusyList(interviewerId, startTime, endTime);
//把startTime和endTime转换成yyyy-MM-dd格式
String startTimeStr = DateUtil.format(startDateTime, "yyyy-MM-dd");
String endTimeStr = DateUtil.format(endDateTime, "yyyy-MM-dd");
List<CalendarInfo> daySlots = TimeSlotGenerator.generateDaySlots(startTimeStr, endTimeStr);
freeBusyListVO.setCalendarList(daySlots);
//如果查询结果为空,则直接返回全部时间段都是空闲的
if (!CollectionUtils.isEmpty(UserFreeBusyInfoList)) {
for (CalendarInfo daySlot : daySlots) {
List<FreeBusyInfo> freeBusyList = daySlot.getFreeBusyList();
for (FreeBusyInfo freeBusyInfo : freeBusyList) {
//把日期和时间拼接成yyyy-MM-dd HH:mm格式
String startStr = daySlot.getDateStr() + " " + freeBusyInfo.getStartTime();
String endStr = daySlot.getDateStr() + " " + freeBusyInfo.getEndTime();
//把yyyy-MM-dd HH:mm格式转换成long类型
long startTimeLong = DateUtil.parse(startStr).getTime();
long endTimeLong = DateUtil.parse(endStr).getTime();
for (UserFreeBusyInfoDTO userFreeBusyInfoDTO : UserFreeBusyInfoList) {
//如果查询结果中的开始时间和结束时间在时间段内,则设置为忙碌
if ((startTimeLong > userFreeBusyInfoDTO.getStartTime() && startTimeLong < userFreeBusyInfoDTO.getStartTime())
|| (endTimeLong > userFreeBusyInfoDTO.getStartTime() && endTimeLong < userFreeBusyInfoDTO.getStartTime())) {
freeBusyInfo.setFree(false);
break;
}
if (endTimeLong <= userFreeBusyInfoDTO.getStartTime()) {
break;
}
}
}
}
}
return freeBusyListVO;
}
}

View File

@@ -0,0 +1,88 @@
package com.cool.store.service.impl;
import cn.hutool.core.date.DateUtil;
import com.cool.store.utils.StringUtil;
import com.cool.store.vo.interview.CalendarInfo;
import com.cool.store.vo.interview.FreeBusyInfo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class TimeSlotGenerator {
public static void main(String[] args) {
String startDate = "2023-02-27";
String endDate = "2023-03-01";
List<CalendarInfo> daySlots = generateDaySlots(startDate, endDate);
for (CalendarInfo daySlot : daySlots) {
System.out.println(daySlot);
}
}
public static List<CalendarInfo> generateDaySlots(String startDate, String endDate) {
List<CalendarInfo> daySlots = new ArrayList<>();
Date start = DateUtil.parse(startDate);
Date end = DateUtil.parse(endDate);
Calendar startCal = DateUtil.calendar(start);
Calendar endCal = DateUtil.calendar(end);
while (!startCal.after(endCal)) {
int year = startCal.get(Calendar.YEAR);
int month = startCal.get(Calendar.MONTH) + 1;
int day = startCal.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = startCal.get(Calendar.DAY_OF_WEEK);
List<FreeBusyInfo> hourSlots = generateHourSlots();
CalendarInfo daySlot = new CalendarInfo(transDayOfWeek(dayOfWeek),
String.valueOf(year),
StringUtil.addZeroForNum(String.valueOf(month),2),
StringUtil.addZeroForNum(String.valueOf(day),2),
DateUtil.format(startCal.getTime(), "yyyy-MM-dd"),
hourSlots);
daySlots.add(daySlot);
startCal.add(Calendar.DATE, 1);
}
return daySlots;
}
private static List<FreeBusyInfo> generateHourSlots() {
List<FreeBusyInfo> hourSlots = new ArrayList<>();
for (int hour = 10; hour < 17; hour++) {
String startTime = String.format("%02d:00", hour);
String endTime = String.format("%02d:00", hour + 1);
//默认空闲
FreeBusyInfo hourSlot = new FreeBusyInfo(startTime, endTime, true);
hourSlots.add(hourSlot);
}
return hourSlots;
}
public static int transDayOfWeek(int dayOfWeek) {
switch (dayOfWeek) {
case 1:
return 7;
case 2:
return 1;
case 3:
return 2;
case 4:
return 3;
case 5:
return 4;
case 6:
return 5;
case 7:
return 6;
default:
return 0;
}
}
}