Commit a8d8ed01 authored by chenyuling's avatar chenyuling

处理人数

parent bc659d3f
package com.srthinker.statinfo.presenter;
import com.srthinker.statinfo.database.entity.PersonEntity;
import java.util.ArrayList;
import java.util.List;
public class StatSize {
//id和时间相同的进行去重过滤
public static List<PersonEntity> filterList(List<PersonEntity> personEntityList){
List<PersonEntity> filterPersonList = new ArrayList<>();
if (personEntityList != null) {
for (PersonEntity personEntity : personEntityList) {
boolean shouldAdd = true;
for (PersonEntity entity : filterPersonList) {
if (equals(personEntity.getId(),entity.getId())&&equals(personEntity.getTimestamp(),entity.getTimestamp())){
shouldAdd = false;
break;
}
}
if (shouldAdd){
filterPersonList.add(personEntity);
}
}
}
return filterPersonList;
}
private static boolean equals(String str1, String str2) {
if (str1 == null && str2 == null) {
return true;
} else if (str1 == null || str2 == null) {
return false;
} else {
return str1.equals(str2);
}
}
}
......@@ -50,6 +50,7 @@ import com.srthinker.statinfo.listener.QueryPersonCallback;
import com.srthinker.statinfo.presenter.ApiManager;
import com.srthinker.statinfo.presenter.ApiQuest;
import com.srthinker.statinfo.presenter.RealTimeClock;
import com.srthinker.statinfo.presenter.StatSize;
import com.srthinker.statinfo.uis.fragment.ConfigDlgFragment;
import com.srthinker.statinfo.update.MDownloadCallBack;
import com.srthinker.statinfo.update.NetworkViewModel;
......@@ -100,7 +101,6 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
private HashMap<Integer,Integer> groupPresentHashMap=null;
private boolean isPermission;
public static boolean NEED_NO_REPET = true; //是否需要去重,false:不需要,true:需要
private boolean NEED_SEE_NEXT = true; //去重是怎么去,true只去重相邻的,false则全部去重
@Override
......@@ -377,7 +377,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
enterAdapter.setDateList(enterBeans);
//更新人员情况(是否要根据id去重)
if (NEED_NO_REPET) {
List<PersonEntity> enterListNo = Utils.removeDuplicates(enterBeans, "id",NEED_SEE_NEXT);
List<PersonEntity> enterListNo = StatSize.filterList(enterBeans);
lastEnterSize = enterListNo.size();
}else{
lastEnterSize = size;
......@@ -404,7 +404,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
leaveAdapter.setDateList(leaveBeans);
//更新人员情况(是否要根据id去重)
if (NEED_NO_REPET) {
List<PersonEntity> leaveListNo = Utils.removeDuplicates(leaveBeans, "id",NEED_SEE_NEXT);
List<PersonEntity> leaveListNo = StatSize.filterList(leaveBeans);
lastLeaveSize = leaveListNo.size();
}else{
lastLeaveSize = size;
......@@ -431,7 +431,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
private HashMap<Integer, Integer> getGroupSizeHashMap(List<PersonEntity> personEntityList) {
HashMap<Integer, Integer> groupSizeHashMap = new HashMap<>();
if (NEED_NO_REPET) {
personEntityList = Utils.removeDuplicates(personEntityList,"id",NEED_SEE_NEXT);
personEntityList = StatSize.filterList(personEntityList);
}
if (mQueryPersonList != null) {
for (PersonEntity entity : mQueryPersonList) {
......
......@@ -2,6 +2,7 @@ package com.srthinker.statinfo.util.common;
import android.text.TextUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
......@@ -313,4 +314,22 @@ public class DateUtil {
Date date = fromLocalZoneFormattedDateTime(zoneString, DEFAULT_FORMAT);
return getFormattedDateTime(date, format);
}
//时间相差为多少s,先将字符串转化成date再比较
public static long getTimeDifference(String format,String time1, String time2) {
DateFormat dateFormat = new SimpleDateFormat(format);
try {
Date date1 = dateFormat.parse(time1);
Date date2 = dateFormat.parse(time2);
if (date1 != null && date2!=null) {
long diffInMillis = Math.abs(date1.getTime() - date2.getTime());
long diffInSeconds = diffInMillis / 1000;
return diffInSeconds;
}
return -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
......@@ -273,40 +273,8 @@ public class Utils {
return distinctList;
}
public static <T> List<T> removeDuplicates(List<T> originalList, String fieldName,boolean seeNextOne) {
Set<Object> set = new LinkedHashSet<>();
List<T> distinctList = new ArrayList<>();
if (seeNextOne) {
for (int i = 0; i < originalList.size(); i++) {
T item = originalList.get(i);
Object fieldValue = getFieldValue(item, fieldName);
if (i<originalList.size()-1){
T nextItem = originalList.get(i + 1);
Object nextFileValue = getFieldValue(nextItem, fieldName);
if (!fieldValue.equals(nextFileValue)){
distinctList.add(item);
}else{
i++;
distinctList.add(nextItem);
}
}else{
distinctList.add(item);
}
}
}else{
for (T item : originalList) {
Object fieldValue = getFieldValue(item, fieldName);
if (!set.contains(fieldValue)) {
set.add(fieldValue);
distinctList.add(item);
}
}
}
return distinctList;
}
private static Object getFieldValue(Object obj, String fieldName) {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
......
package com.srthinker.statinfo;
import com.srthinker.statinfo.database.entity.PersonEntity;
import com.srthinker.statinfo.presenter.StatSize;
import com.srthinker.statinfo.util.common.DateUtil;
import com.srthinker.statinfo.util.common.Utils;
import org.junit.Test;
......@@ -9,7 +11,7 @@ import java.util.ArrayList;
import java.util.List;
public class UtilTest {
@Test
/*@Test
public void noRepet(){
List<PersonEntity> personEntities = new ArrayList<>();
for (int i = 0; i < 4; i++) {
......@@ -27,6 +29,35 @@ public class UtilTest {
for (PersonEntity personEntity : personEntities1) {
System.out.println("去重后:id="+personEntity.getId()+" name="+personEntity.getPerson_name()+" time="+personEntity.getTimestamp());
}
}*/
@Test
public void filter(){
List<PersonEntity> personEntities = new ArrayList<>();
for (int i = 0; i < 4; i++) {
PersonEntity person = new PersonEntity();
person.setId("1");
person.setPerson_name("王大崔");
person.setTimestamp("11:0"+0);
if (i==3){
person.setTimestamp("11:0"+i);
}
personEntities.add(person);
}
for (PersonEntity personEntity : personEntities) {
System.out.println("去重前:id="+personEntity.getId()+" name="+personEntity.getPerson_name()+" time="+personEntity.getTimestamp());
}
List<PersonEntity> personEntities2 = Utils.reverseList(personEntities);
List<PersonEntity> personEntities1 = StatSize.filterList(personEntities2);
for (PersonEntity personEntity : personEntities1) {
System.out.println("去重后:id="+personEntity.getId()+" name="+personEntity.getPerson_name()+" time="+personEntity.getTimestamp());
}
}
@Test
public void getDiffer(){
long timeDifference = DateUtil.getTimeDifference("HH:mm", "", "10:01");
System.out.println("相差为"+timeDifference);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment