dbController.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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='wdshy1gxf',db=db,port=3306,charset="utf8");
  18. # pool = PooledDB(pymysql,10,host='10.100.81.179',user='root',passwd=pwd,db=db,port=3306,charset="utf8");
  19. import random
  20. import string
  21. import threading
  22. class DbController():
  23. # 好像是认证的token?
  24. def getToken(self):
  25. token = ""
  26. headers = {"Content-Type": "application/json",
  27. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"}
  28. param = {
  29. "grant_type": config.get("baseconf", "grant_type"),
  30. "client_id": config.get("baseconf", "client_id"),
  31. "client_secret": config.get("baseconf", "client_secret"),
  32. "tenant_id": config.get("baseconf", "tenant_id"),
  33. "timestamp": int(int(round(time.time() * 1000))),
  34. "nonce": ''.join(random.sample(string.ascii_letters + string.digits, 16))
  35. }
  36. print("发送报文")
  37. print(headers)
  38. print(param)
  39. try:
  40. result = requests.post(tokenApiUrl, json=param, headers=headers, timeout=10)
  41. print("result是")
  42. print(result)
  43. data = json.loads(result.text)
  44. errorCode = data["errcode"];
  45. if errorCode == '0':
  46. token = data["data"]["access_token"]
  47. print("token是:"+str(token))
  48. except Exception:
  49. logger.error("获取token失败 ")
  50. logger.error(traceback.print_exc())
  51. logger.error(traceback.format_exc())
  52. return token;
  53. def getBussinessNum(self,cerf_id):
  54. business_num = "";
  55. try:
  56. self.conn = pool.connection();
  57. self.cursor = self.conn.cursor()
  58. sql = "select business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  59. res = self.cursor.execute(sql);
  60. data = self.cursor.fetchone();
  61. if len(data)>0:
  62. business_num = data[0]
  63. except:
  64. logger.error(traceback.print_exc())
  65. logger.error(traceback.format_exc())
  66. self.cursor.close();
  67. self.conn.close();
  68. return business_num;
  69. def updateParseInd(self,cerf_id,parseInd):
  70. try:
  71. self.conn = pool.connection();
  72. self.cursor = self.conn.cursor()
  73. sql = "update querycustomer set parse_ind='"+parseInd+"' where cerf_id='"+cerf_id+"'";
  74. res = self.cursor.execute(sql);
  75. self.conn.commit()
  76. except:
  77. logger.error(" updateParseInd error")
  78. finally:
  79. self.cursor.close();
  80. self.conn.close();
  81. def getParseInd(self,cerf_id):
  82. parse_ind = "";
  83. try:
  84. self.conn = pool.connection();
  85. self.cursor = self.conn.cursor()
  86. sql = "select parse_ind from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  87. res = self.cursor.execute(sql);
  88. data = self.cursor.fetchone();
  89. if len(data)>0:
  90. parse_ind = data[0]
  91. except:
  92. logger.error(" getBussinessNum error"+sql)
  93. info = sys.exc_info()
  94. logger.error(info[0])
  95. logger.error(info[1])
  96. # logging.log(logging.ERROR, info[2])
  97. logger.error(traceback.extract_tb(info[2], 1))
  98. finally:
  99. self.cursor.close();
  100. self.conn.close();
  101. return parse_ind;
  102. def getProductNum(self,cerf_id):
  103. product_num = "";
  104. try:
  105. self.conn = pool.connection();
  106. self.cursor = self.conn.cursor()
  107. sql = "select product_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  108. res = self.cursor.execute(sql);
  109. data = self.cursor.fetchone();
  110. if data !=None and len(data)>0:
  111. product_num = data[0]
  112. except:
  113. logger.error(traceback.print_exc())
  114. logger.error(traceback.format_exc())
  115. finally:
  116. self.cursor.close();
  117. self.conn.close();
  118. return product_num;
  119. def getCoopBussinessNum(self,cerf_id):
  120. coop_business_num = "";
  121. try:
  122. self.conn = pool.connection();
  123. self.cursor = self.conn.cursor()
  124. sql = "select coop_business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  125. res = self.cursor.execute(sql);
  126. data = self.cursor.fetchone();
  127. if len(data)>0:
  128. coop_business_num = data[0]
  129. except:
  130. logger.error(" getBussinessNum error")
  131. self.cursor.close();
  132. self.conn.close();
  133. return coop_business_num;
  134. def checkLast1Month(self,cerf_id):
  135. isExists = False;
  136. try:
  137. self.conn = pool.connection();
  138. self.cursor = self.conn.cursor()
  139. sql = "select coop_business_num from querycustomer where cerf_id='"+cerf_id+"' and DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(query_time)";
  140. res = self.cursor.execute(sql);
  141. data = self.cursor.fetchone();
  142. if len(data)>0:
  143. isExists = True
  144. except:
  145. logger.error(" getBussinessNum error")
  146. self.cursor.close();
  147. self.conn.close();
  148. return isExists;
  149. def getCustomerNum(self,cerf_id):
  150. customer_num = "";
  151. try:
  152. self.conn = pool.connection();
  153. self.cursor = self.conn.cursor()
  154. sql = "select customer_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  155. res = self.cursor.execute(sql);
  156. data = self.cursor.fetchone();
  157. if len(data)>0:
  158. customer_num = data[0]
  159. except:
  160. logger.error(" getBussinessNum error")
  161. self.cursor.close();
  162. self.conn.close();
  163. return customer_num;
  164. def execSql(self,sql):
  165. try:
  166. # self.conn = pool.connection();
  167. # self.cursor = self.conn.cursor()
  168. res = self.cursor.execute(sql);
  169. # self.conn.commit()
  170. # headers = {"Content-Type": "application/json"}
  171. # request_data = {"execSql":sql}
  172. # data = json.dumps(request_data)
  173. # requests.post(execUrl,data,headers)
  174. # logger.info(sql)
  175. except:
  176. logger.error(" saveNode error")
  177. logger.error(traceback.print_exc())
  178. logger.error(traceback.format_exc())
  179. logger.info(sql)
  180. finally:
  181. # self.cursor.close();
  182. # self.conn.close();
  183. pass
  184. # def saveNode(self,sql):
  185. # logger.info(sql)
  186. # t = threading.Thread(target=self.execSql, args=[sql]);
  187. # t.start();
  188. def getConn(self):
  189. self.conn = pool.connection();
  190. self.cursor = self.conn.cursor()
  191. def commit(self):
  192. self.conn.commit()
  193. self.cursor.close();
  194. self.conn.close();