package io.hmit.modules.appointment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import io.hmit.common.constant.Constant;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.common.utils.DateUtils;
import io.hmit.modules.appointment.dao.AppointmentTimeManageDao;
import io.hmit.modules.appointment.dto.AppointmentOrderDTO;
import io.hmit.modules.appointment.dto.AppointmentTimeManageDTO;
import io.hmit.modules.appointment.dto.CalendarDTO;
import io.hmit.modules.appointment.dto.ReservationTimeDTO;
import io.hmit.modules.appointment.entity.AppointmentTimeManageEntity;
import io.hmit.modules.appointment.service.AppointmentTimeManageService;
import io.hmit.modules.appointment.service.CalendarService;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.apache.commons.lang3.Conversion;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 预约时间管理
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-02-15
 */
@Service
public class AppointmentTimeManageServiceImpl extends CrudServiceImpl<AppointmentTimeManageDao, AppointmentTimeManageEntity, AppointmentTimeManageDTO> implements AppointmentTimeManageService {

    @Autowired
    private CalendarService calendarService;

    @Autowired
    private AppointmentTimeManageService appointmentTimeManageService;

    @Override
    public QueryWrapper<AppointmentTimeManageEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String useState = (String)params.get("useState");

        QueryWrapper<AppointmentTimeManageEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(useState), "use_state", useState);

        return wrapper;
    }

    @Override
    public AppointmentTimeManageDTO getByUseState(){
        AppointmentTimeManageEntity entity = baseDao.selectOne(new QueryWrapper<AppointmentTimeManageEntity>()
                .lambda().eq( AppointmentTimeManageEntity::getUseState, "1"));

        return ConvertUtils.sourceToTarget(entity, AppointmentTimeManageDTO.class);
    }

    @Override
    public List<ReservationTimeDTO> getReservationTimeList() throws ParseException {

        AppointmentTimeManageDTO timeManageDTO = appointmentTimeManageService.getByUseState();
        String startTime = timeManageDTO.getStartTime();
        String endTime = timeManageDTO.getEndTime();
        String period = timeManageDTO.getPeriod();

        List<AppointmentTimeManageDTO> timeManageDTOS = new ArrayList<>();
        List<String> timePeriods = new ArrayList<>();

        List<String> amLists = DateUtils.getTimePeriod(startTime,Constant.AM_END_TIME,period);
        List<String> pmLists = DateUtils.getTimePeriod(Constant.PM_START_TIME,endTime,period);
        timePeriods.addAll(amLists);
        timePeriods.addAll(pmLists);

        timePeriods.stream().forEach(item->{
            AppointmentTimeManageDTO dto = new AppointmentTimeManageDTO();
            dto.setReservationTime(item);
            timeManageDTOS.add(dto);
        });


        List<ReservationTimeDTO> reservationTimeDTOList = new ArrayList<>();

        List<CalendarDTO> dayLists = calendarService.weekDayList().subList(0,6);
        dayLists.stream().forEach(item->{
            ReservationTimeDTO reservationTime = new ReservationTimeDTO();
            reservationTime.setReservationTime(item.getDay());
            reservationTime.setPeriods(timeManageDTOS);
            reservationTimeDTOList.add(reservationTime);
        });


        return reservationTimeDTOList;
    }

}
