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.SubstationMicBlqDao;
import com.huatek.busi.entity.SubstationMicBlq;
import com.huatek.busi.mapper.SubstationMicBlqMapper;
import com.huatek.busi.service.substation.SubstationMicBlqService;
import com.huatek.busi.vo.MeteorologicalStatisticsVo;

/**
 * 避雷器微气象日统计 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 17, 2019 10:21:44 AM
 */
@Component
@Service(interfaceClass = SubstationMicBlqService.class)
@Transactional(rollbackFor = Exception.class)
public class SubstationMicBlqServiceImpl implements SubstationMicBlqService {

    @Autowired
    private SubstationMicBlqMapper substationMicBlqMapper;
    @Autowired
    private SubstationMicBlqDao substationMicBlqDao;

    @Override
    public synchronized void saveSubstationMicBlq(MeteorologicalStatisticsVo item, LocalDate day) {
        // 删除记录
        substationMicBlqMapper.deleteItem(item.getMainTreeId(), day);
        // 保存新记录
        SubstationMicBlq entity = new SubstationMicBlq();
        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));
        substationMicBlqDao.saveOrUpdate(entity);
    }
}