| | |
| | | import com.cloud.count.model.Config; |
| | | import com.cloud.count.model.People; |
| | | import com.cloud.count.service.CountService; |
| | | import io.swagger.models.auth.In; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import java.text.ParseException; |
| | |
| | | */ |
| | | public static volatile int totalExits = 0; |
| | | /** |
| | | * 统计设备可统计的最大数量 |
| | | */ |
| | | private static final int MAXCOUNT = 65535; |
| | | /** |
| | | * 统计设备单词可统计的最大数量 |
| | | */ |
| | | private static final int MAXCOUNTONCE = 100; |
| | | /** |
| | | * 计算的进入中间变量 |
| | | */ |
| | | private static volatile int tmpEnter = 0; |
| | | private static volatile int tmpExit = 0; |
| | | /** |
| | | * 实时计算时作为计算的进校人数基准值 |
| | | */ |
| | | public static volatile int realtimeBaseEnters = 0; |
| | |
| | | private static String[] time2={"00","05","10","15","20","25", |
| | | "30","35","40","45","50","55"}; |
| | | public static volatile int countType = 0; |
| | | |
| | | private static Map<Integer,Integer> dayAllMap = new HashMap<>(); |
| | | private static Map<Integer,Integer> dayOutMap = new HashMap<>(); |
| | | private static Map<Integer,Integer> dayInMap = new HashMap<>(); |
| | | |
| | | public static int addEnter(int count) { |
| | | int inCre = 0; |
| | | if(tmpEnter <= count){ |
| | | inCre = count - tmpEnter; |
| | | } else { |
| | | if(tmpEnter >= (MAXCOUNT-MAXCOUNTONCE) && count <= MAXCOUNT) { |
| | | inCre = count + MAXCOUNT - tmpEnter; |
| | | } |
| | | } |
| | | tmpEnter = count; |
| | | return inCre; |
| | | } |
| | | |
| | | public static int addExit(int count) { |
| | | int inCre = 0; |
| | | if(tmpExit <= count){ |
| | | inCre = count - tmpExit; |
| | | } else { |
| | | if(tmpExit >= (MAXCOUNT-MAXCOUNTONCE) && count <= MAXCOUNT) { |
| | | inCre = count + MAXCOUNT - tmpExit; |
| | | } |
| | | } |
| | | tmpExit = count; |
| | | return inCre; |
| | | } |
| | | |
| | | /** |
| | | * 统计参数初始化,用于统计设备重启情况(发送的第一条报文) |
| | |
| | | Map<String,Object> map = new HashMap<>(16); |
| | | // 获取一些初始值 |
| | | Config config = dao.getConfig(); |
| | | if(config==null) { |
| | | config = new Config(); |
| | | config.setCor(1); |
| | | config.setPassword("123456"); |
| | | config.setCountStartTime("06:00"); |
| | | config.setInit(1); |
| | | config.setInitialPeople(60); |
| | | config.setInitialTime("00:00"); |
| | | } |
| | | // 初始人数值 |
| | | int initialPeople = (config.getInit()==1)?config.getInitialPeople():0; |
| | | // 校正人数值 |
| | |
| | | if (now>start-1 && now<start+11){//表示从设置的初始化时间开始重新计数 |
| | | baseEnters = totalEnters; |
| | | baseExits = totalExits; |
| | | |
| | | dayAllMap = new HashMap<>(); |
| | | dayInMap = new HashMap<>(); |
| | | dayOutMap = new HashMap<>(); |
| | | } |
| | | |
| | | // 从开始计算时间到现在的进校数 |
| | | //进校人数 = 当前报文totalEnters - 基准进校人数 + 校正值 |
| | | int inPeople = totalEnters - baseEnters + correctionPeople; |
| | | int inPeople = totalEnters - baseEnters + correctionPeople + initialPeople; |
| | | |
| | | int outPeople = totalExits - baseExits; |
| | | |
| | | // 校内总人数 = 进校人数 - 出校人数 + 校内初始化人数 + 误差数量 |
| | | int allPeople = inPeople - outPeople + initialPeople; |
| | | int allPeople = inPeople - outPeople; |
| | | |
| | | // 现在离开始统计的时间的小时数 7个多小时 按7个小时算 |
| | | //int hour = (int)(Math.abs(now-set)+3599)/3600; |
| | |
| | | // 开始统计时间 分开 如06:40 |
| | | String[] times = config.getCountStartTime().split(":"); |
| | | // 初始设置时间分开 |
| | | String[] times2 = initialTime.split(":"); |
| | | String[] times2 = initialTime.split(":");//初始化时间06:00 |
| | | // 设置时间图表的项 |
| | | int charat = Integer.parseInt(times[0]); |
| | | int mm =Integer.parseInt(times2[0])-charat; |
| | | int charat = Integer.parseInt(times[0]);//统计开始时间06:00 |
| | | int mm =Integer.parseInt(times2[0])-charat;//分钟是00 |
| | | // 设置时间图表的项,比如图表的开始统计时间为06:40,则time_set就是[06:40,07:40,08:40,09:40,10:40,11:40,12:40,] |
| | | for(int i=0;i<minute;i++){ |
| | | //time_set[i]=time[charat]+":"+times[1]; |
| | |
| | | // 但是只会统计7:30,则7:30统计的数据默认为初始人数,并且以后的从7:30开始统计 |
| | | long hourset = set; |
| | | for(int i=0;i<minute;i++){ |
| | | if( (mm==i) && (countType==0) ){ |
| | | if( (mm == i) && (countType==0) ){ |
| | | people[i]=initialPeople; |
| | | set = hourset; //这行代码很重要,表示重新统计 |
| | | }else { |
| | | //System.out.println("第"+i+"个小时的数据:"); |
| | | |
| | | // people[i] = dao.countInPeople(set-60,hourset+3599)- dao.countOutPeople(set-60,hourset+3599)+initialPeople+correctionPeople; |
| | | people[i] = getDataValue(map,countType,set-60,hourset+1799,initialPeople,correctionPeople); |
| | | //每次都实时计算,会重复计算历史数据 |
| | | // people[i] = getDataValue(map,countType,set-60,hourset+1799,initialPeople,correctionPeople); |
| | | //采用缓存的方式,算过的就不再计算了 |
| | | if(countType == 1){ |
| | | if(dayInMap.containsKey(i) && minute >1 && (i != minute-1)){ |
| | | people[i] = dayInMap.get(i); |
| | | }else{ |
| | | people[i] = getDataValue(map,countType,set-60,hourset+1799,initialPeople,correctionPeople); |
| | | dayInMap.put(i, people[i]); |
| | | } |
| | | } else if(countType == 0) { |
| | | if(dayAllMap.containsKey(i) && minute >1 && (i != minute-1)){ |
| | | people[i] = dayAllMap.get(i); |
| | | }else{ |
| | | people[i] = getDataValue(map,countType,set-60,hourset+1799,initialPeople,correctionPeople); |
| | | dayAllMap.put(i, people[i]); |
| | | } |
| | | } else { |
| | | if(dayOutMap.containsKey(i) && minute >1 && (i != minute-1)){ |
| | | people[i] = dayOutMap.get(i); |
| | | }else{ |
| | | people[i] = getDataValue(map,countType,set-60,hourset+1799,initialPeople,correctionPeople); |
| | | dayOutMap.put(i, people[i]); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | hourset +=1800; |
| | | } |
| | |
| | | * @return |
| | | */ |
| | | private int getDataValue( Map<String, Object> map,int i,long start,long end,int initialPeople,int correctionPeople){ |
| | | String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); |
| | | switch (i){ |
| | | case 1: |
| | | map.put("countType","进校人数"); |
| | | Integer countInPeople = dao.countInPeople(start,end); |
| | | countInPeople=countInPeople==null||countInPeople<0?0:countInPeople; |
| | | Integer countInPeople = dao.countInPeople(start, end, todayStr); |
| | | countInPeople=countInPeople==null||countInPeople < 0? 0 : countInPeople; |
| | | return countInPeople+correctionPeople; |
| | | case -1: |
| | | map.put("countType","出校人数"); |
| | | Integer countOutPeople = dao.countOutPeople(start,end); |
| | | return countOutPeople==null||countOutPeople<0?0:countOutPeople; |
| | | Integer countOutPeople = dao.countOutPeople(start, end, todayStr); |
| | | return countOutPeople==null||countOutPeople < 0? 0 : countOutPeople; |
| | | case 0: |
| | | map.put("countType","校内人数"); |
| | | Integer in = dao.countInPeople(start,end); |
| | | Integer out = dao.countOutPeople(start,end); |
| | | Integer in = dao.countInPeople(start, end, todayStr); |
| | | Integer out = dao.countOutPeople(start, end, todayStr); |
| | | in = in==null||in<0?0:in; |
| | | out = out==null||out<0?0:out; |
| | | return in-out+initialPeople+correctionPeople; |