dbController.py0708 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. base_dir = os.path.dirname(os.path.abspath(__file__))
  11. config = Config(base_dir+"/config.ini");
  12. tokenApiUrl = config.get("baseconf", "tokenApiUrl");
  13. pwd = config.get("baseconf", "pwd");
  14. db = config.get("baseconf", "db");
  15. pool = PooledDB(pymysql,10,host='localhost',user='root',passwd=pwd,db=db,port=3306,charset="utf8");
  16. class DbController():
  17. def getToken(self):
  18. token = ""
  19. headers = {"Content-Type": "application/json",
  20. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"}
  21. param = {
  22. "grant_type": config.get("baseconf", "grant_type"),
  23. "client_id": config.get("baseconf", "client_id"),
  24. "client_secret": config.get("baseconf", "client_secret"),
  25. "tenant_id": config.get("baseconf", "tenant_id"),
  26. "timestamp": int(int(round(time.time() * 1000))),
  27. "nonce": config.get("baseconf", "nonce")
  28. }
  29. try:
  30. result = requests.post(tokenApiUrl, json=param, headers=headers, timeout=10)
  31. data = json.loads(result.text)
  32. errorCode = data["errcode"];
  33. if errorCode == '0':
  34. token = data["data"]["access_token"];
  35. except:
  36. logger.error("获取token失败 ")
  37. return token;
  38. def getBussinessNum(self,cerf_id):
  39. business_num = "";
  40. try:
  41. self.conn = pool.connection();
  42. self.cursor = self.conn.cursor()
  43. sql = "select business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  44. res = self.cursor.execute(sql);
  45. data = self.cursor.fetchone();
  46. if len(data)>0:
  47. business_num = data[0]
  48. except:
  49. logger.error(" getBussinessNum error")
  50. self.cursor.close();
  51. self.conn.close();
  52. return business_num;
  53. def updateParseInd(self,cerf_id,parseInd):
  54. try:
  55. self.conn = pool.connection();
  56. self.cursor = self.conn.cursor()
  57. sql = "update querycustomer set parse_ind='"+parseInd+"' where cerf_id='"+cerf_id+"'";
  58. res = self.cursor.execute(sql);
  59. self.conn.commit()
  60. except:
  61. logger.error(" updateParseInd error")
  62. finally:
  63. self.cursor.close();
  64. self.conn.close();
  65. def getParseInd(self,cerf_id):
  66. parse_ind = "";
  67. try:
  68. self.conn = pool.connection();
  69. self.cursor = self.conn.cursor()
  70. sql = "select parse_ind from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  71. res = self.cursor.execute(sql);
  72. data = self.cursor.fetchone();
  73. if len(data)>0:
  74. parse_ind = data[0]
  75. except:
  76. logger.error(" getBussinessNum error")
  77. finally:
  78. self.cursor.close();
  79. self.conn.close();
  80. return parse_ind;
  81. def getProductNum(self,cerf_id):
  82. product_num = "";
  83. try:
  84. self.conn = pool.connection();
  85. self.cursor = self.conn.cursor()
  86. sql = "select product_num 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. product_num = data[0]
  91. except:
  92. logger.error(" product_num error")
  93. finally:
  94. self.cursor.close();
  95. self.conn.close();
  96. return product_num;
  97. def getCoopBussinessNum(self,cerf_id):
  98. coop_business_num = "";
  99. try:
  100. self.conn = pool.connection();
  101. self.cursor = self.conn.cursor()
  102. sql = "select coop_business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  103. res = self.cursor.execute(sql);
  104. data = self.cursor.fetchone();
  105. if len(data)>0:
  106. coop_business_num = data[0]
  107. except:
  108. logger.error(" getBussinessNum error")
  109. self.cursor.close();
  110. self.conn.close();
  111. return coop_business_num;