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 org.springframework.util.ObjectUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dao.TransformerTgThresholdDao;
import com.huatek.busi.dto.TransformerTgThresholdDto;
import com.huatek.busi.entity.TransformerTgThreshold;
import com.huatek.busi.mapper.TransformerTgThresholdMapper;
import com.huatek.busi.service.threshold.TransformerTgThresholdService;
import com.huatek.busi.vo.TransformerTgThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 变压器套管阈值 ServiceImpl
 *
 * @author anker_cheng
 * @date 2019-8-8 19:26:29
 */
@Component
@Service(interfaceClass = TransformerTgThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerTgThresholdServiceImpl implements TransformerTgThresholdService {

    @Autowired
    private TransformerTgThresholdMapper transformerTgThresholdMapper;
    @Autowired
    private TransformerTgThresholdDao transformerTgThresholdDao;

    @Override
    public PageInfo<TransformerTgThresholdVo> findTransformerTgThresholdPage(TransformerTgThresholdDto transformerTgThresholdDto){
        return PageHelper.startPage(transformerTgThresholdDto.getPage(), transformerTgThresholdDto.getLimit())
            .doSelectPageInfo(() -> transformerTgThresholdMapper.selectTransformerTgThresholdPage(transformerTgThresholdDto));
    }

    @Override
    public int saveTransformerTgThreshold(String token,TransformerTgThresholdDto transformerTgThresholdDto){
        int count = 0;
    	TransformerTgThreshold transformerTgThreshold = new TransformerTgThreshold();
        BeanUtils.copyPropertiesWithIgnoreNull(transformerTgThresholdDto, transformerTgThreshold);
        if (StringUtils.isNotEmpty(transformerTgThreshold.getId())) {
        	transformerTgThreshold.setModifiedTime(LocalDateTime.now());
        	count = transformerTgThresholdDao.updateLocal(transformerTgThreshold);
        } else {
        	transformerTgThreshold.setId(Uuid.getUUID());
        	transformerTgThreshold.setCreatedTime(LocalDateTime.now());
        	count = transformerTgThresholdDao.add(transformerTgThreshold);
        }
        return count;
    }

    @Override
    public TransformerTgThresholdVo findTransformerTgThreshold(TransformerTgThresholdDto transformerTgThresholdDto){
    	TransformerTgThresholdVo  transformerTgThresholdVo = new TransformerTgThresholdVo();
    	TransformerTgThreshold transformerTgThreshold = transformerTgThresholdMapper.selectTransformerTgThreshold(transformerTgThresholdDto);
    	if (!ObjectUtils.isEmpty(transformerTgThreshold)) {
        	BeanUtils.copyPropertiesWithIgnoreNull(transformerTgThreshold, transformerTgThresholdVo);
    	}
    	transformerTgThresholdVo.setRunState(transformerTgThresholdDto.getRunState());
    	return transformerTgThresholdVo;
    }


    @Override
    public int deleteTransformerTgThreshold(String token, TransformerTgThresholdDto transformerTgThresholdDto){
        int count = transformerTgThresholdDao.del(transformerTgThresholdDto.getId());
        return count;
    }

	@Override
	public int updateTransformerTgThresholdBySensorCode(String oldSensorCode, String newSensorCode) {
		return transformerTgThresholdMapper.updateTransformerTgThresholdBySensorCode(oldSensorCode, newSensorCode);
	}

	@Override
	public int deleteTransformerTgThresholdBySensorCode(String sensorCode) {
		return transformerTgThresholdMapper.delTransformerTgThresholdBySensorCode(sensorCode);
	}

    @Override
    public List<TransformerTgThresholdVo> findTransformerTgThresholdList(TransformerTgThresholdDto transformerTgThresholdDto) {
        return transformerTgThresholdMapper.findTransformerTgThresholdList(transformerTgThresholdDto);
    }

}