package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisCoal;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisConverter;
import cn.cecep.talroad.mapper.analysis.SRaRealTimeAnalysisConverterMapper;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisConverterService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisConverterQuery;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 转炉产量分析Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-24*/
@Service
public class SRaRealTimeAnalysisConverterServiceImpl extends ServiceImpl<SRaRealTimeAnalysisConverterMapper, SRaRealTimeAnalysisConverter> implements ISRaRealTimeAnalysisConverterService
{
    @Autowired
    private SRaRealTimeAnalysisConverterMapper sRaRealTimeAnalysisConverterMapper;

    /**
     * 查询转炉产量分析列表
     *
     * @param sRaRealTimeAnalysisConverter 转炉产量分析
     * @return 转炉产量分析
     */
    @Override
    public List<SRaRealTimeAnalysisConverter> selectSRaRealTimeAnalysisConverterList(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter)
    {
        Page<SRaRealTimeAnalysisConverter> page=new Page<>(1,999999);
        return sRaRealTimeAnalysisConverterMapper.selectSRaRealTimeAnalysisConverterList(page,sRaRealTimeAnalysisConverter);
    }

    @Override
    public List<Map<String, Object>> pullDeviceNameOptions(String factoryName) {
        return sRaRealTimeAnalysisConverterMapper.pullDeviceNameOptions(factoryName);
    }

    @Override
    public IPage<SRaRealTimeAnalysisConverter> statisticsList(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter) {
        Page<SRaRealTimeAnalysisConverter> iPage=new  Page<SRaRealTimeAnalysisConverter>(sRaRealTimeAnalysisConverter.getPageNum(),sRaRealTimeAnalysisConverter.getPageSize());
        List<SRaRealTimeAnalysisConverter> list=new ArrayList<>();
        if(sRaRealTimeAnalysisConverter.getType().equals("1")){
            list=sRaRealTimeAnalysisConverterMapper.statisticsList(iPage,sRaRealTimeAnalysisConverter);
        }else {
            list=sRaRealTimeAnalysisConverterMapper.statisticsListByDevice(iPage,sRaRealTimeAnalysisConverter);
        }
        list.forEach(item->{
            item.setAnalyseTimeStr(sRaRealTimeAnalysisConverter.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisConverter.getAnalyseEndTime());
        });
        iPage.setRecords(list);
        return iPage;
    }

    @Override
    public List<SRaRealTimeAnalysisConverter> statisticsExport(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter) {
        Page<SRaRealTimeAnalysisConverter> iPage=new  Page<SRaRealTimeAnalysisConverter>(1,999999);
        List<SRaRealTimeAnalysisConverter> list=new ArrayList<>();
        if(sRaRealTimeAnalysisConverter.getType().equals("1")){
            list=sRaRealTimeAnalysisConverterMapper.statisticsList(iPage,sRaRealTimeAnalysisConverter);
        }else {
            list=sRaRealTimeAnalysisConverterMapper.statisticsListByDevice(iPage,sRaRealTimeAnalysisConverter);
        }
        list.forEach(item->{
            item.setAnalyseTimeStr(sRaRealTimeAnalysisConverter.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisConverter.getAnalyseEndTime());
        });
        return list;
    }

    @Override
    public EChartsVO resultCurve(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisCoa) {
        EChartsVO eChartsVO=new EChartsVO();
        List<SRaRealTimeAnalysisConverter> sRaRealTimeAnalysisRuns = sRaRealTimeAnalysisConverterMapper.resultCurve(sRaRealTimeAnalysisCoa);
        List<String> list=new ArrayList<>();
        List<EChartsVO.SeriesObj> items= new ArrayList<>();
        EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
        ser.setName("出钢量");
        ser.setProp("tapNumber");
        ser.setUnit("");
        ser.setValues(new ArrayList<>());
        items.add(ser);
        EChartsVO.SeriesObj ser1 = new EChartsVO.SeriesObj();
        ser1.setName("炉数");
        ser1.setProp("converterNumber");
        ser1.setUnit("");
        ser1.setValues(new ArrayList<>());
        items.add(ser1);
        sRaRealTimeAnalysisRuns.forEach(item->{
            list.add(DateUtil.format(item.getAnalyseTime(), DatePattern.NORM_DATETIME_FORMATTER));
            items.get(0).getValues().add(item.getTapNumber());
            items.get(1).getValues().add(item.getConverterNumber());
        });
        eChartsVO.setXData(list);
        eChartsVO.setSeries(items);
        return eChartsVO;
    }
}
