package com.bonus.material.largeScreen.service.impl;

import com.bonus.common.biz.utils.MathUtil;
import com.bonus.material.device.domain.DevInfo;
import com.bonus.material.device.domain.vo.DevInfoVo;
import com.bonus.material.device.mapper.DevInfoMapper;
import com.bonus.material.largeScreen.service.LargeScreenService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.rmi.ServerException;
import java.util.List;

@Service
@Slf4j
public class LargeScreenServiceImpl implements LargeScreenService {

    @Resource
    private DevInfoMapper devInfoMapper;
    @Override
    public DevInfo getIdleDevRatio() throws ServerException {
        DevInfoVo devInfoVo = new DevInfoVo();
        DevInfo devInfo = new DevInfo();
        try {
            //获取所有设备信息
            List<DevInfoVo>devInfoList = devInfoMapper.selectDevInfoHotListRatio(devInfoVo);
            //计算使用率
            int idle80 = 0;
            int idle3080 = 0;
            int idle30 = 0;
            for (DevInfoVo devInfoVoNew:devInfoList) {
                if (devInfoVoNew.getTotalUpDay()==null){
                    idle30++;
                    continue;
                }
                if (devInfoVoNew.getTotalLeaseDay()==null){
                    devInfoVoNew.setTotalLeaseDay(0);
                }
                log.info("============{}",devInfoVoNew.getMaId());
               BigDecimal result =  MathUtil.calculatePercentage(devInfoVoNew.getTotalUpDay(), devInfoVoNew.getTotalUpDay() + devInfoVoNew.getTotalLeaseDay());
                int re1 = result.compareTo(new BigDecimal(80));
                int re2 = result.compareTo(new BigDecimal(30));
                //使用率>80
                if (re1>0){
                    idle80++;
                    //30<=使用率<=80
                }else if (re1<=0 && re2>=0){
                    idle3080++;
                    //使用率<30
                }else if (re2<0){
                    idle30++;
                }
            }
            devInfo.setIdle30(idle30);
            devInfo.setIdle3080(idle3080);
            devInfo.setIdle80(idle80);
        }catch (Exception e){
            log.info("获取利用率失败{}",e.getMessage());
            throw  new ServerException("获取利用率失败");
        }
        return devInfo;
    }
}
