package com.huatek.busi.service.impl.gis;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.RptGisMonitorSf6ChartsDao;
import com.huatek.busi.entity.RptGisMonitorSf6Charts;
import com.huatek.busi.mapper.GisSf6PressureMapper;
import com.huatek.busi.mapper.RptGisMonitorSf6ChartsMapper;
import com.huatek.busi.service.gis.RptGisMonitorSf6ChartsService;
import com.huatek.busi.util.CalcUtil;

@Component
@Service(interfaceClass = RptGisMonitorSf6ChartsService.class)
@Transactional(rollbackFor = Exception.class)
public class RptGisMonitorSf6ChartsServiceImpl implements RptGisMonitorSf6ChartsService {

    @Autowired
    private RptGisMonitorSf6ChartsMapper rptGisMonitorSf6ChartsMapper;
    @Autowired
    private GisSf6PressureMapper gisSf6PressureMapper;
    @Autowired
    private RptGisMonitorSf6ChartsDao rptGisMonitorSf6ChartsDao;

    @Override
    public List<RptGisMonitorSf6Charts> findRptGisMonitorSf6Chartses(String siteId, String mainTreeId) {
        return rptGisMonitorSf6ChartsMapper.findRptGisMonitorSf6Chartses(siteId, mainTreeId);
    }

    @Override
    public List<Map<String, Object>> getTemperRecordAvgByDay(LocalDateTime startTime, LocalDateTime endTime) {
        return rptGisMonitorSf6ChartsMapper.getTemperRecordAvgByDay(startTime, endTime);
    }

    @Override
    public int saveRptGisMonitorSf6Charts(LocalDate day) {
        // 删除数据
        rptGisMonitorSf6ChartsMapper.deleteOneDayData(day);
        // 重新统计
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        List<Map<String, Object>> list = getTemperRecordAvgByDay(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        for (Map<String, Object> map : list) {
            RptGisMonitorSf6Charts entity = new RptGisMonitorSf6Charts();
            entity.setSiteId(getString(map, "siteId"));
            entity.setMainTreeId(getString(map, "mainTreeId"));
            entity.setSensorCode(getString(map, "sensorCode"));
            
            List<Map<String, Object>> listObj = gisSf6PressureMapper.getRecordBySensorCode(getString(map, "sensorCode"), startTime, endTime);
            List<BigDecimal> listRes = new ArrayList<BigDecimal>();
            for(Map<String, Object> mapObj:listObj)
            {
            	listRes.add(new BigDecimal(mapObj.get("pressure").toString()));
            }
            
            entity.setPosition(getString(map, "phase"));
            entity.setTemper(getString(map, "temperature"));
            entity.setMoisture(getString(map, "moisture"));
//            entity.setPressure(getString(map, "pressure20C"));
            entity.setPressure(CalcUtil.avgInfo(listRes).toString());
            entity.setRptTime(day);
            entity.setCreateTime(LocalDateTime.now());
            rptGisMonitorSf6ChartsDao.saveOrUpdate(entity);
        }
        return list.size();
    }

    private static String getString(Map<String, Object> map, String key) {
        Object val = map.get(key);
        return val == null ? "" : val.toString();
    }
}
