package com.younike.system.service.impl;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.time.LocalDateTime;

import com.google.common.collect.Lists;
import com.younike.common.utils.SecurityUtils;
import com.younike.system.domain.ServerTimeSection;
import com.younike.system.service.IServerTimeSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.younike.system.mapper.ServerTimeBookMapper;
import com.younike.system.domain.ServerTimeBook;
import com.younike.system.service.IServerTimeBookService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务人员预约信息Service业务层处理
 * 
 * @author chenpf
 * @date 2022-02-17
 */
@Service
public class ServerTimeBookServiceImpl implements IServerTimeBookService {
    @Autowired
    private ServerTimeBookMapper serverTimeBookMapper;
    @Autowired
    private IServerTimeSectionService serverTimeSectionService;

    /**
     * 查询服务人员预约信息
     * 
     * @param id 服务人员预约信息主键
     * @return 服务人员预约信息
     */
    @Override
    public ServerTimeBook selectServerTimeBookById(Long id) {
        return serverTimeBookMapper.selectServerTimeBookById(id);
    }

    /**
     * 查询服务人员预约信息列表
     * 
     * @param serverTimeBook 服务人员预约信息
     * @return 服务人员预约信息
     */
    @Override
    public List<ServerTimeBook> selectServerTimeBookList(ServerTimeBook serverTimeBook) {
        return serverTimeBookMapper.selectServerTimeBookList(serverTimeBook);
    }

    /**
     * 新增服务人员预约信息
     * 
     * @param serverTimeBook 服务人员预约信息
     * @return 结果
     */
    @Override
    public int insertServerTimeBook(ServerTimeBook serverTimeBook) {
        serverTimeBook.setCreateTime(LocalDateTime.now());
        return serverTimeBookMapper.insertServerTimeBook(serverTimeBook);
    }

    /**
     * 修改服务人员预约信息
     * 
     * @param serverTimeBook 服务人员预约信息
     * @return 结果
     */
    @Override
    public int updateServerTimeBook(ServerTimeBook serverTimeBook) {
        return serverTimeBookMapper.updateServerTimeBook(serverTimeBook);
    }

    /**
     * 批量删除服务人员预约信息
     * 
     * @param ids 需要删除的服务人员预约信息主键
     * @return 结果
     */
    @Override
    public int deleteServerTimeBookByIds(Long[] ids) {
        return serverTimeBookMapper.deleteServerTimeBookByIds(ids);
    }

    /**
     * 删除服务人员预约信息信息
     * 
     * @param id 服务人员预约信息主键
     * @return 结果
     */
    @Override
    public int deleteServerTimeBookById(Long id)
    {
        return serverTimeBookMapper.deleteServerTimeBookById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generate() {
        long interval = 7L;
        LocalDateTime today = LocalDate.now().atStartOfDay();
        LocalDateTime insertDay = today.plusDays(interval);
        // 1. 获取该日期所有服务人员的服务时间配置
        Integer dayOfWeek = DayOfWeek.SUNDAY.equals(insertDay.getDayOfWeek()) ? 0: insertDay.getDayOfWeek().getValue();
        List<ServerTimeSection> sectionList = serverTimeSectionService.selectAllSectionBy(dayOfWeek);

        // 2. 根据起始时间生成时间段
        List<ServerTimeBook> bookList = Lists.newArrayList();
        sectionList.stream().forEach(serverTimeSection -> {
            LocalTime startTime = serverTimeSection.getHoursFrom();
            LocalTime endTime = serverTimeSection.getHoursEnd();
            while(startTime.isBefore(endTime)) {
                ServerTimeBook serverTimeBook = new ServerTimeBook();
                serverTimeBook.setServerId(Long.valueOf(serverTimeSection.getServerId()));
                serverTimeBook.setServerDate(insertDay);
                serverTimeBook.setWeek(dayOfWeek);
                serverTimeBook.setStartTime(startTime);
                LocalTime next = startTime.plusMinutes(30L);
                serverTimeBook.setEndTime(next);
                serverTimeBook.setBooked(0);
                serverTimeBook.setCreateBy("admin");
                serverTimeBook.setCreateTime(LocalDateTime.now());
                bookList.add(serverTimeBook);
                startTime = next;
            }
        });
        serverTimeBookMapper.removeBookedListAfter(insertDay);
        serverTimeBookMapper.insertBatch(bookList);

        LocalDateTime deleteDay = today.minusDays(interval);
        serverTimeBookMapper.removeBookedListBefore(deleteDay);
    }

    /**
     * @param serverId 服务人员ID
     * @param start 预约开始时间
     * @param end 预约结束时间（包含Gap Time）
     */
    public List<ServerTimeBook> selectAvailableServerTimeBookList(Long serverId, LocalDate date, LocalTime start, LocalTime end){
        return serverTimeBookMapper.selectAvailableServerTimeBookList(serverId, date, start, end);
    }

    @Override
    public void updateServerTimeBooks(List<ServerTimeBook> list) {
        serverTimeBookMapper.updateServerTimeBooks(list);
    }
}
