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

import java.math.BigDecimal;
import java.util.*;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.domain.SRaRealTimeAnalysicComsitSt;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysicComsit;
import cn.cecep.talroad.mapper.analysis.SRaRealTimeAnalysicComsitMapper;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysicComsitService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysicComsitMonitorVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysicComsitQuery;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;




/**
 * 关联分析系统-企业生产过程分析-监测点位对比分析-分组Service业务层处理
 *
 * @author szhpt
 * @date 2023-07-14*/
@Service
public class SRaRealTimeAnalysicComsitServiceImpl extends ServiceImpl<SRaRealTimeAnalysicComsitMapper, SRaRealTimeAnalysicComsit> implements ISRaRealTimeAnalysicComsitService
{
    @Autowired
    private SRaRealTimeAnalysicComsitMapper sRaRealTimeAnalysicComsitMapper;

    /**
     * 查询关联分析系统-企业生产过程分析-监测点位对比分析-分组列表
     *
     * @param sRaRealTimeAnalysicComsit 关联分析系统-企业生产过程分析-监测点位对比分析-分组
     * @return 关联分析系统-企业生产过程分析-监测点位对比分析-分组
     */
    @Override
    public List<SRaRealTimeAnalysicComsit> selectSRaRealTimeAnalysicComsitList(SRaRealTimeAnalysicComsit sRaRealTimeAnalysicComsit)
    {
        return sRaRealTimeAnalysicComsitMapper.selectSRaRealTimeAnalysicComsitList(sRaRealTimeAnalysicComsit);
    }

    @Override
    public IPage<SRaRealTimeAnalysicComsitMonitorVo> selectSRaRealTimeAnalysicComsitListBy(SRaRealTimeAnalysicComsitMonitorVo sRaRealTimeAnalysicComsit) {
        Page<SRaRealTimeAnalysicComsitMonitorVo> page = new Page<>(sRaRealTimeAnalysicComsit.getPageNum(), sRaRealTimeAnalysicComsit.getPageSize());
        List<SRaRealTimeAnalysicComsitMonitorVo> list= sRaRealTimeAnalysicComsitMapper.selectSRaRealTimeAnalysicComsitListBy(page,sRaRealTimeAnalysicComsit);
        list.forEach(item->{
            item.setParameterName(sRaRealTimeAnalysicComsit.getParameterName());
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public IPage<SRaRealTimeAnalysicComsit> selectSRaRealTimeAnalysicComsitListBys(SRaRealTimeAnalysicComsitQuery sRaRealTimeAnalysicComsit) {
        Page<SRaRealTimeAnalysicComsit> page = new Page<>(sRaRealTimeAnalysicComsit.getPageNum(), sRaRealTimeAnalysicComsit.getPageSize());
        List<SRaRealTimeAnalysicComsit> list= sRaRealTimeAnalysicComsitMapper.selectSRaRealTimeAnalysicComsitListBys(page,sRaRealTimeAnalysicComsit);
        list.forEach(item->{
            item.setAnalysisTime(sRaRealTimeAnalysicComsit.getAnalysisTime());
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Map<String, Object>> pullParam() {
        List<Map<String, Object>> map =new ArrayList<>();
            List<String> numbers = Arrays.asList("SO2-SO2", "NOx-NOx", "CO-CO","颗粒物-颗粒物","总磷-总磷","总氮-总氮","氨氮-氨氮","COD-COD");
            numbers.forEach(item->{
                Map<String, Object> objectMap=new HashMap<>();
                objectMap.put("label",item.split("-")[0]);
                objectMap.put("value",item.split("-")[1]);
                map.add(objectMap);
            });
            return map;
    }

    @Override
    public IPage<SRaRealTimeAnalysicComsitMonitorVo> abnormalList(SRaRealTimeAnalysicComsitMonitorVo sRaRealTimeAnalysicComsit) {
        Page<SRaRealTimeAnalysicComsitMonitorVo> page = new Page<>(sRaRealTimeAnalysicComsit.getPageNum(), sRaRealTimeAnalysicComsit.getPageSize());
        List<SRaRealTimeAnalysicComsitMonitorVo> list= sRaRealTimeAnalysicComsitMapper.abnormalList(page,sRaRealTimeAnalysicComsit);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<SRaRealTimeAnalysicComsitMonitorVo> abnormalExport(SRaRealTimeAnalysicComsitMonitorVo sRaRealTimeAnalysicComsit) {

        Page<SRaRealTimeAnalysicComsitMonitorVo> page = new Page<>(1, 999999);
        List<SRaRealTimeAnalysicComsitMonitorVo> list= sRaRealTimeAnalysicComsitMapper.abnormalList(page,sRaRealTimeAnalysicComsit);
        return list;
    }

    @Override
    public EChartsVO.DiagramVo abnormalEcharts(SRaRealTimeAnalysicComsitMonitorVo sRaRealTimeAnalysicComsit) {
        EChartsVO.DiagramVo diagramVo=new EChartsVO.DiagramVo();
        diagramVo.setDiagrams(new ArrayList<>());
        List<SRaRealTimeAnalysicComsitSt> list= sRaRealTimeAnalysicComsitMapper.abnormalEcharts(sRaRealTimeAnalysicComsit);
        if(CollectionUtil.isNotEmpty(list)){
            double asDouble = list.stream().mapToDouble(l -> l.getAcgValue()).average().getAsDouble();
            diagramVo.setAvgValue(new BigDecimal(asDouble).setScale(4, BigDecimal.ROUND_HALF_UP));
        }
        list.forEach(item->{
            EChartsVO.Diagram diagram=new EChartsVO.Diagram();
            diagram.setName(item.getParamName());
            diagram.setAvgValue(item.getAcgValue());
            diagram.setMaxValue(item.getMaxValue());
            diagram.setMinValue(item.getMinValue());
            diagram.setQ1Value(item.getQ1Value());
            diagram.setQ2Value(item.getQ2Value());
            diagramVo.getDiagrams().add(diagram);
        });
        return diagramVo;
    }

    @Override
    public List<Map<String, Object>> editListDropdown(SRaRealTimeAnalysicComsitMonitorVo sRaRealTimeAnalysicComsit) {
        return sRaRealTimeAnalysicComsitMapper.editListDropdown(sRaRealTimeAnalysicComsit);
    }
}
