package com.hzya.frame.plugin.lets.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.hzya.frame.plugin.lets.queryvo.StartAndEndVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：liuyang
 * @Package：com.hzya.frame.plugin.lets.util
 * @Project：kangarooDataCenterV3
 * @name：OffsetTimeTime
 * @Date：2024/8/19 16:27
 * @Filename：OffsetTimeTime
 */
@Component
public class OffsetTimeTime {

    Logger logger = LoggerFactory.getLogger(OffsetTimeTime.class);

    /**
     * 实时执行，计算时间
     *
     * @author liuyang
     */
    public StartAndEndVo offsetTime() {
        StartAndEndVo startAndEndVo = new StartAndEndVo();
        Date currentDate = new Date();
        Date startTime = DateUtil.offset(currentDate, DateField.MINUTE, -10);
        Date endTime = DateUtil.offset(currentDate, DateField.MINUTE, -2);

        String startTimeStr = DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss");
        String endTimeStr = DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss");

        startAndEndVo.setStart_time(startTimeStr);
        startAndEndVo.setEnd_time(endTimeStr);

        return startAndEndVo;
    }

    /**
     * 计算时间间隔
     *
     * @author liuyang
     */
    public String computingTime(String param) {
        if (param != null && !"".equals(param)) {
            String[] split = param.split("/");
            if (!(split.length == 2)) {
                Assert.state(false, "传递的日期格式不完整 格式参考：2024-04-01/2024-04-30");
            }

            LocalDate startDate = LocalDate.parse(split[0]);
            LocalDate endDate = LocalDate.parse(split[1]);

            List<String> stringArrayList = new ArrayList<>();
            List<String> dateRange = getDateRange(startDate, endDate);
            if (dateRange != null && dateRange.size() > 0) {
                for (int i = 0; i < dateRange.size(); i++) {
                    String dateStr = dateRange.get(i);
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append(dateStr);
                    stringBuffer.append(" 00:00:00");
                    stringBuffer.append(",");
                    stringBuffer.append(dateStr);
                    stringBuffer.append(" 23:59:59");
                    stringArrayList.add(stringBuffer.toString());
                }
            }
            String collectDateStr = stringArrayList.stream().collect(Collectors.joining("*"));
            logger.info("类型：time_frame 生成的日期范围：" + collectDateStr);
            return collectDateStr;
        }
        return null;
    }

    /**
     * 生成范围
     */
    private List<String> getDateRange(LocalDate startDate, LocalDate endDate) {
        List<String> dateRange = new ArrayList<>();
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            dateRange.add(currentDate.format(DateTimeFormatter.ISO_DATE));
            currentDate = currentDate.plusDays(1);
        }
        return dateRange;
    }
}
