package com.tbyoung.kafka.utils.count;
import com.tbyoung.kafka.bean.Sbinfo;
import com.tbyoung.kafka.service.TestService;
import com.tbyoung.kafka.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 存储统计没有的点每个小时发送3条数据
*/
public class MapCountUtils {
private static final Logger LOG = LoggerFactory.getLogger(MapCountUtils.class);
/**
* map集合存放点编号和点的统计情况
*/
public static Map<String,String> vmap = new HashMap<>(4000);
//存放开始计时时间
public static long startTime = 0;
//50分钟的毫秒数
public static long time = 1000 * 60 * 25;
//补发数量
public static int count = 1;
static {
//testinit();
init();
}
/**
* 测试初始化
*/
public static void testinit(){
startTime = new Date().getTime();
vmap.clear();
vmap.put("1","0-NULL");
vmap.put("2","0-NULL");
vmap.put("3","0-NULL");
vmap.put("4","0-NULL");
vmap.put("5","0-NULL");
vmap.put("6","0-NULL");
vmap.put("7","0-NULL");
}
/**
* 初始化
*/
public static void init(){
//清空之间打印map
log();
//初始化时间为当前时间
startTime = new Date().getTime();
//初始化map
vmap.clear();
TestService testService = (TestService)SpringUtils.getBean("testService");
List<Sbinfo> list = testService.findAll();
vmap = list.stream().collect(HashMap::new,(m,v)->m.put(v.getCode(),"0-NULL"),HashMap::putAll);
}
//存储Map点数据的写入
public synchronized static String writeMap (String code) throws Exception{
return add(code);
}
/**
* 新增数据
* @param code
*/
public static String add(String code){
String key = code,status = "NULL";
//清洗集合数据
updateMaxKey();
String [] values = vmap.get(code).split("-");
//时间大于一小时后查找map中卡口为0的编号
if(checkTime()){
//重发数据小于3条改变code值返回
String codes = min();
String [] valuesMin = vmap.get(code).split("-");
if(!StringUtils.isEmpty(codes)){
code = codes;
valuesMin = vmap.get(code).split("-");
}
if(Integer.parseInt(valuesMin[0])<=count){
//code = valuesMin[1];
status = key;
}
values = valuesMin;
}
//计算写入map中
vmap.put(code, (Long.parseLong(values[0]) + 1) + "-"+status);
//时间大于一小时验证是否补发完成,完成后初始化map
if(checkTime() && checkAll()){
init();
}
return code;
}
/**
* 获取所有不为NULL的数据的计数是否变成count 未完成
* @return true 可以重置静态变量
*/
public static boolean checkAll(){
boolean iS = vmap.entrySet().stream()
.noneMatch(e-> {
String []values = e.getValue().split("-");
return (!"NULL".equals(values[1])&&Integer.parseInt(values[0])<3);
});
return iS;
}
/**
* 为0的赋值NULL替换成key
*/
public static void updateMaxKey(){
if(checkTime()) {
vmap.entrySet().stream()
.filter(e -> {
String[] values = e.getValue().split("-");
return "0".equals(values[0]);
})
.forEach(e -> {
e.setValue(e.getValue().replaceAll("NULL", max()));
});
}
}
/**
* 返回最大的key
* @return
*/
public static String max(){
return vmap.entrySet().stream()
.sorted(Map.Entry.<String,String>comparingByValue().reversed())
.findFirst()
.get()
.getKey();
}
/**
* 获取没有发送完成的数据
* @return
*/
public static String min(){
return vmap.entrySet().stream()
.filter((e)->{
String[] values = e.getValue().split("-");
return (Integer.parseInt(values[0])<3 && !"NULL".equals(values[1]));
})
.findFirst()
.get()
.getKey();
}
/**
* 时间比较
* @return
*/
public static boolean checkTime(){
return (new Date().getTime() -startTime) > time;
}
//打印方法
public static void log(){
vmap.entrySet().stream().forEach(e -> {
String []values=e.getValue().split("-");
LOG.info(e.getKey()+":"+values[0]+"-"+values[1]);
});
}
//测试方法
public static void main(String[] args){
try {
while (true) {
System.out.print("输入");
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
writeMap(read);
log();
}
}catch (Exception e){
}
}
}