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

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.BusiCableCirculationCacheDao;
import com.huatek.busi.dto.cable.BusiCableCirculationCacheDto;
import com.huatek.busi.entity.BusiCableCirculation;
import com.huatek.busi.entity.BusiCableCirculationCache;
import com.huatek.busi.mapper.cable.BusiCableCirculationCacheMapper;
import com.huatek.busi.service.cable.BusiCableCirculationCacheService;
import com.huatek.busi.vo.cable.BusiCableCirculationCacheVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;

/**
 * @author Bobi_huo
 * @date 2020-11-24 11:19
 */
@Component
@Service(interfaceClass = BusiCableCirculationCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiCableCirculationCacheServiceImpl implements BusiCableCirculationCacheService {

    @Autowired
    private BusiCableCirculationCacheMapper busiCableCirculationCacheMapper;

    @Autowired
    private BusiCableCirculationCacheDao busiCableCirculationCacheDao;

    @Override
    public Page<BusiCableCirculationCacheVo> findCableCirculationCachePage(BusiCableCirculationCacheDto dto) {
        if (dto.getPage() != null && dto.getLimit() != null) {
            PageHelper.startPage(dto.getPage(), dto.getLimit());
        }
        Page<BusiCableCirculationCacheVo> circulationCacheVos = busiCableCirculationCacheMapper.findCableCirculationCachePage(dto);
        return circulationCacheVos;
    }

    @Override
    public int saveCableCirculationCache(BusiCableCirculation cacEntity) {
        BusiCableCirculationCache cableCirculationCache = busiCableCirculationCacheMapper
                .findByTreeId(cacEntity.getMainTreeId(), cacEntity.getSubTreeId());
        if (cableCirculationCache == null) {
            cableCirculationCache = new BusiCableCirculationCache();
        }
        cableCirculationCache.setMainTreeId(cacEntity.getMainTreeId());
        cableCirculationCache.setSubTreeId(cacEntity.getSubTreeId());
        cableCirculationCache.setDTime(cacEntity.getTimeStamp());
        cableCirculationCache.setPhase(cacEntity.getPhase());
        cableCirculationCache.setTotalCoreCurrent(transforeData(cacEntity.getTotalCoreCurrent()));
        cableCirculationCache.setSensorId(cacEntity.getSensorId());
        cableCirculationCache.setSiteId(cacEntity.getSiteId());
        cableCirculationCache.setRunState(cacEntity.getRunState());
        return busiCableCirculationCacheDao.saveOrUpdate(cableCirculationCache);
    }

    private static String transforeData(String value) {
        return DecimalUtil.getVal(value).setScale(2, RoundingMode.HALF_DOWN).toString();
    }

    @Override
    public BusiCableCirculationCache findByTreeId(String mainTreeId, String subTreeId, String type) {
        return busiCableCirculationCacheMapper.findByTreeId(mainTreeId, subTreeId);
    }
}
