dbTools.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. from typing import Any, Dict, Callable
  2. from sqlalchemy import create_engine, Integer, Column, String, Boolean, Float, DateTime, Date
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import sessionmaker
  5. engine = create_engine('mysql+pymysql://yixue_gzh:Lai123@test.db.cxhy.cn/yixue_gzh', pool_pre_ping=True)
  6. Base = declarative_base()
  7. def not_none_of_all(*args: Any):
  8. result = True
  9. for arg in args:
  10. if arg is None:
  11. result = False
  12. break
  13. return result
  14. class DBBase(Base):
  15. __abstract__ = True
  16. def __repr__(self):
  17. result = self.__class__.__name__ + "("
  18. for attr in self.__dict__:
  19. if attr.startswith("_") or attr.startswith("__") or attr == "__tablename__":
  20. continue
  21. result += attr + "=" + str(getattr(self, attr)) + " "
  22. result += ")"
  23. return result
  24. def to_json_object(self, **kwargs: Any):
  25. obj = {}
  26. for attr in self.__dict__:
  27. if attr.startswith("_") or attr.startswith("__") or attr == "__tablename__":
  28. continue
  29. obj[attr] = getattr(self, attr)
  30. for key, value in kwargs.items():
  31. obj[key] = value
  32. return obj
  33. @classmethod
  34. def new_session(cls):
  35. Session = sessionmaker(bind=engine)
  36. session = Session()
  37. return session
  38. @classmethod
  39. def insert(cls, **kwargs: Any):
  40. ss = cls.new_session()
  41. try:
  42. obj = cls()
  43. for key in kwargs.keys():
  44. if hasattr(obj, key):
  45. setattr(obj, key, kwargs[key])
  46. ss.add(obj)
  47. ss.commit()
  48. finally:
  49. ss.close()
  50. @classmethod
  51. def insert_all(cls, items: []):
  52. ss = cls.new_session()
  53. try:
  54. lst = []
  55. for item in items:
  56. obj = cls()
  57. for key in item.keys():
  58. if hasattr(obj, key):
  59. setattr(obj, key, item[key])
  60. lst.append(obj)
  61. ss.add_all(lst)
  62. ss.commit()
  63. finally:
  64. ss.close()
  65. @classmethod
  66. def delete_by(cls, **kwargs: Any):
  67. ss = cls.new_session()
  68. try:
  69. data = ss.query(cls).filter_by(**kwargs).all()
  70. for dt in data:
  71. ss.delete(dt)
  72. ss.commit()
  73. finally:
  74. ss.close()
  75. @classmethod
  76. def query_by(cls, **kwargs: Any):
  77. ss = cls.new_session()
  78. try:
  79. data = ss.query(cls).filter_by(**kwargs).all()
  80. return data
  81. finally:
  82. ss.close()
  83. @classmethod
  84. def query_first_by(cls, **kwargs: Any):
  85. ss = cls.new_session()
  86. try:
  87. data = ss.query(cls).filter_by(**kwargs).first()
  88. return data
  89. finally:
  90. ss.close()
  91. @classmethod
  92. def check_exist(cls, **kwargs: Any):
  93. return cls.query_first_by(**kwargs) is not None
  94. @classmethod
  95. def update(cls, conditions: Callable, values: Dict[str, Any]):
  96. # DBSysUser.update(conditions=(DBSysUser.id == user.id), values={"finger": finger})
  97. session = cls.new_session()
  98. try:
  99. session.query(cls).filter(conditions).update(values)
  100. session.commit()
  101. finally:
  102. session.close()
  103. class DB_WX_GZH_User(DBBase):
  104. __tablename__ = 'wx_gzh_user'
  105. id = Column(Integer, primary_key=True)
  106. open_id = Column(String)
  107. user_name = Column(String)
  108. is_man = Column(Boolean)
  109. date_mode = Column(Integer)
  110. born_year = Column(Integer)
  111. born_month = Column(Integer)
  112. born_day = Column(Integer)
  113. born_hour = Column(Integer)
  114. born_minute = Column(Integer)
  115. know_time = Column(Integer)
  116. born_sheng = Column(String)
  117. born_shi = Column(String)
  118. born_qu = Column(String)
  119. nian_gan = Column(String)
  120. nian_zhi = Column(String)
  121. yue_gan = Column(String)
  122. yue_zhi = Column(String)
  123. ri_gan = Column(String)
  124. ri_zhi = Column(String)
  125. shi_gan = Column(String)
  126. shi_zhi = Column(String)
  127. nian_cang1 = Column(String)
  128. nian_cang2 = Column(String)
  129. nian_cang3 = Column(String)
  130. yue_cang1 = Column(String)
  131. yue_cang2 = Column(String)
  132. yue_cang3 = Column(String)
  133. ri_cang1 = Column(String)
  134. ri_cang2 = Column(String)
  135. ri_cang3 = Column(String)
  136. shi_cang1 = Column(String)
  137. shi_cang2 = Column(String)
  138. shi_cang3 = Column(String)
  139. def is_know_time(self):
  140. return self.know_time == 0
  141. def has_sizhu(self):
  142. return not_none_of_all(
  143. self.nian_gan, self.nian_zhi,
  144. self.yue_gan, self.yue_zhi,
  145. self.ri_gan, self.ri_zhi,
  146. self.shi_gan, self.shi_zhi)
  147. def get_sizhu_text(self):
  148. return self.nian_gan + self.nian_zhi + " " + self.yue_gan + self.yue_zhi + " " + self.ri_gan + self.ri_zhi + " " + self.shi_gan + self.shi_zhi
  149. class DBWanNianLi(DBBase):
  150. __tablename__ = 'wan_nian_li'
  151. id = Column(Integer, primary_key=True)
  152. nian = Column(Integer)
  153. yue = Column(Integer)
  154. ri = Column(Integer)
  155. nian_gan = Column(String)
  156. nian_zhi = Column(String)
  157. yue_gan = Column(String)
  158. yue_zhi = Column(String)
  159. ri_gan = Column(String)
  160. ri_zhi = Column(String)
  161. class DBJieQi(DBBase):
  162. __tablename__ = 'jie_qi'
  163. id = Column(Integer, primary_key=True)
  164. nian = Column(Integer)
  165. chuxi = Column(String)
  166. lichun = Column(String)
  167. jingzhe = Column(String)
  168. qingming = Column(String)
  169. lixia = Column(String)
  170. mangzhong = Column(String)
  171. xiaoshu = Column(String)
  172. liqiu = Column(String)
  173. bailu = Column(String)
  174. hanlu = Column(String)
  175. lidong = Column(String)
  176. daxue = Column(String)
  177. xiaohan = Column(String)
  178. yiyue = Column(String)
  179. yiri = Column(String)
  180. class DBTaiYangShi(DBBase):
  181. __tablename__ = 'tai_yang_shi'
  182. id = Column(Integer, primary_key=True)
  183. month = Column(Integer)
  184. day = Column(Integer)
  185. diff = Column(Integer)
  186. class DBAreaInfo(DBBase):
  187. __tablename__ = 'area_info'
  188. id = Column(Integer, primary_key=True)
  189. sheng = Column(String)
  190. shi = Column(String)
  191. qu = Column(String)
  192. weidu = Column(Float)
  193. jingdu = Column(Float)
  194. hour = Column(Integer)
  195. minute = Column(Integer)
  196. second = Column(Integer)
  197. shi_pinyin = Column(String)
  198. qu_pinyin = Column(String)
  199. class DBDaYunNan(DBBase):
  200. __tablename__ = 'dayun_nan'
  201. id = Column(Integer, primary_key=True)
  202. time = Column(DateTime)
  203. year = Column(Integer)
  204. month = Column(Integer)
  205. day = Column(Integer)
  206. hour = Column(Integer)
  207. sign = Column(String)
  208. class DBDaYunNv(DBBase):
  209. __tablename__ = 'dayun_nv'
  210. id = Column(Integer, primary_key=True)
  211. time = Column(DateTime)
  212. year = Column(Integer)
  213. month = Column(Integer)
  214. day = Column(Integer)
  215. hour = Column(Integer)
  216. sign = Column(String)
  217. class DBNongLi(DBBase):
  218. __tablename__ = 'nong_li'
  219. id = Column(Integer, primary_key=True)
  220. new_date = Column(Date)
  221. month = Column(String)
  222. day = Column(String)
  223. year = Column(Integer)
  224. class DBYiXueUser(DBBase):
  225. __tablename__ = 'yixue_user'
  226. id = Column(Integer, primary_key=True)
  227. name = Column(String)
  228. isMan = Column(Boolean)
  229. dateMode = Column(Integer)
  230. year = Column(Integer)
  231. month = Column(Integer)
  232. day = Column(Integer)
  233. hourMode = Column(Integer)
  234. hour = Column(Integer)
  235. minute = Column(Integer)
  236. areaMode = Column(Integer)
  237. sheng = Column(String)
  238. shi = Column(String)
  239. qu = Column(String)
  240. extra = Column(String)
  241. nongli_year = Column(Integer)
  242. nongli_month = Column(String)
  243. nongli_day = Column(String)
  244. req_nian_gan = Column(String)
  245. req_nian_zhi = Column(String)
  246. req_yue_gan = Column(String)
  247. req_yue_zhi = Column(String)
  248. req_ri_gan = Column(String)
  249. req_ri_zhi = Column(String)
  250. req_shi_gan = Column(String)
  251. req_shi_zhi = Column(String)
  252. rsp_nian_gan = Column(String)
  253. rsp_nian_zhi = Column(String)
  254. rsp_yue_gan = Column(String)
  255. rsp_yue_zhi = Column(String)
  256. rsp_ri_gan = Column(String)
  257. rsp_ri_zhi = Column(String)
  258. rsp_shi_gan = Column(String)
  259. rsp_shi_zhi = Column(String)
  260. owner_id = Column(Integer)
  261. class DBSysUser(DBBase):
  262. __tablename__ = 'sys_user'
  263. id = Column(Integer, primary_key=True)
  264. name = Column(String)
  265. account = Column(String)
  266. password = Column(String)
  267. finger = Column(String)
  268. dev_change = Column(Integer)
  269. allow_change_dev = Column(Boolean)