CouponController.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import time
  4. from django.db.models import F
  5. from django.http import HttpResponse
  6. from django.views.generic.base import View
  7. from Controller.CloudPhoto.CloudServiceController import CloudServiceController
  8. from Model.models import CouponModel, Device_User
  9. from Object.ResponseObject import ResponseObject
  10. from Object.TokenObject import TokenObject
  11. from Service.CommonService import CommonService
  12. from Ansjer.config import LOGGER
  13. # 优惠券
  14. class CouponView(View):
  15. def get(self, request, *args, **kwargs):
  16. request.encoding = 'utf-8'
  17. operation = kwargs.get('operation')
  18. return self.validation(request.GET, request, operation)
  19. def post(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. return self.validation(request.POST, request, operation)
  23. def validation(self, request_dict, request, operation):
  24. response = ResponseObject()
  25. if operation is None:
  26. return response.json(444, 'error path')
  27. if operation == 'generateCoupon': # 用户优惠券
  28. return self.generate_coupon(request_dict, response)
  29. else:
  30. token = request_dict.get('token', None)
  31. tko = TokenObject(token)
  32. response.lang = tko.lang
  33. if tko.code != 0:
  34. return response.json(tko.code)
  35. userID = tko.userID
  36. if operation == 'UserCoupon': # 用户优惠券
  37. return self.query_user_coupon(request_dict, userID, response)
  38. elif operation == 'createCoupon':
  39. self.generate_coupon_by_user(userID)
  40. return response.json(0)
  41. else:
  42. return response.json(414)
  43. def generate_coupon(self, request_dict, response):
  44. username = request_dict.get('username', None)
  45. num = request_dict.get('num', None)
  46. userID = Device_User.objects.filter(username=username).values('userID')[0]['userID']
  47. coupon_config_id = request_dict.get('coupon_config_id', 1)
  48. try:
  49. data = []
  50. # CouponConfigModel.objects.create(type=1, use_range=1, coupon_discount=8)
  51. # CouponLangObj = CouponLang.objects.create(
  52. # lang='cn',
  53. # instruction='用于自动续费套餐的首月',
  54. # quota='八',
  55. # unit='折',
  56. # remark='每台摄像机上只能用一次'
  57. # )
  58. # CouponConfigModel.objects.get(id=1).lang.add(CouponLangObj.id)
  59. now_time = int(time.time())
  60. CouponModel.objects.create(
  61. use_status=0,
  62. distribute_time=now_time,
  63. valid_time=now_time + 10000000,
  64. userID=userID,
  65. coupon_config_id=coupon_config_id,
  66. update_time=now_time,
  67. create_time=now_time
  68. )
  69. return HttpResponse('success')
  70. except Exception as e:
  71. return HttpResponse(
  72. "错误行数:{errLine}, 错误信息: {errmsg}".format(errLine=e.__traceback__.tb_lineno, errmsg=repr(e)))
  73. def query_user_coupon(self, request_dict, userID, response): # 用户优惠券列表
  74. now_time = int(time.time())
  75. lang = request_dict.get('lang', 'en')
  76. coupon_obj = CouponModel.objects.filter(
  77. userID=userID,
  78. use_status=0,
  79. distribute_time__lte=now_time,
  80. valid_time__gt=now_time,
  81. coupon_config__lang__lang=lang,
  82. ).annotate(
  83. coupon_id=F('id'),
  84. type=F('coupon_config__type'),
  85. coupon_discount=F('coupon_config__coupon_discount'),
  86. instruction=F('coupon_config__lang__instruction'),
  87. remark=F('coupon_config__lang__remark'),
  88. quota=F('coupon_config__lang__quota'),
  89. unit=F('coupon_config__lang__unit'),
  90. config_id=F('coupon_config_id')
  91. ).values(
  92. "coupon_id",
  93. "type",
  94. "coupon_discount",
  95. "valid_time",
  96. "instruction",
  97. "remark",
  98. "quota",
  99. "unit",
  100. "config_id"
  101. )
  102. for couponList in coupon_obj:
  103. couponList['valid_time'] = CommonService.timestamp_to_str(couponList['valid_time'])
  104. couponList['comboList'] = CloudServiceController.get_combo_list(0, couponList['config_id'])
  105. result = {
  106. 'count': coupon_obj.count(),
  107. 'couponList': list(coupon_obj),
  108. }
  109. return response.json(0, result)
  110. @staticmethod
  111. def generate_coupon_by_user(user_id):
  112. """
  113. 赠送优惠券圣诞节
  114. @param user_id: 用户ID
  115. @return: True | False
  116. """
  117. try:
  118. end_timestamp = 1735534799 # 活动时间结束: 东部时间2024-12-29 23:59:59
  119. now_time = int(time.time())
  120. if now_time >= end_timestamp:
  121. LOGGER.info('活动已结束,无法赠送优惠券')
  122. return False
  123. # 赠送三个优惠券
  124. for i in range(1, 4): # 赠送三个优惠券
  125. coupon_config_id = 21 if i < 3 else 22 # 21 9折 22 8折
  126. CouponModel.objects.create(
  127. use_status=0,
  128. distribute_time=now_time,
  129. valid_time=end_timestamp,
  130. userID=user_id,
  131. coupon_config_id=coupon_config_id,
  132. update_time=now_time,
  133. create_time=now_time
  134. )
  135. return True
  136. except Exception as e:
  137. # 记录异常信息
  138. LOGGER.error('赠送优惠券异常 user: {}: errLine: {}, errMsg: {}'.format(
  139. user_id, e.__traceback__.tb_lineno, repr(e)))
  140. return False