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.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.CapacitanceEquipmentInsulationMonitorThresholdDao;
import com.huatek.busi.dto.CapacitanceEquipmentInsulationMonitorThresholdDto;
import com.huatek.busi.entity.CapacitanceEquipmentInsulationMonitorThreshold;
import com.huatek.busi.mapper.CapacitanceEquipmentInsulationMonitorThresholdMapper;
import com.huatek.busi.service.threshold.CapacitanceEquipmentInsulationMonitorThresholdService;
import com.huatek.busi.vo.CapacitanceEquipmentInsulationMonitorThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;

/**
 * 变压器容性设备监测值-阈值：022001 ServiceImpl
 *
 * @author anker_cheng
 * @date Aug 5, 2019 5:29:30 PM
 */
@Component
@Service(interfaceClass = CapacitanceEquipmentInsulationMonitorThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class CapacitanceEquipmentInsulationMonitorThresholdServiceImpl
    implements CapacitanceEquipmentInsulationMonitorThresholdService {

    @Autowired
    private CapacitanceEquipmentInsulationMonitorThresholdMapper capacitanceEquipmentInsulationMonitorThresholdMapper;
    @Autowired
    private CapacitanceEquipmentInsulationMonitorThresholdDao capacitanceEquipmentInsulationMonitorThresholdDao;

    @Override
    public CapacitanceEquipmentInsulationMonitorThreshold
        getCapacitanceEquipmentInsulationMonitorThreshold(String mainTreeId, String state, String sensorCode) {
        return capacitanceEquipmentInsulationMonitorThresholdMapper
            .getCapacitanceEquipmentInsulationMonitorThreshold(mainTreeId, state, sensorCode);
    }

    @Override
    public CapacitanceEquipmentInsulationMonitorThresholdVo getThresholdVo(String state, String sensorCode) {
        return capacitanceEquipmentInsulationMonitorThresholdMapper.getThresholdVo(state, sensorCode);
    }

    @Override
    public int saveOrUpdate(String token, CapacitanceEquipmentInsulationMonitorThresholdDto entity) {
        CapacitanceEquipmentInsulationMonitorThreshold threshold = new CapacitanceEquipmentInsulationMonitorThreshold();
        BeanUtils.copyProperties(entity, threshold, "createdTime", "creator", "modifiedTime", "modifier");
        LocalUser user = UserUtil.getLocalUser(token);
        if (StringUtils.isEmpty(threshold.getId())) {
            threshold.setCreatedTime(LocalDateTime.now());
            if (user != null) {
                threshold.setCreator(user.getUid());
            }
        }
        threshold.setModifiedTime(LocalDateTime.now());
        if (user != null) {
            threshold.setModifier(user.getUid());
        }
        return capacitanceEquipmentInsulationMonitorThresholdDao.saveOrUpdate(threshold);
    }
}