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

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.PcPollutant;
import cn.cecep.talroad.domain.analysis.SRaOnlineEmissionsWater;
import cn.cecep.talroad.mapper.PcPollutantMapper;
import cn.cecep.talroad.mapper.analysis.SRaOnlineEmissionsWaterMapper;
import cn.cecep.talroad.service.analysis.ISRaOnlineEmissionsWaterService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaOnlineEmissionsWaterVo;
import cn.cecep.talroad.vo.query.analysis.SRaOnlineEmissionsWaterQuery;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 在线监测排放监控-水污染要素Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-24*/
@Service
public class SRaOnlineEmissionsWaterServiceImpl extends ServiceImpl<SRaOnlineEmissionsWaterMapper, SRaOnlineEmissionsWater> implements ISRaOnlineEmissionsWaterService
{
    @Autowired
    private SRaOnlineEmissionsWaterMapper sRaOnlineEmissionsWaterMapper;

    @Autowired
    private PcPollutantMapper pollutantMapper;

    /**
     * 查询在线监测排放监控-水污染要素列表
     *
     * @param sRaOnlineEmissionsWater 在线监测排放监控-水污染要素
     * @return 在线监测排放监控-水污染要素
     */
    @Override
    public List<SRaOnlineEmissionsWater> selectSRaOnlineEmissionsWaterList(SRaOnlineEmissionsWater sRaOnlineEmissionsWater)
    {
        return sRaOnlineEmissionsWaterMapper.selectSRaOnlineEmissionsWaterList(sRaOnlineEmissionsWater);
    }

    @Override
    public EChartsVO getChartData(SRaOnlineEmissionsWaterQuery.CharDataSearchVo searchVo) {
        List<String> xdata = DateUtils.getRangeTimes("D", searchVo.getStartTime(), searchVo.getEndTime());
        EChartsVO chartsVO = new EChartsVO();
        chartsVO.setXData(xdata);
        chartsVO.setSeries(new ArrayList<>());

        PcPollutant pcPollutant = pollutantMapper.selectById(searchVo.getPollutantCode());

        if (pcPollutant == null) {
            return chartsVO;
        }
        List<SRaOnlineEmissionsWaterVo.ChartDataVo> chartDataVos = sRaOnlineEmissionsWaterMapper.selectChartData(searchVo);
        Map<String, SRaOnlineEmissionsWaterVo.ChartDataVo> chartDataVoMap = chartDataVos.stream().collect(Collectors.toMap(o -> o.getMonitorTime(), o -> o));

        EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
        ser.setName(pcPollutant.getPollName());
        ser.setUnit("kg");

        // 设置许可量标记线
        if (StringUtils.isNotEmpty(searchVo.getAllowableValue())) {
            List<Object> markLinedata = new ArrayList<>();

            Map<String, String> labelMap = new HashMap<>();
            labelMap.put("formatter", "{b}: {c}");

            markLinedata.add(
                    new HashMap<String, Object>() {{
                        put("name", "许可量");
                        put("yAxis", searchVo.getAllowableValue());
                        put("label", labelMap);
                    }}
            );
            EChartsVO.MarkLine markLine = new EChartsVO.MarkLine();
            markLine.setData(markLinedata);
            ser.setMarkLine(markLine);
        }

        List<Object> values = xdata.stream().map(time -> {
            SRaOnlineEmissionsWaterVo.ChartDataVo chartDataVo = chartDataVoMap.get(time);
            if (chartDataVo == null) {
                return null;
            }
            return chartDataVo.getPollutantEmissions();
        }).collect(Collectors.toList());
        ser.setValues(values);

        chartsVO.setSeries(Arrays.asList(ser));
        return chartsVO;
    }

}
