package com.basic.security.manager; import android.os.SystemClock; import android.text.TextUtils; import com.basic.security.base.BaseApplication; import com.basic.security.model.Guest; import com.basic.security.model.ModelAdapter; import com.basic.security.model.Person; import com.basic.security.utils.Constants; import org.apache.commons.io.FileUtils; import java.io.File; import java.util.ArrayList; import java.util.List; public class GuestManager extends BaseManager { static long deleteMoreThanLastTime = 0; public static void saveGuest(ModelAdapter guest) { save(guest); deleteMoreThan(guest, 50); } public static ModelAdapter findGuestListRecentOne(int guestListCameraType) { String sql = "select * from guest where " + Guest.camera_id + "='" + guestListCameraType + "' order by " + Guest.create_time + " desc limit 1"; return findOne(sql); } public static List findGuestList(String cameraId) { List guestList = findList("select * from guest where 1=1 " + " and " + Guest.camera_id + "='" + cameraId + "'" + " order by " + Guest.create_time + " desc limit 50"); List guestToRemoveList = new ArrayList<>(); for (ModelAdapter guest : guestList) { byte[] cameraImageFeature = guest.getBlob(Guest.camera_image_feature); if (TextUtils.isEmpty(guest.getString(Guest.person_id)) && (cameraImageFeature == null || cameraImageFeature.length < 10)) { guestToRemoveList.add(guest); } } for (ModelAdapter guestToRemove : guestToRemoveList) { deleteGuest(guestToRemove); guestList.remove(guestToRemove); } return guestList; } public static void deleteMoreThan(ModelAdapter guest, int rows) { if (System.currentTimeMillis() - deleteMoreThanLastTime > 10000) { String sql = "select * from guest where " + Guest.camera_id + "='" + guest.getString(Guest.camera_id) + "'" + " order by create_time desc limit " + rows + ",1000000 "; List modelAdapterList = findList(sql); for (ModelAdapter modelAdapter : modelAdapterList) { deleteGuestAttachment(modelAdapter); } deleteMoreThanLastTime = System.currentTimeMillis(); } } public static void deleteGuestAttachment(ModelAdapter modelAdapter) { try { File camera_image_pathFile = new File(ModelAdapter.getAttachmentPath(modelAdapter.getId(), Person.camera_image_path, Guest.tableName)); if (camera_image_pathFile.exists()) { FileUtils.forceDelete(camera_image_pathFile); } } catch (Exception e) { System1.out.println("GuestManager.deleteGuestAttachment " + e.getMessage()); } deletePhysically(modelAdapter); } public static void deleteGuest(ModelAdapter checkedGuest) { deleteGuestAttachment(checkedGuest); } public static void removePersonId(String personId) { if (!TextUtils.isEmpty(personId)) { ModelAdapter person = PersonManager.findPersonById(personId); if (person != null) { String idCardNumber = person.getString(Person.id_card_number); if (!TextUtils.isEmpty(idCardNumber)) { DatabaseManager.getDatabase().execSQL("delete from " + Guest.tableName + " where " + Guest.id_card_number + "='" + idCardNumber + "'"); } } DatabaseManager.getDatabase().execSQL("delete from " + Guest.tableName + " where " + Guest.person_id + "='" + personId + "'"); BaseApplication.getApplication().activity.fragment_guest_mode.reloadCameraList(); SystemClock.sleep(1 * 1000); BaseApplication.getApplication().activity.fragment_guest_mode.initGuest(null); } } public static void updateGuest(ModelAdapter checkedGuest, ModelAdapter newPersonAsGuest) { try { checkedGuest.setString(Guest.person_id, newPersonAsGuest.getString(Person.id)); checkedGuest.setString(Guest.name, newPersonAsGuest.getString(Person.name)); save(checkedGuest); } catch (Exception e) { e.printStackTrace(); } } public static boolean twoCameras() { if (Constants.USE_GRAY_CAMERA) { return false; } return true; } public static void updateGuestName(ModelAdapter person) { try { String personId = person.getString(Person.id); if (!TextUtils.isEmpty(personId)) { String personName = person.getString(Person.name); DatabaseManager.execSQL("update " + Guest.tableName + " set " + Guest.name + "='" + personName + "' where 1=1 and " + Guest.person_id + "='" + personId + "'"); } } catch (Exception e) { e.printStackTrace(); } } }