CouponController.py 5.8 KB

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