MealManage.py 18 KB

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