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

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

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.TransformerRunStatusThresholdDao;
import com.huatek.busi.dto.TransformerRunStatusThresholdDto;
import com.huatek.busi.entity.TransformerRunStatusThreshold;
import com.huatek.busi.mapper.TransformerRunStatusThresholdMapper;
import com.huatek.busi.service.transformer.TransformerRunStatusThresholdService;
import com.huatek.busi.vo.TransformerRunStatusThresholdVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;

@Component
@Service(interfaceClass=TransformerRunStatusThresholdService.class)
@Transactional(rollbackFor=Exception.class)
public class TransformerRunStatusThresholdServiceImpl implements TransformerRunStatusThresholdService{
    @Autowired
    private TransformerRunStatusThresholdMapper transformerRunStatusThresholdMapper;
    @Autowired
    private TransformerRunStatusThresholdDao transformerRunStatusThresholdDao;
    
    /**
     * 查询变压器运行工况阈值
     */
    @Override
    public TransformerRunStatusThresholdVo queryRunStatusThreshold(String siteId,String mainTreeId,String workStatus) {
        return transformerRunStatusThresholdMapper.queryRunStatusThreshold(siteId,mainTreeId,workStatus);
    }
    
    /**
     * 保存或者更新变压器运行工况阈值
     */
    @Override
    public int insertOrUpdate(String token,TransformerRunStatusThresholdDto transformerRunStatusThresholdDto) {
        int count = 0;
        TransformerRunStatusThreshold transformerRunStatusThreshold = new TransformerRunStatusThreshold();
        BeanUtils.copyPropertiesWithIgnoreNull(transformerRunStatusThresholdDto, transformerRunStatusThreshold);
        LocalUser user = UserUtil.getLocalUser(token);
        if(ObjectUtils.isEmpty(transformerRunStatusThreshold.getId()))
        {
            transformerRunStatusThreshold.setId(Uuid.getUUID());
            transformerRunStatusThreshold.setCreator(user.getUid());
            transformerRunStatusThreshold.setCreatedTime(LocalDateTime.now());
            count = transformerRunStatusThresholdDao.add(transformerRunStatusThreshold);
        }
        else
        {
            transformerRunStatusThreshold.setModifier(user.getUid());
            transformerRunStatusThreshold.setModifiedTime(LocalDateTime.now());
            transformerRunStatusThreshold.setModifier(user.getUid());
            count = transformerRunStatusThresholdDao.updateLocal(transformerRunStatusThreshold);
        }
        return count;
    }

    @Override
    public List<TransformerRunStatusThresholdVo> queryRunStatusThresholdByMainTreeId(String mainTreeId) {
        return transformerRunStatusThresholdMapper.queryRunStatusThresholdByMainTreeId(mainTreeId);
    }

    @Override
    public int insertTransformerRunStatusThreshold(String mainTreeId,String siteId) {
        TransformerRunStatusThreshold transformerRunStatusThreshold = new TransformerRunStatusThreshold();
        for(int i=0;i<3;i++)
        {
            transformerRunStatusThreshold.setId(Uuid.getUUID());
            transformerRunStatusThreshold.setSiteId(siteId);
            transformerRunStatusThreshold.setWorkStatus((i+1)+"");
            transformerRunStatusThreshold.setMainTreeId(mainTreeId);
            transformerRunStatusThresholdDao.add(transformerRunStatusThreshold);
        }
         return 0;
    }
}
