MealManage.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/5/29 17:07
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: MealManage.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import traceback
  15. from Ansjer.config import SERVER_DOMAIN_SSL
  16. from django.db.models import F
  17. from django.utils import timezone
  18. from django.utils.decorators import method_decorator
  19. from django.views.decorators.csrf import csrf_exempt
  20. from django.views.generic.base import View
  21. from Model.models import Store_Meal, VodBucketModel, Pay_Type, Lang
  22. from Object.ResponseObject import ResponseObject
  23. from Object.TokenObject import TokenObject
  24. from Service.CommonService import CommonService
  25. from Service.ModelService import ModelService
  26. '''
  27. http://192.168.136.40:8077/meal/manage?operation=add&token=local&title=套餐A&price=$199&content=存7天&day=7&id=1
  28. http://192.168.136.45:8077/meal/manage?operation=update&token=test&id=1&title=套餐A&price=$199&content=存3天&day=7
  29. http://192.168.136.40:8077/meal/manage?operation=query&token=test&page=1&line=10
  30. http://192.168.136.40:8077/meal/manage?operation=delete&token=test&id=1&id=2&id=3&id=4&id=5
  31. '''
  32. class MealManage(View):
  33. @method_decorator(csrf_exempt)
  34. def dispatch(self, *args, **kwargs):
  35. return super(MealManage, self).dispatch(*args, **kwargs)
  36. def get(self, request, *args, **kwargs):
  37. request.encoding = 'utf-8'
  38. return self.validation(request_dict=request.GET)
  39. def post(self, request, *args, **kwargs):
  40. request.encoding = 'utf-8'
  41. return self.validation(request_dict=request.POST)
  42. def validation(self, request_dict, *args, **kwargs):
  43. response = ResponseObject()
  44. token = request_dict.get('token', None)
  45. operation = request_dict.get('operation', None)
  46. if token is None:
  47. return response.json(309)
  48. tko = TokenObject(token)
  49. response.lang = tko.lang
  50. if tko.code != 0:
  51. return response.json(tko.code)
  52. userID = tko.userID
  53. if userID is None:
  54. return response.json(309)
  55. if operation == 'query':
  56. return self.query(request_dict, response)
  57. elif operation == 'add':
  58. return self.add(request_dict, userID, response)
  59. elif operation == 'update':
  60. return self.update(request_dict, userID, response)
  61. elif operation == 'delete':
  62. return self.delete(request_dict, userID, response)
  63. elif operation == 'find':
  64. return self.find(request_dict, userID, response)
  65. elif operation == 'query_language':
  66. return self.query_language(request_dict, response)
  67. elif operation == 'add_language':
  68. return self.add_language(request_dict, response)
  69. elif operation == 'delete_language':
  70. return self.delete_language(request_dict, response)
  71. elif operation == 'edit_language':
  72. return self.edit_language(request_dict, response)
  73. else:
  74. return response.json(444, 'operation')
  75. def add(self, request_dict, userID, response):
  76. # title = request_dict.get('title', None)
  77. id = request_dict.get('id', None)
  78. price = request_dict.get('price', None)
  79. # content = request_dict.get('content', None)
  80. day = request_dict.get('day', None)
  81. commodity_code = request_dict.get('commodity_code', None)
  82. currency = request_dict.get('currency', None)
  83. bucketID = request_dict.get('bucketID', None)
  84. paytype = request_dict.get('paytype', None)
  85. virtual_price = request_dict.get('virtual_price', None)
  86. is_discounts = request_dict.get('is_discounts', None)
  87. discount_price = request_dict.get('discount_price', None)
  88. expire = request_dict.get('expire', None)
  89. symbol = request_dict.get('symbol', None)
  90. is_show = request_dict.get('is_show', None)
  91. # if not title or not id or not price or not day or not content:
  92. if not id or not price or not day:
  93. return response.json(444, 'title,id,price,content,day,bucketID')
  94. own_perm = ModelService.check_perm(userID=userID, permID=40)
  95. if own_perm is not True:
  96. return response.json(404)
  97. try:
  98. bucketQs = VodBucketModel.objects.filter(id=bucketID)
  99. if Store_Meal.objects.filter(id=id):
  100. return response.json(10, '已存在')
  101. store_meal = Store_Meal(id=id, price=price, day=day, bucket_id=bucketID, commodity_code=commodity_code,
  102. currency=currency, virtual_price=virtual_price, is_discounts=is_discounts,
  103. discount_price=discount_price, expire=expire, symbol=symbol, is_show=is_show)
  104. store_meal.save()
  105. paytype = paytype.split(',')
  106. if len(paytype) > 0:
  107. Store_Meal.objects.get(id=id).pay_type.set(paytype)
  108. except Exception:
  109. errorInfo = traceback.format_exc()
  110. print(errorInfo)
  111. return response.json(500, {'details': errorInfo})
  112. else:
  113. if store_meal.id:
  114. return response.json(0, {
  115. 'bucket__bucket': bucketQs[0].bucket,
  116. 'bucket__storeDay': bucketQs[0].storeDay,
  117. 'id': id,
  118. 'price': price,
  119. 'currency': currency,
  120. 'day': day,
  121. 'add_time': str(store_meal.add_time),
  122. 'update_time': str(store_meal.update_time)})
  123. def query(self, request_dict, response):
  124. page = int(request_dict.get('page', None))
  125. line = int(request_dict.get('line', None))
  126. lang = request_dict.get('lang', 'cn')
  127. if page is None or line is None:
  128. return response.json(444)
  129. qs = Store_Meal.objects.filter(lang__lang=lang)
  130. qs = qs.annotate(title=F('lang__title'), content=F('lang__content'))
  131. qs = qs.values("id", "title", "price", "day", "add_time", "update_time", "currency", "expire", "symbol"
  132. , "commodity_type", "commodity_code", "virtual_price", "is_discounts", "discount_price"
  133. , "bucket_id", "bucket__bucket", "bucket__area", "bucket__storeDay", "bucket__mold", "is_show",
  134. "is_ai", "pixel_level")
  135. res = {}
  136. items_list = []
  137. if qs.exists():
  138. ql = list(qs)
  139. from operator import itemgetter
  140. from itertools import groupby
  141. ql.sort(key=itemgetter('bucket__area'))
  142. ql = CommonService.qs_to_list(ql[(page - 1) * line:page * line])
  143. # for area, items in groupby(ql, key=itemgetter('bucket__area')):
  144. items_list = list(ql)
  145. for key, val in enumerate(items_list):
  146. pay_types = Pay_Type.objects.filter(store_meal=items_list[key]['id']).values("id", "payment")
  147. items_list[key]['pay_type'] = list(pay_types)
  148. res['count'] =Store_Meal.objects.filter(lang__lang=lang).count()
  149. res['data'] = items_list
  150. return response.json(0, res)
  151. def update(self, request_dict, userID, response):
  152. id = request_dict.get('id', None)
  153. # title = request_dict.get('title', None)
  154. price = request_dict.get('price', None)
  155. day = request_dict.get('day', None)
  156. # content = request_dict.get('content', None)
  157. currency = request_dict.get('currency', None)
  158. bucketID = request_dict.get('bucketID', None)
  159. commodity_type = request_dict.get('commodity_type', None)
  160. commodity_code = request_dict.get('commodity_code', None)
  161. virtual_price = request_dict.get('virtual_price', None)
  162. is_discounts = request_dict.get('is_discounts', None)
  163. discount_price = request_dict.get('discount_price', None)
  164. expire = request_dict.get('expire', None)
  165. symbol = request_dict.get('symbol', None)
  166. type = request_dict.get('type', None)
  167. is_show = request_dict.get('is_show', None)
  168. is_ai = request_dict.get('is_ai', None)
  169. pixel_level = request_dict.get('pixel_level', None)
  170. # if not id or not title or not price or not content or not day or not type:
  171. if not id or not price or not day or not type:
  172. # return response.json(444, 'id, title, price, content, day,type')
  173. return response.json(444, 'id, price, day,type')
  174. own_perm = ModelService.check_perm(userID=userID, permID=30)
  175. if own_perm is not True:
  176. return response.json(404)
  177. try:
  178. store_meal = Store_Meal.objects.get(id=id)
  179. now_time = timezone.localtime(timezone.now())
  180. print(now_time)
  181. # store_meal.title = title
  182. store_meal.price = price
  183. # store_meal.content = content
  184. store_meal.commodity_type = commodity_type
  185. store_meal.commodity_code = commodity_code
  186. store_meal.virtual_price = virtual_price
  187. store_meal.is_discounts = is_discounts
  188. store_meal.discount_price = discount_price
  189. store_meal.expire = expire
  190. store_meal.symbol = symbol
  191. store_meal.is_show = is_show
  192. store_meal.is_ai = is_ai
  193. store_meal.pixel_level = pixel_level
  194. store_meal.day = day
  195. if bucketID:
  196. store_meal.bucket_id = bucketID
  197. if currency:
  198. store_meal.currency = currency
  199. store_meal.save()
  200. type = type.split(',')
  201. if len(type) > 0:
  202. Store_Meal.objects.get(id=id).pay_type.set(type)
  203. else:
  204. Store_Meal.objects.get(id=id).pay_type.clear()
  205. except Exception:
  206. errorInfo = traceback.format_exc()
  207. print(errorInfo)
  208. return response.json(424, {'details': errorInfo})
  209. else:
  210. return response.json(0, {'update_id': store_meal.id, 'update_time': str(now_time)})
  211. def delete(self, request_dict, userID, response):
  212. id_list = request_dict.getlist('id', None)
  213. if not id_list:
  214. return response.json(444, 'id')
  215. own_perm = ModelService.check_perm(userID=userID, permID=10)
  216. if own_perm is not True:
  217. return response.json(404)
  218. try:
  219. for id in id_list:
  220. Store_Meal.objects.filter(id=id).delete()
  221. except Exception as e:
  222. errorInfo = traceback.format_exc()
  223. print(errorInfo)
  224. return response.json(424, {'details': repr(e)})
  225. else:
  226. return response.json(0)
  227. def find(self, request_dict, userID, response):
  228. page = int(request_dict.get('page', None))
  229. line = int(request_dict.get('line', None))
  230. if not page or not line:
  231. return response.json(444, 'page,line')
  232. own_perm = ModelService.check_perm(userID=userID, permID=30)
  233. if own_perm is not True:
  234. return response.json(404)
  235. qs = Store_Meal.objects.all()
  236. if qs.exists():
  237. count = qs.count()
  238. res = qs[(page - 1) * line:page * line]
  239. send_json = CommonService.qs_to_dict(res)
  240. send_json['count'] = count
  241. return response.json(0, send_json)
  242. return response.json(0)
  243. def query_language(self, request_dict, response):
  244. # 查询套餐语言
  245. page = int(request_dict.get('page', None))
  246. line = int(request_dict.get('line', None))
  247. id = request_dict.get('id', None)
  248. if page is None or line is None:
  249. return response.json(444)
  250. if id:
  251. # 如果传入id,只查询id下的语言
  252. storeMeal_lang_qs = Store_Meal.objects.filter(id=id, lang__isnull=False).values('id', 'lang__id',
  253. 'lang__lang',
  254. 'lang__title',
  255. 'lang__content',
  256. 'lang__discount_content')
  257. else:
  258. storeMeal_lang_qs = Store_Meal.objects.filter(lang__isnull=False).values('id', 'lang__id', 'lang__lang',
  259. 'lang__title', 'lang__content',
  260. 'lang__discount_content')
  261. count = storeMeal_lang_qs.count()
  262. storeMeal_lang_qs = storeMeal_lang_qs[(page - 1) * line:page * line]
  263. res = {
  264. 'datas': list(storeMeal_lang_qs),
  265. 'count': count
  266. }
  267. return response.json(0, res)
  268. def add_language(self, request_dict, response):
  269. # 添加套餐语言
  270. store_meal_id = request_dict.get('store_meal_id', None)
  271. # lang_id = request_dict.get('lang_id', None)
  272. lang = request_dict.get('lang', None)
  273. title = request_dict.get('title', None)
  274. content = request_dict.get('content', None)
  275. discount_content = request_dict.get('discount_content', '')
  276. if not store_meal_id or not lang or not title or not content:
  277. return response.json(444, 'store_meal_id,lang,title,content')
  278. # 查询该套餐是否存在
  279. storeMeal_qs = Store_Meal.objects.get(id=store_meal_id)
  280. if not storeMeal_qs:
  281. return response.json(500)
  282. lang_obj = Lang.objects.filter(lang=lang, title=title, content=content, discount_content=discount_content)
  283. if not lang_obj.exists():
  284. # 数据不存在,lang表创建数据
  285. Lang.objects.create(lang=lang, title=title, content=content, discount_content=discount_content)
  286. lang_obj = Lang.objects.filter(lang=lang, title=title, content=content, discount_content=discount_content)
  287. storeMeal_qs.lang.add(*lang_obj) # store_meal表添加语言数据
  288. return response.json(0)
  289. def edit_language(self, request_dict, response):
  290. # 编辑套餐语言
  291. store_meal_id = request_dict.get('store_meal_id', None)
  292. lang_id = request_dict.get('lang_id', None)
  293. lang = request_dict.get('lang', None)
  294. title = request_dict.get('title', None)
  295. content = request_dict.get('content', None)
  296. discount_content = request_dict.get('discount_content', '')
  297. if not store_meal_id or not lang_id or not lang or not title or not content:
  298. return response.json(444, 'store_meal_id,lang_id,lang,title,content')
  299. storeMeal_qs = Store_Meal.objects.get(id=store_meal_id)
  300. if not storeMeal_qs:
  301. return response.json(500)
  302. Lang.objects.filter(id=lang_id).update(lang=lang, title=title, content=content, discount_content=discount_content)
  303. return response.json(0)
  304. # # 删除原有数据
  305. # lang_qs = Lang.objects.filter(id=lang_id)
  306. # storeMeal_qs.lang.remove(*lang_qs)
  307. # lang_obj = Lang.objects.filter(lang=lang, title=title, content=content, discount_content=discount_content)
  308. # if not lang_obj.exists():
  309. # # 数据不存在,lang表创建数据
  310. # Lang.objects.create(lang=lang, title=title, content=content, discount_content=discount_content)
  311. # lang_obj = Lang.objects.filter(lang=lang, title=title, content=content, discount_content=discount_content)
  312. # storeMeal_qs.lang.add(*lang_obj) # store_meal表添加语言数据
  313. # return response.json(0)
  314. def delete_language(self, request_dict, response):
  315. # 删除套餐语言
  316. store_meal_id = request_dict.get('store_meal_id', None)
  317. lang_id = request_dict.get('lang_id', None)
  318. storeMeal_qs = Store_Meal.objects.get(id=store_meal_id)
  319. if not storeMeal_qs:
  320. return response.json(500)
  321. lang_qs = Lang.objects.filter(id=lang_id)
  322. storeMeal_qs.lang.remove(*lang_qs)
  323. return response.json(0)
  324. '''
  325. 用户获取全部套餐信息
  326. http://192.168.136.40:8077/meal/list?token=local
  327. '''
  328. class MealView(View):
  329. @method_decorator(csrf_exempt)
  330. def dispatch(self, *args, **kwargs):
  331. return super(MealView, self).dispatch(*args, **kwargs)
  332. def get(self, request, *args, **kwargs):
  333. request.encoding = 'utf-8'
  334. operation = kwargs.get('operation')
  335. return self.validation(request.GET, request, operation)
  336. def post(self, request, *args, **kwargs):
  337. request.encoding = 'utf-8'
  338. operation = kwargs.get('operation')
  339. return self.validation(request.POST, request, operation)
  340. def validation(self, request_dict, request, operation):
  341. response = ResponseObject()
  342. if operation is None:
  343. return response.json(444, 'error path')
  344. token = request_dict.get('token', None)
  345. # 设备主键uid
  346. tko = TokenObject(token)
  347. response.lang = tko.lang
  348. if tko.code != 0:
  349. return response.json(tko.code)
  350. elif operation == 'query':
  351. return self.do_query(request_dict, response)
  352. else:
  353. return response.json(414)
  354. def do_query(self, request_dict, response):
  355. mold = request_dict.get('request_dict', None)
  356. if mold:
  357. qs = Store_Meal.objects.filter(bucket__mold=1). \
  358. values("id", "title", "content", "price", "day", "currency",
  359. "bucket__storeDay", "bucket__bucket", "bucket__area",
  360. "type", "symbol")
  361. else:
  362. qs = Store_Meal.objects.all(). \
  363. values("id", "title", "content", "price", "day", "currency",
  364. "bucket__storeDay", "bucket__bucket", "bucket__area",
  365. "type", "symbol")
  366. if qs.exists():
  367. ql = list(qs)
  368. from operator import itemgetter
  369. from itertools import groupby
  370. ql.sort(key=itemgetter('bucket__area'))
  371. res = []
  372. for area, items in groupby(ql, key=itemgetter('bucket__area')):
  373. res_c = {'area': area, 'items': list(items)}
  374. res.append(res_c)
  375. result = {
  376. 'meals': res,
  377. 'extra':
  378. {
  379. 'cloud_banner': SERVER_DOMAIN_SSL+'web/images/cloud_cn_banner.png',
  380. 'cloud_en_baner': SERVER_DOMAIN_SSL+'web/images/cloud_en_banner.png'
  381. }
  382. }
  383. return response.json(0, result)
  384. else:
  385. return response.json(0)