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

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.SubstationMicGisDao;
import com.huatek.busi.entity.SubstationMicGis;
import com.huatek.busi.mapper.SubstationMicGisMapper;
import com.huatek.busi.service.substation.SubstationMicGisService;
import com.huatek.busi.vo.MeteorologicalStatisticsVo;
import com.huatek.busi.vo.SubstationMicGisVo;

/**
 * GIS微气象日统计 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 17, 2019 10:21:43 AM
 */
@Component
@Service(interfaceClass = SubstationMicGisService.class)
@Transactional(rollbackFor = Exception.class)
public class SubstationMicGisServiceImpl implements SubstationMicGisService {

    @Autowired
    private SubstationMicGisMapper substationMicGisMapper;
    @Autowired
    private SubstationMicGisDao substationMicGisDao;

    @Override
    public SubstationMicGisVo findSubstationMicGis(String siteId, String mainTreeId) {
        return substationMicGisMapper.findSubstationMicGis(siteId, mainTreeId);
    }

    @Override
    public void saveSubstationMicGis(MeteorologicalStatisticsVo item, LocalDate day) {
        // 删除旧数据
        substationMicGisMapper.deleteItem(item.getMainTreeId(), day);
        // 保存新数据
        SubstationMicGis entity = new SubstationMicGis();
        entity.setSiteId(item.getSiteId());
        entity.setSiteId(item.getSiteId());
        entity.setMainEquipmentId(item.getMainEquipmentId());
        entity.setMainTreeId(item.getMainTreeId());
        entity.setDTime(day);
        entity.setAirTemperature(item.getAvgAirTemperature());
        entity.setAirPressure(item.getAvgAirPressure());
        entity.setHumidity(item.getAvgHumidity());
        entity.setLoadRate(BigDecimal.ZERO);
        entity.setCreatedTime(LocalDateTime.now());
        // 负荷率先放"0"
        entity.setLoadRate(new BigDecimal(0));
        substationMicGisDao.saveOrUpdate(entity);
    }
}