package com.amms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amms.mapper.ReservationTimeSlotMapper;
import com.amms.domain.ReservationTimeSlot;
import com.amms.service.IReservationTimeSlotService;
import com.amms.security.SecurityUtils;

/**
 * 预约时段Service业务层处理
 */
@Service
public class ReservationTimeSlotServiceImpl implements IReservationTimeSlotService {

    @Autowired
    private ReservationTimeSlotMapper reservationTimeSlotMapper;

    /**
     * 查询预约时段列表
     *
     * @param reservationTimeSlot 预约时段
     * @return 预约时段
     */
    @Override
    public List<ReservationTimeSlot> selectReservationTimeSlotList(ReservationTimeSlot reservationTimeSlot) {
        return reservationTimeSlotMapper.selectReservationTimeSlotList(reservationTimeSlot);
    }

    /**
     * 查询预约时段
     *
     * @param id 预约时段主键
     * @return 预约时段
     */
    @Override
    public ReservationTimeSlot selectReservationTimeSlotById(Long id) {
        return reservationTimeSlotMapper.selectReservationTimeSlotById(id);
    }

    /**
     * 新增预约时段
     *
     * @param reservationTimeSlot 预约时段
     * @return 结果
     */
    @Override
    public int insertReservationTimeSlot(ReservationTimeSlot reservationTimeSlot) {
        reservationTimeSlot.setCreateTime(new Date());
        reservationTimeSlot.setCreator(SecurityUtils.getCurrentUser().getId());
        return reservationTimeSlotMapper.insertReservationTimeSlot(reservationTimeSlot);
    }

    /**
     * 修改预约时段
     *
     * @param reservationTimeSlot 预约时段
     * @return 结果
     */
    @Override
    public int updateReservationTimeSlot(ReservationTimeSlot reservationTimeSlot) {
        return reservationTimeSlotMapper.updateReservationTimeSlot(reservationTimeSlot);
    }

    @Override
    public int batchInsertReservationTimeSlots(Date startDate, Date endDate, Date startTime, Date endTime, Integer maxPeople, Long status) {
        if (startDate == null || endDate == null) {
            return 0;
        }
        int count = 0;
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        Long creator = SecurityUtils.getCurrentUser().getId();
        Calendar calStart = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        if (startTime != null) calStart.setTime(startTime);
        if (endTime != null) calEnd.setTime(endTime);
        while (!cal.getTime().after(endDate)) {
            Calendar dayStart = (Calendar) cal.clone();
            dayStart.set(Calendar.HOUR_OF_DAY, calStart.get(Calendar.HOUR_OF_DAY));
            dayStart.set(Calendar.MINUTE, calStart.get(Calendar.MINUTE));
            dayStart.set(Calendar.SECOND, calStart.get(Calendar.SECOND));
            dayStart.set(Calendar.MILLISECOND, 0);

            Calendar dayEnd = (Calendar) cal.clone();
            dayEnd.set(Calendar.HOUR_OF_DAY, calEnd.get(Calendar.HOUR_OF_DAY));
            dayEnd.set(Calendar.MINUTE, calEnd.get(Calendar.MINUTE));
            dayEnd.set(Calendar.SECOND, calEnd.get(Calendar.SECOND));
            dayEnd.set(Calendar.MILLISECOND, 0);

            Date startDt = dayStart.getTime();
            Date endDt = dayEnd.getTime();

            ReservationTimeSlot query = new ReservationTimeSlot();
            query.setDate(cal.getTime());
            List<ReservationTimeSlot> existing = reservationTimeSlotMapper.selectReservationTimeSlotList(query);

            if (existing != null && !existing.isEmpty()) {
                ReservationTimeSlot slot = new ReservationTimeSlot();
                slot.setId(existing.get(0).getId());
                slot.setDate(cal.getTime());
                slot.setStartTime(startDt);
                slot.setEndTime(endDt);
                slot.setMaxPeople(maxPeople);
                slot.setStatus(status);
                count += reservationTimeSlotMapper.updateReservationTimeSlot(slot);
            } else {
                ReservationTimeSlot slot = new ReservationTimeSlot();
                slot.setDate(cal.getTime());
                slot.setStartTime(startDt);
                slot.setEndTime(endDt);
                slot.setMaxPeople(maxPeople);
                slot.setCurrentPeople(0);
                slot.setStatus(status);
                slot.setCreateTime(new Date());
                slot.setCreator(creator);
                count += reservationTimeSlotMapper.insertReservationTimeSlot(slot);
            }
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        return count;
    }

    /**
     * 删除预约时段信息
     *
     * @param id 预约时段主键
     * @return 结果
     */
    @Override
    public int deleteReservationTimeSlotById(Long id) {
        return reservationTimeSlotMapper.deleteReservationTimeSlotById(id);
    }

}
