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.DateUtils;
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.entity.Jzr60Day;
import com.jxy.marketall.mapper.Jzr60DayMapper;
import com.jxy.marketall.service.Jzr60DayService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description TODO
 * @Date 2022/6/21 12:13
 * @Author 杜懿
 */
@Service
public class Jzr60DayServiceImpl extends ServiceImpl<Jzr60DayMapper, Jzr60Day> implements Jzr60DayService {
    @Override
    public AjaxResult findJzr60Day() {

        LambdaQueryWrapper<Jzr60Day> Jzr60DayAllLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Jzr60Day> findAllDataList = baseMapper.selectList(Jzr60DayAllLambdaQueryWrapper);

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


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

        List<Jzr60Day> list = baseMapper.selectList(Jzr60DayLambdaQueryWrapper);

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

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

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

        return AjaxResult.success(returnList);
    }
}
