gpt4 book ai didi

java - 尝试在将文档接受到集合 Cloud Firestore 之前添加检查

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:54 28 4
gpt4 key购买 nike

我正在构建一个二维码考勤扫描仪,希望查看扫描是否在我的 session 集合上的开始结束时间字段的限制内。如果扫描发生在两次之间,则应接受扫描,否则将不接受扫描并显示 toast。目前,我检查 session 是否仅存在,而不是在开始时间和结束时间字段的时间之间存在。 rawData 是从 QR 码中提取的 sessionID,提取后会检查它是否是有效的 sessionID。现在我只需要检查扫描时间。任何帮助都会很棒:

private void barcodeRecognition(Bitmap photo) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(photo);
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
.getVisionBarcodeDetector();
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
@Override
public void onSuccess(List<FirebaseVisionBarcode> barcodes) {

for (FirebaseVisionBarcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();

final String rawValue = barcode.getRawValue();
int valueType = barcode.getValueType();


mAuth.getCurrentUser();
// TRY LINKING TO userUID on Attendance on Firebase console
FirebaseUser currentUser = mAuth.getCurrentUser();
final String user = currentUser.getUid(); // USED TO GET CURRENT USER UID OF PERSON LOGGED IN
//THIS CAN BE SET TO .getUserName() or getEmail() depending on preference

Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
final String time = format.format(calendar.getTime());

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference docRef = rootRef.collection("Session").document(rawValue);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if (document.exists()){
Attendance attendance = new Attendance(rawValue,user,time);
attendanceRef3.add(attendance);
Toast.makeText(StudentAccount.this, "Your attendance has been recorded", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StudentAccount.this, "Session is not available", Toast.LENGTH_SHORT).show();
}
}
}
});

enter image description here

更新的代码

 final FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
final DocumentReference docRef = rootRef.collection("Session").document(rawValue);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if (document.exists()){

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Session")
.whereGreaterThanOrEqualTo("startTime", calendar.getTime())
.whereLessThan("endTime", calendar.getTime());

Attendance attendance = new Attendance(rawValue,user,time);
attendanceRef3.add(attendance);
Toast.makeText(StudentAccount.this, "Your attendance has been recorded", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(StudentAccount.this, "Session is not available", Toast.LENGTH_SHORT).show();
}
}
}
});

编辑代码

 private void barcodeRecognition(Bitmap photo) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(photo);
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
.getVisionBarcodeDetector();
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
@Override
public void onSuccess(List<FirebaseVisionBarcode> barcodes) {

for (FirebaseVisionBarcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();

final String rawValue = barcode.getRawValue();
int valueType = barcode.getValueType();


mAuth.getCurrentUser();
// TRY LINKING TO userUID on Attendance on Firebase console
FirebaseUser currentUser = mAuth.getCurrentUser();
final String user = currentUser.getUid(); // USED TO GET CURRENT USER UID OF PERSON LOGGED IN
//THIS CAN BE SET TO .getUserName() or getEmail() depending on preference

final Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
final String time = format.format(calendar.getTime());

final FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
final DocumentReference docRef = rootRef.collection("Session").document(rawValue);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if (document.exists()) {

Date timeToCheck = calendar.getTime();
Session session = document.toObject(Session.class);
Date startTime = session.getStartTime();
Date endTime = session.getEndTime();
if (timeToCheck.after(startTime) && (timeToCheck.before(endTime))) {

Attendance attendance = new Attendance(rawValue, user, time);
attendanceRef3.add(attendance);
Toast.makeText(StudentAccount.this, "Your attendance has been recorded", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(StudentAccount.this, "Your attendance attempt is too late", Toast.LENGTH_SHORT).show();
}
}
}
});

最佳答案

根据您的数据库结构,要根据 startTimeendTime 属性的限制过滤数据,请使用以下查询:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("session")
.whereGreaterThanOrEqualTo("startTime", timeToCheck)
.whereLessThanOrEqualTo("endTime", timeToCheck);

如果不想相等,也可以使用下面的代码:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("session")
.whereGreaterThan("startTime", timeToCheck)
.whereLessThan("endTime", timeToCheck);

因此,使用此代码,您将能够获取 session 集合上开始和结束时间字段限制内的所有 session 对象。

编辑:

如果要检查单个文档,则需要使用另一个 if 语句。因此,在您实际的 if (document.exists()){ 中创建另一个如下所示的代码:

Attendance attendance = document.toObject(Attendance.class);
Date startTime = attendance.getStartTime();
Date endTime = attendance.getEndTime();
if(timeToCheck.after(startTime) && (timeToCheck.before(endTime)) {
//Do what you need to do
}

其中 timeToCheckDate 类型的对象,您需要根据数据库中的实际日期进行检查。

关于java - 尝试在将文档接受到集合 Cloud Firestore 之前添加检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968957/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com