userController.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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='localhost',user='root',passwd='$661883$',db=db,port=3306,charset="utf8");
  19. class UserController():
  20. def getConn(self):
  21. self.conn = pool.connection();
  22. self.cursor = self.conn.cursor();
  23. # 获取当前所有商户
  24. def getUser(self):
  25. try:
  26. res=[];
  27. self.conn=pool.connection();
  28. self.cursor = self.conn.cursor();
  29. sql = "select * from sys_user where `role_id` = 4";
  30. count = self.cursor.execute(sql);
  31. for i in range(count):
  32. user_info={};
  33. user_info['user_name']=self.cursor.fetchone()[2];
  34. res.append(user_info);
  35. except:
  36. logger.error(traceback.print_exc())
  37. logger.error(traceback.format_exc())
  38. self.cursor.close();
  39. self.conn.close();
  40. return res;
  41. #现在需要getModelByUserAndNum以及getFieldByUserAndModel
  42. # def getModelByNum(self,product_num):
  43. # try:
  44. # res=[];
  45. # self.conn=pool.connection();
  46. # self.cursor = self.conn.cursor();
  47. #
  48. # sql = "select * from user_model where `product_num` = '"+product_num+"' and model_state ='已上线'" ;
  49. # count = self.cursor.execute(sql);
  50. # for i in range(count):
  51. # data=self.cursor.fetchone();
  52. #
  53. # except:
  54. # logger.error(traceback.print_exc())
  55. # logger.error(traceback.format_exc())
  56. # self.cursor.close();
  57. # self.conn.close();
  58. def getModelByUserAndNum(self,user_list,product_num):
  59. product_num = product_num;
  60. try:
  61. res=[];
  62. self.conn=pool.connection();
  63. self.cursor = self.conn.cursor();
  64. for k in range(len(user_list)-1):
  65. # sql = "select * from user_model where `user_name` = '"+user_list[k]['user_name']+"' and model_state='审核通过'" ;
  66. sql = "select * from user_model where `user_name` = '" + user_list[k][
  67. 'user_name'] +"' and product_num='" + product_num + "' and model_state='已上线'"
  68. count = self.cursor.execute(sql);
  69. model_list=[]
  70. for i in range(count):
  71. data=self.cursor.fetchone()
  72. model_info={}
  73. model_info["model_name"]=data[2]
  74. model_info["product_num"]=data[3]
  75. model_info["model_type"]=data[7]
  76. model_info["other_data"]=data[8]
  77. model_list.append(model_info)
  78. # user_list[k]['model_name']=data[2];
  79. # # product_str=data[3];
  80. # # product_list=product_str.split(";");
  81. # user_list[k]['product_num']=data[3];
  82. user_list[k]["model_list"]=model_list;
  83. except:
  84. logger.error(traceback.print_exc())
  85. logger.error(traceback.format_exc())
  86. self.cursor.close();
  87. self.conn.close();
  88. # return res;
  89. def getFieldByUserAndModel(self,user_list,model_name):
  90. try:
  91. res=[];
  92. self.conn=pool.connection();
  93. self.cursor = self.conn.cursor();
  94. for k in range(len(user_list)-1):
  95. sql = "select * from user_field where `user_name` = '"+user_list[k]['user_name']+ "' and model_name='" +model_name+ "' and field_state ='审核通过'" ;
  96. count = self.cursor.execute(sql);
  97. for i in range(count):
  98. data=self.cursor.fetchone();
  99. user_list[k]['field_data']=data[5];
  100. except:
  101. logger.error(traceback.print_exc())
  102. logger.error(traceback.format_exc())
  103. self.cursor.close();
  104. self.conn.close();
  105. userController=UserController();
  106. userController.getConn();
  107. user_list=userController.getUser();
  108. # print(user_list);
  109. #
  110. # userController.getModelByUserAndNum(user_list,);
  111. # # print(user_list);
  112. #
  113. # userController.getFieldByUser(user_list);
  114. # print(user_list);
  115. # print(res);
  116. # for i in res:
  117. # print(userController.getModelByUser(i));
  118. # for i in res:
  119. # print(userController.getFieldByUser(i));