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

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
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.TransformerIcgeThresholdDao;
import com.huatek.busi.dto.TransformerIcgeThresholdDto;
import com.huatek.busi.entity.TransformerIcgeThreshold;
import com.huatek.busi.mapper.TransformerIcgeThresholdMapper;
import com.huatek.busi.service.threshold.TransformerIcgeThresholdService;
import com.huatek.busi.vo.TransformerIcgeThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 变压器铁芯、夹件阈值表 ServiceImpl
 *
 * @author anker_cheng
 * @date Aug 2, 2019 1:56:18 PM
 */
@Component
@Service(interfaceClass = TransformerIcgeThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerIcgeThresholdServiceImpl implements TransformerIcgeThresholdService {

    @Autowired
    private TransformerIcgeThresholdMapper transformerIcgeThresholdMapper;
    @Autowired
    private TransformerIcgeThresholdDao transformerIcgeThresholdDao;

    @Override
    public TransformerIcgeThreshold getTransformerIcgeThreshold(String mainTreeId, String runState, String sensorCode) {
        return transformerIcgeThresholdMapper.getTransformerIcgeThreshold(mainTreeId, runState, sensorCode);
    }

    @Override
    public TransformerIcgeThresholdVo
        getTransformerIcgeThresholdVo(TransformerIcgeThresholdDto transformerIcgeThresholdDto) {
        return transformerIcgeThresholdMapper.getTransformerIcgeThresholdVo(transformerIcgeThresholdDto);
    }

	@Override
	public int saveTransformerIcgeThreshold(String token, TransformerIcgeThresholdDto transformerIcgeThresholdDto) {
		int result = 0;
		TransformerIcgeThreshold entity = new TransformerIcgeThreshold();
	    BeanUtils.copyPropertiesWithIgnoreNull(transformerIcgeThresholdDto, entity);
	    if (StringUtils.isNotEmpty(entity.getId())) {
	    	entity.setModifiedTime(LocalDateTime.now());
		    result = transformerIcgeThresholdDao.updateLocal(entity);
	    } else {
		    entity.setId(Uuid.getUUID());
		    entity.setCreatedTime(LocalDateTime.now());
		    result = transformerIcgeThresholdDao.add(entity);
	    }
	    return result;
	}

	@Override
	public int updateTransformerIcgeThresholdBySensorCode(String oldSensorCode, String newSensorCode) {
		return transformerIcgeThresholdMapper.updateTransformerIcgeThresholdBySensorCode(oldSensorCode, newSensorCode);
	}

	@Override
	public int deleteTransformerTgThresholdBySensorCode(String sensorCode) {
		return transformerIcgeThresholdMapper.delTransformerIcgeThresholdBySensorCode(sensorCode);
	}

    @Override
    public List<TransformerIcgeThresholdVo>
        getTransformerIcgeThresholdVoList(TransformerIcgeThresholdDto transformerIcgeThresholdDto) {
        return transformerIcgeThresholdMapper.getTransformerIcgeThresholdVoList(transformerIcgeThresholdDto);
    }
}