package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.constants.Constants;
import com.zbkj.common.model.deliveryset.DeliverySetTime;
import com.zbkj.common.response.DeliveryTimeResponse;
import com.zbkj.common.utils.DeliveryUtil;
import com.zbkj.service.dao.DeliverySetTimeDao;
import com.zbkj.service.service.DeliverySetTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;

@Service
public class DeliverySetTimeServiceImpl extends ServiceImpl<DeliverySetTimeDao, DeliverySetTime> implements DeliverySetTimeService {

    @Autowired private DeliverySetTimeDao dao;

    @Override
    public List<DeliveryTimeResponse> getDeliveryTime() {
        LambdaQueryWrapper<DeliverySetTime> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DeliverySetTime::getType, 1);
        lqw.eq(DeliverySetTime::getStatus, Constants.COMMON_SWITCH_OPEN);
        DeliverySetTime deliverySetTime = dao.selectOne(lqw);
        // 设置起始日期和天数
        LocalDate startDate = LocalDate.now();; // 起始日期
        int numberOfDays = deliverySetTime.getAppointDay(); // 需要生成的天数
        int splitMin = deliverySetTime.getSendSplit();      // 间隔分钟数
        String[] amSplit = deliverySetTime.getAmSendHour().split("-");
        String[] pmSplit = deliverySetTime.getPmSendHour().split("-");
        // 定义上午和下午的时间区间
        LocalTime morningStart = LocalTime.of(Integer.parseInt(amSplit[0]), 0);  // 上午开始时间
        LocalTime morningEnd = LocalTime.of(Integer.parseInt(amSplit[1]), 0);   // 上午结束时间
        LocalTime afternoonStart = LocalTime.of(Integer.parseInt(pmSplit[0]), 0); // 下午开始时间
        LocalTime afternoonEnd = LocalTime.of(Integer.parseInt(pmSplit[1]), 0);   // 下午结束时间
        // 获取时间段
        return DeliveryUtil.getHourlyTimeIntervals(startDate, numberOfDays, splitMin, morningStart, morningEnd, afternoonStart, afternoonEnd);
    }

}
