package com.gic.haoban.manage.service.util;

import com.gic.haoban.manage.service.pojo.HBQwMonitorBO;
import com.gic.redis.data.util.RedisUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.redisson.api.RAtomicLong;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
/**
 * 好办统计
 */
@Component
public class HBQwMonitorUtils {

    private static Logger logger = LogManager.getLogger(HBQwMonitorUtils.class);

    private static final List<String> keyList = Arrays.asList("hbDatakey:qwFriendNotice","hbDataKey:addFriendFail") ;

    /**
     * @param index 0加好友企微通知数量 1加好友查询企微接口限制数量
     */
    public static void addForFriend(int index) {
        logger.info("addForFriend");
        String key = keyList.get(index) ;
        RAtomicLong cache = RedisUtil.getRedisClient().getAtomicLong(key);
        long val = cache.addAndGet(1);
    }


    public static Object query(String sql) {
        return InfluxDbMonitorUtils.getInstance().query(sql);
    }

    public static void addByTimer() {
        logger.info("add timer");
        for(int i=0;i<keyList.size();i++) {
            String key = keyList.get(i) ;
            int count = 0 ;
            Object countObj = RedisUtil.getCache(key) ;
            if(null != countObj) {
                count = Integer.valueOf(countObj.toString()) ;
            }
            RAtomicLong cache = RedisUtil.getRedisClient().getAtomicLong(key);
            cache.addAndGet(-count) ;
            HBQwMonitorBO bo = new HBQwMonitorBO() ;
            bo.setHost("127.0.0.1");
            bo.setType(""+i);
            bo.setNum(count);
            InfluxDbMonitorUtils.getInstance().friendAdd(bo);
        }
    }


}
