def consumption_ratio_update_when_register(**kwargs): """ 更新消费换积分比率,未认证商家也可以操作 :param kwargs: {"numbers": 118688982240, "merchant_identity": "", "consumption_ratio": 100} :return: (50400, "yes")/成功,(>50400, "errmsg")/失败 """ try: # 检查要请求用户numbers必须是平台管理员 numbers = kwargs.get("numbers", "") if not account_is_valid_merchant(numbers): g_log.warning("not manager %s", numbers) return 50421, "not manager" # 检查管理员和商家关系 merchant_identity = kwargs.get("merchant_identity", "") # TODO... 消费换积分比率检查 consumption_ratio = kwargs.get("consumption_ratio", 0) value = {"merchant_identity": merchant_identity, "consumption_ratio": consumption_ratio, "deleted": 0} # 存入数据库 collection = get_mongo_collection("parameters") if not collection: g_log.error("get collection parameters failed") return 50423, "get collection parameters failed" business_parameters = collection.find_one_and_update({"merchant_identity": merchant_identity, "deleted": 0}, {"$set": value}) # 第一次更新,则插入一条 if not business_parameters: g_log.debug("insert new parameters") value["bond"] = 0 value["balance"] = 0 value["balance_ratio"] = 0 business_parameters = collection.insert_one(value) if not business_parameters: g_log.error("update merchant %s parameters failed", merchant_identity) return 50424, "update failed" # business_parameters = collection.find_one_and_replace({"merchant_identity": merchant_identity, "deleted": 0}, # value, upsert=True, return_document=ReturnDocument.AFTER) # if not business_parameters: # g_log.error("update merchant %s parameters failed", merchant_identity) # return 50424, "update failed" # g_log.debug("update consumption done") # 更新记录入库 collection = get_mongo_collection("parameters_record") if not collection: g_log.error("get collection parameters record failed") return 50425, "get collection parameters record failed" quantization = "consumption_ratio:%s" % (consumption_ratio,) result = collection.insert_one({"merchant_identity": merchant_identity, "time": datetime.now(), "operator": numbers, "quantization": quantization}) if not result: g_log.error("insert parameters record failed") # return 50426, "insert parameters record failed" return 50400, "yes" except Exception as e: g_log.error("%s", e) return 50427, "exception"
def confirm_voucher(**kwargs): """ 商家确认优惠券 :param kwargs: {"numbers": 186889882240, "merchant_identity": "", "manager": "18688982240", "voucher_identity": "a1der234", "activity_identity": "iof", "exec_confirm": 1} :return: """ try: # 检查合法账号 numbers = kwargs.get("numbers", "") if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 80111, "invalid account" manager = kwargs.get("manager", "") merchant_identity = kwargs.get("merchant_identity", "") merchant = user_is_merchant_manager(manager, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", manager, merchant_identity) return 80312, "not manager" voucher_identity = kwargs.get("voucher_identity", "") activity_identity = kwargs.get("activity_identity", "") exec_confirm = kwargs.get("exec_confirm", "") collection = get_mongo_collection("voucher") if not collection: g_log.error("get collection voucher failed") return 80314, "get collection voucher failed" if not exec_confirm: voucher = collection.find_one({"_id": ObjectId(voucher_identity), "merchant_identity": merchant_identity, "activity_identity": activity_identity, "numbers": numbers}) if not voucher: g_log.error("voucher %s not exist", voucher_identity) return 80300, "invalid" return 80300, "used" if voucher["used"] == 1 else "valid" voucher = collection.find_one_and_update({"merchant_identity": merchant_identity, "numbers": numbers, "_id": ObjectId(voucher_identity), "used": 0}, {"$set": {"used": 1}}) if not voucher: g_log.error("voucher %s not exist", voucher_identity) return 80315, "confirm voucher failed" # 更新记录入库 collection = get_mongo_collection("voucher_record") if not collection: g_log.error("get collection voucher record failed") return 80316, "get collection voucher record failed" result = collection.insert_one({"voucher_identity": voucher_identity, "time": datetime.now(), "operator": manager}) if not result: g_log.error("insert voucher record failed") return 80300, "yes" except Exception as e: g_log.error("%s", e) return 80317, "exception"
def upper_bound_update(**kwargs): """ 商家积分上限变更,保证金变更的时候调 :param kwargs: {"numbers": 1000000, "merchant_identity": "", "bond": 1000} :return: (60100, "yes")/成功,(>60100, "errmsg")/失败 """ try: # 检查请求用户numbers必须是平台管理员 numbers = kwargs.get("numbers", "") if not account_is_platform(numbers): g_log.warning("not platform %s", numbers) return 60311, "no privilege" # 必须是已认证商家,在更新保证金已经做过验证,此处省略 merchant_identity = kwargs.get("merchant_identity", "") bond = kwargs.get("bond", 0) ratio = kwargs.get("ratio", 0) upper_bound = bond_to_upper_bound(bond, ratio) value = {"merchant_identity": merchant_identity, "upper_bound": upper_bound, "deleted": 0} # 存入数据库 collection = get_mongo_collection("flow") if not collection: g_log.error("get collection flow failed") return 60313, "get collection flow failed" flow = collection.find_one_and_update({"merchant_identity": merchant_identity, "deleted": 0}, {"$set": value}) # 第一次更新,则插入一条 if not flow: g_log.debug("insert new flow") flow = collection.insert_one(value) if not flow: g_log.error("update merchant %s credit upper bound failed", merchant_identity) return 60314, "update failed" g_log.debug("update upper bound succeed") # 更新记录入库 collection = get_mongo_collection("flow_record") if not collection: g_log.error("get collection flow record failed") return 60315, "get collection flow record failed" quantization = "bond:%d, bound:%d" % (bond, upper_bound) result = collection.insert_one({"merchant_identity": merchant_identity, "time": datetime.now(), "operator": numbers, "quantization": quantization}) if not result: g_log.error("insert flow record failed") # return 60316, "insert flow record failed" return 60300, "yes" except Exception as e: g_log.error("%s", e) return 60317, "exception"
def boot_report(version): """ 启动上报 :param numbers: :param version: 版本 :return: """ try: # 检查合法账号 # if not account_is_valid(numbers): # g_log.warning("invalid customer account %s", numbers) # return 90311, "invalid account" collection = get_mongo_collection("boot") if not collection: g_log.error("get collection boot failed") return 90213, "get collection boot failed" boot_time = datetime.now().strftime("%Y-%m-%d %H:%M") boot = collection.find_one_and_update( {"boot_time": boot_time, "version": version}, {"$inc": {"total": 1}}, upsert=True, return_document=ReturnDocument.AFTER, ) if not boot: g_log.error("boot report failed") return 50714, "boot report failed" return 90200, "yes" except Exception as e: g_log.error("%s", e) return 90215, "exception"
def version_report(numbers, version): """ 版本上报 :param numbers: :param version: 版本 :return: """ try: # 检查合法账号 if not account_is_valid(numbers): g_log.warning("invalid customer account %s", numbers) return 90111, "invalid account" collection = get_mongo_collection("version") if not collection: g_log.error("get collection version failed") return 90113, "get collection version failed" value = {"numbers": numbers, "version": version, "feedback_time": datetime.now().strftime("%Y-%m-%d %H:%M")} version_identity = collection.insert_one(value).inserted_id version_identity = str(version_identity) g_log.debug("insert version %s", version_identity) return 90100, version_identity except Exception as e: g_log.error("%s", e) return 90115, "exception"
def merchant_retrieve_voucher(numbers, merchant_identity): """ 商家读取优惠券 :param numbers: :param merchant_identity: 商家ID :return: """ try: # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 80111, "not manager" collection = get_mongo_collection("voucher") if not collection: g_log.error("get collection voucher failed") return 80212, "get collection voucher failed" records = collection.find({"merchant_identity": merchant_identity, "used": 0}) if not records: g_log.error("retrieve voucher failed") return 80213, "retrieve voucher failed" return 80200, records except Exception as e: g_log.error("%s", e) return 80214, "exception"
def consumer_retrieve_voucher(numbers, merchant_identity): """ 用户读取指定商家优惠券,没给出merchant_identity则读取全部 :param numbers: 平台账号或管理员账号 :param merchant_identity: 商家ID :return: """ try: if not merchant_identity: return consumer_retrieve_all_voucher(numbers) # 检查合法账号 if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 80111, "invalid account" collection = get_mongo_collection("voucher") if not collection: g_log.error("get collection voucher failed") return 80113, "get collection voucher failed" # g_log.debug("%s", datetime.now()) voucher = collection.find({"numbers": numbers, "merchant_identity": merchant_identity, "expire_time": {"$gte": datetime.now()}, "used": 0}) if not voucher: g_log.error("consumer %s retrieve %s voucher failed", numbers, merchant_identity) return 80114, "consumer retrieve voucher failed" g_log.debug("retrieve voucher success") return 80100, voucher except Exception as e: g_log.error("%s", e) return 80115, "exception"
def business_parameters_retrieve_with_numbers(numbers, merchant_identity): """ 读取商家经营参数 :param numbers: 管理员电话号码 :param merchant_identity: 商家ID :return: (50200, parameters)/成功,(>50200, "errmsg")/失败 """ try: # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.warning("invalid manager account %s", numbers) return 50201, "invalid manager" founder = merchant["merchant_founder"] g_log.debug("merchant %s founder %s", merchant_identity, founder) collection = get_mongo_collection("parameters") if not collection: g_log.error("get collection parameters failed") return 50202, "get collection parameters failed" business_parameters = collection.find_one({"merchant_identity": merchant_identity, "deleted": 0}) if not business_parameters: g_log.debug("merchant %s parameters not exist", merchant_identity) return 50203, "parameters not exist" return 50200, business_parameters except Exception as e: g_log.error("%s", e) return 50204, "exception"
def consumer_retrieve_with_numbers(numbers): """ 读取用户资料 :param numbers: 用户电话号码 :return: (20200, consumer)/成功,(>20200, "errmsg")/失败 """ try: # 检查合法账号 if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 20211, "invalid account" collection = get_mongo_collection("consumer") if not collection: g_log.error("get collection consumer failed") return 20212, "get collection consumer failed" consumer = collection.find_one({"numbers": numbers, "deleted": 0}) if not consumer: g_log.debug("consumer %s not exist", numbers) return 20213, "consumer not exist" return 20200, consumer except Exception as e: g_log.error("%s", e) return 20214, "exception"
def balance_record_retrieve(numbers, merchant_identity): """ 读取充值、提现纪录,没给出merchant_identity则读取全部 :param numbers: 平台账号或管理员账号 :param merchant_identity: 商家ID :return: """ try: if not merchant_identity: # 平台读取所有操作纪录 return balance_record_retrieve_all(numbers) # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 60512, "not manager" # 广播查找所有商家的经营参数更新纪录 collection = get_mongo_collection("balance_record") if not collection: g_log.error("get collection balance record failed") return 60513, "get collection balance record failed" records = collection.find({"merchant_identity": merchant_identity}) if not records: g_log.error("retrieve balance record failed") return 60514, "retrieve balance record failed" return 60500, records except Exception as e: g_log.error("%s", e) return 60514, "exception"
def parameters_record_retrieve(numbers, merchant_identity): """ 读取商家经营参数,没给出merchant_identity则读取全部 :param numbers: 平台账号或管理员账号 :param merchant_identity: 商家ID :return: """ try: if not merchant_identity: # 平台读取所有操作纪录 return parameters_record_retrieve_all(numbers) # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 50612, "not manager" collection = get_mongo_collection("parameters_record") if not collection: g_log.error("get collection parameters record failed") return 50613, "get collection parameters record failed" records = collection.find({"merchant_identity": merchant_identity}) if not records: g_log.error("insert parameters record failed") return 50600, records except Exception as e: g_log.error("%s", e) return 50614, "exception"
def merchant_credit_flow_retrieve(numbers, merchant_identity): """ 读取商家积分详情,没给出merchant_identity则读取全部 :param numbers: 平台账号或管理员账号 :param merchant_identity: 商家ID :return: """ try: if not merchant_identity: # 平台读取所有操作纪录 return merchant_credit_flow_retrieve_all(numbers) # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 60112, "not manager" collection = get_mongo_collection("flow") if not collection: g_log.error("get collection flow failed") return 60113, "get collection flow failed" records = collection.find({"merchant_identity": merchant_identity, "deleted": 0}) if not records: g_log.error("retrieve flow failed") return 60114, "retrieve failed" return 60100, records except Exception as e: g_log.error("%s", e) return 60115, "exception"
def active_report(numbers, mode): """ 活跃上报 :param numbers: :param mode: merchant or consumer :return: """ try: # 检查合法账号 if not account_is_valid(numbers): g_log.warning("invalid customer account %s", numbers) return 90311, "invalid account" collection = get_mongo_collection("active") if not collection: g_log.error("get collection active failed") return 90313, "get collection active failed" value = {"numbers": numbers, "mode": mode, "active_time": datetime.now().strftime("%Y-%m-%d %H:%M")} active_identity = collection.insert_one(value).inserted_id active_identity = str(active_identity) g_log.debug("insert active report %s", active_identity) return 90300, active_identity except Exception as e: g_log.error("%s", e) return 90315, "exception"
def balance_overdraft(merchant_identity): """ 商家账户是否有余额 :param merchant_identity: "merchant_identity" :return: (61100, yes)/有,(>61100, "errmsg")/无 """ try: collection = get_mongo_collection("flow") if not collection: g_log.error("get collection flow failed") return 61111, "get collection flow failed" flow = collection.find_one({"merchant_identity": merchant_identity, "deleted": 0}) if not flow: g_log.error("merchant %s not exist", merchant_identity) return 61112, "merchant not exist" balance = int(flow["balance"]) if balance <= 0: g_log.debug("balance[%d] <= 0, overdraft", balance) return 61113, "balance overdraft" return 61100, "yes" except Exception as e: g_log.error("%s", e) return 61114, "exception"
def consumer_retrieve_all_voucher(numbers): """ 用户读取优惠券 :param numbers: :return: """ try: # 检查合法账号 if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 80116, "invalid account" collection = get_mongo_collection("voucher") if not collection: g_log.error("get collection voucher failed") return 80117, "get collection voucher failed" records = collection.find({"numbers": numbers, "expire_time": {"$gte": datetime.now()}, "used": 0}).sort("merchant_identity") if not records: g_log.error("retrieve voucher failed") return 80118, "retrieve voucher failed" return 80100, records except Exception as e: g_log.error("%s", e) return 80119, "exception"
def credit_conversion(from_merchant, to_merchant, quantity): """ 积分换算 :param from_merchant: 换出商家 :param to_merchant: 换入商家 :param quantity: 换出积分量 :return: (51100, (from_balance_ratio, to_balance_ratio, from_quantity, to_quantity))/成功,(>51100, "errmsg")/失败 """ try: collection = get_mongo_collection("parameters") if not collection: g_log.error("get collection business_parameters failed") return 51111, "get collection business_parameters failed" from_parameters = collection.find_one({"merchant_identity": from_merchant, "deleted": 0}) if not from_parameters: g_log.error("merchant %s parameters not exist", from_merchant) return 51112, "from merchant parameters not exist" to_parameters = collection.find_one({"merchant_identity": to_merchant, "deleted": 0}) if not to_parameters: g_log.error("merchant %s parameters not exist", to_merchant) return 51113, "to merchant parameters not exist" to_quantity = quantity * to_parameters["balance_ratio"] / from_parameters["balance_ratio"] return 51100, to_quantity # return 51100, (from_parameters["balance_ratio"], to_parameters["balance_ratio"], quantity, to_quantity) except Exception as e: g_log.error("%s", e) return 51114, "exception"
def activity_retrieve_with_numbers(numbers, merchant_identity): """ 读取活动资料 :param numbers: 用户电话号码 :return: (70200, activity)/成功,(>70200, "errmsg")/失败 """ try: # 检查合法账号 if not account_is_valid_merchant(numbers): g_log.warning("invalid customer account %s", numbers) return 70211, "invalid account" merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not activity %s manager", numbers, merchant_identity) return 70212, "not manager" collection = get_mongo_collection("activity") if not collection: g_log.error("get collection activity failed") return 70213, "get collection activity failed" activity = collection.find({"merchant_identity": merchant_identity, "expire_time": {"$gte": datetime.now()}, "deleted": 0}).sort("create_time", -1) if not activity: g_log.debug("activity %s not exist", numbers) return 70214, "activity not exist" return 70200, activity except Exception as e: g_log.error("%s", e) return 70215, "exception"
def activity_delete_with_numbers(numbers, merchant_identity, activity_identity): """ 删除活动资料 :param numbers: 用户电话号码 :return: (70500, "yes")/成功,(>70500, "errmsg")/失败 """ try: # 检查合法账号 if not account_is_valid_merchant(numbers): g_log.warning("invalid customer account %s", numbers) return 70511, "invalid phone number" merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 70512, "not manager" collection = get_mongo_collection("activity") if not collection: g_log.error("get collection activity failed") return 70513, "get collection activity failed" activity = collection.find_one_and_update({"merchant_identity": merchant_identity, "_id": ObjectId(activity_identity), "deleted": 0}, {"$set": {"deleted": 1}}) if not activity: g_log.error("activity %s not exist", activity_identity) return 70514, "activity not exist" return 70500, "yes" except Exception as e: g_log.error("%s", e) return 70515, "exception"
def business_parameters_delete_with_numbers(numbers, merchant_identity): """ 删除商家经营参数 :param numbers: 管理员电话号码 :param merchant_identity: 商家ID :return: (50500, "yes")/成功,(>50500, "errmsg")/失败 """ try: # 检查合法账号 # if not account_is_valid_merchant(numbers): # g_log.warning("invalid customer account %s", numbers) # return 50501, "invalid phone number" # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 50501, "not manager" merchant_founder = merchant["merchant_founder"] g_log.debug("merchant %s founder %s", merchant_identity, merchant_founder) collection = get_mongo_collection("parameters") if not collection: g_log.error("get collection business_parameters failed") return 50502, "get collection business_parameters failed" business_parameters = collection.find_one_and_update({"merchant_identity": merchant_identity, "deleted": 0}, {"$set": {"deleted": 1}}) if not business_parameters: g_log.error("merchant %s parameters not exist", merchant_identity) return 50503, "merchant parameters not exist" # 更新记录入库 collection = get_mongo_collection("parameters_record") if not collection: g_log.error("get collection parameters record failed") return 50504, "get collection parameters record failed" quantization = "deleted:1" result = collection.insert_one({"merchant_identity": merchant_identity, "time": datetime.now(), "operator": numbers, "quantization": quantization}) if not result: g_log.error("insert parameters record failed") return 50505, "insert parameters record failed" return 50500, "yes" except Exception as e: g_log.error("%s", e) return 50506, "exception"
def activity_create(**kwargs): """ 新增活动 :param kwargs: {"numbers": "18688982240", "title": "good taste", "introduce": "ego cogito ergo sum", "poster": "a/18688982240/x87cd", "credit": 100, "merchant_identity": "xij923f0a8m"} :return: (70100, "yes")/成功,(>70100, "errmsg")/失败 """ try: # 检查要创建的用户numbers numbers = kwargs.get("numbers", "") if not account_is_valid_merchant(numbers): g_log.warning("not manager %s", numbers) return 70111, "not manager" merchant_identity = kwargs.get("merchant_identity", "") merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not activity %s manager", numbers, merchant_identity) return 70112, "not manager" # 活动标题不能超过32字节,超过要截取前32字节 title = kwargs.get("title", "") if len(title) > 32: g_log.warning("too long title %s", title) title = title[0:32] # 活动介绍不能超过512字节,超过要截取前512字节 introduce = kwargs.get("introduce", "") if len(introduce) > 512: g_log.warning("too long introduce %s", introduce) introduce = introduce[0:512] poster = kwargs.get("poster", "") credit = kwargs.get("credit", 0) expire_time = kwargs.get("expire_time", "") expire_time = datetime.strptime(expire_time, "%Y-%m-%d") value = {"numbers": numbers, "title": title, "introduce": introduce, "introduce": introduce, "credit": credit, "merchant_identity": merchant_identity, "poster": poster, "deleted": 0, "create_time": datetime.now(), "expire_time": expire_time, "volume": 0} # 存入数据库 collection = get_mongo_collection("activity") if not collection: g_log.error("get collection activity failed") return 70113, "get collection activity failed" # activity = collection.find_one_and_replace({"numbers": numbers}, value, upsert=True, # return_document=ReturnDocument.AFTER) identity = collection.insert_one(value).inserted_id if not identity: g_log.error("create activity %s failed", numbers) return 70114, "create activity failed" identity = str(identity) return 70100, identity except Exception as e: g_log.error("%s", e) return 70115, "exception"
def consumer_retrieve_activity_with_numbers(numbers, mark): """ 读取活动资料 :param numbers: 用户电话号码 :return: (70200, activity)/成功,(>70200, "errmsg")/失败 """ try: # 检查合法账号 if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 70611, "invalid account" collection = get_mongo_collection("activity") if not collection: g_log.error("get collection activity failed") return 70613, "get collection activity failed" try: last_time = datetime.strptime(mark, '%Y-%m-%d %H:%M:%S') g_log.warning("mark %s, last time %s", mark, last_time) except: last_time = datetime.now() g_log.warning("2 mark %s, last time %s", mark, last_time) activity = collection.find({"deleted": 0, "create_time": {"$lt": last_time}, "expire_time": {"$gte": datetime.now()}}, limit=10).sort("create_time", -1) if not activity: g_log.debug("activity %s not exist", numbers) return 70614, "activity not exist" collection2 = get_mongo_collection("merchant") if not collection2: g_log.error("get collection merchant failed") return 70615, "get collection merchant failed" activity_dict = [] # type(activity_one) = "dict" for activity_one in activity: merchant = collection2.find_one({"_id": ObjectId(activity_one["merchant_identity"])}) activity_one["merchant"] = merchant activity_dict.append(activity_one) # activity.rewind() return 70600, activity_dict except Exception as e: g_log.error("%s", e) return 70617, "exception"
def business_parameters_delete_by_platform(numbers, merchant_identity): """ 平台删除商家经营参数 :param numbers: 平台账号 :param merchant_identity: 商家ID :return: """ try: # 检查合法账号 if not account_is_valid_merchant(numbers): g_log.warning("invalid customer account %s", numbers) return 50510, "invalid phone number" collection = get_mongo_collection("parameters") if not collection: g_log.error("get collection business parameters failed") return 50511, "get collection business parameters failed" business_parameters = collection.find_one_and_update({"merchant_identity": merchant_identity, "deleted": 0}, {"$set": {"deleted": 1}}) if not business_parameters: g_log.error("merchant %s parameters not exist", merchant_identity) return 50512, "merchant parameters not exist" # 更新记录入库 collection = get_mongo_collection("parameters_record") if not collection: g_log.error("get collection parameters record failed") return 50513, "get collection parameters record failed" quantization = "deleted:1" result = collection.insert_one({"merchant_identity": merchant_identity, "time": datetime.now(), "operator": numbers, "quantization": quantization}) if not result: g_log.error("insert parameters record failed") return 50514, "insert parameters record failed" return 50500, "yes" except Exception as e: g_log.error("%s", e) return 50515, "exception"
def balance_retrieve(numbers, merchant_identity): """ 读取商家帐户余额 :param numbers: 平台账号或管理员账号 :param merchant_identity: 商家ID :return: """ try: # 检查管理员和商家关系 merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 60611, "not manager" collection = get_mongo_collection("flow") if not collection: g_log.error("get collection flow failed") return 60612, "get collection flow failed" flow = collection.find_one({"merchant_identity": merchant_identity, "deleted": 0}) if not flow: g_log.error("retrieve flow failed") return 60613, "retrieve failed" balance = flow["balance"] collection2 = get_mongo_collection("parameters") if not collection2: g_log.error("get collection parameters failed") return 60614, "get collection parameters failed" parameters = collection2.find_one({"merchant_identity": merchant_identity, "deleted": 0}) if not parameters: g_log.error("retrieve parameters failed") return 60615, "retrieve failed" balance_ratio = parameters["balance_ratio"] g_log.debug("balance / ratio = %d / %d", balance, balance_ratio) return 60600, float('%.2f' % (balance / float(balance_ratio))) except Exception as e: g_log.error("%s", e) return 60616, "exception"
def query_withdrawals_times(merchant): """ 获取商家今天提现次数 :param merchant: :return: """ collection = get_mongo_collection("withdrawals") if not collection: g_log.error("get collection withdrawals failed") return 999999 withdrawals = collection.find_one({"merchant_identity": merchant, "date": datetime.now().strftime('%Y-%m-%d')}) if not withdrawals: return 0 return withdrawals["times"]
def register_request(**kwargs): """ 账号注册 :param kwargs: {"numbers": "18688982240", "password": "******", "password_md5": "c56d0e9a7ccec67b4ea131655038d604"} :return: (10200, "yes")/成功,(>10210, "errmsg")/失败 """ try: # 检查要创建的用户numbers numbers = kwargs.get("numbers", "") if not account_is_valid(numbers): g_log.warning("invalid account %s", numbers) return 10211, "invalid account" # TODO... 检查密码字符 password = kwargs.get("password", "") password_md5 = kwargs.get("password_md5", "") # password_md5 = md5(md5(md5(password))) if 1 == check_md5(password, numbers, password_md5, 3): g_log.warning("password_md5 != md5(md5(md5(password)))") return 10212, "invalid password" value = {"numbers": numbers, "password": password, "password_md5": password_md5, "deleted": 0, "time": datetime.now(), "update_time": datetime.now()} # 存入数据库 collection = get_mongo_collection("account") if not collection: g_log.error("get collection account failed") return 10214, "get collection account failed" account = collection.find_one_and_update({"numbers": numbers}, {"$set": value}) # 第一次更新,则插入一条 if not account: g_log.debug("register new account") account = collection.insert_one(value) if not account: g_log.error("register account %s failed", numbers) return 10215, "register account failed" g_log.debug("register succeed") # 增加客户资料信息 consumer_create(numbers=numbers) return 10200, "yes" except Exception as e: g_log.error("%s", e) return 10216, "exception"
def recharge_trade_no(**kwargs): """ 获取充值订单号,未认证商家不允许操作 :param kwargs: {"numbers": 118688982240, "merchant_identity": "", "money": 100} :return: (60300, "yes")/成功,(>60300, "errmsg")/失败 """ try: # 检查要请求用户numbers必须是平台管理员 numbers = kwargs.get("numbers", "") if not account_is_valid_merchant(numbers): g_log.warning("not manager %s", numbers) return 60711, "not manager" # 检查管理员和商家关系 merchant_identity = kwargs.get("merchant_identity", "") merchant = user_is_merchant_manager(numbers, merchant_identity) if not merchant: g_log.error("%s is not merchant %s manager", numbers, merchant_identity) return 60712, "not manager" # 认证用户才可以充值 if not merchant_is_verified(merchant_identity): g_log.error("merchant %s not verified", merchant_identity) return 60713, "not verified" money = kwargs.get("money", 0) # 生成唯一订单号 now = datetime.now() trade_no = generate_trade_no(numbers, merchant_identity, money, now) g_log.debug("generate trade number: %s", trade_no) # 更新记录入库 collection = get_mongo_collection("trade_no") if not collection: g_log.error("get collection trade_no failed") return 60715, "get collection trade_no failed" result = collection.insert_one({"merchant_identity": merchant_identity, "time": now, "state": "pending", "operator": numbers, "money": money, "trade_no": trade_no}) if not result: g_log.error("insert trade_no failed") return 60716, "insert trade_no failed" return 60700, trade_no except Exception as e: g_log.error("%s", e) return 60717, "exception"
def verify_session_key(account, session_key): """ 验证session key :param session_key: session key :param account: 账号 :return: """ try: # session 算法验证 plain = decrypt_session_key(session_key) g_log.debug(plain) if not plain: g_log.error("illegal session key %s", session_key) return 10411, "illegal session key" (timestamp, numbers) = plain if account != numbers: g_log.error("not account %s session", account) return 10412, "account not match" # session 落地验证 collection = get_mongo_collection("session") if not collection: g_log.error("get collection session failed") return 10413, "get collection session failed" create_time = datetime.fromtimestamp(timestamp) session = collection.find_one({"numbers": numbers, "session_key": session_key, "create_time": create_time}) if not session: g_log.debug("session %s not exist", session_key) return 10414, "session not exist" # TODO... 比较活跃时间 g_log.debug("last active time %s", session["active_time"]) # 更新活跃时间 active_time = datetime.now() session = collection.find_one_and_update({"numbers": numbers, "session_key": session_key}, {"$set": {"active_time": active_time}}) if not session: g_log.warning("update active time failed") return 10400, "yes" except Exception as e: g_log.error("%s", e) return 10415, "exception"
def numbers_to_identity(numbers): """ 账号转换成账号ID :param numbers: 账号 :return: """ try: collection = get_mongo_collection("account") if not collection: g_log.error("get collection account failed") return 10514, "get collection account failed" account = collection.find_one({"numbers": numbers, "deleted": 0}) if not account: g_log.debug("account %s not exist", numbers) return 10515, "account not exist" identity = str(account["identity"]) return 10500, identity except Exception as e: g_log.error("%s", e) return 10516, "exception"
def gift_upper_bound(**kwargs): """ 创建商家默认提供bound积分 :param kwargs: {"numbers": 1000000, "merchant_identity": "", "bound": 1000} :return: (60100, "yes")/成功,(>60100, "errmsg")/失败 """ try: merchant_identity = kwargs.get("merchant_identity", "") bound = kwargs.get("bound", 0) value = { "merchant_identity": merchant_identity, "upper_bound": bound, "may_issued": 0, "issued": 0, "interchange_in": 0, "interchange_out": 0, "consumption": 0, "balance": 0, "deleted": 0, } # 存入数据库 collection = get_mongo_collection("flow") if not collection: g_log.error("get collection flow failed") return 60113, "get collection flow failed" flow = collection.find_one_and_update({"merchant_identity": merchant_identity, "deleted": 0}, {"$set": value}) # 第一次更新,则插入一条 if not flow: g_log.debug("insert new flow") flow = collection.insert_one(value) if not flow: g_log.error("update merchant %s credit upper bound failed", merchant_identity) return 60114, "update failed" g_log.debug("update upper bound succeed") return 60100, "yes" except Exception as e: g_log.error("%s", e) return 60117, "exception"
def identity_to_numbers(identity): """ 账号ID转换成账号 :param identity: 账号ID :return: """ try: if not identity: return 10514, "illegal identity" collection = get_mongo_collection("account") if not collection: g_log.error("get collection account failed") return 10511, "get collection account failed" account = collection.find_one({"_id": ObjectId(identity), "deleted": 0}) if not account: g_log.debug("account %s not exist", identity) return 10512, "account not exist" numbers = account["numbers"] return 10500, numbers except Exception as e: g_log.error("%s", e) return 10513, "exception"