package com.fizz.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fizz.business.domain.ShiftHistory;
import com.fizz.business.mapper.ShiftHistoryMapper;
import com.fizz.business.service.ShiftHistoryService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShiftHistoryServiceImpl extends ServiceImpl<ShiftHistoryMapper, ShiftHistory> implements ShiftHistoryService {
    @Override
    public ShiftHistory getCurrent() {
        QueryWrapper<ShiftHistory> queryWrapper = new QueryWrapper<>();
        return this.getOne(queryWrapper.orderByDesc("insdate").last("LIMIT 1"));
    }

    @Override
    public List<ShiftHistory> getShiftCurrentListByDate(LocalDateTime startTime, LocalDateTime endTime) {
        return lambdaQuery()
                .ge(ShiftHistory::getInsdate, startTime)
                .lt(ShiftHistory::getInsdate, endTime)
                .isNotNull(ShiftHistory::getCrew)
                .orderByDesc(ShiftHistory::getInsdate)
                .list();
    }

    @Override
    public List<String> getCrews() {
        QueryWrapper<ShiftHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT crew");
        List<ShiftHistory> shiftCurrentList = baseMapper.selectList(queryWrapper);

        return shiftCurrentList
                .stream()
                .map(ShiftHistory::getCrew)
                .collect(Collectors.toList());
    }
}
