package com.jxy.marketall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.marketall.entity.Arh99Day;
import com.jxy.marketall.mapper.Arh99DayMapper;
import com.jxy.marketall.service.Arh99DayService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description arh99最新24小时数据
 * @Date 2022/6/20 19:33
 * @Author 杜懿
 */
@Service
public class Arh99DayServiceImpl extends ServiceImpl<Arh99DayMapper, Arh99Day> implements Arh99DayService {
    @Override
    public AjaxResult findArh99Day() {

        LambdaQueryWrapper<Arh99Day> findAllLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Arh99Day> allDataList = baseMapper.selectList(findAllLambdaQueryWrapper);

        //获取最后一条数据时间前一天的时间
        Date newDate = allDataList.get(allDataList.size() - 1).getDate();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(newDate);
        calendar.add(calendar.DATE,-1);
        calendar.add(calendar.SECOND,+58);
        Date date = calendar.getTime();

        //时间转为yyyy-MM-dd HH:mm:ss格式的字符串

        //构造查询条件
        LambdaQueryWrapper<Arh99Day> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(!StringUtils.isEmpty(date.toString()),Arh99Day::getDate,date)
                .le(!StringUtils.isEmpty(newDate.toString()),Arh99Day::getDate,newDate)
                .orderByAsc(Arh99Day::getDate);

        List<Arh99Day> list = baseMapper.selectList(lambdaQueryWrapper);

        List<Arh99Day> dataList = new ArrayList<>();
        dataList.add(list.get(0));
        int index = 0;

        for(int i=1; i < list.size(); i++){
            Arh99Day nowData = list.get(i);
            Arh99Day returnData = dataList.get(index);
            if(returnData.getDate().getMinutes() != nowData.getDate().getMinutes()){
                //分钟数不相等才能放入返回队列
                dataList.add(nowData);
                index++;
            }
        }

        List<Arh99Day> returnList = new ArrayList<>();
        for(int i=0; i < dataList.size(); ){
            returnList.add(dataList.get(i));
            i += 5;
        }
        return AjaxResult.success(returnList);
    }
}
