def getRoles(request): if request.method == 'GET': req = request.GET code = 0 page = int(req.get('page', 1)) size = int(req.get('size', 10)) start = (page - 1) * size end = start + size total = 0 message = None roles = [] ids = req.getlist('ids', []) logging.info("ids is %s", ids) try: # 根据ids获取数据 if len(ids) == 0: roles = Role.objects.all() pass else: roles = Role.objects.filter(id__in=ids) total = roles.count() roles = list(roles[start:end]) logging.info("modules size is %d", len(roles)) pass except Exception as e: logging.error(e) traceback.print_exc() code = -10000 pass finally: return set_list_response(code, message, roles, page, size, total) else: return
def getBannerList(request): if request.method == 'GET': logging.info("get banner list start") code = 0 bannerList = [] req = request.GET page = int(req.get('page', 1)) size = int(req.get('size', 10)) start = (page - 1) * size end = start + size total = 0 try: fields = [f.name for f in Banner._meta.fields] kwargs = DynamicQuery(request, [], *fields) logging.info('kwargs is %s , page is %d ,size is %d', kwargs, page, size) bannerList = Banner.objects.filter(**kwargs).order_by( '-status', '-updateAt') total = bannerList.count() bannerList = list(bannerList[start:end]) except Exception as e: logging.error(e) code = -10000 finally: return set_list_response(code, None, bannerList, page, size, total) else: return
def getMediumLinkList(request, mediumId): logging.info('API is /admin/u/medium/list ,get mediumList begin :') code = 0 mediumLinkList = [] total = 0 if request.method == 'GET': req =request.GET try: # fields = [f.name for f in MediumLink._meta.fields] # kwargs = DynamicQuery(request, ['link'], *fields) kwargs = {} link = req.get("link",None) logging.info("link is %s", link) if link: kwargs = {"link__contains": unquote(link)} logging.info("kwargs is %s", kwargs) mediumLinkList = MediumLink.objects.filter(**kwargs, mediumId=mediumId) total = mediumLinkList.count() logging.info("link count is %d", total) mediumLinkList = list(mediumLinkList) except Exception as e: logging.error(e) code = -10000 finally: return set_list_response(code, None, mediumLinkList, 1, 10000, total) else: return
def getMediumList(request): if request.method == "GET": logging.info('API is /admin/u/medium/list ,get mediumList begin :') code = 0 if request.method == 'GET': mediumList = [] req = request.GET page = int(req.get('page', 1)) size = int(req.get('size', 10)) start = (page - 1) * size end = start + size total = 0 try: fields = [f.name for f in Medium._meta.fields] kwargs = DynamicQuery(request, ['name'], *fields) logging.info('kwargs is %s , page is %d ,size is %d', kwargs, page, size) mediumList = Medium.objects.filter(**kwargs).order_by('sort', '-createAt') total = mediumList.count() logging.info("total is %d", total) mediumList = list(mediumList[start:end]) except Exception as e: logging.error(e) code = -10000 finally: return set_list_response(code, None, mediumList, page, size, total) else: return
def mediumStatistics(request): logging.info('get medium statistics begin :') code = 0 req = request.GET page = int(req.get('page', 1)) size = int(req.get('size', 10)) start = (page - 1) * size end = start + size total = 0 mediumStatisticsList = [] message = None newTotal = 0 oldTotal = 0 newUserIds = [] oldUserIds = [] if request.method == 'GET': try: fields = [f.name for f in Medium_Statistics._meta.fields] kwargs = DynamicQuery(request, [], *fields) startTime = req.get("startTime", None) endTime = req.get("endTime", None) if startTime: kwargs.__setitem__("createAt__gte", startTime) if endTime: kwargs.__setitem__("createAt__lte", endTime) result = Medium_Statistics.objects.filter(**kwargs).order_by('-createAt') total = result.count() logging.info('result size is %d ', total) # 统计人数 for statistics in result: newUserIds += json.loads(statistics.newIds) oldUserIds += json.loads(statistics.oldIds) logging.info("new user ids is %s",newUserIds) logging.info("old user ids is %s",oldUserIds) # 新旧用户去重复处理 # 新用户数量为 新用户数组中排除掉老用户数组中的id newUserIds = set(newUserIds).difference(set(oldUserIds)) newTotal = len(newUserIds) # 老用户数量为旧用户数组去重 oldUserIds = set(oldUserIds) oldTotal = len(oldUserIds) mediumStatisticsList = list(result[start:end]) except Exception as e: logging.error(e) traceback.print_exc() code = -10000 finally: sup = { 'newTotal': newTotal, 'newIds': list(newUserIds), 'oldTotal': oldTotal, 'oldIds': list(oldUserIds) } return set_list_response(code, message, mediumStatisticsList, page, size, total, **sup) else: return
def getManagerList(request): if request.method == 'GET': req = request.GET code = 0 page = int(req.get('page', 1)) size = int(req.get('size', 10)) total = 0 start = (page - 1) * size end = start + size logging.info("start is %s,end is %s", start, end) message = None managers = [] ids = req.getlist('ids', []) fields = [f.name for f in Manager._meta.fields] container = ['name'] kwargs = DynamicQuery(request, container, *fields) try: # 根据ids获取数据 if len(ids) == 0: managers = Manager.objects.filter(**kwargs) pass else: managers = Manager.objects.filter(id__in=ids) total = managers.count() managers = list(managers[start:end]) logging.info("managers size is %d", len(managers)) return set_list_response(code, message, managers, page, size, total) except Exception as e: logging.error(e) traceback.print_exc() code = -10000 return set_list_response(code, message, managers, page, size, total) else: return
def getProductStatistics(request): if request.method == "GET": code = 0 req = request.GET page = int(req.get('page', 1)) size = int(req.get('size', 10)) start = (page - 1) * size end = start + size total = 0 statisticsList = [] orderTotal = 0 message = None try: fields = [f.name for f in Product_Statistics._meta.fields] kwargs = DynamicQuery(request, [], *fields) startTime = req.get("startTime", None) endTime = req.get("endTime", None) if startTime: kwargs.__setitem__("createAt__gte", startTime) if endTime: kwargs.__setitem__("createAt__lte", endTime) logging.info("kwargs is %s", kwargs) result = Product_Statistics.objects.filter( **kwargs).order_by('-createAt') total = result.count() orderTotal = 0 # 当前条件总单数 for statistics in result: orderTotal += statistics.orderCount logging.info('result size is %d ,orderTotal is %s', total, orderTotal) statisticsList = list(result[start:end]) pass except Exception as e: logging.error(e) code = -10000 pass finally: sup = {'orderTotal': orderTotal} return set_list_response(code, message, statisticsList, page, size, total, **sup) pass else: return
def getRecommendProductStatistics(request): if request.method == "GET" or request.method == "POST": code = 0 req = request.GET page = int(req.get('page', 1)) size = int(req.get('size', 10)) start = (page - 1) * size end = start + size total = 0 statisticsList = [] message = None try: startTime = str(req.get("startTime", getTodayZeroTime())) endTime = str(req.get("endTime", getTimeStamp())) productId = req.get("productId", None) logging.info("startTime is %s, endTime is %s, productId is %s", startTime, endTime, productId) sql = "1" if startTime: sql += " and rs.create_at >= " + str(startTime) if endTime: sql += " and rs.create_at <= " + str(endTime) if productId: sql += " and rs.product_id = " + productId logging.info("sql is %s", sql) result = Recommend_Statistics.objects.raw( "select rs.id,rs.medium_id,rs.medium_name,rs.product_id,rs.product_name,rs.link_id," "(select count(distinct(loan_record.user_id)) from loan_record where rs.product_id=loan_record.product_id and rs.link_id=loan_record.link_id) uv," "sum(rs.pv) as pv,rs.update_at,rs.create_at " "from loan_recommend_statistics as rs where " + sql + " group by rs.product_id,rs.link_id") total = len(list(result)) logging.info('result size is %d', total) statisticsList = list(result[start:end]) if req.get('output') == '1': # 指定返回为excel文件 response = HttpResponse( content_type='application/vnd.ms-excel') response[ 'Content-Disposition'] = 'attachment;filename=statistics.xls' # 创建工作簿 workbook = xlwt.Workbook(encoding='utf-8') # 创建工作页 sheet = workbook.add_sheet("sheet1") row0 = [ u'序号', u'日期', u'推荐客户名称', u'媒介名称', u'链接ID', u'UV', u'PV' ] for i in range(0, len(row0)): sheet.write(0, i, row0[i]) num = 1 logging.info( "date is %s", timeStampFormatDate(int(startTime), '%Y-%m-%d') + "~" + timeStampFormatDate(int(endTime), '%Y-%m-%d')) for d in list(result): sheet.write(num, 0, num) sheet.write( num, 1, timeStampFormatDate(int(startTime), '%Y-%m-%d') + "~" + timeStampFormatDate(int(endTime), '%Y-%m-%d')) sheet.write(num, 2, d.productName) sheet.write(num, 3, d.mediumName) sheet.write(num, 4, d.linkId) sheet.write(num, 5, d.uv) sheet.write(num, 6, d.pv) num = num + 1 workbook.save(response) return response else: sup = {'startTime': startTime, 'endTime': endTime} return set_list_response(code, message, statisticsList, page, size, total, **sup) except Exception as e: logging.error(e) traceback.print_exc() code = -10000 logging.info("startTime = getTimeStamp() %s", startTime) sup = {'startTime': startTime, 'endTime': endTime} return set_list_response(code, message, statisticsList, page, size, total, **sup) pass else: return
def getProductList(request): logging.info("get product list start") if request.method == 'GET': req = request.GET code = 0 page = req.get('page', 1) size = req.get('size', 10) total = 0 productList = [] try: target = None targetId = None # 需要进行模糊搜索的参数 containsParams = ['name'] fields = [f.name for f in Product._meta.fields] kwargs = DynamicQuery(request, containsParams, *fields) linkId = req.get('linkId', None) mediumId = req.get('mediumId', None) if linkId or mediumId: if linkId: logging.info("is link sort") targetId = linkId target = MediumLink.objects.filter(id=linkId).first() pass else: logging.info("is medium sort") targetId = mediumId target = Medium.objects.filter(id=mediumId).first() pass logging.info("query params is %s", kwargs) productList = Product.objects.filter(**kwargs).order_by( 'sort', '-createAt') # 取出当前客户列表ids,避免出现新加了客户,但是不在排序列表里的情况出现,这里把排序里不存在的id push进去 productIds = list(productList.values_list('id', flat=True)) logging.info("product ids size is %d", len(productIds)) logging.info("targetId is %s ", targetId) # 处理不同媒介、链接的排序,排序只能生效一种,链接优先 if target: sort = [] if req.get('type', None) == '1' and target.productSort1: sort = json.loads(target.productSort1) pass elif req.get('type', None) == '2' and target.productSort2: sort = json.loads(target.productSort2) pass newList = [] # 开始把取出的结果按照载体顺序进行排序 if len(sort) > 0: # 把sort里没有的客户id推进去 if len(productIds) > len(sort): logging.info("Sort missing some id, should be push ") differenceIds = list( set(productIds).difference(set(sort))) logging.info("difference is %s", differenceIds) sort.extend(differenceIds) productList = dict( (product.id, product) for product in productList) logging.info("dict product is %s", productList) # productList = [productList[pid] for pid in sort] for productId in sort: logging.info("productList item is %s , id is %s", productList.get(productId), productId) # 先判断排序里的id是不是存在,可能出现改id被删除的情况,有的话就直接跳过 if productList.get(productId): newList.append(productList[productId]) pass else: logging.info("item not found ,id is %s", productId) pass pass productList = newList logging.info("new list is %s", newList) pass else: # QuerySet to list logging.info("queryset to list %s", productList) productList = list(productList) pass logging.info("sort size is %s", len(productList)) pass else: total = productList.count() logging.info("list size is " + productList.count().__str__()) productList = list(productList) pass pass except Exception as e: logging.error(e) traceback.print_exc() code = -10000 pass finally: return set_list_response(code, None, productList, page, size, total=total) else: return
def getProductList(request): logging.info("get product list start") if request.method == 'GET': productList = [] req = request.GET page = int(req.get('page', 1)) size = int(req.get('size', 10)) total = 0 code = 0 logging.info("page is " + page.__str__() + " size is " + size.__str__()) start = (page - 1) * size end = start + size try: ProductForm(request.GET) containsParams = ['name'] fields = [f.name for f in Product._meta.fields] kwargs = DynamicQuery(request, containsParams, *fields) logging.info("query params is " + kwargs.__str__()) # 取客户数据 productList = Product.objects.filter(**kwargs, status=1).order_by( 'sort', '-createAt') productIds = list(productList.values_list('id', flat=True)) logging.info("product ids is %s", productIds) total = productList.count() logging.info("list size is " + productList.count().__str__()) # 取用户信息 user = json.loads(request.__getattribute__('user')) linkId = user.get('linkId') # 取对应链接 link = MediumLink.objects.filter(id=linkId).first() if link: logging.info("linkId is %d", linkId) # 判断该链接是否有排序 if link.productSort1 or link.productSort2: type = req.get('type', None) productSort = [] # 获取链接下客户排序id list if type == 1: logging.info("link.productSort2 is %s", link.productSort1) if link.productSort1: productSort = json.loads(link.productSort1) else: logging.info("link.productSort2 is %s", link.productSort2) if link.productSort2: productSort = json.loads(link.productSort2) logging.info("productSort is %s", productSort) newList = [] # 当链接下有排序数据时,进行排序 if len(productSort) > 0: if len(productIds) > len(productSort): logging.info( "Sort missing some id, should be push ") differenceIds = list( set(productIds).difference(set(productSort))) logging.info("difference is %s", differenceIds) productSort.extend(differenceIds) # 按 id 入参顺序进行排序 logging.info("sort start") productList = dict([(product.id, product) for product in productList]) for productId in productSort: logging.info("productList item is %s", productList.get(productId)) if productList.get(productId, None): newList.append(productList[productId]) pass else: logging.info("item not found ,id is %s", productId) pass pass productList = newList logging.info("new list is %s", newList) # 链接下没有拍数据时 按照 else: # QuerySet to list logging.info("queryset to list %s", productList) # productList = list(productList) pass pass else: logging.info("this link productSort is null") pass productList = list(productList) allList = productList.copy() # 输出新排序 只有当type是分单的时候才进行切割处理 if req.get('type') == '1': # 用户当前看到的排序下标 productIndex = user.get('productIndex') logging.info('user linkId is %s productIndex is %s', linkId, productIndex) ''' 根据下标除以数据量取余,判断是否需要进行数据填充 ''' if productIndex > total: n = productIndex % total else: n = productIndex # 预补一次数据用以展示 # productList.extend(productList) if len(productList) > 3: productList.extend(productList) productList = list(productList[n:n + size]) pass else: pass logging.info("list size is %s", len(productList)) # 将用户分发给客户, 获取N值 content = Contents.objects.get(id=1) valueN = int(content.value) logging.info("content N is %d", valueN) # 每次登录根据用户登录次数和n值取推送的数据进行推送 allList.extend(allList) pushList = list(allList[n:n + valueN]) logging.info("push limit is %d - %d", n, n + valueN) # 调用数据推送方法 distributionOrder(request, uid=user.get('id'), productList=pushList) pass # type为推荐的时候不切割数据 else: productList = list(productList[start:end]) logging.info("list size is %s", len(productList)) logging.info("list is %s", productList) except Exception as e: logging.error(e) traceback.print_exc() code = -10000 finally: return set_list_response(code, None, productList, page, size, total=total) pass else: return
def getUserList(request): logging.info("get user list start") logging.info("method -%s", request.method) if request.method == "GET" or request.method == "POST": code = 0 req = request.GET or request.POST page = int(req.get("page", 1)) size = int(req.get("size", 10)) total = 0 ids = req.getlist('ids', []) # 处理搜索入参 try: fields = [field.name for field in User._meta.fields] kwargs = DynamicQuery(request, ['mobile'], *fields) # 注册时间 registerStart = req.get("registerStart", None) registerEnd = req.get("registerEnd", None) userType = req.get("userType", None) logging.info("userType is %s", userType) doneStart = req.get("doneStart", None) doneEnd = req.get("doneEnd", None) productId = req.get("productId", None) logging.info("productId is %s", productId) # 搜新注册用户 linkParams = {} if registerStart: kwargs.__setitem__("createAt__gte", registerStart) linkParams.__setitem__("createAt__gte", registerStart) if registerEnd: kwargs.__setitem__("createAt__lte", registerEnd) linkParams.__setitem__("createAt__lte", registerEnd) # 搜索某段时间内注册的1新用户2旧用户3总用户 4推荐统计用户 if userType == '1': linkParams.__setitem__("linkId", req.get("linkId")) logging.info("search new user by %s", linkParams) medium_Statistics = Medium_Statistics.objects.filter( **linkParams).first() if medium_Statistics: newIds = json.loads(medium_Statistics.newIds) ids = ids + newIds kwargs.__setitem__("id__in", newIds) pass pass # 搜索所有用户,直接用统计里取两者之和返回 elif userType == '3': allParams = {} allStart = req.get("allStart", None) allEnd = req.get("allStart", None) if allStart: allParams.__setitem__("createAt__gte", allStart) if doneEnd: allParams.__setitem__("createAt__lte", allEnd) allParams.__setitem__("linkId", req.get("linkId")) logging.info("search old user by %s", allParams) medium_Statistics = Medium_Statistics.objects.filter( **allParams).first() if medium_Statistics: newIds = json.loads(medium_Statistics.newIds) oldIds = json.loads(medium_Statistics.oldIds) allIds = newIds + oldIds ids = ids + allIds kwargs.__setitem__("id__in", ids) pass pass # 搜索推荐统计的用户,timeStart/end查询主体为推荐客户访问记录表,查出相关的ids列表 elif userType == '4': recommendParams = {} statisticsStart = req.get("statisticsStart", None) statisticsEnd = req.get("statisticsEnd", None) if statisticsStart: recommendParams.__setitem__("createAt__gte", statisticsStart) if statisticsEnd: recommendParams.__setitem__("createAt__lte", statisticsEnd) recommendId = req.get("recommendId", None) linkId = req.get("linkId", None) recommendParams.__setitem__("productId", recommendId) recommendParams.__setitem__("linkId", linkId) logging.info("recommendParams is %s", recommendParams) record = Record.objects.filter(**recommendParams).values_list( "userId", flat=True) logging.info("record is %s", list(record)) kwargs.__setitem__('id__in', list(record)) kwargs.__delitem__('linkId') pass # 搜填单老用户 if doneStart: kwargs.__setitem__("doneAt__gte", doneStart) if doneEnd: kwargs.__setitem__("doneAt__lte", doneEnd) # 用户分单归属 if productId: p_kwargs = { 'productId': productId, } startTime = req.get('startTime', None) endTime = req.get('endTime', None) if startTime: p_kwargs.__setitem__("createAt__gte", startTime) if endTime: p_kwargs.__setitem__("createAt__lte", endTime) logging.info("p_kwargs is %s ", p_kwargs) p_uid = Product_User_Relation.objects.filter( **p_kwargs).values_list('userId', flat=True) ids = ids + list(p_uid) kwargs.__setitem__('id__in', ids) else: pass ids = list(set(ids)) logging.info("ids is %s", ids) if len(ids) > 0: kwargs.__setitem__('id__in', ids) logging.info("kwargs is %s", kwargs) userList = User.objects.filter(**kwargs).order_by("-createAt") # 所有媒介相关ids mediumIds = userList.values_list('source', flat=True) # 导出list参数 outputList = userList userIds = list(userList.values_list( 'id', flat=True))[(page - 1) * size:(page - 1) * size + size] logging.info("user ids is %s", userIds) total = userList.count() userList = userList[(page - 1) * size:(page - 1) * size + size] # 取媒介信息 returnMediumIds = list(userList.values_list('source', flat=True)) logging.info("return ids is %s", returnMediumIds) # 在列表中返回给前端的数据 returnMediumList = Medium.objects.filter(id__in=returnMediumIds) logging.info("return medium list size is %s", returnMediumList.count()) returnMediumList = list(medium.to_dire() for medium in returnMediumList) for user in userList: user.mobile = strEncrypted(user.mobile, 3, 7) userList = list(user.to_dire() for user in userList) logging.info("user list size is %d", len(userList)) # 查分单信息 relations = Product_User_Relation.objects.filter( userId__in=userIds) logging.info("relations size is %d", relations.count()) # 拼接用户归属信息 productInfo = {} for relation in relations: if productInfo.get(relation.userId): productInfo[relation.userId].append(relation.productName) productInfo[relation.userId] = list( set(productInfo[relation.userId])) pass else: productInfo[relation.userId] = [relation.productName] obj = { 'userList': userList, 'productInfo': productInfo, 'mediumList': returnMediumList } from loan.constant import constant r = { 'code': code, 'message': constant.code[code], 'page': page, 'size': size, 'total': total, 'data': obj } obj = json.dumps(r, cls=DecimalEncoder) # 导出操作 if req.get('output') == '1': mediumList = Medium.objects.filter(id__in=mediumIds) mediumDirt = dict((medium.id, medium) for medium in mediumList) logging.info("mediumDirt is %s", mediumDirt) # 指定返回为excel文件 response = HttpResponse( content_type='application/vnd.ms-excel') response[ 'Content-Disposition'] = 'attachment;filename=users.xls' # 创建工作簿 workbook = xlwt.Workbook(encoding='utf-8') # 创建工作页 sheet = workbook.add_sheet("sheet1") row0 = [ u'id', u'姓名', u'电话', u'房产', u'汽车', u'信用卡', u'寿单', u'年保费', u'居住时间', u'职业', u'注册时间', u'媒介来源', u'媒介链接', u'客户归属' ] for i in range(0, len(row0)): sheet.write(0, i, row0[i]) num = 1 for d in outputList.values(): sheet.write(num, 0, d['id']) sheet.write(num, 1, d['nick']) sheet.write(num, 2, strEncrypted(d['mobile'], 3, 7)) sheet.write( num, 3, constant.userContent.get('estate').get(d['estate'])) sheet.write(num, 4, constant.userContent.get('car').get(d['car'])) sheet.write( num, 5, constant.userContent.get('card').get(d['card'])) sheet.write( num, 6, constant.userContent.get('lifeInsurance').get( d['lifeInsurance'])) sheet.write( num, 7, constant.userContent.get('feeInsurance').get( d['feeInsurance'])) sheet.write( num, 8, constant.userContent.get('livingTime').get( d['livingTime'])) sheet.write( num, 9, constant.userContent.get('profession').get( d['profession'])) sheet.write(num, 10, timeStampToDate(d['createAt'])) if mediumDirt.get(d['source']): sheet.write(num, 11, mediumDirt.get(d['source']).name) pass else: pass sheet.write(num, 12, d['sourceLink']) if productInfo.get(d['id'], None): sheet.write( num, 13, ",".join(iter(productInfo.get(d['id'], None)))) pass num = num + 1 workbook.save(response) return response pass return HttpResponse(obj, content_type="application/json") except Exception as e: logging.error(e) traceback.print_exc() code = -10000 return set_list_response(code, None, {}, page, size, total) finally: pass else: return