package org.jeecg.modules.largeScreen.service.impl;

import org.jeecg.modules.itdmDataHistory.entity.ItdmWenxiang;
import org.jeecg.modules.itdmDataHistory.mapper.ItdmWenxiangMapper;
import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
import org.jeecg.modules.itdmDevice.service.IItdmDeviceService;
import org.jeecg.modules.largeScreen.service.IASecondScreenService;
import org.jeecg.modules.largeScreen.vo.ASecondScreenVO;
import org.jeecg.modules.largeScreen.vo.AZhexianDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ASecondScreenServiceImpl implements IASecondScreenService {

    @Autowired
    @SuppressWarnings("all")
    private IItdmDeviceService deviceService;
    @Autowired
    @SuppressWarnings("all")
    private ItdmWenxiangMapper wenxiangMapper;


    public Map<String,Object> getList(String id){
        Map<String,Object> secondMap = new HashMap<>();
        ItdmDevice itdmDevice = deviceService.getById(id);
        String deviceName = itdmDevice.getDeviceName();
        if(itdmDevice.getDeviceType().equals("温箱")){
            List<ItdmWenxiang> wenxiangList = wenxiangMapper.selectWXGraphList(id);
            List<String> xAxis = wenxiangList.stream().map(i->{
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = i.getCreateTime();
                String x = sdf.format(date);
                return x;
            }).collect(Collectors.toList());//x轴，时间列表

            AZhexianDataVO[] data = new AZhexianDataVO[2];
            String typeName = "";
            int d = 0;
            List<Map<String,Object>> seriesT = new ArrayList<>();//y
            List<BigDecimal> listT = wenxiangList.stream().map(i->{
                BigDecimal decimal = i.getTemperature();
                return decimal;
            }).collect(Collectors.toList());
            typeName = "温度";
            Map<String,Object> mapT = new HashMap<>();
            mapT.put("data", listT);
            mapT.put("name", typeName);
            seriesT.add(mapT);
            AZhexianDataVO voT = new AZhexianDataVO();
            voT.setType(typeName);
            voT.setXAxis(xAxis);
            voT.setSeries(seriesT);
            data[d] = voT;
            d++;

            List<Map<String,Object>> seriesH = new ArrayList<>();//y
            List<BigDecimal> listH = wenxiangList.stream().map(i->{
                BigDecimal decimal = i.getHumidity();
                return decimal;
            }).collect(Collectors.toList());
            typeName = "湿度";
            Map<String,Object> mapH = new HashMap<>();
            mapH.put("data", listH);
            mapH.put("name", typeName);
            seriesH.add(mapH);
            AZhexianDataVO voH = new AZhexianDataVO();
            voH.setType(typeName);
            voH.setXAxis(xAxis);
            voH.setSeries(seriesH);
            data[d] = voH;
            d++;

            ASecondScreenVO vo = new ASecondScreenVO();
            vo.setId(id);
            vo.setTotal(data.length);
            vo.setDeviceName(deviceName);
            vo.setData(data);

            secondMap.put("data", vo);
            secondMap.put("name", "二级大屏数据");
        }
        return secondMap;
    }

}
