package com.jwipc.order.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.order.entity.AppointmentTime;
import com.jwipc.order.mapper.AppointmentTimeMapper;
import com.jwipc.order.model.po.AppointmentTime2PO;
import com.jwipc.order.model.po.AppointmentTimePO;
import com.jwipc.order.model.query.AppointmentRecordQuery;
import com.jwipc.order.model.query.AppointmentTimeQuery;
import com.jwipc.order.service.IAppointmentTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 预约时间范围数据配置 服务实现类
 * </p>
 *
 * @author WT
 * @since 2024-03-05
 */
@Service
public class AppointmentTimeServiceImpl extends ServiceImpl<AppointmentTimeMapper, AppointmentTime> implements IAppointmentTimeService {

    @Override
    public List<AppointmentTimePO> findPageByQuery(Page<AppointmentTime> dbPage, AppointmentTimeQuery query) {
        if(dbPage == null){
            return baseMapper.findPageByQuery(query);
        }
        return baseMapper.findPageByQuery(dbPage,query);
    }

    @Override
    public List<AppointmentTime2PO> getTimeRangeData(AppointmentRecordQuery query) {
        return baseMapper.getTimeRangeData(query);
    }

    @Override
    public boolean checkAppointmentTime(String selectDay, Long appointmentId) {
        AppointmentTime appointmentTime = this.getById(appointmentId);
        String endTime = appointmentTime.getEndTime();

        DateTime couldEndTime = DateUtil.parse(selectDay + ' ' + endTime, DatePattern.NORM_DATETIME_MINUTE_FORMAT);
        DateTime now = DateUtil.parse(DateUtil.now(), DatePattern.NORM_DATETIME_MINUTE_FORMAT);
        if (now.compareTo(couldEndTime) > 0) {
            return false;
        }
        return true;
    }
}
