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

import java.math.BigDecimal;
import java.math.RoundingMode;

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.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.SubstationMicGisNowDao;
import com.huatek.busi.dto.SubstationMicGisNowDto;
import com.huatek.busi.entity.SubstationMicGisNow;
import com.huatek.busi.entity.TransformerMeteorological;
import com.huatek.busi.mapper.SubstationMicGisNowMapper;
import com.huatek.busi.service.gis.SubstationMicGisNowService;
import com.huatek.busi.vo.MeteorologicalVo;
import com.huatek.busi.vo.SubstationMicGisNowVo;

/**
 * GIS气象实时数据展示 ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 28, 2019 4:19:38 PM
 */
@Component
@Service(interfaceClass = SubstationMicGisNowService.class)
@Transactional(rollbackFor = Exception.class)
public class SubstationMicGisNowServiceImpl implements SubstationMicGisNowService {

    @Autowired
    private SubstationMicGisNowMapper substationMicGisNowMapper;
    @Autowired
    private SubstationMicGisNowDao substationMicGisNowDao;

    @Override
    public Page<SubstationMicGisNowVo> findSubstationMicGisNowPage(SubstationMicGisNowDto substationMicGisNowDto) {
        if (substationMicGisNowDto.getPage() != null && substationMicGisNowDto.getLimit() != null) {
            PageHelper.startPage(substationMicGisNowDto.getPage(), substationMicGisNowDto.getLimit());
        }
        Page<SubstationMicGisNowVo> substationMicGisNows = substationMicGisNowMapper
                .selectSubstationMicGisNowPage(substationMicGisNowDto);
        return substationMicGisNows;
    }

    @Override
    public synchronized int saveSubstationMicGisNow(TransformerMeteorological cacEntity) {
        SubstationMicGisNow substationMicGisNow = substationMicGisNowMapper
                .getSubstationMicGisNow(cacEntity.getSiteId(), cacEntity.getEquipmentCode());
        if (substationMicGisNow == null) {
            substationMicGisNow = new SubstationMicGisNow();
        }
        substationMicGisNow.setAirPressure(transData(cacEntity.getAirPressure()));
        substationMicGisNow.setAirTemperature(transData(cacEntity.getAirTemperature()));
        substationMicGisNow.setDTime(cacEntity.getTimeStamp());
        substationMicGisNow.setHumidity(transData(cacEntity.getHumidity()));
        substationMicGisNow.setSiteEqmCode(cacEntity.getEquipmentCode());
        substationMicGisNow.setEqmId(cacEntity.getEquipmentId());
        substationMicGisNow.setSiteId(cacEntity.getSiteId());
        substationMicGisNow.setWdgustspd(transData(cacEntity.getWdgustspd()));
        return substationMicGisNowDao.saveOrUpdate(substationMicGisNow);
    }

    private static BigDecimal transData(String val) {
        return DecimalUtil.getVal(val).setScale(2, RoundingMode.HALF_DOWN);
    }

    @Override
    public MeteorologicalVo queryMeteorologicalInfo(String sensorCode) {
        // TODO Auto-generated method stub
         return substationMicGisNowMapper.queryMeteorologicalInfo(sensorCode);
    }
}