postgresql_local.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from .flightDB import flightDB
  2. import datetime
  3. from unitls.LogerinTxt import app_logger
  4. from unitls.StaticDataclass import get_dic
  5. def postgresql_local(local:flightDB,host_online, port_online, user_online, password_online, database_online):
  6. print(datetime.datetime.now(), "[同步中心]开始同步数据库")
  7. today = datetime.datetime.now().strftime('%Y%m%d')
  8. yesterday=(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y%m%d')
  9. tomorrow=(datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%Y%m%d')
  10. online=flightDB(host_online, port_online, user_online, password_online, database_online)
  11. '''
  12. table_colmuns={"flightinfo%s"%today:flightinfoLabel,"flightinfo%s"%yesterday:flightinfoLabel,"flightinfo%s"%tomorrow:flightinfoLabel,
  13. "peopleschedule%s"%today:peopleScheduleLabel,"peopleschedule%s"%yesterday:peopleScheduleLabel,"peopleschedule%s"%tomorrow:peopleScheduleLabel,
  14. "sortFlight%s"%today:sortLabel,"sortFlight%s"%yesterday:sortLabel,"sortFlight%s"%tomorrow:sortLabel,
  15. "display":displayLabel}
  16. '''
  17. table_colmuns={"TaskFlightinfo%s"%today:get_dic("TaskflightinfoLabel"),"TaskFlightinfo%s"%yesterday:get_dic("TaskflightinfoLabel"),"TaskFlightinfo%s"%tomorrow:get_dic("TaskflightinfoLabel"),
  18. "peopleschedule%s"%today:get_dic('peopleScheduleLabel'),"peopleschedule%s"%yesterday:get_dic('peopleScheduleLabel'),"peopleschedule%s"%tomorrow:get_dic('peopleScheduleLabel'),
  19. "sortFlight%s"%today:get_dic('sortLabel'),"sortFlight%s"%yesterday:get_dic("sortLabel"),"sortFlight%s"%tomorrow:get_dic("sortLabel"),
  20. "display":get_dic("displayLabel"),"flightsearch%s"%today:get_dic("flightsearchLabel"),"flightsearch%s"%yesterday:get_dic("flightsearchLabel"),"flightsearch%s"%tomorrow:get_dic("flightsearchLabel"),}
  21. dbtables=["TaskFlightinfo%s"%today,"TaskFlightinfo%s"%yesterday,"TaskFlightinfo%s"%tomorrow,
  22. "peopleschedule%s"%today,"peopleschedule%s"%yesterday,"peopleschedule%s"%tomorrow,
  23. "flightsearch%s"%today,"flightsearch%s"%yesterday,"flightsearch%s"%tomorrow,
  24. "sortFlight%s"%today,"sortFlight%s"%yesterday,"sortFlight%s"%tomorrow,"display"]
  25. try:
  26. tablesok=""
  27. tablenum=0
  28. for dbtable in dbtables:
  29. try:
  30. Dic = {}
  31. primaryKey2 = 'text'
  32. for key in list(table_colmuns[dbtable])[1:]:
  33. Dic[key] = 'text'
  34. online.initTable(dbtable, table_colmuns[dbtable][0], primaryKey2, Dic)
  35. all_source_data=local.getAlldata(dbtable)
  36. if len(all_source_data)!=0:
  37. online.lazydeleteTable(dbtable)
  38. if dbtable=="display":
  39. dispaly_res=online.getSingledata("A", 'display')
  40. if dispaly_res == None or len(dispaly_res) == 0:
  41. online.insertData('display', {'ID': 1, 'A': '{}', 'B': '{}'})
  42. online.insertData('display', {'ID': 2, 'A': '{}', 'B': '{}'})
  43. online.insertData('display', {'ID': 3, 'A': '{}', 'B': '{}'})
  44. for row in all_source_data:
  45. newdic = {"A": '"{}"'.format(row[1]),"B": '"{}"'.format(row[2])}
  46. online.lazyUpdateItem(dbtable, newdic, "ID = '%s'"%row[0])
  47. #online.FunctionCommit()
  48. else:
  49. for row in all_source_data:
  50. online.lazyInsertData2(dbtable,', '.join(table_colmuns[dbtable]),row)
  51. online.FunctionCommit()
  52. tablesok=tablesok+dbtable+"/"
  53. tablenum+=1
  54. except Exception as e:
  55. app_logger.log_error(e)
  56. continue
  57. print(datetime.datetime.now(), "[同步中心]完成数据库同步(%s[%s])"%(tablesok,tablenum))
  58. return "ok"
  59. except Exception as e:
  60. app_logger.log_error(e)
  61. return "fail"