package com.energy.manage.service.service.chartsdata.impl;

import com.energy.manage.common.po.chartsdata.ChartsDataPo;
import com.energy.manage.common.util.UUIDTools;
import com.energy.manage.service.domain.dto.analysis.PlotlyDataItem;
import com.energy.manage.service.mappers.chartsdata.ChartsDataMapper;
import com.energy.manage.service.service.chartsdata.ChartsDataService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chy
 * @date 2024/8/15 10:25
 * @desc
 */
@Slf4j
@Service
public class ChartsDataServiceImpl implements ChartsDataService {

    @Autowired
    private ChartsDataMapper chartsDataMapper;

    @Override
    public List<PlotlyDataItem> queryChartsData(Integer pageNum) {
        List<PlotlyDataItem> plotlyDataItems = new ArrayList<>();
        long time = System.currentTimeMillis();
        int startNum = (pageNum - 1) * PAGE_SIZE;
        List<ChartsDataPo> chartsDataPos = chartsDataMapper.queryData(startNum,PAGE_SIZE);
        log.info("数据库执行时间time = {}",System.currentTimeMillis() - time);
        for(int i = 0 ; i < chartsDataPos.size();i++){
            if(i % 10 == 0){
                PlotlyDataItem plotlyDataItem = new PlotlyDataItem();
                plotlyDataItem.setEngineCode(UUIDTools.getShortUUID())
                        .setEngineName(UUIDTools.getShortUUID());
                plotlyDataItems.add(plotlyDataItem);
            }
            PlotlyDataItem plotlyDataItem = plotlyDataItems.get(plotlyDataItems.size() -1);
            plotlyDataItem.getXData().add(chartsDataPos.get(i).getActivePower());
            plotlyDataItem.getYData().add(chartsDataPos.get(i).getTrueWindDirection());
        }
        return plotlyDataItems;
    }

    @Override
    public int getChartsDataCount() {
        int count = chartsDataMapper.selectAllCount();
        return count / PAGE_SIZE;
    }

    @Override
    public List<PlotlyDataItem> queryChartsDataByTime(String timeStamp) {
        List<PlotlyDataItem> plotlyDataItems = new ArrayList<>();
        long time = System.currentTimeMillis();
        List<ChartsDataPo> chartsDataPos = chartsDataMapper.queryDataByTime(timeStamp, PAGE_SIZE);
        log.info("数据库执行时间time = {}",System.currentTimeMillis() - time);
        PageInfo<ChartsDataPo> pageInfo = new PageInfo<>(chartsDataPos);
        pageInfo.setTotal(pageInfo.getTotal()/PAGE_SIZE);
        for(int i = 0 ; i < chartsDataPos.size();i++){
            if(i % 10 == 0){
                PlotlyDataItem plotlyDataItem = new PlotlyDataItem();
                plotlyDataItem.setEngineCode(UUIDTools.getShortUUID())
                        .setEngineName(UUIDTools.getShortUUID());
                plotlyDataItems.add(plotlyDataItem);
            }
            PlotlyDataItem plotlyDataItem = plotlyDataItems.get(plotlyDataItems.size() -1);
            plotlyDataItem.getXData().add(chartsDataPos.get(i).getActivePower());
            plotlyDataItem.getYData().add(chartsDataPos.get(i).getTrueWindDirection());
        }
        return plotlyDataItems;
    }
}
