sql_format.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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": []}, "oxford": {"name": "牛津", "items": []}}}
  54. s = "select Id,WordMeaning from dictionary_meaningitem where WordSpelling = %s"
  55. r = self.m.query_data(s, (wordspelling,))
  56. for row_data in r:
  57. return_data["meanings"]["default"].append({"id": row_data[0], "text": row_data[1]})
  58. s2 = "select Id,WordMeaning from dictionary_meaningitem_spring_bamboo where WordSpelling = %s"
  59. r2 = self.m.query_data(s2, (wordspelling,))
  60. for row_data in r2:
  61. return_data["meanings"]["sun_english"]["items"].append({"id": row_data[0], "text": row_data[1]})
  62. s2 = "select Id,WordMeaning from dictionary_meaningitem_oxford where WordSpelling = %s"
  63. r2 = self.m.query_data(s2, (wordspelling,))
  64. for row_data in r2:
  65. return_data["meanings"]["oxford"]["items"].append({"id": row_data[0], "text": row_data[1]})
  66. return return_data
  67. def delete_word_meaning_by_wordmeaningid(self, wordmeaningid):
  68. s = "DELETE FROM dictionary_meaningitem where Id = %s"
  69. r = self.m.execute_(s, (wordmeaningid,))
  70. logger.info(f"根据词义id删除,{wordmeaningid}。结果{r}")
  71. return True if r is True else False
  72. def get_word_all_info(self,word_id, spell,frequency):
  73. def get_associational_words_info(word_meaning_id) -> list:
  74. return_data = []
  75. s = "select Id,BaseWordMeaningId,BaseWord,BaseWordMeaning,AssociationalWord,AssociationalWordMeaningId,AssociationalWordMeaning," \
  76. "AssociationReason,ReverseAssociationReason,CreatedTime,UpdatedTime " \
  77. "from dictionary_associational_word where BaseWordMeaningId = %s"
  78. r = self.m.query_data(s, (word_meaning_id,))
  79. if not r:
  80. return return_data
  81. for single_meaning in r:
  82. associational_id, base_word_meaning_id, base_word, base_word_meaning, associational_word, \
  83. associational_word_meaning_id, associational_word_meaning, association_reason,\
  84. reverse_association_reason, created_time, updated_time = single_meaning
  85. r_data = {"id":associational_id,"base_word":{"word":base_word,"meaning_id":base_word_meaning_id,"meaning":base_word_meaning},
  86. "associational_word":{"word":associational_word,"meaning_id":associational_word_meaning_id,"meaning":associational_word_meaning},
  87. "association_reason":association_reason,"reverse_association_reason":reverse_association_reason,
  88. "create_time":created_time.strftime('%Y-%m-%d %H:%M:%S'),"update_time":updated_time.strftime('%Y-%m-%d %H:%M:%S')}
  89. return_data.append(r_data)
  90. return return_data
  91. def get_phrases_info(word_meaning_id) -> list:
  92. return_data = []
  93. s = "select Id,PhraseSpellingText,PhraseChineseTranslation,FromType,CreatedTime,UpdatedTime " \
  94. "from dictionary_phrase where WordMeaningId = %s"
  95. r = self.m.query_data(s, (word_meaning_id,))
  96. if not r:
  97. return return_data
  98. for single_phrase in r:
  99. phrase_id, phrase_spelling_text, phrase_chinese_translation, from_type, created_time, updated_time = single_phrase
  100. r_data = {"id":phrase_id,"english":phrase_spelling_text,"chinese":phrase_chinese_translation,"from":from_type,
  101. "create_time":created_time.strftime('%Y-%m-%d %H:%M:%S'),"update_time":updated_time.strftime('%Y-%m-%d %H:%M:%S')}
  102. return_data.append(r_data)
  103. return return_data
  104. def get_exchanges_info(word_id) -> list:
  105. return_data = []
  106. s = "select Id,InflectedWordSpelling,Properties,WordTransformationDescription " \
  107. "from dictionary_exchange where WordId = %s"
  108. r = self.m.query_data(s, (word_id,))
  109. if not r:
  110. return return_data
  111. for single_exchange in r:
  112. exchange_id,spell,properties,description = single_exchange
  113. r_data = {"id": exchange_id, "spell": spell, "properties": properties, "description": description}
  114. return_data.append(r_data)
  115. return return_data
  116. return_data_all = {"word_id":word_id,"spell":spell,"frequency":frequency,"meanings":[],"exchanges":[]}
  117. if spell in self.people_place_name:
  118. return_data_all["type"] = "人名地名"
  119. return_data_all["type"] = "一般词汇"
  120. s = "select Id,WordMeaning,OperateAccount from dictionary_meaningitem where WordId = %s"
  121. r = self.m.query_data(s, (word_id,))
  122. if not r:
  123. return resp_200(data=return_data_all)
  124. for single_meaning in r:
  125. meaning_id, word_meaning, operate_account = single_meaning
  126. single_meaning_item = {"id":meaning_id,"text":word_meaning,"editor":operate_account}
  127. associational_words_list = get_associational_words_info(meaning_id)
  128. single_meaning_item["associational_words"] = associational_words_list
  129. phrases_list = get_phrases_info(meaning_id)
  130. single_meaning_item["phrases"] = phrases_list
  131. return_data_all["meanings"].append(single_meaning_item)
  132. exchanges_info_list = get_exchanges_info(word_id)
  133. return_data_all["exchanges"] = exchanges_info_list
  134. return resp_200(data=return_data_all)
  135. def delete_associational_word(self,word_id,associational_id):
  136. s = "select Id from dictionary_meaningitem where WordId = %s"
  137. r = self.m.query_data(s, (word_id,))
  138. if not r:
  139. return resp_400(message="词义表内没有这个单词的词义")
  140. meaning_id = r[0][0]
  141. s = "select BaseWordMeaningId from dictionary_associational_word where Id = %s"
  142. r = self.m.query_data(s, (associational_id,))
  143. if r and r[0][0]==meaning_id:
  144. s = "DELETE FROM dictionary_associational_word where Id = %s"
  145. r = self.m.execute_(s, (associational_id,))
  146. logger.info(f"根据联想词id删除,{associational_id}。结果{r}")
  147. data = True if r is True else False
  148. return resp_200(data=[data]) if data else resp_500(message="数据库内部错误")
  149. else:
  150. logger.info(f"删除联想词时,单词id与联想词id校验失败。{r} {meaning_id}")
  151. return resp_400(message="单词id与联想词id校验失败")
  152. def delete_phrese_word(self,word_id,phrase_id):
  153. s = "select Id from dictionary_meaningitem where WordId = %s"
  154. r = self.m.query_data(s, (word_id,))
  155. if not r:
  156. return resp_400(message="词义表内没有这个单词的词义")
  157. meaning_id = r[0][0]
  158. s = "select WordMeaningId from dictionary_phrase where Id = %s"
  159. r = self.m.query_data(s, (phrase_id,))
  160. if r and r[0][0] == meaning_id:
  161. s = "DELETE FROM dictionary_phrase where Id = %s"
  162. r = self.m.execute_(s, (phrase_id,))
  163. logger.info(f"根据联想词id删除,{phrase_id}。结果{r}")
  164. data = True if r is True else False
  165. return resp_200(data=[data]) if data else resp_500(message="数据库内部错误")
  166. else:
  167. logger.info(f"删除语块时,单词id与语块id校验失败。{r} {meaning_id}")
  168. return resp_400(message="单词id与联想词id校验失败")
  169. def close_connection(self):
  170. """关闭数据库连接"""
  171. self.m.close_connection()
  172. class UserCRUD:
  173. def __init__(self):
  174. self.m = MySQLUploader()
  175. def get_userinfo_by_account(self, account):
  176. s = "select id,account,password,uname,create_time from user where account = %s"
  177. r = self.m.query_data(s, (account,))
  178. if r:
  179. user_info = (r[0][0], r[0][1], r[0][2], r[0][3], r[0][4].strftime('%Y-%m-%d %H:%M:%S'))
  180. return user_info
  181. return None
  182. def close_connection(self):
  183. """关闭数据库连接"""
  184. self.m.close_connection()
  185. if __name__ == '__main__':
  186. crud = CRUD()
  187. r = crud.get_wordid_by_wordspelling("abcdefg")
  188. print(type(r))
  189. print(r)
  190. crud.close_connection()