package com.xinelu.applet.service.scheduleplan.impl;

import com.xinelu.applet.dto.scheduleplan.SchedulePlanDTO;
import com.xinelu.applet.service.scheduleplan.IAppletSchedulePlanService;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.scheduleplandetail.SchedulePlanDetail;
import com.xinelu.manage.mapper.scheduleplandetail.SchedulePlanDetailMapper;
import com.xinelu.manage.vo.scheduleplan.SchedulePlanVO;
import com.xinelu.manage.vo.scheduleplandetail.SchedulePlanDetailVO;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

/**
* @author haown
* @description 针对表【schedule_plan(医生排班计划表)】的数据库操作Service实现
* @createDate 2023-09-21 17:16:40
*/
@Service
public class AppletSchedulePlanServiceImpl implements IAppletSchedulePlanService {

	@Resource
	private SchedulePlanDetailMapper planDetailMapper;


	@Override public List<SchedulePlanVO> getListByDoctor(SchedulePlanDTO schedulePlan) {
		List<SchedulePlanVO> retList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(schedulePlan.getWeekdays())) {
			for (int i = 0; i < schedulePlan.getWeekdays().size(); i++) {
				// 查询排班明细表
				SchedulePlanDetail query = new SchedulePlanDetail();
				query.setDoctorId(schedulePlan.getDoctorId());
				query.setScheduleDate(schedulePlan.getWeekdays().get(i).getHealthConsultationDate());
				List<SchedulePlanDetail> detailList = planDetailMapper.getList(query);
				List<SchedulePlanDetailVO> morningList = new ArrayList<>();
				List<SchedulePlanDetailVO> afternoonList = new ArrayList<>();
				LocalDate scheduleDate = schedulePlan.getWeekdays().get(i).getHealthConsultationDate();
				if (CollectionUtils.isNotEmpty(detailList)) {
					detailList.forEach(detail -> {
						// 可预约状态
						boolean status = true;
						if (scheduleDate.isEqual(LocalDate.now()) && detail.getScheduleStartTime().isBefore(LocalTime.now())) {
							status = false;
						} else if (StringUtils.equals("1", detail.getStatus()) || StringUtils.equals("1", detail.getApplyState())) {
							status = false;
						}

						DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
						if (StringUtils.equals("1", detail.getType())) {
							morningList.add(SchedulePlanDetailVO.builder().schedulePlanDetailId(detail.getId())
								.scheduleTimeSlot(detail.getScheduleStartTime().format(formatter) + "~" + detail.getScheduleEndTime().format(formatter))
								.status(status).build());
						}
						if (StringUtils.equals("2", detail.getType())) {
							afternoonList.add(SchedulePlanDetailVO.builder().schedulePlanDetailId(detail.getId())
								.scheduleTimeSlot(detail.getScheduleStartTime().format(formatter) + "~" + detail.getScheduleEndTime().format(formatter))
								.status(status).build());
						}
					});
				}
				SchedulePlanVO schedulePlanVO = SchedulePlanVO.builder()
					.scheduleDate(schedulePlan.getWeekdays().get(i).getHealthConsultationDate())
					.week(schedulePlan.getWeekdays().get(i).getWeek())
					.morningList(morningList)
					.afternoonList(afternoonList).build();

				retList.add(schedulePlanVO);
			}
		}
		return retList;
	}

	/**
	 * 生成计划明细
	 * @param number：人数
	 * @param startTime：开始时间
	 * @return
	 */
	private List<SchedulePlanDetail> genPlanDetail(SchedulePlanDetail schedulePlanDetail, int number, LocalTime startTime, int minutesPerPatient) {
		List<SchedulePlanDetail> planDetails = new ArrayList<>();
		for (int i = 0; i < number; i++) {
			SchedulePlanDetail planDetail = new SchedulePlanDetail();
			BeanUtils.copyBeanProp(planDetail, schedulePlanDetail);
			planDetail.setScheduleStartTime(startTime.plusMinutes((long) minutesPerPatient * i));
			planDetail.setScheduleEndTime(startTime.plusMinutes((long) minutesPerPatient * (i + 1)));
			planDetail.setApplyState("0");
			planDetails.add(planDetail);
		}
		return planDetails;
	}
}




