sql_format.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # -*- coding: utf-8 -*-
  2. from tools.new_mysql import MySQLUploader
  3. from tools.loglog import logger
  4. from core.respone_format import *
  5. class CRUD:
  6. def __init__(self):
  7. self.m = MySQLUploader()
  8. self.people_place_name = []
  9. self.get_people_place_name()
  10. def get_word_by_wordid(self, wordid):
  11. s = "select WordSpelling from dictionary_word where Id = %s"
  12. r = self.m.query_data(s, (wordid,))
  13. if r:
  14. word = r[0][0]
  15. return word
  16. return None
  17. def get_wordid_by_wordspelling(self, wordspelling, auto_insert=False):
  18. """加一个功能。大字典内没有这个单词就自动插入,返回id。auto_insert为真,自动插入大字典,获取其id"""
  19. s = "select Id from dictionary_word where wordspelling = %s"
  20. r = self.m.query_data(s, (wordspelling,))
  21. if r:
  22. wordid = r[0][0]
  23. return wordid
  24. if auto_insert:
  25. s = "insert into dictionary_word (WordSpelling) VALUES (%s);"
  26. self.m.execute_(s, (wordspelling,))
  27. s = "select Id from dictionary_word where wordspelling = %s"
  28. r = self.m.query_data(s, (wordspelling,))
  29. wordid = r[0][0]
  30. return wordid
  31. def get_exchange_prototype(self, wordspelling):
  32. s = "select Word from dictionary_exchange where Word = %s"
  33. r = self.m.query_data(s, (wordspelling,))
  34. if r:
  35. return list({i[0] for i in r})
  36. s2 = "select Word from dictionary_exchange where InflectedWordSpelling = %s"
  37. r2 = self.m.query_data(s2, (wordspelling,))
  38. if r2:
  39. return list({i[0] for i in r2})
  40. def get_word_meaning_by_wordid(self, wordid):
  41. s = "select Id,WordSpelling,WordMeaning from dictionary_meaningitem where WordId = %s"
  42. r = self.m.query_data(s, (wordid,))
  43. return r
  44. def get_people_place_name(self):
  45. s2 = "select word from people_place_name"
  46. r = self.m.query_data(s2)
  47. for i in r:
  48. self.people_place_name.append(i[0])
  49. def get_word_meaning_by_wordspelling(self, wordspelling, frequency):
  50. """根据单词获取其全部词义"""
  51. wordid = self.get_wordid_by_wordspelling(wordspelling)
  52. return_data = {"word_id": wordid, "frequency": frequency, "word": wordspelling,
  53. "meanings": {"default": [], "sun_english": {"name": "春笋英语", "items": []},
  54. "oxford": {"name": "牛津", "items": []}}}
  55. s = "select Id,WordMeaning from dictionary_meaningitem where WordSpelling = %s"
  56. r = self.m.query_data(s, (wordspelling,))
  57. for row_data in r:
  58. return_data["meanings"]["default"].append({"id": row_data[0], "text": row_data[1]})
  59. s2 = "select Id,WordMeaning from dictionary_meaningitem_spring_bamboo where WordSpelling = %s"
  60. r2 = self.m.query_data(s2, (wordspelling,))
  61. for row_data in r2:
  62. return_data["meanings"]["sun_english"]["items"].append({"id": row_data[0], "text": row_data[1]})
  63. s2 = "select Id,WordMeaning from dictionary_meaningitem_oxford where WordSpelling = %s"
  64. r2 = self.m.query_data(s2, (wordspelling,))
  65. for row_data in r2:
  66. return_data["meanings"]["oxford"]["items"].append({"id": row_data[0], "text": row_data[1]})
  67. return return_data
  68. def delete_word_meaning_by_wordmeaningid(self, wordmeaningid):
  69. s = "DELETE FROM dictionary_meaningitem where Id = %s"
  70. r = self.m.execute_(s, (wordmeaningid,))
  71. logger.info(f"根据词义id删除,{wordmeaningid}。结果{r}")
  72. return True if r is True else False
  73. def get_word_all_info(self, word_id, spell, frequency):
  74. def get_associational_words_info(word_meaning_id) -> list:
  75. return_data = []
  76. s = "select Id,BaseWordMeaningId,BaseWord,BaseWordMeaning,AssociationalWord,AssociationalWordMeaningId,AssociationalWordMeaning," \
  77. "AssociationReason,ReverseAssociationReason,CreatedTime,UpdatedTime " \
  78. "from dictionary_associational_word where BaseWordMeaningId = %s"
  79. r = self.m.query_data(s, (word_meaning_id,))
  80. if not r:
  81. return return_data
  82. for single_meaning in r:
  83. associational_id, base_word_meaning_id, base_word, base_word_meaning, associational_word, \
  84. associational_word_meaning_id, associational_word_meaning, association_reason, \
  85. reverse_association_reason, created_time, updated_time = single_meaning
  86. r_data = {"id": associational_id,
  87. "base_word": {"word": base_word, "meaning_id": base_word_meaning_id, "meaning": base_word_meaning},
  88. "associational_word": {"word": associational_word, "meaning_id": associational_word_meaning_id,
  89. "meaning": associational_word_meaning},
  90. "association_reason": association_reason, "reverse_association_reason": reverse_association_reason,
  91. "create_time": created_time.strftime('%Y-%m-%d %H:%M:%S'),
  92. "update_time": updated_time.strftime('%Y-%m-%d %H:%M:%S')}
  93. return_data.append(r_data)
  94. return return_data
  95. def get_phrases_info(word_meaning_id) -> list:
  96. return_data = []
  97. s = "select Id,PhraseSpellingText,PhraseChineseTranslation,FromType,CreatedTime,UpdatedTime " \
  98. "from dictionary_phrase where WordMeaningId = %s"
  99. r = self.m.query_data(s, (word_meaning_id,))
  100. if not r:
  101. return return_data
  102. for single_phrase in r:
  103. phrase_id, phrase_spelling_text, phrase_chinese_translation, from_type, created_time, updated_time = single_phrase
  104. r_data = {"id": phrase_id, "english": phrase_spelling_text, "chinese": phrase_chinese_translation, "from": from_type,
  105. "create_time": created_time.strftime('%Y-%m-%d %H:%M:%S'),
  106. "update_time": updated_time.strftime('%Y-%m-%d %H:%M:%S')}
  107. return_data.append(r_data)
  108. return return_data
  109. def get_exchanges_info(word_id) -> list:
  110. return_data = []
  111. s = "select Id,InflectedWordSpelling,Properties,WordTransformationDescription " \
  112. "from dictionary_exchange where WordId = %s"
  113. r = self.m.query_data(s, (word_id,))
  114. if not r:
  115. return return_data
  116. for single_exchange in r:
  117. exchange_id, spell, properties, description = single_exchange
  118. r_data = {"id": exchange_id, "spell": spell, "properties": properties, "description": description}
  119. return_data.append(r_data)
  120. return return_data
  121. return_data_all = {"word_id": word_id, "spell": spell, "frequency": frequency, "meanings": [], "exchanges": []}
  122. if spell in self.people_place_name:
  123. return_data_all["type"] = "人名地名"
  124. return_data_all["type"] = "一般词汇"
  125. s = "select Id,WordMeaning,OperateAccount from dictionary_meaningitem where WordId = %s"
  126. r = self.m.query_data(s, (word_id,))
  127. if not r:
  128. return resp_200(data=return_data_all)
  129. for single_meaning in r:
  130. meaning_id, word_meaning, operate_account = single_meaning
  131. single_meaning_item = {"id": meaning_id, "text": word_meaning, "editor": operate_account}
  132. associational_words_list = get_associational_words_info(meaning_id)
  133. single_meaning_item["associational_words"] = associational_words_list
  134. phrases_list = get_phrases_info(meaning_id)
  135. single_meaning_item["phrases"] = phrases_list
  136. return_data_all["meanings"].append(single_meaning_item)
  137. exchanges_info_list = get_exchanges_info(word_id)
  138. return_data_all["exchanges"] = exchanges_info_list
  139. return resp_200(data=return_data_all)
  140. def delete_associational_word(self, word_id, associational_id):
  141. s = "select Id from dictionary_meaningitem where WordId = %s"
  142. r = self.m.query_data(s, (word_id,))
  143. if not r:
  144. return resp_400(message="词义表内没有这个单词的词义")
  145. meaning_id = r[0][0]
  146. s = "select BaseWordMeaningId from dictionary_associational_word where Id = %s"
  147. r = self.m.query_data(s, (associational_id,))
  148. if r and r[0][0] == meaning_id:
  149. s = "DELETE FROM dictionary_associational_word where Id = %s"
  150. r = self.m.execute_(s, (associational_id,))
  151. logger.info(f"根据联想词id删除,{associational_id}。结果{r}")
  152. data = True if r is True else False
  153. return resp_200(data=[data]) if data else resp_500(message="数据库内部错误")
  154. else:
  155. logger.info(f"删除联想词时,单词id与联想词id校验失败。{r} {meaning_id}")
  156. return resp_400(message="单词id与联想词id校验失败")
  157. def delete_phrese_word(self, word_id, phrase_id):
  158. s = "select Id from dictionary_meaningitem where WordId = %s"
  159. r = self.m.query_data(s, (word_id,))
  160. if not r:
  161. return resp_400(message="词义表内没有这个单词的词义")
  162. meaning_id = r[0][0]
  163. s = "select WordMeaningId from dictionary_phrase where Id = %s"
  164. r = self.m.query_data(s, (phrase_id,))
  165. if r and r[0][0] == meaning_id:
  166. s = "DELETE FROM dictionary_phrase where Id = %s"
  167. r = self.m.execute_(s, (phrase_id,))
  168. logger.info(f"根据联想词id删除,{phrase_id}。结果{r}")
  169. data = True if r is True else False
  170. return resp_200(data=[data]) if data else resp_500(message="数据库内部错误")
  171. else:
  172. logger.info(f"删除语块时,单词id与语块id校验失败。{r} {meaning_id}")
  173. return resp_400(message="单词id与联想词id校验失败")
  174. def close_connection(self):
  175. """关闭数据库连接"""
  176. self.m.close_connection()
  177. class UserCRUD:
  178. def __init__(self):
  179. self.m = MySQLUploader()
  180. def get_userinfo_by_account(self, account):
  181. s = "select id,account,password,uname,create_time from user where account = %s"
  182. r = self.m.query_data(s, (account,))
  183. if r:
  184. user_info = (r[0][0], r[0][1], r[0][2], r[0][3], r[0][4].strftime('%Y-%m-%d %H:%M:%S'))
  185. return user_info
  186. return None
  187. def close_connection(self):
  188. """关闭数据库连接"""
  189. self.m.close_connection()
  190. if __name__ == '__main__':
  191. crud = CRUD()
  192. r = crud.get_wordid_by_wordspelling("abcdefg")
  193. print(type(r))
  194. print(r)
  195. crud.close_connection()