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

import cn.cecep.talroad.domain.analysis.SRaOverWaterPollutionTraceability;
import cn.cecep.talroad.mapper.analysis.SRaOverWaterPollutionTraceabilityMapper;
import cn.cecep.talroad.service.analysis.SRaOverWaterPollutionTraceabilityService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.SEnvWaterMonRealVo;
import cn.cecep.talroad.vo.analysis.WaterPollutionTraceabilityVo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
@Service
public class SRaOverWaterPollutionTraceabilityServiceImpl extends ServiceImpl<SRaOverWaterPollutionTraceabilityMapper, SRaOverWaterPollutionTraceability> implements SRaOverWaterPollutionTraceabilityService{

    @Override
    public Map<String, Object> workWithMapData(List<SRaOverWaterPollutionTraceability> list) {
        Map<String, Object> map=new HashMap<>();
        List<WaterPollutionTraceabilityVo> vos=new ArrayList<>();
        List<Map<String, List<String[]>>> locations1=new ArrayList<>();
        for (SRaOverWaterPollutionTraceability st : list) {
            boolean falg=true;
            Map<String, List<String[]>> map1=new HashMap<>();
            List<String[]> location1=new ArrayList<>();
            //排口的点位信息
            WaterPollutionTraceabilityVo vo= new WaterPollutionTraceabilityVo();
            vo.setId(st.getId());
            vo.setLatitude(st.getOutletLatitude());
            vo.setLongitude(st.getOutletLongitude());
            vo.setName(st.getOutletName());
            vo.setNowConcentration("");
            vo.setCode(st.getOutletCode());
            vo.setPointName(st.getFactoryName()+st.getOutletName());
            String[] s1=new String[]{st.getOutletLongitude(),st.getOutletLatitude()};
            if(Arrays.stream(s1).anyMatch(Objects::isNull)){
                falg=false;
            }
            location1.add(s1);
            //直接排放--入河口的数据
            WaterPollutionTraceabilityVo vo1= new WaterPollutionTraceabilityVo();
            if("直接排放".equals(st.getEmissionMethod())){
                vo.setType("1");
                vo1.setCode(st.getEstuaryCode());
                vo1.setLatitude(st.getEstuaryLatitude());
                vo1.setLongitude(st.getEstuaryLongitude());
                vo1.setName(st.getEstuaryName()==null?st.getReceivingWaterName():st.getEstuaryCode());
                vo1.setType("4");
                vo1.setNowConcentration("");
                vo1.setPointName(st.getFactoryName()+(st.getEstuaryName()==null?st.getReceivingWaterName():st.getEstuaryCode()));
                String[] s2=new String[]{st.getEstuaryLongitude(),st.getEstuaryLatitude()};
                location1.add(s2);
                if(Arrays.stream(s2).anyMatch(Objects::isNull)){
                    falg=false;
                }
            }else if("间接排放".equals(st.getEmissionMethod())){
                vo.setType("2");
                vo1.setCode(st.getReceivingSewagePlantCode());
                vo1.setLatitude(st.getReceivingSewagePlantLatitude());
                vo1.setLongitude(st.getReceivingSewagePlantLongitude());
                vo1.setName(st.getReceivingSewagePlantName());
                vo1.setType("3");
                vo1.setNowConcentration("");
                vo1.setPointName(st.getFactoryName()+st.getReceivingSewagePlantName());
                String[] s2=new String[]{st.getReceivingSewagePlantLongitude(),st.getReceivingSewagePlantLatitude()};
                location1.add(s2);
                if(Arrays.stream(s2).anyMatch(Objects::isNull)){
                    falg=false;
                }
            }
            map1.put("coordinates",location1);
            if(falg) {
                locations1.add(map1);
            }
            vos.add(vo);
            vos.add(vo1);
        }
        map.put("info",vos);
        map.put("line",locations1);
        return map;
    }

    @Override
    public EChartsVO chartData(List<SEnvWaterMonRealVo> records, String pollName , Date startTime , Date endTime) {
        EChartsVO eChartsVO = new EChartsVO();
        List<String> dateList = CollUtil.map(DateUtil.rangeToList(startTime,
                        endTime, DateField.HOUR),
                x -> DateUtil.format(x,"yyyy-MM-dd HH"), true);
        eChartsVO.setXData(dateList);
        Map<String, List<SEnvWaterMonRealVo>> map = records.stream().collect(Collectors.groupingBy(SEnvWaterMonRealVo::getMonitoringTime));
        List<EChartsVO.SeriesObj> collect = map.entrySet().stream().map(entry -> {
            EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
            ser.setName(entry.getKey());
            ser.setProp(entry.getKey());
            List<Object> values=new ArrayList<>();
            for (String s : dateList) {
                List<SEnvWaterMonRealVo> value = entry.getValue();
                Optional<SEnvWaterMonRealVo> first = value.stream().filter(v -> v.getMonitoringTime().substring(0,13).equals(s)).findFirst();
                if(first.isPresent()){
                    SEnvWaterMonRealVo sEnvWaterMonRealVo = first.get();
                    if("COD".equals(pollName)) {
                        values.add(sEnvWaterMonRealVo.getCodConcentration());
                    }if("氨氮".equals(pollName)){
                        values.add(sEnvWaterMonRealVo.getAmmoniaNitrogen());
                    }
                }else {
                    values.add(0L);
                }
            }
            ser.setValues(values);
            return ser;
        }).collect(Collectors.toList());
        eChartsVO.setSeries(collect);
        eChartsVO.setName("24 小时趋势图");
        return eChartsVO;
    }
}
