def bankcard_approve(rst_id, bankcard_id): args = _get_bankcard_args() args.update({ 'id': bankcard_id, "status": RstBankCard.STATUS_VALID, "rst_id": rst_id }) rst_admin = get_rst_admin(rst_id) current_zeus_bankcard = None mobile = None if rst_admin: current_zeus_bankcard = \ user_base.get_bankcard(rst_admin.get('user_id')) mobile = rst_admin.get("mobile") try: if rst_admin: _unbind_admin_mobile(rst_admin.get('user_id')) _unbind_admin_bankcard(rst_admin.get('user_id')) bankcard_base.super_user_bind(username=args['username'], mobile=args['mobile'], restaurant_id=rst_id, bank_id=args['bank_id'], card_id=args['card_id'], cardholder_name=args['cardholder_name'], process_user_id=current_user.id) _current_to_history(rst_id) bankcard_base.update_bankcard(**args) bankcard_base.add_bankcard_processing_record( rst_id=rst_id, bankcard_id=bankcard_id, process_user_id=current_user.id, messages=u'审核通过该银行卡信息', status_to=RstBankCard.STATUS_VALID, ) rst = rst_base.get(rst_id) bank = bankcard_base.get_bank(int(args['bank_id'])) async .send_task(send_bankcard_approve_sms, phone=args['mobile'], params={ "restaurant_name": rst.get('name'), "card_holder": args["cardholder_name"], "bank": bank.bank_name, "card_tail_number": args["card_id"][-4:] }) except thirdparty_svc.eus.EUSUserException as e: # rollback bankcard and mobile if mobile and rst_admin.get("is_mobile_valid") == 1: user_base.bind_mobile(rst_admin.get("user_id"), mobile) if current_zeus_bankcard: user_base.bankcard_bind( user_id=current_zeus_bankcard.user_id, cardholder_name=current_zeus_bankcard.cardholder_name, bank_id=current_zeus_bankcard.bank_id, card_id=current_zeus_bankcard.card_id) raise_user_exc(BANKCARD_APPROVE_ERR, error_msg=e.message)
def preprocess_pay_records(pay_records): """ Preprocess : assemble pay_records attributes """ pay_record_infos = [] for record in pay_records: if record[1] is None: continue restaurant = rst_base.get(record[1]) act_stats = get_activity_stats(record[0]) for act_stat in act_stats: activity_name = pay_base.get_activity_name(act_stat[0], act_stat[1]) try: pay_record_infos.append({ 'record_id': record[0], 'restaurant_id': record[1], 'phone': _get_restaurant_phone(restaurant), 'activity_name': activity_name, 'first_date': date_format(act_stat[3]), 'last_date': date_format(act_stat[4]), 'amount': act_stat[5], 'total_subsidy': act_stat[2], 'process_date': datetime_format( record[3]), 'card_num_tail': record[2][-4:], 'restaurant_name': sms_content_filter( restaurant.name), }) except Exception as e: log.error(e) continue return pay_record_infos
def bankcard_approve(rst_id, bankcard_id): args = _get_bankcard_args() args.update({'id': bankcard_id, "status": RstBankCard.STATUS_VALID, "rst_id": rst_id}) rst_admin = get_rst_admin(rst_id) current_zeus_bankcard = None mobile = None if rst_admin: current_zeus_bankcard = \ user_base.get_bankcard(rst_admin.get('user_id')) mobile = rst_admin.get("mobile") try: if rst_admin: _unbind_admin_mobile(rst_admin.get('user_id')) _unbind_admin_bankcard(rst_admin.get('user_id')) bankcard_base.super_user_bind( username=args['username'], mobile=args['mobile'], restaurant_id=rst_id, bank_id=args['bank_id'], card_id=args['card_id'], cardholder_name=args['cardholder_name'], process_user_id=current_user.id) _current_to_history(rst_id) bankcard_base.update_bankcard(**args) bankcard_base.add_bankcard_processing_record( rst_id=rst_id, bankcard_id=bankcard_id, process_user_id=current_user.id, messages=u'审核通过该银行卡信息', status_to=RstBankCard.STATUS_VALID, ) rst = rst_base.get(rst_id) bank = bankcard_base.get_bank(int(args['bank_id'])) async.send_task(send_bankcard_approve_sms, phone=args['mobile'], params={"restaurant_name": rst.get('name'), "card_holder": args["cardholder_name"], "bank": bank.bank_name, "card_tail_number": args["card_id"][-4:]}) except thirdparty_svc.eus.EUSUserException as e: # rollback bankcard and mobile if mobile and rst_admin.get("is_mobile_valid") == 1: user_base.bind_mobile(rst_admin.get("user_id"), mobile) if current_zeus_bankcard: user_base.bankcard_bind(user_id=current_zeus_bankcard.user_id, cardholder_name=current_zeus_bankcard.cardholder_name, bank_id=current_zeus_bankcard.bank_id, card_id=current_zeus_bankcard.card_id) raise_user_exc(BANKCARD_APPROVE_ERR, error_msg=e.message)
def preprocess_pay_records(pay_records): """ Preprocess : assemble pay_records attributes """ pay_record_infos = [] for record in pay_records: if record[1] is None: continue restaurant = rst_base.get(record[1]) act_stats = get_activity_stats(record[0]) for act_stat in act_stats: activity_name = pay_base.get_activity_name(act_stat[0], act_stat[1]) try: pay_record_infos.append({ 'record_id': record[0], 'restaurant_id': record[1], 'phone': _get_restaurant_phone(restaurant), 'activity_name': activity_name, 'first_date': date_format(act_stat[3]), 'last_date': date_format(act_stat[4]), 'amount': act_stat[5], 'total_subsidy': act_stat[2], 'process_date': datetime_format(record[3]), 'card_num_tail': record[2][-4:], 'restaurant_name': sms_content_filter(restaurant.name), }) except Exception as e: log.error(e) continue return pay_record_infos
def query_suspicous(): args_spec = { 'start_datetime':Arg(unicode, default=arrow.now().replace(days=-1).__str__()), 'end_datetime':Arg(unicode, default=arrow.now().__str__()), 'restaurant_id':Arg(int), 'offset':Arg(int,default=0), 'limit':Arg(int,default=100), } args = args_parser.parse(args_spec) if not args['restaurant_id']: return { 'total':0, 'objects':[], } client = pymongo.MongoClient(config.EYES_MONGO) collection = client.evileye.suspicious_order # db.tickets.find({"date":{$lt:ISODate("2013-01-17T01:16:33.303Z")}}).limit (5); start_datetime = args['start_datetime'].split('+')[0] end_datetime = args['end_datetime'].split('+')[0] start_datetime = arrow.get(start_datetime).to('local').datetime end_datetime = arrow.get(end_datetime).to('local').datetime restaurant_id = args['restaurant_id'] offset = args['offset'] limit = args['limit'] query = { 'created_at':{'$lte':end_datetime,'$gte':start_datetime}, } if restaurant_id: query.update({'restaurant_id':restaurant_id}) docs = collection.find(query) total = docs.count() result_docs = list(docs.limit(limit).skip(offset)) time_fields = ['deliver_time','settled_at'] with thrift_client('eos') as eos: for doc in result_docs: order_id = doc['_id'] order = eos.get(int(order_id)) order = getresult_to_raw(order) for k,v in order.items(): doc.setdefault(k,v) doc['detail_json'] = json.loads(doc['detail_json']) doc['_order_id'] = doc['_id'] for field in time_fields: new_value = arrow.get(doc[field]).__str__() doc[field] = new_value doc['phone'] = [doc['phone']] with thrift_client('eus') as eus_client: chaos = eus_client.get_order_payment_constitution_map([int(order_id)]) payment_constituion = chaos.get(order['id'],[]) doc['payment_constituion'] = order_query_helper.whatzfvcksay(payment_constituion,doc['total']) doc['order_id'] = unicode(doc['_id']) order_ids = [doc['id'] for doc in result_docs] with thrift_client('eyes') as eyes_client: suspicious_orders = eyes_client.walle_get_suspicious_order_detail(order_ids) suspicious_orders = getresult_to_raw(suspicious_orders) suspicious_orders_map = {order.id:order for order in suspicious_orders} for _object in result_docs: suspicious = suspicious_orders_map.get(_object['id'],{}) _object['_order_suspicious_reason'] = suspicious.get('reasons',[]) restaurant_ids = list(set([o['restaurant_id'] for o in result_docs])) restaurants = rst_base.get(restaurant_ids) for r in restaurants: r['_image_url'] = image_hash_to_url(r['image_hash']) restaurant_map = {r.id:r for r in restaurants} for order in result_docs: order['_restaurant'] = restaurant_map[order['restaurant_id']] result = { 'total':total, 'objects':result_docs, } return result
def get_suspicious_page(): args_spec = { 'restaurant_id': Arg(int), 'date': Arg(unicode, default=arrow.now().replace(days=-1).date().__str__()), # 当前使用前端分页 } args = args_parser.parse(args_spec) restaurant_id = args['restaurant_id'] date = args['date'] auditor_id = current_user.id if not restaurant_id: auditor_amount = CustomerServiceUserBase.get_auditor_amount() if not auditor_amount: auditor_amount = 1 try: restaurant_id = order_audit_base.assign_suspicious_orders( auditor_id, auditor_amount, date) orders = order_audit_base.get_suspicious_orders(restaurant_id, date) except: restaurant_id = 1 orders = [] else: orders = order_audit_base.get_suspicious_orders(restaurant_id, date) restaurant = rst_base.get(restaurant_id) # restaurant_suspicious spec = { 'restaurant_id': restaurant_id, } doc = mongo.restaurant.find_one(spec) if not doc: doc = { 'is_suspicious': False, 'suspicious_description': '', } restaurant.update(doc) today_total = \ order_audit_base.filter_suspicious_orders_amount(None, None, date)[ 'total_count'] # status0 = order_audit_base.filter_suspicious_orders_amount(auditor_id,None,date) # status1 = order_audit_base.filter_suspicious_orders_amount(auditor_id,STATUS_DOING,date) status2 = order_audit_base.filter_suspicious_orders_amount(auditor_id, STATUS_FINISHED, date) total_auditor = mongo.dop_user.find({'allow_order_audit': True}).count() if not total_auditor: total_auditor = 1 need_process_count = int(today_total // total_auditor) status = { 'default': need_process_count, 'doing': need_process_count, 'finished': status2['total_count'], } return { 'restaurant': restaurant, 'orders': orders, 'status': status, }
def ess_search2(**args): def calc_index(query): return 'order' doc_type = 'eleme_order' query = args['query'] index = calc_index(query) q = json.loads(query) if 'filter' not in q: q['filter'] = {} if 'bool' not in q['filter']: q['filter']['bool'] = {} struct = args['struct'] struct = json.loads(struct) regions = [] current_user_struct = get_struct(current_user.id) if not current_user_struct: return {'total':0,'objects':[]} #处理未分组 is_ungroup = False _region_groups_ids = struct.get('region_group_ids',[]) with thrift_client('ers') as ers: _result = ers.mget_region_group(_region_groups_ids) _result = getresult_to_raw(_result) for _region_group in _result.values(): if _region_group.name.find(u'未分组') >= 0: is_ungroup = True with thrift_client('ers') as ers: region_query = thirdparty_svc.ers.TRegionQuery() region_query.city_ids = [_region_group.city_id] regions = ers.query_region(region_query) regions = any_to_raw(regions) regions = getresult_to_raw(regions) should = [] for region in regions: should.append( { 'geo_polygon':{ 'eleme_order.location':{ 'points':region_area_to_es_points(region.area) } } } ) q['filter']['bool']['must_not'] = [ #{'filter':{ { 'bool':{ 'should':should, #'mininum_should_match':1, } } #} ] restaurant_q = { "fields" : [], "filter" : { "term" : { "city_id" : _region_group.city_id} }, "from" : 0, "size" : random.randint(100000,500000), } restaurant_q = json.dumps(restaurant_q) result1 = _ess_search2('restaurant','restaurant',restaurant_q) result1 = result1['hits'] city_restaurant_ids = [r['_id'] for r in result1] q['filter']['bool']['must'].append( { 'terms':{'restaurant_id':city_restaurant_ids} } ) break if not is_ungroup: city_ids = struct.get('city_ids',[]) region_group_ids = struct.get('region_group_ids',[]) region_ids = struct.get('region_ids',[]) if struct: regions = regions_from_struct(input_struct=struct,his_struct=current_user_struct) if not city_ids and not region_group_ids and not region_ids: regions = [] for city in current_user_struct: for region_groups in city._region_groups: for region in region_groups._regions: regions.append(region) if regions: should = [] for region in regions: points = region_area_to_es_points(region.area) if points: should.append( { 'geo_polygon':{ 'eleme_order.location':{ 'points':region_area_to_es_points(region.area) } } } ) q['filter']['bool']['should'] = should query = json.dumps(q) result = _ess_search2(index,doc_type,query) result = extract_ess_search_result(result) order_ids = [order['id'] for order in result['objects']] with thrift_client('eos') as eos: orders_info = eos.mget_order_info(order_ids) orders_info = getresult_to_raw(orders_info) orders_info = {info['order_id']:info for info in orders_info} for order in result['objects']: order.update(orders_info[order['id']]) for order in result['objects']: if order['phone_rating'] == 1: order['is_new_user'] = True else: order['is_new_user'] = False with thrift_client('eus') as eus_client: chaos = eus_client.get_order_payment_constitution_map(order_ids) for order in result['objects']: payment_constituion = chaos.get(order['id'],[]) order['payment_constituion'] = whatzfvcksay(payment_constituion,order['total']) restaurant_ids = list(set([o['restaurant_id'] for o in result['objects']])) restaurants = rst_base.get(restaurant_ids) for r in restaurants: r['_image_url'] = image_hash_to_url(r['image_hash']) restaurant_map = {r.id:r for r in restaurants} for order in result['objects']: order['_restaurant'] = restaurant_map[order['restaurant_id']] for _object in result['objects']: _object['order_id'] = unicode(_object['id']) with thrift_client('eyes') as eyes_client: suspicious_orders = eyes_client.walle_get_suspicious_order_detail(order_ids) suspicious_orders = getresult_to_raw(suspicious_orders) suspicious_orders_map = {order.id:order for order in suspicious_orders} for _object in result['objects']: suspicious = suspicious_orders_map.get(_object['id'],{}) _object['_order_suspicious_reason'] = suspicious.get('reasons',[]) return result
def ess_search2(**args): def calc_index(query): return "order" doc_type = "eleme_order" query = args["query"] index = calc_index(query) q = json.loads(query) if "filter" not in q: q["filter"] = {} if "bool" not in q["filter"]: q["filter"]["bool"] = {} struct = args["struct"] struct = json.loads(struct) regions = [] current_user_struct = get_struct(current_user.id) if not current_user_struct: return {"total": 0, "objects": []} # 处理未分组 is_ungroup = False _region_groups_ids = struct.get("region_group_ids", []) with thrift_client("ers") as ers: _result = ers.mget_region_group(_region_groups_ids) _result = getresult_to_raw(_result) for _region_group in _result.values(): if _region_group.name.find(u"未分组") >= 0: is_ungroup = True with thrift_client("ers") as ers: region_query = thirdparty_svc.ers.TRegionQuery() region_query.city_ids = [_region_group.city_id] regions = ers.query_region(region_query) regions = any_to_raw(regions) regions = getresult_to_raw(regions) should = [] for region in regions: should.append( {"geo_polygon": {"eleme_order.location": {"points": region_area_to_es_points(region.area)}}} ) q["filter"]["bool"]["must_not"] = [ # {'filter':{ { "bool": { "should": should, #'mininum_should_match':1, } } # } ] restaurant_q = { "fields": [], "filter": {"term": {"city_id": _region_group.city_id}}, "from": 0, "size": random.randint(100000, 500000), } restaurant_q = json.dumps(restaurant_q) result1 = _ess_search2("restaurant", "restaurant", restaurant_q) result1 = result1["hits"] city_restaurant_ids = [r["_id"] for r in result1] q["filter"]["bool"]["must"].append({"terms": {"restaurant_id": city_restaurant_ids}}) break if not is_ungroup: city_ids = struct.get("city_ids", []) region_group_ids = struct.get("region_group_ids", []) region_ids = struct.get("region_ids", []) if struct: regions = regions_from_struct(input_struct=struct, his_struct=current_user_struct) if not city_ids and not region_group_ids and not region_ids: regions = [] for city in current_user_struct: for region_groups in city._region_groups: for region in region_groups._regions: regions.append(region) if regions: should = [] for region in regions: points = region_area_to_es_points(region.area) if points: should.append( {"geo_polygon": {"eleme_order.location": {"points": region_area_to_es_points(region.area)}}} ) q["filter"]["bool"]["should"] = should query = json.dumps(q) result = _ess_search2(index, doc_type, query) result = extract_ess_search_result(result) order_ids = [order["id"] for order in result["objects"]] with thrift_client("eos") as eos: orders_info = eos.mget_order_info(order_ids) orders_info = getresult_to_raw(orders_info) orders_info = {info["order_id"]: info for info in orders_info} for order in result["objects"]: order.update(orders_info[order["id"]]) for order in result["objects"]: if order["phone_rating"] == 1: order["is_new_user"] = True else: order["is_new_user"] = False with thrift_client("eus") as eus_client: chaos = eus_client.get_order_payment_constitution_map(order_ids) for order in result["objects"]: payment_constituion = chaos.get(order["id"], []) order["payment_constituion"] = whatzfvcksay(payment_constituion, order["total"]) restaurant_ids = list(set([o["restaurant_id"] for o in result["objects"]])) restaurants = rst_base.get(restaurant_ids) for r in restaurants: r["_image_url"] = image_hash_to_url(r["image_hash"]) restaurant_map = {r.id: r for r in restaurants} for order in result["objects"]: order["_restaurant"] = restaurant_map[order["restaurant_id"]] for _object in result["objects"]: _object["order_id"] = unicode(_object["id"]) with thrift_client("eyes") as eyes_client: suspicious_orders = eyes_client.walle_get_suspicious_order_detail(order_ids) suspicious_orders = getresult_to_raw(suspicious_orders) suspicious_orders_map = {order.id: order for order in suspicious_orders} for _object in result["objects"]: suspicious = suspicious_orders_map.get(_object["id"], {}) _object["_order_suspicious_reason"] = suspicious.get("reasons", []) return result
def query_suspicous(): args_spec = { 'start_datetime': Arg(unicode, default=arrow.now().replace(days=-1).__str__()), 'end_datetime': Arg(unicode, default=arrow.now().__str__()), 'restaurant_id': Arg(int), 'offset': Arg(int, default=0), 'limit': Arg(int, default=100), } args = args_parser.parse(args_spec) if not args['restaurant_id']: return { 'total': 0, 'objects': [], } client = pymongo.MongoClient(config.EYES_MONGO) collection = client.evileye.suspicious_order # db.tickets.find({"date":{$lt:ISODate("2013-01-17T01:16:33.303Z")}}).limit (5); start_datetime = args['start_datetime'].split('+')[0] end_datetime = args['end_datetime'].split('+')[0] start_datetime = arrow.get(start_datetime).to('local').datetime end_datetime = arrow.get(end_datetime).to('local').datetime restaurant_id = args['restaurant_id'] offset = args['offset'] limit = args['limit'] query = { 'created_at': { '$lte': end_datetime, '$gte': start_datetime }, } if restaurant_id: query.update({'restaurant_id': restaurant_id}) docs = collection.find(query) total = docs.count() result_docs = list(docs.limit(limit).skip(offset)) time_fields = ['deliver_time', 'settled_at'] with thrift_client('eos') as eos: for doc in result_docs: order_id = doc['_id'] order = eos.get(int(order_id)) order = getresult_to_raw(order) for k, v in order.items(): doc.setdefault(k, v) doc['detail_json'] = json.loads(doc['detail_json']) doc['_order_id'] = doc['_id'] for field in time_fields: new_value = arrow.get(doc[field]).__str__() doc[field] = new_value doc['phone'] = [doc['phone']] with thrift_client('eus') as eus_client: chaos = eus_client.get_order_payment_constitution_map( [int(order_id)]) payment_constituion = chaos.get(order['id'], []) doc['payment_constituion'] = order_query_helper.whatzfvcksay( payment_constituion, doc['total']) doc['order_id'] = unicode(doc['_id']) order_ids = [doc['id'] for doc in result_docs] with thrift_client('eyes') as eyes_client: suspicious_orders = eyes_client.walle_get_suspicious_order_detail( order_ids) suspicious_orders = getresult_to_raw(suspicious_orders) suspicious_orders_map = { order.id: order for order in suspicious_orders } for _object in result_docs: suspicious = suspicious_orders_map.get(_object['id'], {}) _object['_order_suspicious_reason'] = suspicious.get('reasons', []) restaurant_ids = list(set([o['restaurant_id'] for o in result_docs])) restaurants = rst_base.get(restaurant_ids) for r in restaurants: r['_image_url'] = image_hash_to_url(r['image_hash']) restaurant_map = {r.id: r for r in restaurants} for order in result_docs: order['_restaurant'] = restaurant_map[order['restaurant_id']] result = { 'total': total, 'objects': result_docs, } return result
def get_suspicious_page(): args_spec = { 'restaurant_id': Arg(int), 'date': Arg(unicode, default=arrow.now().replace(days=-1).date().__str__()), # 当前使用前端分页 } args = args_parser.parse(args_spec) restaurant_id = args['restaurant_id'] date = args['date'] auditor_id = current_user.id if not restaurant_id: auditor_amount = CustomerServiceUserBase.get_auditor_amount() if not auditor_amount: auditor_amount = 1 try: restaurant_id = order_audit_base.assign_suspicious_orders( auditor_id, auditor_amount, date) orders = order_audit_base.get_suspicious_orders( restaurant_id, date) except: restaurant_id = 1 orders = [] else: orders = order_audit_base.get_suspicious_orders(restaurant_id, date) restaurant = rst_base.get(restaurant_id) # restaurant_suspicious spec = { 'restaurant_id': restaurant_id, } doc = mongo.restaurant.find_one(spec) if not doc: doc = { 'is_suspicious': False, 'suspicious_description': '', } restaurant.update(doc) today_total = \ order_audit_base.filter_suspicious_orders_amount(None, None, date)[ 'total_count'] # status0 = order_audit_base.filter_suspicious_orders_amount(auditor_id,None,date) # status1 = order_audit_base.filter_suspicious_orders_amount(auditor_id,STATUS_DOING,date) status2 = order_audit_base.filter_suspicious_orders_amount( auditor_id, STATUS_FINISHED, date) total_auditor = mongo.dop_user.find({'allow_order_audit': True}).count() if not total_auditor: total_auditor = 1 need_process_count = int(today_total // total_auditor) status = { 'default': need_process_count, 'doing': need_process_count, 'finished': status2['total_count'], } return { 'restaurant': restaurant, 'orders': orders, 'status': status, }