package com.xinelu.applet.controller.appletscheduleplan;

import com.xinelu.applet.dto.scheduleplan.SchedulePlanDTO;
import com.xinelu.applet.dto.scheduleplan.SchedulePlanDetailDTO;
import com.xinelu.applet.service.scheduleplan.IAppletSchedulePlanService;
import com.xinelu.applet.utils.AppointmentTimeUtil;
import com.xinelu.applet.vo.specialdisease.WeekDaysVO;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.scheduleplandetail.SchedulePlanDetail;
import com.xinelu.manage.service.scheduleplandetail.ISchedulePlanDetailService;
import com.xinelu.manage.vo.scheduleplan.SchedulePlanVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 小程序医生排班计划控制器
 * @author: haown
 * @create: 2023-09-22 10:28
 **/
@Api(tags = "小程序医生排班计划控制器")
@RestController
@RequestMapping("/nurseApplet/schedule/plan")
public class AppletSchedulePlanController extends BaseController {

	@Resource
	private IAppletSchedulePlanService appletPlanService;

	@Resource
	private ISchedulePlanDetailService schedulePlanDetailService;

	@Resource
	private AppointmentTimeUtil appointmentTimeUtil;

	@ApiOperation("查询医生排班明细")
	@GetMapping("/getListByDoctor")
	public R<List<SchedulePlanVO>> getListByDoctor(SchedulePlanDTO plan) {
		if (plan.getDoctorId() == null) {
			return R.fail("请选择医生");
		}
		LocalDate nowTime = LocalDate.now();
		List<WeekDaysVO> weekDaysList = Lists.newArrayList();
		appointmentTimeUtil.setWeekDayAndWeekDate(nowTime, weekDaysList);
		plan.setWeekdays(weekDaysList);
		List<SchedulePlanVO> list = appletPlanService.getListByDoctor(plan);
		return R.ok(list);
	}

	@PostMapping("/updateStatus")
	@ApiOperation(value = "医生批量修改排班时间段的可预约状态")
	public R<String> batchUpdateStatus(@RequestBody SchedulePlanDetailDTO schedulePlanDetailDTO) {
		if (CollectionUtils.isEmpty(schedulePlanDetailDTO.getSchedulePlanDetailIds())) {
			return R.fail("请选择时间段");
		}
		int timeoutCount = 0;
		List<SchedulePlanDetail> planDetailList = new ArrayList<>();
		for (long planDetailId : schedulePlanDetailDTO.getSchedulePlanDetailIds()) {
			SchedulePlanDetail planDetail = schedulePlanDetailService.getById(planDetailId);
			if (planDetail != null) {
				DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
				// 判断当前时间段是否已有居民预约
				if (StringUtils.equals("1", planDetail.getApplyState()) && StringUtils.equals("1", schedulePlanDetailDTO.getStatus())) {
					return R.fail(planDetail.getScheduleStartTime().format(formatter) + "~" + planDetail.getScheduleEndTime().format(formatter) + "已有居民预约，请取消订单后再修改");
				}
				// 判断时间段是否已过
				if (planDetail.getScheduleDate().equals(LocalDate.now()) && planDetail.getScheduleStartTime().isBefore(LocalTime.now())) {
					timeoutCount++;
				}
				planDetail.setStatus(schedulePlanDetailDTO.getStatus());
				planDetailList.add(planDetail);
			}
		}

		if (timeoutCount == schedulePlanDetailDTO.getSchedulePlanDetailIds().size()) {
			return R.fail("该时间段已过不可修改");
		}
		int flag = 0;
		if (CollectionUtils.isNotEmpty(planDetailList)) {
			flag = schedulePlanDetailService.batchUpdate(planDetailList);
		}
		return flag < 0 ? R.fail() : R.ok();
	}
}
