dbController - 副本.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import pymysql;
  2. from DBUtils.PooledDB import PooledDB;
  3. import logApi
  4. logger = logApi.logger
  5. import os
  6. from ini_op import Config;
  7. import json
  8. import time
  9. import requests
  10. import sys
  11. import traceback
  12. base_dir = os.path.dirname(os.path.abspath(__file__))
  13. config = Config(base_dir+"/config.ini");
  14. tokenApiUrl = config.get("baseconf", "tokenApiUrl");
  15. pwd = config.get("baseconf", "pwd");
  16. db = config.get("baseconf", "db");
  17. pool = PooledDB(pymysql,10,host='localhost',user='root',passwd=pwd,db=db,port=3306,charset="utf8");
  18. class DbController():
  19. def getToken(self):
  20. token = ""
  21. headers = {"Content-Type": "application/json",
  22. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"}
  23. param = {
  24. "grant_type": config.get("baseconf", "grant_type"),
  25. "client_id": config.get("baseconf", "client_id"),
  26. "client_secret": config.get("baseconf", "client_secret"),
  27. "tenant_id": config.get("baseconf", "tenant_id"),
  28. "timestamp": int(int(round(time.time() * 1000))),
  29. "nonce": config.get("baseconf", "nonce")
  30. }
  31. try:
  32. result = requests.post(tokenApiUrl, json=param, headers=headers, timeout=10)
  33. data = json.loads(result.text)
  34. errorCode = data["errcode"];
  35. if errorCode == '0':
  36. token = data["data"]["access_token"];
  37. except:
  38. logger.error("获取token失败 ")
  39. return token;
  40. def getBussinessNum(self,cerf_id):
  41. business_num = "";
  42. try:
  43. self.conn = pool.connection();
  44. self.cursor = self.conn.cursor()
  45. sql = "select business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  46. res = self.cursor.execute(sql);
  47. data = self.cursor.fetchone();
  48. if len(data)>0:
  49. business_num = data[0]
  50. except:
  51. logger.error(" getBussinessNum error")
  52. self.cursor.close();
  53. self.conn.close();
  54. return business_num;
  55. def updateParseInd(self,cerf_id,parseInd):
  56. try:
  57. self.conn = pool.connection();
  58. self.cursor = self.conn.cursor()
  59. sql = "update querycustomer set parse_ind='"+parseInd+"' where cerf_id='"+cerf_id+"'";
  60. res = self.cursor.execute(sql);
  61. self.conn.commit()
  62. except:
  63. logger.error(" updateParseInd error")
  64. finally:
  65. self.cursor.close();
  66. self.conn.close();
  67. def getParseInd(self,cerf_id):
  68. parse_ind = "";
  69. try:
  70. self.conn = pool.connection();
  71. self.cursor = self.conn.cursor()
  72. sql = "select parse_ind from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  73. res = self.cursor.execute(sql);
  74. data = self.cursor.fetchone();
  75. if len(data)>0:
  76. parse_ind = data[0]
  77. except:
  78. logger.error(" getBussinessNum error"+sql)
  79. info = sys.exc_info()
  80. logger.error(info[0])
  81. logger.error(info[1])
  82. # logging.log(logging.ERROR, info[2])
  83. logger.error(traceback.extract_tb(info[2], 1))
  84. finally:
  85. self.cursor.close();
  86. self.conn.close();
  87. return parse_ind;
  88. def getProductNum(self,cerf_id):
  89. product_num = "";
  90. try:
  91. self.conn = pool.connection();
  92. self.cursor = self.conn.cursor()
  93. sql = "select product_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  94. res = self.cursor.execute(sql);
  95. data = self.cursor.fetchone();
  96. if data !=None and len(data)>0:
  97. product_num = data[0]
  98. except:
  99. logger.error(" getBussinessNum error" + sql)
  100. info = sys.exc_info()
  101. logger.error(info[0])
  102. logger.error(info[1])
  103. # logging.log(logging.ERROR, info[2])
  104. logger.error(traceback.extract_tb(info[2], 1))
  105. finally:
  106. self.cursor.close();
  107. self.conn.close();
  108. return product_num;
  109. def getCoopBussinessNum(self,cerf_id):
  110. coop_business_num = "";
  111. try:
  112. self.conn = pool.connection();
  113. self.cursor = self.conn.cursor()
  114. sql = "select coop_business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  115. res = self.cursor.execute(sql);
  116. data = self.cursor.fetchone();
  117. if len(data)>0:
  118. coop_business_num = data[0]
  119. except:
  120. logger.error(" getBussinessNum error")
  121. self.cursor.close();
  122. self.conn.close();
  123. return coop_business_num;
  124. def checkLast1Month(self,cerf_id):
  125. isExists = False;
  126. try:
  127. self.conn = pool.connection();
  128. self.cursor = self.conn.cursor()
  129. sql = "select coop_business_num from querycustomer where cerf_id='"+cerf_id+"' and DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(query_time)";
  130. res = self.cursor.execute(sql);
  131. data = self.cursor.fetchone();
  132. if len(data)>0:
  133. isExists = True
  134. except:
  135. logger.error(" getBussinessNum error")
  136. self.cursor.close();
  137. self.conn.close();
  138. return isExists;
  139. def getCustomerNum(self,cerf_id):
  140. customer_num = "";
  141. try:
  142. self.conn = pool.connection();
  143. self.cursor = self.conn.cursor()
  144. sql = "select customer_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  145. res = self.cursor.execute(sql);
  146. data = self.cursor.fetchone();
  147. if len(data)>0:
  148. customer_num = data[0]
  149. except:
  150. logger.error(" getBussinessNum error")
  151. self.cursor.close();
  152. self.conn.close();
  153. return customer_num;