Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import pymssql
  4. import sys
  5. import locale
  6. reload(sys)
  7. sys.setdefaultencoding('utf-8')
  8. server = "ip"
  9. user = "sa"
  10. password = "password"
  11. database = "base"
  12.  
  13. conn = pymssql.connect(server, user, password, database)
  14. cursor = conn.cursor()
  15.  
  16. banknotes = ["5000", "2000", "1000", "500", "200", "100", "50"]
  17.  
  18. def get_sum_for_terminals(acceptor_id):
  19. total_sum = 0
  20. for banknote_value in banknotes:
  21. cursor.execute("SELECT (denomination * count) FROM acceptorsupplies WHERE id = (SELECT MAX(id) FROM acceptorsupplies WHERE denomination = '%s.000000' AND Acceptorid='%s' )" % (banknote_value, acceptor_id))
  22. banknote_sum = cursor.fetchall()
  23. if banknote_sum:
  24. total_sum += int(banknote_sum[0][0])
  25. return total_sum
  26.  
  27. def get_acceptor_id(terminal_id):
  28. cursor.execute("SELECT id FROM acceptor WHERE terminalid = '%s'" % terminal_id)
  29. acceptor_id = cursor.fetchall()[0][0]
  30. return acceptor_id
  31.  
  32. def get_cards_in(terminal_id):
  33. cursor.execute('''SELECT cardin.count - cardout.dispensecount - cardout.rejectcount
  34. FROM carddispensersuppliesin as cardin
  35. LEFT JOIN CardDispenserSuppliesOut as cardout on cardin.TerminalId = cardout.TerminalId
  36. WHERE cardin.id = (SELECT max(id) FROM carddispensersuppliesin WHERE terminalid = '%s')
  37. AND cardout.id = (SELECT max(id) FROM CardDispenserSuppliesOut WHERE terminalid = '%s')''' % (terminal_id, terminal_id))
  38. cards_in = cursor.fetchall()[0][0]
  39. return cards_in
  40.  
  41. def get_cards_out(terminal_id):
  42. cursor.execute("SELECT dispensecount FROM carddispensersuppliesout WHERE id = (SELECT max(id) FROM carddispensersuppliesout WHERE terminalid = '%s')" % terminal_id)
  43. cards_out = cursor.fetchall()[0][0]
  44. return cards_out
  45.  
  46. def get_cards_bad(terminal_id):
  47. cursor.execute("SELECT rejectcount FROM carddispensersuppliesout WHERE id = (SELECT max(id) FROM carddispensersuppliesout WHERE terminalid = '%s')" % terminal_id)
  48. cards_bad = cursor.fetchall()[0][0]
  49. return cards_bad
  50.  
  51. def get_online_status(terminal_id):
  52. cursor.execute("SELECT count(*) FROM TerminalOnline WHERE terminalid = '%s' and EndTime > dateadd(minute,-181, getdate())" % terminal_id)
  53. online_status = cursor.fetchall()[0][0]
  54. return online_status
  55. def stat():
  56. cursor.execute("SELECT id FROM terminal WHERE isenabled = 'True' AND isremoved = 'False'")
  57. terminals = cursor.fetchall()
  58.  
  59. terminal_names = {1: 'Term1', 2: 'Term2', 6: 'Term3', 7: 'Term4', 9: 'Term5', 10: 'Term6', 11: 'Term7', 12: 'Term8', 13: 'Term9', 14: 'Term10', 15: 'Term11', 16: 'Term12', 17: 'Term13'}
  60. term_id_acc_id = {}
  61. for terminal_id in terminals:
  62. term_id_acc_id[terminal_id] = [get_acceptor_id(terminal_id)]
  63.  
  64. acc_id_sum = {}
  65. for acceptor_id in term_id_acc_id.values():
  66. acc_id_sum[acceptor_id[0]] = [get_sum_for_terminals(acceptor_id[0])]
  67.  
  68. term_id_cards_in = {}
  69. for terminal_id in terminals:
  70. term_id_cards_in[terminal_id] = [get_cards_in(terminal_id[0])]
  71.  
  72. term_id_cards_out = {}
  73. for terminal_id in terminals:
  74. term_id_cards_out[terminal_id] = [get_cards_out(terminal_id)]
  75.  
  76. term_id_cards_bad = {}
  77. for terminal_id in terminals:
  78. term_id_cards_bad[terminal_id] = [get_cards_bad(terminal_id)]
  79.  
  80. total_sum = 0
  81. for acc_id, sum in acc_id_sum.items():
  82. total_sum += sum[0]
  83.  
  84. online_status = {}
  85. for terminal_id in terminals:
  86. if int(get_online_status(terminal_id)) == 0:
  87. if terminal_names.has_key(terminal_id[0]):
  88. term_name = terminal_names[terminal_id[0]]
  89. online_status[term_name] = [get_online_status(terminal_id)]
  90.  
  91. total_shit = []
  92. term_id_sum = {}
  93. for term_id, acc_id in term_id_acc_id.items():
  94. for acc_id_2, sum in acc_id_sum.items():
  95. if acc_id[0] == acc_id_2:
  96. for term_id_2, cards_in in term_id_cards_in.items():
  97. if term_id_2 == term_id:
  98. for term_id_3, cards_out in term_id_cards_out.items():
  99. if term_id_3 == term_id:
  100. for term_id_4,cards_bad in term_id_cards_bad.items():
  101. if term_id_4 == term_id:
  102. if terminal_names.has_key(term_id[0]):
  103. term_name = terminal_names[term_id[0]]
  104. #total_shit.append([term_name, sum[0], '11', cards_out[0],cards_bad[0]])
  105. total_shit.append([term_name, sum[0], cards_in[0], cards_out[0],cards_bad[0]])
  106. return total_shit, total_sum, online_status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement