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

import java.time.LocalDateTime;

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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.GisSF6ThresholdDao;
import com.huatek.busi.dto.GisSf6ThresholdDto;
import com.huatek.busi.entity.GisSf6Threshold;
import com.huatek.busi.mapper.GisSF6ThresholdMapper;
import com.huatek.busi.service.gis.GisSF6ThresholdService;
import com.huatek.busi.vo.GisSf6ThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;

/**
 * GIS SF6阈值
 * 
 * @author buck_guo
 * @date 2019/08/08
 */
@Component
@Service(interfaceClass = GisSF6ThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class GisSF6ThresholdServiceImpl implements GisSF6ThresholdService {
    @Autowired
    private GisSF6ThresholdMapper gisSF6ThresholdMapper;
    @Autowired
    private GisSF6ThresholdDao gisSF6ThresholdDao;

    @Override
    public GisSf6ThresholdVo queryGisSF6Threshold(String sensorCode) {
    	GisSf6ThresholdVo thresholdVo = gisSF6ThresholdMapper.queryGisSF6Threshold(sensorCode);
    	if (ObjectUtils.isEmpty(thresholdVo)) {
    		thresholdVo = new GisSf6ThresholdVo();
    	}
        return thresholdVo;
    }

    @Override
    public int insertOrUpdate(String token, GisSf6ThresholdDto gisSf6ThresholdDto) {
        GisSf6Threshold gisSf6Threshold = new GisSf6Threshold();
        BeanUtils.copyPropertiesWithIgnoreNull(gisSf6ThresholdDto, gisSf6Threshold);
        LocalUser user = UserUtil.getLocalUser(token);
        if (ObjectUtils.isEmpty(gisSf6Threshold.getId())) {
            gisSf6Threshold.setCreator(user.getUid());
            gisSf6Threshold.setCreatedTime(LocalDateTime.now());
        }
        return gisSF6ThresholdDao.saveOrUpdate(gisSf6Threshold);
    }

    @Override
    public int updateGisSensorThresholdBySensorCode(String oldSensorCode, String newSensorCode) {
        return gisSF6ThresholdMapper.updateGisSensorThresholdBySensorCode(oldSensorCode, newSensorCode);
    }

    @Override
    public int deleteGisThresholdBySensorCode(String sensorCode) {
        return gisSF6ThresholdMapper.deleteGisSensorThresholdBySensorCode(sensorCode);
    }
}
