• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

关闭

返回栏目

关闭

返回python栏目

120 - DAO层 - 数据库 - MySQL

作者:

贺及楼

成为作者

更新日期:2023-12-08 15:00:34

DAO层

作用

这里主要是处理和数据库连接的异常

  1. import inspect
  2. from ..MySQL import logger
  3. from ...models import schedule
  4. from django.db.models import Sum
  5. from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
  6. class schedule_M():
  7. def __init__(self):
  8. self.class_name = "schedule_M()"
  9. def to_json(self, data, info="转成json"):
  10. try:
  11. return_data = {
  12. "id":data.id,
  13. "name":data.name,
  14. "content":data.content,
  15. "date":str(data.date)[0:10]
  16. }
  17. return return_data
  18. except Exception as e:
  19. logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))
  20. raise
  21. # 分页查日程
  22. def select_schedule(self, page, page_size, info):
  23. try:
  24. infoList=schedule.objects.filter(is_deleted = 0).order_by('-date')
  25. paginator=Paginator(infoList,page_size)
  26. try:
  27. pageInfo=paginator.page(page)
  28. except PageNotAnInteger:
  29. pageInfo=paginator.page(1)
  30. except EmptyPage:
  31. pageInfo=paginator.page(paginator.num_pages)
  32. else:
  33. return_data = list(map(self.to_json, pageInfo))
  34. return_data = {
  35. "data":return_data,
  36. "page":page,
  37. "num_pages":paginator.num_pages,
  38. "count":paginator.count
  39. }
  40. return return_data
  41. # data = [{'user':'xxx1','content':'xxxxxxxxx','date':'2023-11-03 00:00:00'},
  42. # {'user':'xxx2','content':'xxxxxxxxx','date':'2023-11-04 00:00:00'}]
  43. except Exception as e:
  44. logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))
  45. raise
  46. # 新增一条日程
  47. def add_schedule(self, data, info):
  48. try:
  49. today = data['date']
  50. date_data = self.date_schedule(today, info)
  51. if date_data:
  52. return today+"已有日程"
  53. else:
  54. # data = {'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }
  55. schedule.objects.create(**data)
  56. return "ok"
  57. except Exception as e:
  58. logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))
  59. raise
  60. # 修改一条日程
  61. def update_schedule(self, id, data, info):
  62. try:
  63. # data = {'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }
  64. schedule.objects.filter(id=id).update(**data)
  65. return "ok"
  66. except Exception as e:
  67. logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))
  68. raise
  69. # 都删除一条日程
  70. def delete_schedule(self, id, info):
  71. try:
  72. # data = {'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }
  73. schedule.objects.filter(id=id).update(is_deleted = 1)
  74. return "ok"
  75. except Exception as e:
  76. logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))
  77. raise
  78. # 指定日期的日程
  79. def date_schedule(self, date, info):
  80. try:
  81. data = schedule.objects.get(date=date, is_deleted = 0)
  82. # data = [{'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }]
  83. return_data = self.to_json(data)
  84. return return_data
  85. except Exception as e:
  86. return {}