No Description

admin_views.py 70KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. import json
  4. from collections import defaultdict
  5. from datetime import datetime
  6. from django.conf import settings
  7. from django.db import transaction
  8. from django.db.models import Count, Q, Sum
  9. from django_logit import logit
  10. from django_response import response
  11. from django.contrib.auth.hashers import make_password
  12. from paginator import pagination
  13. from TimeConvert import TimeConvert as tc
  14. from pysnippets.strsnippets import strip
  15. from account.models import UserInfo
  16. from api.encrypt_views import get_ciphertext
  17. from coupon.models import CouponInfo, UserCouponInfo
  18. from integral.models import SaleclerkSubmitLogInfo
  19. from logs.models import ComplementCodeLogInfo, MchInfoEncryptLogInfo
  20. from mch.models import AdministratorInfo, BrandInfo, ConsumeInfoSubmitLogInfo, DistributorInfo, ModelInfo
  21. from member.models import (GoodsInfo, GoodsOrderInfo, MemberActivityGroupShareInfo, MemberActivityInfo,
  22. MemberActivitySignupInfo)
  23. from pre.custom_message import sendtemplatemessage, sendwxasubscribemessage
  24. from statistic.models import ConsumeModelSaleStatisticInfo, ConsumeSaleStatisticInfo, ConsumeUserStatisticInfo
  25. from utils.error.errno_utils import (AdministratorStatusCode, ComplementCodeStatusCode, ProductBrandStatusCode,
  26. ProductCouponStatusCode, ProductMachineStatusCode, UserStatusCode)
  27. WECHAT = settings.WECHAT
  28. @logit
  29. def querysn(request):
  30. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  31. admin_id = request.POST.get('admin_id', '')
  32. model_id = request.POST.get('model_id', '')
  33. sn = request.POST.get('sn', '')
  34. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  35. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  36. if not AdministratorInfo.objects.filter(admin_id=admin_id, admin_type=AdministratorInfo.MAINTENANCE, user_status=AdministratorInfo.ACTIVATED, status=True).exists():
  37. return response(AdministratorStatusCode.MAINTENANCE_NOT_FOUND)
  38. log = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=brand_id, model_id=model_id, serialNo=sn, submit_during_activity=True, status=True).order_by('has_used', '-pk').first()
  39. if not log:
  40. log = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=brand_id, model_id=model_id, serialNo=sn, status=True).order_by('-submit_during_activity', 'dupload').first()
  41. if not log:
  42. return response(ProductMachineStatusCode.SN_NOT_FOUND)
  43. return response(200, 'Query SN Success', u'查询序列号成功', data=log.data)
  44. @logit
  45. def queryusedsn(request):
  46. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  47. admin_id = request.POST.get('admin_id', '')
  48. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  49. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  50. if not AdministratorInfo.objects.filter(admin_id=admin_id, admin_type=AdministratorInfo.MAINTENANCE, user_status=AdministratorInfo.ACTIVATED, status=True).exists():
  51. return response(AdministratorStatusCode.MAINTENANCE_NOT_FOUND)
  52. logs = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=brand_id, admin_id=admin_id, has_used=True, status=True).order_by('-used_at')
  53. logs = [log.data for log in logs]
  54. return response(200, 'Query Used SN Success', u'查询核销序列号成功', data={
  55. 'logs': logs,
  56. })
  57. @logit
  58. @transaction.atomic
  59. def usecoupon(request):
  60. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  61. admin_id = request.POST.get('admin_id', '')
  62. model_id = request.POST.get('model_id', '')
  63. sn = request.POST.get('sn', '')
  64. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  65. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  66. if not AdministratorInfo.objects.filter(
  67. admin_id=admin_id,
  68. admin_type=AdministratorInfo.MAINTENANCE,
  69. user_status=AdministratorInfo.ACTIVATED,
  70. status=True
  71. ).exists():
  72. return response(AdministratorStatusCode.MAINTENANCE_NOT_FOUND)
  73. log = ConsumeInfoSubmitLogInfo.objects.filter(
  74. brand_id=brand_id,
  75. model_id=model_id,
  76. serialNo=sn,
  77. submit_during_activity=True,
  78. has_used=False,
  79. status=True
  80. ).first()
  81. if not log:
  82. return response(ProductMachineStatusCode.SN_NOT_FOUND)
  83. logdata = log.data
  84. if logdata.get('final_coupon_info', {}).get('coupon_has_expired', True):
  85. return response(ProductCouponStatusCode.COUPON_HAS_EXPIRED)
  86. log.has_used = True
  87. log.used_at = tc.utc_datetime()
  88. log.save()
  89. return response(200, 'Use Coupon Success', u'核销优惠券成功')
  90. @logit
  91. def userinfo(request):
  92. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  93. admin_id = request.POST.get('admin_id', '')
  94. page = request.POST.get('page', 1)
  95. num = request.POST.get('num', 20)
  96. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  97. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  98. try:
  99. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  100. except AdministratorInfo.DoesNotExist:
  101. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  102. userinfos = UserInfo.objects.filter(status=True).order_by('-pk')
  103. count = userinfos.count()
  104. userinfos, left = pagination(userinfos, page, num)
  105. userinfos = [userinfo.admindata for userinfo in userinfos]
  106. return response(200, 'Userinfo Success', u'用户列表获取成功', data={
  107. 'userinfos': userinfos,
  108. 'count': count,
  109. 'left': left,
  110. })
  111. @logit
  112. def query_usercoupons(request):
  113. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  114. admin_id = request.POST.get('admin_id', '')
  115. user_id = request.POST.get('user_id', '')
  116. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  117. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  118. # 校验用户是否存在
  119. try:
  120. user = UserInfo.objects.get(user_id=user_id)
  121. except UserInfo.DoesNotExist:
  122. return response(UserStatusCode.USER_NOT_FOUND)
  123. try:
  124. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  125. except AdministratorInfo.DoesNotExist:
  126. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  127. coupons = UserCouponInfo.objects.filter(brand_id=administrator.brand_id, user_id=user_id, status=True).order_by('-pk')
  128. coupons = [coupon.admindata for coupon in coupons]
  129. return response(200, 'User Coupon Success', u'用户劵列表获取成功', data={
  130. 'coupons': coupons,
  131. })
  132. @logit
  133. def query_usergoods(request):
  134. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  135. admin_id = request.POST.get('admin_id', '')
  136. user_id = request.POST.get('user_id', '')
  137. dupload = request.POST.get('dupload', '')
  138. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  139. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  140. # 校验用户是否存在
  141. try:
  142. user = UserInfo.objects.get(user_id=user_id)
  143. except UserInfo.DoesNotExist:
  144. return response(UserStatusCode.USER_NOT_FOUND)
  145. try:
  146. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  147. except AdministratorInfo.DoesNotExist:
  148. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  149. logs = ConsumeInfoSubmitLogInfo.objects.filter(brand_id=administrator.brand_id, user_id=user_id, status=True).order_by('-pk')
  150. if dupload:
  151. logs = logs.filter(dupload=dupload)
  152. logs = [log.adminuserdata for log in logs]
  153. return response(200, 'Userinfo Goods Success', u'用户商品列表获取成功', data={
  154. 'logs': logs,
  155. })
  156. @logit
  157. def query_userinfo(request):
  158. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  159. admin_id = request.POST.get('admin_id', '')
  160. query = request.POST.get('query', '')
  161. level = request.POST.get('level', '')
  162. is_member = request.POST.get('is_member', '')
  163. code_version = request.POST.get('code_version', 0)
  164. sex = request.POST.get('sex', '')
  165. subscribe = request.POST.get('subscribe', '')
  166. has_membercard = request.POST.get('has_membercard', '')
  167. province = request.POST.get('province', '')
  168. start_time = request.POST.get('start_time', '')
  169. end_time = request.POST.get('end_time', '')
  170. page = request.POST.get('page', 1)
  171. num = request.POST.get('num', 20)
  172. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  173. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  174. try:
  175. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  176. except AdministratorInfo.DoesNotExist:
  177. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  178. userinfos = UserInfo.objects.filter(Q(status=True) & (Q(user_id__icontains=query) | Q(nickname__icontains=query) | Q(phone__icontains=query) | Q(memberusercardcode__icontains=query))).order_by('-pk')
  179. if level:
  180. userinfos = userinfos.filter(level=level)
  181. if code_version:
  182. userinfos = userinfos.filter(code_version=code_version)
  183. if subscribe:
  184. userinfos = userinfos.filter(subscribe=subscribe)
  185. if has_membercard:
  186. userinfos = userinfos.filter(has_membercard=has_membercard)
  187. if is_member:
  188. userinfos = userinfos.filter(level__gte=1)
  189. if sex:
  190. userinfos = userinfos.filter(sex=sex)
  191. if province:
  192. userinfos = userinfos.filter(province_name=province)
  193. if start_time and end_time:
  194. ftime = datetime.strptime(start_time + ' 00:00:00', '%Y%m%d %H:%M:%S')
  195. ttime = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  196. userinfos = userinfos.filter(created_at__range=[ftime, ttime])
  197. count = userinfos.count()
  198. userinfos, left = pagination(userinfos, page, num)
  199. userinfos = [userinfo.admindata for userinfo in userinfos]
  200. return response(200, 'Userinfo Success', u'用户列表获取成功', data={
  201. 'userinfos': userinfos,
  202. 'count': count,
  203. 'left': left
  204. })
  205. @logit
  206. @transaction.atomic
  207. def userinfo_update(request):
  208. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  209. admin_id = request.POST.get('admin_id', '')
  210. user_id = request.POST.get('user_id', '')
  211. integral = int(request.POST.get('integral', 0))
  212. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  213. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  214. try:
  215. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  216. except AdministratorInfo.DoesNotExist:
  217. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  218. # 校验用户是否存在
  219. try:
  220. user = UserInfo.objects.select_for_update().get(user_id=user_id, status=True)
  221. except UserInfo.DoesNotExist:
  222. return response(UserStatusCode.USER_NOT_FOUND)
  223. user.integral = integral
  224. user.save()
  225. return response(200, 'Userinfo Update Success', u'修改用户信息成功', data={})
  226. # 统计
  227. def statistic_userprofile(request):
  228. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  229. admin_id = request.POST.get('admin_id', '')
  230. start_time = request.POST.get('start_time', '')
  231. end_time = request.POST.get('end_time', '')
  232. model_name = request.POST.get('model_name', '')
  233. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  234. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  235. try:
  236. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  237. except AdministratorInfo.DoesNotExist:
  238. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  239. if model_name:
  240. logs = ConsumeModelSaleStatisticInfo.objects.filter(model_name=model_name, ymd__gte=start_time, ymd__lte=end_time)
  241. else:
  242. logs = ConsumeModelSaleStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time)
  243. sexs = {0: 0, 1: 0, 2: 0}
  244. subscribes = {0: 0, 1: 0}
  245. has_membercards = {0: 0, 1: 0}
  246. provinces = defaultdict(int)
  247. for log in logs:
  248. for user in log.users:
  249. try:
  250. user = UserInfo.objects.get(user_id=user, status=True)
  251. except UserInfo.DoesNotExist:
  252. continue
  253. provinces[user.province_name] += 1
  254. sexs[user.sex] += 1
  255. subscribes[user.subscribe] += 1
  256. has_membercards[user.has_membercard] += 1
  257. return response(200, 'Get User Profile Success', u'获取用户画像成功', data={
  258. 'provinces': provinces,
  259. 'sexs': sexs,
  260. 'subscribe': subscribes,
  261. 'has_membercards': has_membercards,
  262. })
  263. def statistic_daily(request):
  264. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  265. admin_id = request.POST.get('admin_id', '')
  266. start_time = request.POST.get('start_time', '')
  267. end_time = request.POST.get('end_time', '')
  268. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  269. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  270. try:
  271. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  272. except AdministratorInfo.DoesNotExist:
  273. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  274. sale_logs = ConsumeSaleStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time)
  275. user_logs = ConsumeUserStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time)
  276. sale_logs = [log.data for log in sale_logs]
  277. user_logs = [log.data for log in user_logs]
  278. return response(200, 'Get User Sales Statistic Success', u'获取销量统计成功', data={
  279. 'sale_logs': sale_logs,
  280. 'user_logs': user_logs,
  281. })
  282. def statistic_model(request):
  283. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  284. admin_id = request.POST.get('admin_id', '')
  285. start_time = request.POST.get('start_time', '')
  286. end_time = request.POST.get('end_time', '')
  287. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  288. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  289. try:
  290. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  291. except AdministratorInfo.DoesNotExist:
  292. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  293. logs = ConsumeModelSaleStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time).values_list('model_name').annotate(Sum('num'))
  294. logs = [{'model_name': log[0], 'num': log[1]} for log in logs]
  295. return response(200, 'Get Model Statistic Success', u'获取型号统计成功', data={
  296. 'logs': logs
  297. })
  298. def statistic_distributor(request):
  299. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  300. admin_id = request.POST.get('admin_id', '')
  301. start_time = request.POST.get('start_time', '')
  302. end_time = request.POST.get('end_time', '')
  303. model_name = request.POST.get('model_name', '')
  304. distributor_name = request.POST.get('distributor_name', '')
  305. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  306. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  307. try:
  308. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  309. except AdministratorInfo.DoesNotExist:
  310. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  311. daily_sql = 'select id, Count(*) AS num, Count(code_version=1 or NULL) AS code1 , Count(code_version=2 or NULL) AS code2, Count((code_version=1 AND has_scan=True) or NULL) AS has_code1_scan_num, Count((code_version=2 AND has_scan=True) or NULL) AS has_code2_scan_num FROM integral_saleclerksubmitloginfo where ymd >= %s AND ymd <= %s AND test_user=False AND dupload=False AND test_sn=False AND status=True %s %s GROUP BY ymd;' % (start_time, end_time, ('AND model_uni_name="%s"' % (model_name)) if model_name else '', ('AND distributor_name="%s"' % (distributor_name)) if distributor_name else '')
  312. daily_logs = SaleclerkSubmitLogInfo.objects.raw(daily_sql)
  313. daily_logs = [{'ymd': log.ymd, 'num': log.num, 'code1': log.code1, 'code2': log.code2, 'has_code1_scan_num': log.has_code1_scan_num, 'has_code2_scan_num': log.has_code2_scan_num} for log in daily_logs]
  314. model_logs = []
  315. if model_name == '':
  316. model_sql = 'select id, model_uni_name, Count(*) AS num, Count(code_version=1 or NULL) AS code1 , Count(code_version=2 or NULL) AS code2, Count((code_version=1 AND has_scan=True) or NULL) AS has_code1_scan_num, Count((code_version=2 AND has_scan=True) or NULL) AS has_code2_scan_num FROM integral_saleclerksubmitloginfo where ymd >= %s AND ymd <= %s AND test_user=False AND dupload=False AND test_sn=False AND status=True %s GROUP BY model_uni_name;' % (start_time, end_time, ('AND distributor_name="%s"' % (distributor_name)) if distributor_name else '')
  317. model_logs = SaleclerkSubmitLogInfo.objects.raw(model_sql)
  318. model_logs = [{'model_uni_name': log.model_uni_name, 'num': log.num, 'code1': log.code1, 'code2': log.code2, 'has_code1_scan_num': log.has_code1_scan_num, 'has_code2_scan_num': log.has_code2_scan_num} for log in model_logs]
  319. distributor_logs = []
  320. if distributor_name == '':
  321. distributor_sql = 'select id, distributor_name, Count(*) AS num, Count(code_version=1 or NULL) AS code1 , Count(code_version=2 or NULL) AS code2, Count((code_version=1 AND has_scan=True) or NULL) AS has_code1_scan_num, Count((code_version=2 AND has_scan=True) or NULL) AS has_code2_scan_num FROM integral_saleclerksubmitloginfo where ymd >= %s AND ymd <= %s AND test_user=False AND dupload=False AND test_sn=False AND status=True %s GROUP BY distributor_name;' % (start_time, end_time, ('AND model_uni_name="%s"' % (model_name)) if model_name else '')
  322. distributor_logs = SaleclerkSubmitLogInfo.objects.raw(distributor_sql)
  323. distributor_logs = [{'distributor_name': log.distributor_name, 'num': log.num, 'code1': log.code1, 'code2': log.code2, 'has_code1_scan_num': log.has_code1_scan_num, 'has_code2_scan_num': log.has_code2_scan_num} for log in distributor_logs]
  324. return response(200, 'Get Distributor Statistic Success', u'获取经销商统计成功', data={
  325. 'daily_logs': daily_logs,
  326. 'model_logs': model_logs,
  327. 'distributor_logs': distributor_logs
  328. })
  329. def dashboard(request):
  330. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  331. admin_id = request.POST.get('admin_id', '')
  332. start_time = request.POST.get('start_time', '')
  333. end_time = request.POST.get('end_time', '')
  334. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  335. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  336. try:
  337. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  338. except AdministratorInfo.DoesNotExist:
  339. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  340. # 销售员统计
  341. sale_logs = SaleclerkSubmitLogInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time, test_user=False, dupload=False, test_sn=False, status=True)
  342. sale_count = sale_logs.count()
  343. sale_daily_logs = list(sale_logs.values('ymd').annotate(num=Count('pk')))
  344. sale_model_logs = list(sale_logs.values('model_uni_name').annotate(num=Count('pk')).order_by('-num'))[:10]
  345. sale_distributor_logs = list(sale_logs.values('distributor_name').annotate(num=Count('pk')).order_by('-num'))[:10]
  346. # 消费者统计
  347. logs = ConsumeInfoSubmitLogInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time, test_user=False, dupload=False, status=True)
  348. consumer_count = logs.count()
  349. consumer_daily_logs = list(logs.values('ymd').annotate(num=Count('pk')).order_by('ymd'))
  350. consumer_model_logs = list(logs.values('model_uni_name').annotate(num=Count('pk')).order_by('-num'))[:10]
  351. consumer_province_logs = list(logs.values('province').annotate(num=Count('pk')).order_by('-num'))[:10]
  352. return response(200, 'Get Distributor Statistic Success', u'获取经销商统计成功', data={
  353. 'sale_daily_logs': sale_daily_logs,
  354. 'sale_model_logs': sale_model_logs,
  355. 'sale_distributor_logs': sale_distributor_logs,
  356. 'consumer_daily_logs': consumer_daily_logs,
  357. 'consumer_model_logs': consumer_model_logs,
  358. 'consumer_province_logs': consumer_province_logs,
  359. 'sale_count': sale_count,
  360. 'consumer_count': consumer_count,
  361. })
  362. def statistic_consumer(request):
  363. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  364. admin_id = request.POST.get('admin_id', '')
  365. start_time = request.POST.get('start_time', '')
  366. end_time = request.POST.get('end_time', '')
  367. model_name = request.POST.get('model_name', '')
  368. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  369. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  370. try:
  371. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  372. except AdministratorInfo.DoesNotExist:
  373. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  374. # 消费者统计
  375. daily_sql = 'select id, Count(id) AS num, Count(code_version=1 or NULL) AS code1 , Count(code_version=2 or NULL) AS code2 FROM mch_consumeinfosubmitloginfo where ymd >= %s AND ymd <= %s AND test_user=False AND dupload=False AND status=True %s GROUP BY ymd;' % (start_time, end_time, ('AND model_uni_name="%s"' % (model_name)) if model_name else '')
  376. daily_logs = ConsumeInfoSubmitLogInfo.objects.raw(daily_sql)
  377. daily_logs = [{'ymd': log.ymd, 'num': log.num, 'code1': log.code1, 'code2': log.code2} for log in daily_logs]
  378. model_logs = []
  379. if model_name == '':
  380. model_sql = 'select id, model_uni_name, Count(id) AS num, Count(code_version=1 or NULL) AS code1 , Count(code_version=2 or NULL) AS code2 FROM mch_consumeinfosubmitloginfo where ymd >= %s AND ymd <= %s AND test_user=False AND dupload=False AND status=True GROUP BY model_uni_name ORDER BY num desc;' % (start_time, end_time)
  381. model_logs = ConsumeInfoSubmitLogInfo.objects.raw(model_sql)
  382. model_logs = [{'model_uni_name': log.model_uni_name, 'num': log.num, 'code1': log.code1, 'code2': log.code2} for log in model_logs]
  383. province_sql = 'select id, province, Count(id) AS num, Count(code_version=1 or NULL) AS code1, Count(code_version=2 or NULL) AS code2 FROM mch_consumeinfosubmitloginfo where ymd >= %s AND ymd <= %s AND test_user=False AND dupload=False AND status=True %s GROUP BY province ORDER BY num desc;' % (start_time, end_time, ('AND model_uni_name="%s"' % (model_name)) if model_name else '')
  384. province_logs = ConsumeInfoSubmitLogInfo.objects.raw(province_sql)
  385. province_logs = [{'province': log.province, 'num': log.num, 'code1': log.code1, 'code2': log.code2} for log in province_logs]
  386. return response(200, 'Get Consumer Statistic Success', u'获取消费者统计成功', data={
  387. 'daily_logs': daily_logs,
  388. 'model_logs': model_logs,
  389. 'province_logs': province_logs,
  390. })
  391. def statistic_member(request):
  392. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  393. admin_id = request.POST.get('admin_id', '')
  394. start_time = request.POST.get('start_time', '')
  395. end_time = request.POST.get('end_time', '')
  396. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  397. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  398. try:
  399. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  400. except AdministratorInfo.DoesNotExist:
  401. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  402. # 消费者统计
  403. if start_time and end_time:
  404. start_time = datetime.strptime(start_time, '%Y%m%d')
  405. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  406. userinfos = UserInfo.objects.filter(test_user=False, level__gte=1, resgister_at__range=(start_time, end_time))
  407. else:
  408. userinfos = UserInfo.objects.filter(test_user=False, level__gte=1, status=True)
  409. # 关注公众号数
  410. mp_num = userinfos.filter(subscribe=True).count()
  411. # 领取保卡数
  412. membercard_num = userinfos.filter(has_membercard=True).count()
  413. # 会员级别
  414. level_logs = list(userinfos.values('level').annotate(num=Count('user_id')))
  415. # 会员积分
  416. integral_logs = list(userinfos.values('integral').annotate(num=Count('user_id')))
  417. # 镜头数
  418. shot_logs = list(userinfos.values('shots_num').annotate(num=Count('user_id')))
  419. # 性别
  420. sex_logs = list(userinfos.values('sex').annotate(num=Count('user_id')))
  421. # 省份
  422. province_logs = list(userinfos.values('province_name').annotate(num=Count('user_id')).order_by('-num'))
  423. return response(200, 'Get Consumer Statistic Success', u'获取消费者统计成功', data={
  424. 'mp_num': mp_num,
  425. 'membercard_num': membercard_num,
  426. 'level_logs': level_logs,
  427. 'integral_logs': integral_logs,
  428. 'shot_logs': shot_logs,
  429. 'sex_logs': sex_logs,
  430. 'province_logs': province_logs,
  431. })
  432. def statistic_coupon(request):
  433. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  434. admin_id = request.POST.get('admin_id', '')
  435. start_time = request.POST.get('start_time', '')
  436. end_time = request.POST.get('end_time', '')
  437. if start_time and end_time:
  438. start_time = datetime.strptime(start_time, '%Y%m%d')
  439. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  440. coupons = UserCouponInfo.objects.filter(active_at__range=(start_time, end_time), status=True)
  441. else:
  442. coupons = UserCouponInfo.objects.filter(status=True)
  443. froms_logs = list(coupons.values('coupon_from').annotate(num=Count('user_coupon_id')))
  444. type_logs = list(coupons.values('coupon_title', 'coupon_value').annotate(num=Count('user_coupon_id')))
  445. if start_time and end_time:
  446. used_coupons = UserCouponInfo.objects.filter(used_at__range=(start_time, end_time), status=True)
  447. else:
  448. used_coupons = UserCouponInfo.objects.filter(has_used=True, status=True)
  449. type_has_used_logs = list(used_coupons.values('coupon_title', 'coupon_value').annotate(num=Count('user_coupon_id')))
  450. return response(200, 'Get Consumer Statistic Success', u'获取用户劵统计成功', data={
  451. 'froms_logs': froms_logs,
  452. 'type_logs': type_logs,
  453. 'type_has_used_logs': type_has_used_logs,
  454. })
  455. def record_consumer(request):
  456. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  457. page = request.POST.get('page', 1)
  458. num = request.POST.get('num', 20)
  459. admin_id = request.POST.get('admin_id', '')
  460. start_time = request.POST.get('start_time', '')
  461. end_time = request.POST.get('end_time', '')
  462. query = request.POST.get('query', '')
  463. dupload = request.POST.get('dupload', '')
  464. code_version = request.POST.get('code_version', 0)
  465. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  466. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  467. try:
  468. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  469. except AdministratorInfo.DoesNotExist:
  470. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  471. logs = ConsumeInfoSubmitLogInfo.objects.filter(Q(status=True) & Q(test_user=False) & (Q(model_name__icontains=query) | Q(phone__icontains=query) | Q(serialNo__icontains=query))).order_by('-pk')
  472. if dupload:
  473. logs = logs.filter(dupload=dupload)
  474. if code_version:
  475. logs = logs.filter(code_version=code_version)
  476. if start_time and end_time:
  477. logs = logs.filter(ymd__gte=start_time, ymd__lte=end_time)
  478. count = logs.count()
  479. logs, left = pagination(logs, page, num)
  480. logs = [log.admindata for log in logs]
  481. return response(200, 'Get Consumer Record Success', u'获取消费者销售记录成功', data={
  482. 'logs': logs,
  483. 'count': count,
  484. 'left': left
  485. })
  486. def record_consumer_batch(request):
  487. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  488. admin_id = request.POST.get('admin_id', '')
  489. logs = json.loads(request.POST.get('logs', '[]'))
  490. infos = []
  491. unknows = []
  492. for log in logs:
  493. try:
  494. infos.append(ConsumeInfoSubmitLogInfo.objects.get(model_name__icontains=log['model_name'], serialNo=log['code'], status=True, dupload=False).admindata)
  495. except ConsumeInfoSubmitLogInfo.DoesNotExist:
  496. unknows.append(log)
  497. return response(200, 'Batch Get Record Sale Success', u'批量获取销售员销售记录', data={
  498. 'logs': infos,
  499. 'unknows': unknows
  500. })
  501. def record_sale(request):
  502. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  503. admin_id = request.POST.get('admin_id', '')
  504. page = request.POST.get('page', 1)
  505. num = request.POST.get('num', 20)
  506. start_time = request.POST.get('start_time', '')
  507. end_time = request.POST.get('end_time', '')
  508. query = request.POST.get('query', '')
  509. dupload = request.POST.get('dupload', '')
  510. has_scan = request.POST.get('has_scan', '')
  511. code_version = request.POST.get('code_version', 0)
  512. is_online_sale = request.POST.get('is_online_sale', '')
  513. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  514. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  515. try:
  516. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  517. except AdministratorInfo.DoesNotExist:
  518. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  519. logs = SaleclerkSubmitLogInfo.objects.filter(Q(status=True) & Q(test_user=False) & Q(test_sn=False) & (Q(model_name__icontains=query) | Q(clerk_name__icontains=query) | Q(distributor_name__icontains=query) | Q(code__icontains=query))).order_by('-created_at')
  520. if dupload:
  521. logs = logs.filter(dupload=dupload)
  522. if code_version:
  523. logs = logs.filter(code_version=code_version)
  524. if has_scan:
  525. logs = logs.filter(has_scan=has_scan)
  526. if start_time and end_time:
  527. logs = logs.filter(ymd__gte=start_time, ymd__lte=end_time)
  528. if is_online_sale:
  529. logs = logs.filter(remark=('扫描条形码' if is_online_sale == '1' else ''))
  530. count = logs.count()
  531. logs, left = pagination(logs, page, num)
  532. logs = [log.admindata for log in logs]
  533. return response(200, 'Get Sale Record Success', u'获取销售员销售记录成功', data={
  534. 'logs': logs,
  535. 'count': count,
  536. 'left': left
  537. })
  538. def record_sale_batch(request):
  539. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  540. admin_id = request.POST.get('admin_id', '')
  541. logs = json.loads(request.POST.get('logs', '[]'))
  542. infos = []
  543. unknows = []
  544. for log in logs:
  545. try:
  546. infos.append(SaleclerkSubmitLogInfo.objects.get(model_name__icontains=log['model_name'], code=log['code'], status=True, dupload=False).admindata)
  547. except SaleclerkSubmitLogInfo.DoesNotExist:
  548. unknows.append(log)
  549. return response(200, 'Batch Get Record Sale Success', u'批量获取销售员销售记录', data={
  550. 'logs': infos,
  551. 'unknows': unknows
  552. })
  553. def model_list(request):
  554. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  555. admin_id = request.POST.get('admin_id', '')
  556. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  557. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  558. try:
  559. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  560. except AdministratorInfo.DoesNotExist:
  561. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  562. infos = list(ModelInfo.objects.filter(status=True).values_list('model_uni_name', flat=True).distinct().order_by('model_uni_name'))
  563. return response(200, 'Get Model List Success', u'获取型号列表成功', data={
  564. 'models': infos
  565. })
  566. def distributor_list(request):
  567. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  568. admin_id = request.POST.get('admin_id', '')
  569. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  570. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  571. try:
  572. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  573. except AdministratorInfo.DoesNotExist:
  574. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  575. infos = list(DistributorInfo.objects.filter(status=True).values_list('distributor_name', flat=True).distinct().order_by('distributor_name'))
  576. return response(200, 'Get Model List Success', u'获取经销商列表成功', data={
  577. 'distributors': infos
  578. })
  579. def member_goods_order(request):
  580. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  581. admin_id = request.POST.get('admin_id', '')
  582. query = request.POST.get('query', '')
  583. good_type = request.POST.get('good_type', '')
  584. page = request.POST.get('page', 1)
  585. num = request.POST.get('num', 20)
  586. order_status = int(request.POST.get('order_status', 0))
  587. start_time = request.POST.get('start_time', '')
  588. end_time = request.POST.get('end_time', '')
  589. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  590. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  591. try:
  592. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  593. except AdministratorInfo.DoesNotExist:
  594. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  595. orders = GoodsOrderInfo.objects.filter(Q(phone__icontains=query) | Q(user_id=query) | Q(title__icontains=query) & Q(status=True)).order_by('-pk')
  596. if good_type:
  597. orders = orders.filter(good_type=good_type)
  598. if start_time and end_time:
  599. start_time = datetime.strptime(start_time, '%Y%m%d')
  600. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  601. orders = orders.filter(created_at__range=(start_time, end_time))
  602. # 未发货,即快递单号为空
  603. if order_status == 1:
  604. orders = orders.filter(Q(tracking_number__isnull=True) | Q(tracking_number=''))
  605. elif order_status == 2:
  606. orders = orders.filter(tracking_number__isnull=False).exclude(tracking_number='')
  607. count = orders.count()
  608. orders, left = pagination(orders, page, num)
  609. orders = [order.admindata for order in orders]
  610. return response(200, 'Get Member Goods Order List Success', u'获取会员商品订单列表成功', data={
  611. 'orders': orders,
  612. 'left': left,
  613. 'count': count
  614. })
  615. def member_goods_order_update(request):
  616. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  617. admin_id = request.POST.get('admin_id', '')
  618. order_id = request.POST.get('order_id', '')
  619. tracking_number = request.POST.get('tracking_number', '')
  620. remark = request.POST.get('remark', u'您的礼品正在快马加鞭赶来,请耐心等待呦~')
  621. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  622. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  623. try:
  624. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  625. except AdministratorInfo.DoesNotExist:
  626. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  627. if not send_trackingNumber_message(order_id, tracking_number, remark):
  628. return response(400001, 'Member Goods Order Update Failure', u'会员商品订单修改失败')
  629. return response(200, 'Member Goods Order Update Success', u'会员商品订单修改成功')
  630. def member_goods_order_batch_update(request):
  631. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  632. admin_id = request.POST.get('admin_id', '')
  633. orders = json.loads(request.POST.get('orders', '[]'))
  634. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  635. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  636. try:
  637. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  638. except AdministratorInfo.DoesNotExist:
  639. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  640. err_orders = []
  641. for order in orders:
  642. if not send_trackingNumber_message(order.get('order_id', ''), order.get('tracking_number', ''), order.get('remark', u'您的礼品正在快马加鞭赶来,请耐心等待呦~')):
  643. err_orders.append(order.get('order_id', ''))
  644. if err_orders:
  645. return response(40001, 'Member Goods Order Update Error', u'部分会员商品订单修改失败', data={
  646. 'orders': err_orders
  647. })
  648. return response(200, 'Member Goods Order Update Success', u'会员商品订单修改成功')
  649. def send_trackingNumber_message(order_id, tracking_number, remark):
  650. try:
  651. order = GoodsOrderInfo.objects.get(order_id=order_id, status=True)
  652. order.remark = remark
  653. order.tracking_number = tracking_number
  654. if tracking_number and order.good_type == GoodsInfo.PHYSICAL and not order.has_send_template_message:
  655. order.has_send_template_message = True
  656. # 校验用户是否存在
  657. user = UserInfo.objects.get(user_id=order.user_id)
  658. data = {
  659. "thing6": {
  660. "value": order.title,
  661. "color": "#173177"
  662. },
  663. "character_string7": {
  664. "value": tracking_number,
  665. "color": "#173177"
  666. },
  667. "number2": {
  668. "value": order.integral,
  669. "color": "#173177"
  670. },
  671. "thing5": {
  672. "value": order.remark,
  673. "color": "#173177"
  674. },
  675. }
  676. res = sendwxasubscribemessage(openid=user.openid_miniapp, template_id=settings.TEMPLATE_ID_TRACKING, data=data, miniprogram_state=None, lang=None, page=None)
  677. order.send_template_message_res = json.dumps(res)
  678. order.save()
  679. return True
  680. except Exception:
  681. return False
  682. def record_warehouse(request):
  683. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  684. admin_id = request.POST.get('admin_id', '')
  685. model_name = request.POST.get('model_name', '')
  686. sn = request.POST.get('sn', '')
  687. start_time = request.POST.get('start_time', '')
  688. end_time = request.POST.get('end_time', '')
  689. page = request.POST.get('page', 1)
  690. num = request.POST.get('num', 20)
  691. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  692. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  693. try:
  694. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  695. except AdministratorInfo.DoesNotExist:
  696. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  697. models = ModelInfo.objects.filter(model_name__icontains=model_name, status=True).values_list('pk', flat=True)
  698. logs = MchInfoEncryptLogInfo.objects.filter(model_pk__in=models, sn__icontains=sn, status=True)
  699. if start_time and end_time:
  700. start_time = datetime.strptime(start_time, '%Y%m%d')
  701. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  702. logs = logs.filter(created_at__range=(start_time, end_time))
  703. logs = logs.order_by('-pk')
  704. count = logs.count()
  705. logs, left = pagination(logs, page, num)
  706. logs = [log.admindata for log in logs]
  707. return response(200, 'Get Warehouse Record Success', u'获取入库记录成功', data={
  708. 'logs': logs,
  709. 'left': left,
  710. 'count': count
  711. })
  712. def member_activity_list(request):
  713. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  714. admin_id = request.POST.get('admin_id', '')
  715. title = request.POST.get('title', '')
  716. start_time = request.POST.get('start_time', '')
  717. end_time = request.POST.get('end_time', '')
  718. page = request.POST.get('page', 1)
  719. num = request.POST.get('num', 20)
  720. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  721. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  722. try:
  723. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  724. except AdministratorInfo.DoesNotExist:
  725. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  726. logs = MemberActivityInfo.objects.filter(title__icontains=title)
  727. if start_time and end_time:
  728. start_time = datetime.strptime(start_time, '%Y%m%d')
  729. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  730. logs = logs.filter(created_at__range=(start_time, end_time))
  731. logs = logs.order_by('-pk')
  732. count = logs.count()
  733. logs, left = pagination(logs, page, num)
  734. logs = [log.admindata for log in logs]
  735. return response(200, 'Get Member Activity Record Success', u'获取会员活动成功', data={
  736. 'logs': logs,
  737. 'left': left,
  738. 'count': count
  739. })
  740. def member_activity_details(request):
  741. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  742. admin_id = request.POST.get('admin_id', '')
  743. activity_id = request.POST.get('activity_id', '')
  744. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  745. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  746. try:
  747. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  748. except AdministratorInfo.DoesNotExist:
  749. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  750. try:
  751. log = MemberActivityInfo.objects.get(activity_id=activity_id)
  752. except MemberActivityInfo.DoesNotExist:
  753. return response()
  754. log = log.admindetails
  755. return response(200, 'Get Member Activity Record Details Success', u'获取会员活动详情成功', data={
  756. 'log': log,
  757. })
  758. def member_activity_update(request):
  759. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  760. admin_id = request.POST.get('admin_id', '')
  761. activity_id = request.POST.get('activity_id', '')
  762. title = request.POST.get('title', '')
  763. subtitle = request.POST.get('subtitle', '')
  764. date = request.POST.get('date', '')
  765. city = request.POST.get('city', '')
  766. location = request.POST.get('location', '')
  767. integral = int(request.POST.get('integral', 0))
  768. cover_url = request.POST.get('cover_path', '')
  769. slider_image = request.POST.get('banner_path', '')
  770. image = request.POST.get('banner_path', '')
  771. is_signup = request.POST.get('is_signup', '')
  772. group_share_max_integral = int(request.POST.get('share_max_integral', 0))
  773. group_share_integral = int(request.POST.get('share_integral', 0))
  774. is_slider = request.POST.get('is_slider', '')
  775. content_rich_text = request.POST.get('content_rich_text', '')
  776. share_h5_link = request.POST.get('share_h5_link', '')
  777. activity_state = int(request.POST.get('activity_state', 0))
  778. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  779. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  780. try:
  781. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  782. except AdministratorInfo.DoesNotExist:
  783. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  784. try:
  785. log = MemberActivityInfo.objects.get(activity_id=activity_id, status=True)
  786. except MemberActivityInfo.DoesNotExist:
  787. return response()
  788. log.title = title
  789. log.subtitle = subtitle
  790. log.date = datetime.strptime(date + ' 23:59:59', '%Y-%m-%d %H:%M:%S')
  791. log.city = city
  792. log.location = location
  793. log.integral = integral
  794. log.cover = cover_url
  795. log.slider_image = slider_image
  796. log.image = slider_image
  797. log.is_signup = is_signup
  798. log.group_share_integral = group_share_integral
  799. log.group_share_max_integral = group_share_max_integral
  800. log.is_slider = is_slider
  801. log.content_rich_text = content_rich_text
  802. log.share_h5_link = share_h5_link
  803. log.activity_state = activity_state
  804. log.save()
  805. return response(200, 'MemberActivityInfo Update Success', u'会员活动更新成功')
  806. def member_activity_create(request):
  807. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  808. admin_id = request.POST.get('admin_id', '')
  809. title = request.POST.get('title', '')
  810. subtitle = request.POST.get('subtitle', '')
  811. date = request.POST.get('date', '')
  812. city = request.POST.get('city', '')
  813. location = request.POST.get('location', '')
  814. integral = int(request.POST.get('integral', 0))
  815. cover_url = request.POST.get('cover_path', '')
  816. slider_image = request.POST.get('banner_path', '')
  817. image = request.POST.get('banner_path', '')
  818. is_signup = request.POST.get('is_signup', '')
  819. group_share_max_integral = int(request.POST.get('share_max_integral', 0))
  820. group_share_integral = int(request.POST.get('share_integral', 0))
  821. is_slider = request.POST.get('is_slider', '')
  822. content_rich_text = request.POST.get('content_rich_text', '')
  823. share_h5_link = request.POST.get('share_h5_link', '')
  824. activity_state = int(request.POST.get('activity_state', 0))
  825. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  826. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  827. try:
  828. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  829. except AdministratorInfo.DoesNotExist:
  830. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  831. MemberActivityInfo.objects.create(
  832. brand_id=administrator.brand_id,
  833. title=title,
  834. subtitle=subtitle,
  835. date=date,
  836. city=city,
  837. location=location,
  838. integral=integral,
  839. cover=cover_url,
  840. slider_image=slider_image,
  841. image=image,
  842. is_signup=is_signup,
  843. group_share_max_integral=group_share_max_integral,
  844. group_share_integral=group_share_integral,
  845. is_slider=is_slider,
  846. content_rich_text=content_rich_text,
  847. share_h5_link=share_h5_link,
  848. activity_state=activity_state
  849. )
  850. return response(200, 'MemberActivityInfo Create Success', u'会员活动创建成功')
  851. def member_activity_share_list(request):
  852. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  853. admin_id = request.POST.get('admin_id', '')
  854. activity_id = request.POST.get('activity_id', '')
  855. page = int(request.POST.get('page', 1))
  856. num = int(request.POST.get('num', 20))
  857. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  858. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  859. try:
  860. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  861. except AdministratorInfo.DoesNotExist:
  862. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  863. logs = MemberActivityGroupShareInfo.objects.filter(activity_id=activity_id, status=True)
  864. total_integral = logs.aggregate(Sum('integral'))['integral__sum']
  865. logs = logs.values('share_user_id').annotate(integral_sum=Sum('integral'), group_count=Count('open_gid'), user_count=Count('click_user_id')).order_by('-user_count')
  866. count = logs.count()
  867. logs, left = pagination(logs, page, num)
  868. return response(200, 'Get Member Activity Record Success', u'获取会员活动成功', data={
  869. 'logs': list(logs),
  870. 'count': count,
  871. 'left': left,
  872. 'total_integral': total_integral
  873. })
  874. def member_activity_register_list(request):
  875. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  876. admin_id = request.POST.get('admin_id', '')
  877. activity_id = request.POST.get('activity_id', '')
  878. page = int(request.POST.get('page', 1))
  879. num = int(request.POST.get('num', 20))
  880. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  881. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  882. try:
  883. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  884. except AdministratorInfo.DoesNotExist:
  885. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  886. logs = MemberActivitySignupInfo.objects.filter(activity_id=activity_id, status=True)
  887. count = logs.count()
  888. logs, left = pagination(logs, page, num)
  889. logs = [log.admindata for log in logs]
  890. return response(200, 'Get Member Activity Record Success', u'获取会员活动成功', data={
  891. 'logs': list(logs),
  892. 'count': count,
  893. 'left': left,
  894. })
  895. def coupon_list(request):
  896. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  897. admin_id = request.POST.get('admin_id', '')
  898. title = request.POST.get('title', '')
  899. start_time = request.POST.get('start_time', '')
  900. end_time = request.POST.get('end_time', '')
  901. page = request.POST.get('page', 1)
  902. num = request.POST.get('num', 20)
  903. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  904. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  905. try:
  906. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  907. except AdministratorInfo.DoesNotExist:
  908. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  909. logs = CouponInfo.objects.filter(coupon_title__icontains=title, status=True)
  910. if start_time and end_time:
  911. start_time = datetime.strptime(start_time, '%Y%m%d')
  912. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  913. logs = logs.filter(created_at__range=(start_time, end_time))
  914. logs = logs.order_by('-pk')
  915. count = logs.count()
  916. logs, left = pagination(logs, page, num)
  917. logs = [log.admindata for log in logs]
  918. return response(200, 'Get CouponInfo List Success', u'获取劵列表成功', data={
  919. 'logs': logs,
  920. 'left': left,
  921. 'count': count
  922. })
  923. def coupon_details(request):
  924. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  925. admin_id = request.POST.get('admin_id', '')
  926. coupon_id = request.POST.get('coupon_id', '')
  927. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  928. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  929. try:
  930. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  931. except AdministratorInfo.DoesNotExist:
  932. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  933. try:
  934. log = CouponInfo.objects.get(coupon_id=coupon_id)
  935. except CouponInfo.DoesNotExist:
  936. return response()
  937. log = log.admindetails
  938. return response(200, 'Get Member Activity Record Details Success', u'获取会员活动详情成功', data={
  939. 'log': log,
  940. })
  941. def coupon_create(request):
  942. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  943. admin_id = request.POST.get('admin_id', '')
  944. coupon_title = request.POST.get('coupon_title', '')
  945. coupon_detail = request.POST.get('coupon_detail', '')
  946. coupon_value = int(request.POST.get('coupon_value', 0))
  947. coupon_image = request.POST.get('coupon_image', '')
  948. coupon_expire_type = int(request.POST.get('coupon_expire_type', 0))
  949. coupon_valid_period = int(request.POST.get('coupon_valid_period', 0))
  950. coupon_expire_at = request.POST.get('coupon_expire_at', '')
  951. is_coupon_admin_writeoff = int(request.POST.get('is_coupon_admin_writeoff', 1))
  952. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  953. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  954. try:
  955. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  956. except AdministratorInfo.DoesNotExist:
  957. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  958. info = CouponInfo.objects.create(
  959. brand_id=brand_id,
  960. coupon_title=coupon_title,
  961. coupon_detail=coupon_detail,
  962. coupon_value=coupon_value,
  963. coupon_image=coupon_image,
  964. coupon_expire_type=coupon_expire_type,
  965. coupon_valid_period=coupon_valid_period,
  966. is_coupon_admin_writeoff=is_coupon_admin_writeoff,
  967. )
  968. if coupon_expire_at:
  969. info.coupon_expire_at = datetime.strptime(coupon_expire_at + ' 23:59:59', '%Y-%m-%d %H:%M:%S')
  970. info.save()
  971. return response(200, 'CouponInfo Create Success', u'劵创建成功')
  972. def coupon_update(request):
  973. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  974. admin_id = request.POST.get('admin_id', '')
  975. coupon_id = request.POST.get('coupon_id', '')
  976. coupon_title = request.POST.get('coupon_title', '')
  977. coupon_detail = request.POST.get('coupon_detail', '')
  978. coupon_value = int(request.POST.get('coupon_value', 0))
  979. coupon_image = request.POST.get('coupon_image', '')
  980. coupon_expire_type = int(request.POST.get('coupon_expire_type', 0))
  981. coupon_valid_period = int(request.POST.get('coupon_valid_period', 0))
  982. coupon_expire_at = request.POST.get('coupon_expire_at', '')
  983. is_coupon_admin_writeoff = int(request.POST.get('is_coupon_admin_writeoff', 1))
  984. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  985. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  986. try:
  987. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  988. except AdministratorInfo.DoesNotExist:
  989. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  990. try:
  991. log = CouponInfo.objects.get(coupon_id=coupon_id, status=True)
  992. except CouponInfo.DoesNotExist:
  993. return response()
  994. log.brand_id = brand_id
  995. log.coupon_title = coupon_title
  996. log.coupon_detail = coupon_detail
  997. log.coupon_value = coupon_value
  998. log.coupon_image = coupon_image
  999. log.coupon_expire_type = coupon_expire_type
  1000. log.coupon_valid_period = coupon_valid_period
  1001. if coupon_expire_at:
  1002. log.coupon_expire_at = datetime.strptime(coupon_expire_at + ' 23:59:59', '%Y-%m-%d %H:%M:%S')
  1003. log.is_coupon_admin_writeoff = is_coupon_admin_writeoff
  1004. log.save()
  1005. return response(200, 'CouponInfo Update Success', u'劵更新成功')
  1006. def coupon_delete(request):
  1007. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1008. admin_id = request.POST.get('admin_id', '')
  1009. coupon_id = request.POST.get('activity_id', '')
  1010. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1011. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1012. try:
  1013. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1014. except AdministratorInfo.DoesNotExist:
  1015. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1016. try:
  1017. log = CouponInfo.objects.get(coupon_id=coupon_id, status=True)
  1018. except CouponInfo.DoesNotExist:
  1019. return response()
  1020. log.status = False
  1021. log.save()
  1022. return response(200, 'CouponInfo Delete Success', u'劵删除成功')
  1023. def member_goods_list(request):
  1024. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1025. admin_id = request.POST.get('admin_id', '')
  1026. title = request.POST.get('title', '')
  1027. page = request.POST.get('page', 1)
  1028. num = request.POST.get('num', 20)
  1029. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1030. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1031. try:
  1032. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1033. except AdministratorInfo.DoesNotExist:
  1034. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1035. logs = GoodsInfo.objects.filter(title__icontains=title, only_for_member=False).order_by('-pk')
  1036. count = logs.count()
  1037. logs, left = pagination(logs, page, num)
  1038. logs = [log.admindata for log in logs]
  1039. return response(200, 'Get Member Goods Success', u'获取会员商品成功', data={
  1040. 'logs': logs,
  1041. 'left': left,
  1042. 'count': count
  1043. })
  1044. def member_goods_details(request):
  1045. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1046. admin_id = request.POST.get('admin_id', '')
  1047. good_id = request.POST.get('good_id', '')
  1048. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1049. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1050. try:
  1051. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1052. except AdministratorInfo.DoesNotExist:
  1053. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1054. try:
  1055. log = GoodsInfo.objects.get(good_id=good_id)
  1056. except GoodsInfo.DoesNotExist:
  1057. return response()
  1058. log = log.admindetails
  1059. return response(200, 'Get Member Goods Details Success', u'获取积分商品详情成功', data={
  1060. 'log': log,
  1061. })
  1062. def member_goods_update(request):
  1063. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1064. admin_id = request.POST.get('admin_id', '')
  1065. good_id = request.POST.get('good_id', '')
  1066. title = request.POST.get('title', '')
  1067. desc = request.POST.get('desc', '')
  1068. value = int(request.POST.get('value', 0))
  1069. left_num = int(request.POST.get('left_num', 0))
  1070. image = request.POST.get('image_path', '')
  1071. coupon_id = request.POST.get('coupon_id', '')
  1072. slider_image = request.POST.get('slider_path', '')
  1073. integral = int(request.POST.get('integral', 0))
  1074. fee = int(request.POST.get('fee', 0))
  1075. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1076. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1077. try:
  1078. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1079. except AdministratorInfo.DoesNotExist:
  1080. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1081. try:
  1082. log = GoodsInfo.objects.get(good_id=good_id, status=True)
  1083. except GoodsInfo.DoesNotExist:
  1084. return response()
  1085. log.title = title
  1086. log.desc = desc
  1087. log.value = value
  1088. log.left_num = left_num
  1089. log.image = image
  1090. log.coupon_id = coupon_id
  1091. log.slider_image = slider_image
  1092. log.integral = integral
  1093. log.fee = fee
  1094. log.save()
  1095. return response(200, 'GoodsInfo Update Success', u'积分商品更新成功')
  1096. def member_goods_create(request):
  1097. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1098. admin_id = request.POST.get('admin_id', '')
  1099. good_type = int(request.POST.get('good_type', 0))
  1100. title = request.POST.get('title', '')
  1101. desc = request.POST.get('desc', '')
  1102. value = int(request.POST.get('value', 0))
  1103. left_num = int(request.POST.get('left_num', 0))
  1104. image = request.POST.get('image_path', '')
  1105. coupon_id = request.POST.get('coupon_id', '')
  1106. slider_image = request.POST.get('slider_path', '')
  1107. integral = int(request.POST.get('integral', 0))
  1108. fee = int(request.POST.get('fee', 0))
  1109. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1110. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1111. try:
  1112. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1113. except AdministratorInfo.DoesNotExist:
  1114. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1115. GoodsInfo.objects.create(
  1116. brand_id=administrator.brand_id,
  1117. good_type=good_type,
  1118. title=title,
  1119. desc=desc,
  1120. value=value,
  1121. left_num=left_num,
  1122. image=image,
  1123. coupon_id=coupon_id,
  1124. slider_image=slider_image,
  1125. integral=integral,
  1126. fee=fee,
  1127. )
  1128. return response(200, 'GoodsInfo Create Success', u'积分商品创建成功')
  1129. def complement_code_list(request):
  1130. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1131. admin_id = request.POST.get('admin_id', '')
  1132. page = request.POST.get('page', 1)
  1133. num = request.POST.get('num', 20)
  1134. start_time = request.POST.get('start_time', '')
  1135. end_time = request.POST.get('end_time', '')
  1136. type_ = request.POST.get('type', '')
  1137. query = request.POST.get('query', '')
  1138. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1139. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1140. try:
  1141. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1142. except AdministratorInfo.DoesNotExist:
  1143. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1144. logs = ComplementCodeLogInfo.objects.filter(status=True)
  1145. if start_time and end_time:
  1146. start_time = datetime.strptime(start_time, '%Y%m%d')
  1147. end_time = datetime.strptime(end_time + ' 23:59:59', '%Y%m%d %H:%M:%S')
  1148. logs = logs.filter(created_at__range=(start_time, end_time))
  1149. if type_ == 'todo':
  1150. logs = logs.filter(audit_status=ComplementCodeLogInfo.AUDIT_TODO)
  1151. elif type_ == 'pass':
  1152. logs = logs.filter(audit_status=ComplementCodeLogInfo.AUDIT_PASS)
  1153. elif type_ == 'refused':
  1154. logs = logs.filter(audit_status=ComplementCodeLogInfo.AUDIT_REFUSED)
  1155. logs = logs.order_by('audit_status', '-pk')
  1156. if query:
  1157. logs = logs.filter(Q(phone=query) | Q(name__icontains=query) | Q(sn=query))
  1158. count = logs.count()
  1159. logs, left = pagination(logs, page, num)
  1160. logs = [log.data for log in logs]
  1161. return response(200, 'Get Complement Code List Success', u'获取补码列表成功', data={
  1162. 'logs': logs,
  1163. 'left': left,
  1164. 'count': count
  1165. })
  1166. @transaction.atomic
  1167. def complement_code_audit(request):
  1168. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1169. admin_id = request.POST.get('admin_id', '')
  1170. log_id = request.POST.get('log_id', '')
  1171. audit_status = int(request.POST.get('audit_status', 0)) # -1 审核不通过, 1 审核通过
  1172. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1173. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1174. try:
  1175. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1176. except AdministratorInfo.DoesNotExist:
  1177. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1178. if audit_status not in [ComplementCodeLogInfo.AUDIT_REFUSED, ComplementCodeLogInfo.AUDIT_PASS]:
  1179. return response(ComplementCodeStatusCode.COMPLEMENT_CODE_STATUS_INVALID)
  1180. try:
  1181. log = ComplementCodeLogInfo.objects.select_for_update().get(log_id=log_id, status=True)
  1182. except ComplementCodeLogInfo.DoesNotExist:
  1183. return response(ComplementCodeStatusCode.COMPLEMENT_CODE_NOT_FOUND)
  1184. if log.audit_status != ComplementCodeLogInfo.AUDIT_TODO:
  1185. return response(ComplementCodeStatusCode.COMPLEMENT_CODE_HAS_AUDITED)
  1186. log.audit_status = audit_status
  1187. log.save()
  1188. # 如果"审核通过"
  1189. if log.audit_status == ComplementCodeLogInfo.AUDIT_PASS:
  1190. try:
  1191. brand = BrandInfo.objects.get(brand_id=brand_id)
  1192. except BrandInfo.DoesNotExist:
  1193. brand = None
  1194. try:
  1195. model = ModelInfo.objects.get(model_id=log.model_id)
  1196. except ModelInfo.DoesNotExist:
  1197. model = None
  1198. # brand_id#model_id#distributor_id#sn#time
  1199. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  1200. # brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  1201. plaintext = '{}#{}#{}#{}#{}'.format(
  1202. brand.pk if brand else 0,
  1203. model.pk if model else 0,
  1204. 0,
  1205. log.sn,
  1206. tc.local_string(format='%Y%m%d')[2:],
  1207. )
  1208. ciphertext = get_ciphertext(plaintext, admin_id, True)
  1209. log.ciphertext = ciphertext
  1210. log.save()
  1211. try:
  1212. user = UserInfo.objects.get(user_id=log.user_id, status=True)
  1213. except UserInfo.DoesNotExist:
  1214. return response()
  1215. # Send template_message
  1216. data = {
  1217. "first": {
  1218. "value": u'你的补码申请已通过,请点击消息绑定您的电子保修卡',
  1219. "color": "#173177"
  1220. },
  1221. "keyword1": {
  1222. "value": log.name,
  1223. "color": "#173177"
  1224. },
  1225. "keyword2": {
  1226. "value": log.model_name,
  1227. "color": "#173177"
  1228. },
  1229. "keyword3": {
  1230. "value": log.sn,
  1231. "color": "#173177"
  1232. },
  1233. "remark": {
  1234. "value": u'感谢您的使用',
  1235. "color": "#173177"
  1236. }
  1237. }
  1238. wxcfg = WECHAT.get('MINIAPP', {})
  1239. appid = wxcfg.get('appID')
  1240. sendtemplatemessage(openid=user.openid, template_id=settings.TEMPLATE_ID_COMPLEMENT, data=data, miniappid=appid, minipagepath='/pages/register/consumer/consumer?q={}&marketcode=1'.format(ciphertext))
  1241. return response(data={
  1242. 'ciphertext': ciphertext,
  1243. })
  1244. return response()
  1245. @transaction.atomic
  1246. def complement_code_contacted(request):
  1247. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1248. admin_id = request.POST.get('admin_id', '')
  1249. log_id = request.POST.get('log_id', '')
  1250. is_contacted = int(request.POST.get('is_contacted', 0)) # 0 未联系, 1 已联系
  1251. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1252. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1253. try:
  1254. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1255. except AdministratorInfo.DoesNotExist:
  1256. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1257. try:
  1258. log = ComplementCodeLogInfo.objects.select_for_update().get(log_id=log_id, status=True)
  1259. except ComplementCodeLogInfo.DoesNotExist:
  1260. return response(ComplementCodeStatusCode.COMPLEMENT_CODE_NOT_FOUND)
  1261. log.is_contacted = is_contacted
  1262. log.save()
  1263. return response()
  1264. def administrator_list(request):
  1265. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1266. admin_id = request.POST.get('admin_id', '')
  1267. page = request.POST.get('page', 1)
  1268. num = request.POST.get('num', 20)
  1269. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1270. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1271. try:
  1272. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1273. except AdministratorInfo.DoesNotExist:
  1274. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1275. if administrator.admin_type != AdministratorInfo.ADMINISTRATOR:
  1276. return response(AdministratorStatusCode.ADMINISTRATOR_PERMISSION_DENIED)
  1277. logs = AdministratorInfo.objects.filter(status=True).order_by('-created_at')
  1278. count = logs.count()
  1279. logs, left = pagination(logs, page, num)
  1280. logs = [log.admindata for log in logs]
  1281. return response(200, 'Get Admin List Success', u'获取后台管理员成功', data={
  1282. 'logs': logs,
  1283. 'left': left,
  1284. 'count': count
  1285. })
  1286. def administrator_create(request):
  1287. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1288. admin_id = request.POST.get('admin_id', '')
  1289. admin_type = request.POST.get('admin_type', '')
  1290. phone = request.POST.get('phone', '')
  1291. name = request.POST.get('name', '')
  1292. password = request.POST.get('password', '')
  1293. brand_name = request.POST.get('brand_name', '')
  1294. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1295. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1296. try:
  1297. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1298. except AdministratorInfo.DoesNotExist:
  1299. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1300. if administrator.admin_type != AdministratorInfo.ADMINISTRATOR:
  1301. return response(AdministratorStatusCode.ADMINISTRATOR_PERMISSION_DENIED)
  1302. encryption = make_password(strip(password), settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)
  1303. AdministratorInfo.objects.create(
  1304. brand_id=brand_id,
  1305. brand_name=brand_name,
  1306. admin_type=admin_type,
  1307. phone=phone,
  1308. name=name,
  1309. password='',
  1310. encryption=encryption,
  1311. )
  1312. return response(200, 'Create Admin Success', u'创建后台管理员成功')
  1313. def administrator_update(request):
  1314. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1315. admin_id = request.POST.get('admin_id', '')
  1316. target_admin_id = request.POST.get('target_admin_id', '')
  1317. admin_type = int(request.POST.get('admin_type', 3))
  1318. phone = request.POST.get('phone', '')
  1319. name = request.POST.get('name', '')
  1320. password = request.POST.get('password', '')
  1321. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1322. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1323. try:
  1324. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1325. except AdministratorInfo.DoesNotExist:
  1326. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1327. if administrator.admin_type != AdministratorInfo.ADMINISTRATOR:
  1328. return response(AdministratorStatusCode.ADMINISTRATOR_PERMISSION_DENIED)
  1329. encryption = make_password(strip(password), settings.MAKE_PASSWORD_SALT, settings.MAKE_PASSWORD_HASHER)
  1330. AdministratorInfo.objects.filter(admin_id=target_admin_id, brand_id=brand_id).update(
  1331. admin_type=admin_type,
  1332. phone=phone,
  1333. name=name,
  1334. password='',
  1335. encryption=encryption,
  1336. )
  1337. return response(200, 'Update Admin Success', u'更新后台管理员成功')
  1338. def administrator_delete(request):
  1339. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  1340. admin_id = request.POST.get('admin_id', '')
  1341. target_admin_id = request.POST.get('target_admin_id', '')
  1342. if brand_id != settings.KODO_DEFAULT_BRAND_ID:
  1343. return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
  1344. try:
  1345. administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
  1346. except AdministratorInfo.DoesNotExist:
  1347. return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
  1348. if administrator.admin_type != AdministratorInfo.ADMINISTRATOR:
  1349. return response(AdministratorStatusCode.ADMINISTRATOR_PERMISSION_DENIED)
  1350. AdministratorInfo.objects.filter(admin_id=target_admin_id).update(status=False)
  1351. return response(200, 'Delete Admin Success', u'删除后台管理员成功')