package com.mvc.service.beihai;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.DeviceAccMapper;
import com.mvc.dao.TAireadMapper;
import com.mvc.entity.DeviceAcc;
import com.mvc.entity.DeviceAccExample;
import com.mvc.entity.TAiread;
import com.mvc.entity.TAireadExample;
import com.mvc.model.beihai.TRawdata;
import com.mvc.netty.dealconstant.ResultTag;
import com.mvc.netty.dealconstant.ResultType;
import com.mvc.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RawdataService {
    private static final Logger log = LoggerFactory.getLogger(RawdataService.class);

    @Autowired
    private Environment env;

    @Autowired
    private DeviceAccMapper deviceAccMapper;

    @Autowired
    private TAireadMapper tAireadMapper;

    /**
     * 根据stationCode，检索行数据集合
     * @param rawdata
     * @return
     */
    public Map<String, Object> findPage(TRawdata rawdata) {
        Map<String, Object> retMap = new HashMap<>();
       int pagesize= rawdata.getPageSize();
        int offset = rawdata.getOffset();
        String sn=rawdata.getTelemetryStationCode();
        Date startDate=null;
        Date endDate=null;
        if(rawdata.getStartTime()!=null){
            startDate=DateUtil.parseTimeStr(rawdata.getStartTime(),"yyyy-MM-dd HH:mm");
        }
        if(rawdata.getEndTime()!=null){
            endDate=DateUtil.parseTimeStr(rawdata.getEndTime(),"yyyy-MM-dd HH:mm");
        }
        TAireadExample example=new TAireadExample();
        TAireadExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(sn);
        if(startDate!=null){
            criteria.andSampleTimeGreaterThan(startDate);
        }
        if(endDate!=null){
            criteria.andSampleTimeLessThan(endDate);
        }
        example.setOrderByClause("SAMPLE_TIME DESC");
        Page<Object> pageObject = PageHelper.startPage((offset/pagesize)+1, pagesize);
        List<TAiread> tAireads = tAireadMapper.selectByExample(example);
        List<TRawdata> rawdataList =new ArrayList<>();
        long total = pageObject.getTotal();
        int pagecount=(int)(total/pagesize)+1;
        retMap.put("pageCount", pagecount);
        for(int i = 0;i<tAireads.size();i++){
            TRawdata data = new TRawdata();
            TAiread airead = tAireads.get(i);
            data.setUid(airead.getId());
            data.setMsgUuid(airead.getAireadId());
            data.setDataTime(airead.getSampleTime());
            data.setTelemetryStationCode(airead.getStationCode());
            data.setVoltage(airead.getVoltage().doubleValue());
            data.setAccumulatedWater(airead.getIdentValue().doubleValue());
            data.setTemperature(airead.getTemperature().doubleValue());
            data.setElectricQuantity(null);
            data.setSignalQuality(airead.getRsrp());
            data.setPicHeight(airead.getPicHeigth());
            data.setPicWidth(airead.getPicWidth());
            data.setAccumulatedWaterLbl("当前读数");
            data.setPicPath(airead.getSmallJpgUrl());
            data.setCompTime(DateUtil.formateDate(airead.getSampleTime(),"yyyy-MM-dd HH:mm"));
            rawdataList.add(data);
        }
        retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
        retMap.put(ResultTag.DATA.getTag(), rawdataList);
        // 查询结果集
        return retMap;
    }



    /**
     * 检索分类下设备数据最新值
     * @param classifyId
     * @return
     */
    public List<TRawdata> findNewBySearch(Integer classifyId, String eqSearch, Integer companyId) {
        //classifyId   1水表  2压力表
        List<TRawdata> resList=new ArrayList<>();
        String customerid="ff47f29a15e04242ba2f6e4600871290";
        DeviceAccExample deviceAccExample=new DeviceAccExample();
        DeviceAccExample.Criteria devicecriteria = deviceAccExample.createCriteria();
        devicecriteria.andUnitCodeEqualTo(customerid);
        devicecriteria.andDeviceTypeEqualTo(classifyId.intValue()+"");
        List<DeviceAcc> deviceAccs = deviceAccMapper.selectByExample(deviceAccExample);
        //查找设备最新数据
        for(int i = 0;i<deviceAccs.size();i++){
            TAireadExample aireadExample=new TAireadExample();
            TAireadExample.Criteria aircriteria = aireadExample.createCriteria();
            aircriteria.andDeviceSnEqualTo(deviceAccs.get(i).getDeviceSn());
            aireadExample.setOrderByClause("SAMPLE_TIME DESC");
            List<TAiread> tAireads = tAireadMapper.selectByExample(aireadExample);
            if(tAireads.size()>0){
                TAiread airead = tAireads.get(0);
                TRawdata tRawdata = new TRawdata();
                tRawdata.setUid(airead.getId());
                tRawdata.setDataTime(airead.getSampleTime());
                tRawdata.setTelemetryStationCode(airead.getDeviceSn());
                tRawdata.setVoltage(airead.getVoltage().doubleValue());
                tRawdata.setAccumulatedWater(airead.getIdentValue().doubleValue());
                tRawdata.setSignalQuality(airead.getRsrp());
                tRawdata.setPicHeight(airead.getPicHeigth());
                tRawdata.setPicWidth(airead.getPicWidth());
                tRawdata.setEquipName(deviceAccs.get(i).getDeviceName());
                tRawdata.setAccumulatedWaterLbl("当前读数");
                tRawdata.setCompTime(DateUtil.formateDate(airead.getSampleTime(),"yyyy-MM-dd HH:mm"));
                resList.add(tRawdata);
            }
        }
        return resList;
    }

    /**
     * 获得最新数据时间
     * @param record
     * @return
     */
    public Date findLastestTime(TRawdata record) {
        TAireadExample aireadExample=new TAireadExample();
        TAireadExample.Criteria aircriteria = aireadExample.createCriteria();
        aircriteria.andDeviceSnEqualTo(record.getTelemetryStationCode());
        aireadExample.setOrderByClause("SAMPLE_TIME DESC");
        List<TAiread> tAireads = tAireadMapper.selectByExample(aireadExample);
        if(tAireads.size()>0){
            return tAireads.get(0).getSampleTime();
        }else{
            return new Date();
        }

    }
}
