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

import java.time.LocalDateTime;
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.ObjectUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.TransformerOilThresholdDao;
import com.huatek.busi.dto.TransformerOilThresholdDto;
import com.huatek.busi.entity.TransformerOilThreshold;
import com.huatek.busi.mapper.TransformerOilThresholdMapper;
import com.huatek.busi.service.threshold.TransformerOilThresholdService;
import com.huatek.busi.vo.TransformerOilThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 变压器油色谱阈值表 ServiceImpl
 *
 * @author anker_cheng
 * @date Aug 5, 2019 5:29:30 PM
 */
@Component
@Service(interfaceClass = TransformerOilThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerOilThresholdServiceImpl implements TransformerOilThresholdService {
    @Autowired
    private TransformerOilThresholdMapper transformerOilThresholdMapper;

    @Autowired
    private TransformerOilThresholdDao transformerOilThresholdDao;

	@Override
	public int save(TransformerOilThresholdDto dto) {
		TransformerOilThreshold entity = new TransformerOilThreshold();
	    BeanUtils.copyPropertiesWithIgnoreNull(dto, entity);
	    entity.setId(Uuid.getUUID());
	    entity.setCreatedTime(LocalDateTime.now());
	    int result = transformerOilThresholdDao.add(entity);
	    return result;
	}

	@Override
	public int update(TransformerOilThresholdDto dto) {
		TransformerOilThreshold entity = new TransformerOilThreshold();
	    BeanUtils.copyPropertiesWithIgnoreNull(dto, entity);
	    int result = 0;
	    if(ObjectUtils.isEmpty(entity.getId())) {
	    	entity.setId(Uuid.getUUID());
	  	    entity.setCreatedTime(LocalDateTime.now());
	  	    result =transformerOilThresholdDao.addLocal(entity);
	    }else {
	    	entity.setModifiedTime(LocalDateTime.now());
	    	result = transformerOilThresholdDao.updateLocal(entity);
		}
	    return result;
	}

	@Override
	public TransformerOilThresholdVo getBySensorCode(String sensorCode) {
		return transformerOilThresholdMapper.selectBySensorCode(sensorCode);
	}

	@Override
	public int updateTransformerOilThresholdBySensorCode(String oldSensorCode, String newSensorCode) {
		return transformerOilThresholdMapper.updateTransformerOilThresholdBySensorCode(oldSensorCode, newSensorCode);
	}

	@Override
	public int delTransformerOilThresholdBySensorCode(String sensorCode) {
		return transformerOilThresholdMapper.delTransformerOilThresholdBySensorCode(sensorCode);
	}

    @Override
    public Map<String, Object> getThresholdMap(String sensorCode) {
         return transformerOilThresholdMapper.getThresholdMap(sensorCode);
    }
}