mysqlDataBase.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. import re
  2. import pymysql
  3. import time
  4. import LogOut
  5. import traceback
  6. from datetime import datetime, timedelta
  7. """
  8. connect连接对象的方法:
  9. close() --关闭的方法
  10. commit() --如果支持事务则提交挂起的事务
  11. rollback() --回滚挂起的事务
  12. cursor() --返回连接的游标对象
  13. 游标对象的方法:
  14. callproc(name,[params]) --用来执行存储过程,接收的参数为存储过程的名字和参数列表,返回受影响的行数
  15. close() --关闭游标
  16. execute(sql,[params])--执行sql语句,可以使用参数,(使用参数时,sql语句中用%s进行站位注值),返回受影响的行数
  17. executemany(sql,params)--执行单挑sql语句,但是重复执行参数列表里的参数,返回受影响的行数
  18. fetchone() --返回结果的下一行
  19. fetchall() --返回结果的 所有行
  20. fetchmany(size)--返回size条记录,如果size大于返回结果行的数量,则会返回cursor.arraysize条记录
  21. nextset() --条至下一行
  22. setinputsizes(size)--定义cursor
  23. 游标对象的属性:
  24. description--结果列的描述,只读
  25. rowcount --结果中的行数,只读
  26. arraysize --fetchmany返回的行数,默认为1
  27. """
  28. class MysqldbOperational(object):
  29. """
  30. 操作mysql数据库,基本方法
  31. """
  32. # logger = LogOut.Log("mysql")
  33. def __init__(self, host="localhost", username="root", password="", port=3306, database="", logger=None,
  34. charset='utf-8'):
  35. self.host = host
  36. self.username = username
  37. self.password = password
  38. self.database = database
  39. self.port = port
  40. self.charset = charset
  41. self.logger = logger
  42. self.con = None
  43. self.cur = None
  44. self._conn()
  45. def _conn(self):
  46. try:
  47. self.con = pymysql.connect(host=self.host, user=self.username, passwd=self.password, port=self.port,
  48. db=self.database, autocommit=True)
  49. # 所有的查询,都在连接 con 的一个模块 cursor 上面运行的
  50. self.cur = self.con.cursor()
  51. except Exception as e:
  52. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  53. # traceback.format_exc(limit=1)))
  54. self.logger.error('in function _conn.' + repr(e))
  55. def close(self):
  56. """
  57. 关闭数据库连接
  58. """
  59. try:
  60. self.con.close()
  61. self.logger.info("Database connection close.")
  62. except Exception as e:
  63. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  64. # traceback.format_exc(limit=1)))
  65. self.logger.error('in function close.' + repr(e))
  66. def _reConn(self, num=28800, stime=3): # 重试连接总次数为1天,这里根据实际情况自己设置,如果服务器宕机1天都没发现就......
  67. _number = 0
  68. _status = True
  69. while _status and _number <= num:
  70. try:
  71. self.con.ping() # cping 校验连接是否异常
  72. _status = False
  73. except Exception as e:
  74. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  75. # traceback.format_exc(limit=1)))
  76. self.logger.error('in function _reConn.' + repr(e))
  77. if self._conn(): # 重新连接,成功退出
  78. _status = False
  79. break
  80. _number += 1
  81. time.sleep(stime) # 连接不成功,休眠3秒钟,继续循环,知道成功或重试次数结束
  82. # 抽帧相关函数
  83. def getOneData(self):
  84. # 取得上个查询的结果,是单个结果
  85. data = self.cur.fetchone()
  86. return data
  87. def creatTable(self, tablename, attrdict, constraint):
  88. """创建数据库表
  89. args:
  90. tablename :表名字
  91. attrdict :属性键值对,{'book_name':'varchar(200) NOT NULL'...}DEFAULT NULL NOT NULL
  92. constraint :主外键约束,PRIMARY KEY(`id`) 自动增量
  93. """
  94. self._reConn()
  95. if self.isExistTable(tablename):
  96. return
  97. sql = ''
  98. sql_mid = '`id` bigint(11) NOT NULL AUTO_INCREMENT,'
  99. for attr, value in attrdict.items():
  100. sql_mid = sql_mid + '`' + attr + '`' + ' ' + value + ','
  101. sql = sql + 'CREATE TABLE IF NOT EXISTS %s (' % tablename
  102. sql = sql + sql_mid
  103. sql = sql + constraint
  104. sql = sql + ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  105. print('creatTable:' + sql)
  106. self.executeCommit(sql)
  107. def executeSql(self, sql=''):
  108. """执行sql语句,针对读操作返回结果集
  109. args:
  110. sql :sql语句
  111. """
  112. try:
  113. self.cur.execute(sql)
  114. records = self.cur.fetchall()
  115. return records
  116. except pymysql.Error as e:
  117. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  118. # traceback.format_exc(limit=1)))
  119. self.logger.error('in function executeSql.' + repr(e))
  120. def executeCommit(self, sql=''):
  121. """执行数据库sql语句,针对更新,删除,事务等操作失败时回滚
  122. """
  123. try:
  124. self.cur.execute(sql)
  125. self.con.commit()
  126. return True
  127. except pymysql.Error as e:
  128. self.con.rollback()
  129. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  130. # traceback.format_exc(limit=1)))
  131. self.logger.error('in function executeCommit.' + repr(e))
  132. return False
  133. def insert(self, tablename, params):
  134. """插入数据库
  135. args:
  136. tablename :表名字
  137. key :属性键
  138. value :属性值
  139. """
  140. self._reConn()
  141. key = []
  142. value = []
  143. for tmpkey, tmpvalue in params.items():
  144. key.append(tmpkey)
  145. if isinstance(tmpvalue, str):
  146. value.append("\'" + tmpvalue + "\'")
  147. else:
  148. value.append(tmpvalue)
  149. attrs_sql = '(' + ','.join(key) + ')'
  150. values_sql = ' values(' + ','.join(value) + ')'
  151. sql = 'insert into %s' % tablename
  152. sql = sql + attrs_sql + values_sql
  153. print('_insert:' + sql)
  154. self.executeCommit(sql)
  155. def select(self, tablename, cond_dict='', order='', fields='*'):
  156. """查询数据
  157. args:
  158. tablename :表名字
  159. cond_dict :查询条件
  160. order :排序条件
  161. example:
  162. print mydb.select(table)
  163. print mydb.select(table, fields=["name"])
  164. print mydb.select(table, fields=["name", "age"])
  165. print mydb.select(table, fields=["age", "name"])
  166. """
  167. self._reConn()
  168. consql = ' '
  169. if cond_dict != '':
  170. for k, v in cond_dict.items():
  171. consql = consql + "`" + tablename + "`" + '.' + "`" + k + "`" + '=' + "'" + v + "'" + ' and'
  172. consql = consql + ' 1=1 '
  173. if fields == "*":
  174. sql = 'select * from %s where' % tablename
  175. else:
  176. if isinstance(fields, list):
  177. fields = ",".join(fields)
  178. sql = 'select %s from %s where' % (fields, tablename)
  179. else:
  180. self.logger.error("fields input error, please input list fields.")
  181. raise Exception("fields input error, please input list fields.")
  182. sql = sql + consql + order
  183. #print('select:' + sql)
  184. return self.executeSql(sql)
  185. def select_last_one(self, tablename, fields='*'):
  186. """倒叙查询
  187. args:
  188. tablename :表名字
  189. fields :检索的名称
  190. """
  191. sql = 'select %s from %s order by time desc limit 1 ' % (fields, tablename)
  192. #print('select:' + sql)
  193. if fields == "*":
  194. return self.executeSql(sql)
  195. else:
  196. return self.executeSql(sql)[0][0]
  197. def insertMany(self, table, attrs, values):
  198. """插入多条数据
  199. args:
  200. tablename :表名字
  201. attrs :属性键
  202. values :属性值
  203. example:
  204. table='test_mysqldb'
  205. key = ["id" ,"name", "age"]
  206. value = [[101, "liuqiao", "25"], [102,"liuqiao1", "26"], [103 ,"liuqiao2", "27"], [104 ,"liuqiao3", "28"]]
  207. mydb.insertMany(table, key, value)
  208. """
  209. self._reConn()
  210. values_sql = ['%s' for v in attrs]
  211. attrs_sql = '(' + ','.join(attrs) + ')'
  212. values_sql = ' values(' + ','.join(values_sql) + ')'
  213. sql = 'insert into %s' % table
  214. sql = sql + attrs_sql + values_sql
  215. print('insertMany:' + sql)
  216. try:
  217. # print(sql)
  218. for i in range(0, len(values), 20000):
  219. self.cur.executemany(sql, values[i:i + 20000])
  220. self.con.commit()
  221. except pymysql.Error as e:
  222. self.con.rollback()
  223. self.logger.error('in function insertMany.' + repr(e))
  224. def insert_sql(self, table_name, keys, values):
  225. """
  226. 增加数据
  227. :param table_name: (字符串)表名
  228. :param keys: (字符串) 字段名, 如 “(device_name, code)”
  229. :param values: (元组)每个字段的值, 如 ('sm140', '006001001WAVE')
  230. :return: True / False
  231. """
  232. for each in values:
  233. in_sql = "INSERT INTO %s %s VALUES %s;" % (table_name, keys, each)
  234. try:
  235. self._reConn()
  236. self.cur.execute(in_sql)
  237. self.con.commit()
  238. except pymysql.Error as e:
  239. self.con.rollback()
  240. self.logger.error('in function insert_sql.' + repr(e))
  241. def delete(self, tablename, cond_dict):
  242. """删除数据
  243. args:
  244. tablename :表名字
  245. cond_dict :删除条件字典
  246. example:
  247. params = {"name" : "caixinglong", "age" : "38"}
  248. mydb.delete(table, params)
  249. """
  250. self._reConn()
  251. consql = ' '
  252. if cond_dict != '':
  253. for k, v in cond_dict.items():
  254. if isinstance(v, str):
  255. v = "\'" + v + "\'"
  256. consql = consql + tablename + "." + k + '=' + v + ' and '
  257. consql = consql + ' 1=1 '
  258. sql = "DELETE FROM %s where%s" % (tablename, consql)
  259. print(sql)
  260. return self.executeCommit(sql)
  261. def update(self, tablename, attrs_dict, cond_dict):
  262. """更新数据
  263. args:
  264. tablename :表名字
  265. attrs_dict :更改属性键值对字典
  266. cond_dict :更新条件字典
  267. example:
  268. params = {"name" : "caixinglong", "age" : "38"}
  269. cond_dict = {"name" : "liuqiao", "age" : "18"}
  270. mydb.update(table, params, cond_dict)
  271. """
  272. self._reConn()
  273. attrs_list = []
  274. consql = ' '
  275. for y in attrs_dict:
  276. if not isinstance(attrs_dict[y], str):
  277. attrs_dict[y] = str(attrs_dict[y])
  278. for x in cond_dict:
  279. if not isinstance(cond_dict[x], str):
  280. cond_dict[x] = str(cond_dict[x])
  281. for tmpkey, tmpvalue in attrs_dict.items():
  282. attrs_list.append("`" + tmpkey + "`" + "=" + "\'" + tmpvalue + "\'")
  283. attrs_sql = ",".join(attrs_list)
  284. #print("attrs_sql:", attrs_sql)
  285. if cond_dict != '':
  286. for k, v in cond_dict.items():
  287. if isinstance(v, str):
  288. v = "\'" + v + "\'"
  289. consql = consql + "`" + tablename + "`." + "`" + k + "`" + '=' + v + ' and '
  290. consql = consql + ' 1=1 '
  291. sql = "UPDATE %s SET %s where%s" % (tablename, attrs_sql, consql)
  292. #print(sql)
  293. return self.executeCommit(sql)
  294. def dropTable(self, tablename):
  295. """删除数据库表
  296. args:
  297. tablename :表名字
  298. """
  299. sql = "DROP TABLE %s" % tablename
  300. self.executeCommit(sql)
  301. def deleteTable(self, tablename):
  302. """清空数据库表
  303. args:
  304. tablename :表名字
  305. """
  306. sql = "DELETE FROM %s" % tablename
  307. self.executeCommit(sql)
  308. def isExistTable(self, tablename):
  309. """判断数据表是否存在
  310. args:
  311. tablename :表名字
  312. Return:
  313. 存在返回True,不存在返回False
  314. """
  315. sql = "select * from %s" % tablename
  316. result = self.executeCommit(sql)
  317. if result is None:
  318. return True
  319. else:
  320. if re.search("doesn't exist", result):
  321. return False
  322. else:
  323. return True
  324. # 数据上传相关函数
  325. def get_devices_name(self):
  326. list = []
  327. sql = "SELECT DISTINCT device_name FROM data_point_tbl WHERE mqtt_code != 'NULL'"
  328. try:
  329. self._reConn()
  330. self.cursor = self.con.cursor()
  331. self.cursor.execute(sql)
  332. results = self.cursor.fetchall()
  333. for row in results:
  334. if row[0] != None:
  335. list.append(row[0])
  336. self.cursor.close()
  337. return list
  338. except Exception as e:
  339. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  340. # traceback.format_exc(limit=1)))
  341. self.logger.error('in function get_devices_name.' + repr(e))
  342. return False
  343. def get_mqtt_devices_count(self):
  344. list = 0
  345. sql = "SELECT COUNT(DISTINCT device_code) FROM data_point_tbl"
  346. try:
  347. self._reConn()
  348. self.cursor = self.con.cursor()
  349. self.cursor.execute(sql)
  350. results = self.cursor.fetchall()
  351. for row in results:
  352. if row[0] != None:
  353. list = row[0]
  354. self.cursor.close()
  355. return list
  356. except Exception as e:
  357. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  358. # traceback.format_exc(limit=1)))
  359. self.logger.error('in function get_mqtt_devices_count.' + repr(e))
  360. return False
  361. def get_mqtt_devices_from_name(self, device_name):
  362. list = []
  363. sql = "SELECT DISTINCT device_code FROM data_point_tbl WHERE device_name = '%s'" % (device_name)
  364. try:
  365. self._reConn()
  366. self.cursor = self.con.cursor()
  367. self.cursor.execute(sql)
  368. results = self.cursor.fetchall()
  369. for row in results:
  370. if row[0] != None:
  371. list.append(row[0])
  372. self.cursor.close()
  373. return list
  374. except Exception as e:
  375. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  376. # traceback.format_exc(limit=1)))
  377. self.logger.error('in function get_mqtt_devices_from_name.' + repr(e))
  378. return False
  379. def get_mqtt_point(self, deviceCode):
  380. list = []
  381. dict = {}
  382. sql = "SELECT device_name,serial_number,storage_type,mqtt_code,low_limit,up_limit FROM data_point_tbl where device_code = '%s'" % (
  383. deviceCode)
  384. try:
  385. self._reConn()
  386. self.cursor = self.con.cursor()
  387. self.cursor.execute(sql)
  388. results = self.cursor.fetchall()
  389. for row in results:
  390. dict = {}
  391. dict['deviceName'] = row[0]
  392. dict['serialNumber'] = row[1]
  393. dict['storageType'] = row[2]
  394. dict['mqttCode'] = row[3]
  395. dict['lowLimit'] = row[4]
  396. dict['upLimit'] = row[5]
  397. list.append(dict)
  398. self.cursor.close()
  399. return list
  400. except Exception as e:
  401. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  402. # traceback.format_exc(limit=1)))
  403. self.logger.error('in function get_mqtt_point.' + repr(e))
  404. return False
  405. def get_breakpoint_last_time_datetime(self, tableName):
  406. sql = "SELECT times FROM `%s` WHERE is_send = 0 AND times < date_sub(now(), interval 1 hour) ORDER BY id DESC LIMIT 1;" % (
  407. tableName)
  408. try:
  409. self._reConn()
  410. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  411. self.cursor.execute(sql)
  412. results = self.cursor.fetchall()
  413. self.cursor.close()
  414. if len(results) > 0:
  415. return results[0]
  416. else:
  417. #self.logger.debug(results)
  418. return None
  419. except Exception as e:
  420. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  421. # traceback.format_exc(limit=1)))
  422. self.logger.error('in function get_breakpoint_last_time_datetime.' + repr(e))
  423. return None
  424. def get_breakpoint_last_time_date_and_time(self, tableName):
  425. sql = "SELECT Date,Time FROM `%s` WHERE is_send = 0 ORDER BY id LIMIT 1;" % (tableName)
  426. dict = {}
  427. try:
  428. self._reConn()
  429. self.cursor = self.con.cursor()
  430. self.cursor.execute(sql)
  431. results = self.cursor.fetchall()
  432. self.cursor.close()
  433. if len(results) > 0:
  434. dict['times'] = str(results[0][0]) + " " + str(results[0][1])
  435. return dict
  436. else:
  437. return None
  438. except Exception as e:
  439. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  440. # traceback.format_exc(limit=1)))
  441. self.logger.error('in function get_breakpoint_last_time_date_and_time.' + repr(e))
  442. return None
  443. def get_hour_data_datetime(self, tableName, begin, end):
  444. sql = "SELECT * FROM `%s` WHERE times >= '%s' And times <= '%s' ORDER BY id ASC;" % (tableName, begin, end)
  445. try:
  446. self._reConn()
  447. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  448. self.cursor.execute(sql)
  449. results = self.cursor.fetchall()
  450. self.cursor.close()
  451. if len(results) != 0:
  452. return results
  453. else:
  454. return None
  455. except Exception as e:
  456. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  457. # traceback.format_exc(limit=1)))
  458. self.logger.error('in function get_hour_data_datetime.' + repr(e))
  459. return None
  460. def get_hour_data_date_and_time(self, tableName, begin, end):
  461. sql = "SELECT * FROM `%s` WHERE CONCAT(Date,' ', Time) >= '%s' And CONCAT(Date,' ', Time) <= '%s' ORDER BY id ASC;" % (
  462. tableName, begin, end)
  463. try:
  464. self._reConn()
  465. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  466. self.cursor.execute(sql)
  467. results = self.cursor.fetchall()
  468. self.cursor.close()
  469. if len(results) != 0:
  470. return results
  471. else:
  472. return None
  473. except Exception as e:
  474. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  475. # traceback.format_exc(limit=1)))
  476. self.logger.error('in function get_hour_data_date_and_time.' + repr(e))
  477. return None
  478. def set_many_send_status(self, tableName, id_list):
  479. if len(id_list) >= 1:
  480. id = id_list[0]['id']
  481. value = 1
  482. sql = "UPDATE `%s` SET is_send = %s WHERE id = %s" % (tableName, value, id)
  483. for index in range(len(id_list)):
  484. sql = sql + " OR id = %s" % (id_list[index]['id'])
  485. try:
  486. self._reConn()
  487. self.cursor = self.con.cursor()
  488. self.cursor.execute(sql)
  489. # 提交到数据库执行
  490. self.con.commit()
  491. self.cursor.close()
  492. except Exception as e:
  493. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  494. # traceback.format_exc(limit=1)))
  495. self.logger.error('in function set_many_send_status.' + repr(e))
  496. def get_mqtt_devices(self):
  497. list = []
  498. sql = "SELECT DISTINCT device_code FROM data_point_tbl"
  499. try:
  500. self._reConn()
  501. self.cursor = self.con.cursor()
  502. self.cursor.execute(sql)
  503. results = self.cursor.fetchall()
  504. for row in results:
  505. if row[0] != None:
  506. list.append(row[0])
  507. self.cursor.close()
  508. return list
  509. except Exception as e:
  510. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  511. # traceback.format_exc(limit=1)))
  512. self.logger.error('in function get_mqtt_devices.' + repr(e))
  513. return False
  514. def get_newest_data(self, tableName):
  515. sql = "SELECT * FROM `%s` ORDER BY id DESC LIMIT 1;" % (tableName)
  516. results = []
  517. try:
  518. self._reConn()
  519. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  520. self.cursor.execute(sql)
  521. results = self.cursor.fetchall()
  522. self.cursor.close()
  523. if len(results) > 0:
  524. #print(results[0])
  525. return results[0]
  526. else:
  527. self.logger.debug(results)
  528. return results
  529. except Exception as e:
  530. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  531. # traceback.format_exc(limit=1)))
  532. self.logger.error('in function get_newest_data.' + repr(e))
  533. return None
  534. def set_send_status(self, tableName, id, value):
  535. sql = "UPDATE `%s` SET is_send = %s WHERE id = %s;" % (tableName, value, id)
  536. try:
  537. self._reConn()
  538. self.cursor = self.con.cursor()
  539. self.cursor.execute(sql)
  540. # 提交到数据库执行
  541. self.con.commit()
  542. self.cursor.close()
  543. except Exception as e:
  544. # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "[ERROR] {0}\n".format(
  545. # traceback.format_exc(limit=1)))
  546. self.logger.error('in function set_send_status.' + repr(e))
  547. def binocular_data(self):
  548. """
  549. 生物监控子系统:sql1:将值为null的数据状态置为1, sql2:查询双目未上传的数据
  550. :return:[{'datetime': datetime.date(2021, 6, 10), 'fish_species': '许氏平鲉', 'size_average': 288.0, 'size_median': 286.6, 'weight_average': 458.3, 'weight_median': 432.6}]
  551. """
  552. yes_time = datetime.now().strftime("%Y-%m-%d")
  553. sql1 = "UPDATE binocular_dailyreport SET is_send = 1 WHERE size_average IS NULL AND datetime<\'%s\';" % (yes_time)
  554. sql2 = "SELECT datetime,fish_species,size_average,size_median,weight_average,weight_median FROM binocular_dailyreport WHERE datetime<\'%s\' AND is_send=0 AND size_average IS NOT NULL;" % (yes_time)
  555. try:
  556. self._reConn()
  557. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  558. self.cursor.execute(sql1)
  559. self.con.commit()
  560. self.cursor.execute(sql2)
  561. results = self.cursor.fetchall()
  562. self.cursor.close()
  563. return results
  564. except Exception as e:
  565. self.logger.error('in function get_newest_data.' + repr(e))
  566. return None
  567. def sonar_data_dailyReport(self):
  568. """
  569. 生物监控子系统:查询声呐未上传的数据,每日的总数
  570. :return: [{'date': '2021-07-06', 'assess_total_number': 699718}, {'date': '2021-07-07', 'assess_total_number': 579352}]
  571. """
  572. td_time = datetime.now().strftime("%Y-%m-%d")
  573. sql = "SELECT date_format(datetime,'%%Y-%%m-%%d') AS date, assess_total_number FROM fish_distribution_dailyReport WHERE datetime<\'%s\' AND is_send=0 AND assess_total_number IS NOT NULL;" % (td_time)
  574. try:
  575. self._reConn()
  576. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  577. self.cursor.execute(sql)
  578. results = self.cursor.fetchall()
  579. self.cursor.close()
  580. return results
  581. except Exception as e:
  582. self.logger.error('in function get_newest_data.' + repr(e))
  583. return None
  584. def sonar_data_avg(self):
  585. """
  586. 生物监控子系统:查询声呐未上传的数据,上中下三层每日的平均值
  587. :return: [{'date': '2022-08-02', 'assess_top_number': 6855, 'assess_middle_number': 91480, 'assess_bottom_number': 251176}]
  588. """
  589. td_time = datetime.now().strftime("%Y-%m-%d")
  590. sql = "SELECT date_format(datetime,'%%Y-%%m-%%d') AS date, " \
  591. "CAST(AVG(assess_top_number) AS signed) AS assess_top_number," \
  592. "CAST(AVG(assess_middle_number) AS signed) AS assess_middle_number," \
  593. "CAST(AVG(assess_bottom_number) AS signed) AS assess_bottom_number " \
  594. "FROM fish_distribution_data WHERE datetime<\'%s\' AND is_send=0 " \
  595. "GROUP BY date_format(datetime,'%%Y-%%m-%%d');" % (td_time)
  596. try:
  597. self._reConn()
  598. self.cursor = self.con.cursor(cursor=pymysql.cursors.DictCursor)
  599. self.cursor.execute(sql)
  600. results = self.cursor.fetchall()
  601. self.cursor.close()
  602. return results
  603. except Exception as e:
  604. self.logger.error('in function get_newest_data.' + repr(e))
  605. return None
  606. def binocular_send_status(self, dates):
  607. """
  608. 生物监控子系统:将上传成功的双目数据状态置1
  609. :param dates:
  610. :return:
  611. """
  612. sql = "UPDATE binocular_dailyreport SET is_send = 1 WHERE datetime in %s;"
  613. try:
  614. self._reConn()
  615. self.cursor = self.con.cursor()
  616. self.cursor.execute(sql, (dates,))
  617. self.con.commit()
  618. self.cursor.close()
  619. except Exception as e:
  620. self.logger.error('in function set_send_status.' + repr(e))
  621. def sonar_send_status(self, dates):
  622. """
  623. 生物监控子系统:将上传成功的声呐数据状态置1
  624. :param dates:
  625. :return:
  626. """
  627. sql1 = "UPDATE fish_distribution_dailyReport SET is_send = 1 WHERE date_format(datetime,'%%Y-%%m-%%d') in %s;"
  628. sql2 = "UPDATE fish_distribution_data SET is_send = 1 WHERE date_format(datetime,'%%Y-%%m-%%d') in %s;"
  629. try:
  630. self._reConn()
  631. self.cursor = self.con.cursor()
  632. self.cursor.execute(sql1, (dates,))
  633. self.cursor.execute(sql2, (dates,))
  634. self.con.commit()
  635. self.cursor.close()
  636. except Exception as e:
  637. self.logger.error('in function set_send_status.' + repr(e))
  638. def commit_sql(self, sql, params):
  639. """
  640. 执行 MySQL 的删除、插入、更新语句,如
  641. :param table_name: (字符串)表名
  642. :param cond_dict: (字典)条件
  643. :return:
  644. """
  645. try:
  646. self._reConn()
  647. self.cursor = self.con.cursor()
  648. self.cursor.execute(sql, (params,))
  649. self.con.commit()
  650. self.cursor.close()
  651. result = True
  652. except pymysql.Error as err:
  653. self.logger.error("in function delete_sql. " + repr(err))
  654. result = False
  655. return result