93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
import datetime
|
|
from typing import Union
|
|
|
|
import firebase_admin
|
|
from firebase_admin import credentials, firestore
|
|
|
|
import definitions
|
|
from pojo.MailPojo import MailPojo
|
|
from pojo.ReserveResultPojo import ReserveResultPojo
|
|
from pojo.SimInfoPojo import SimInfoPojo
|
|
from pojo.contact_pojo import ContactPojo
|
|
|
|
ERROR_COLLECTION_NAME = "error_items"
|
|
CONTACT_COLLECTION_NAME = "contact_list"
|
|
MAIL_COLLECTION_NAME = "mail_list"
|
|
SIM_INFOS = "sim_infos"
|
|
TIMEOUT = "timeout_items"
|
|
|
|
|
|
class DataManager:
|
|
batch_size = 20
|
|
|
|
def __init__(self):
|
|
cred = credentials.Certificate(definitions.FIREBASE_CONFIG_FILE)
|
|
self._app = firebase_admin.initialize_app(cred)
|
|
self._db = firestore.client()
|
|
|
|
def get_all_sim_infos(self):
|
|
sim_info_collection = self._db.collection(SIM_INFOS)
|
|
return sim_info_collection
|
|
|
|
def get_all_successful_items(self):
|
|
return self.get_all_successful_items_for_day(str(datetime.date.today()), None)
|
|
|
|
def get_all_successful_items_for_day(self, day, source_from: Union[str, None]):
|
|
doc_ref = self._db.collection(day)
|
|
if source_from is not None:
|
|
doc_ref.where(u'source_from', u'==', source_from)
|
|
return doc_ref
|
|
|
|
def save_sim_info(self, sim_info: SimInfoPojo):
|
|
doc_ref = self._db.collection(SIM_INFOS).document(sim_info.phone)
|
|
doc_ref.set(sim_info.to_firestore_dict())
|
|
|
|
def save(self, result: ReserveResultPojo):
|
|
id = result.url.split("/")[-1]
|
|
result.id = id
|
|
document_name = str(datetime.date.today())
|
|
doc_ref = self._db.collection(document_name).document(result.id)
|
|
doc_ref.set(result.to_firestore_dict())
|
|
|
|
def find_appointment_detail_via_phone(self, day, phone) -> ReserveResultPojo:
|
|
doc_ref = self._db.collection(day)
|
|
results = doc_ref.where(u'phone', u'==', phone).stream()
|
|
result_list = []
|
|
for result in results:
|
|
result_list.append(ReserveResultPojo.from_firestore_dict(result.to_dict()))
|
|
if len(result_list) > 0:
|
|
return result_list[0]
|
|
|
|
def save_timeout_contact(self, contact: ContactPojo):
|
|
doc_ref = self._db.collection(TIMEOUT).document(str(contact.phone))
|
|
doc_ref.set(contact.to_firestore_dict())
|
|
|
|
def _delete_collection(self, coll_ref, batch_size):
|
|
docs = coll_ref.limit(batch_size).stream()
|
|
deleted = 0
|
|
|
|
for doc in docs:
|
|
print(f'Deleting doc {doc.id} => {doc.to_dict()}')
|
|
doc.reference.delete()
|
|
deleted = deleted + 1
|
|
|
|
if deleted >= batch_size:
|
|
return self._delete_collection(coll_ref, batch_size)
|
|
|
|
# 删除数据库中所有的sim卡信息
|
|
def clear_all_sim_info(self):
|
|
coll_ref = self._db.collection(SIM_INFOS)
|
|
self._delete_collection(coll_ref, self.batch_size)
|
|
|
|
def read_contacts_from_db(self) -> list:
|
|
contact_collection = self._db.collection(CONTACT_COLLECTION_NAME)
|
|
return contact_collection
|
|
|
|
def get_mail_list(self) -> list:
|
|
mail_collection = self._db.collection(MAIL_COLLECTION_NAME)
|
|
mail_list = []
|
|
for mail in mail_collection.stream():
|
|
mail_pojo = MailPojo.from_firestore_dict(mail.to_dict())
|
|
mail_list.append(mail_pojo)
|
|
return mail_list
|