Ejemplo n.º 1
0
def merchant_allow_exchange_in(**kwargs):
    """
    商家是否允许积分转入
    :param numbers: 平台账号或管理员账号
    :param merchant_identity: 商家ID
    :return:
    """
    try:
        # 检查要创建者numbers
        numbers = kwargs.get("numbers", "")
        merchant_identity = kwargs.get("merchant_identity")

        # 检查管理员和商家关系
        if not account_is_valid_consumer(numbers):
            g_log.error("invalid account, %s", numbers)
            return 60211, "invalid account"

        if merchant_is_verified(merchant_identity) != "y":
            g_log.debug("merchant %s not verified", merchant_identity)
            return 60213, "not verified"

        code, message = credit_exceed_upper(**{"merchant_identity": merchant_identity, "allow_last": "yes"})

        return 60200, "yes" if bool(message) else "no"
    except Exception as e:
        g_log.error("%s", e)
        return 60212, "exception"
Ejemplo n.º 2
0
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"
Ejemplo n.º 3
0
def platform_update_parameters(**kwargs):
    """
    平台修改商家保证金、账户余额换积分比率,必须同时提供
    :param kwargs: {"numbers": "18688982240", "merchant_identity": "", "bond": 1000, "balance_ratio": 1}

    :return: (50100, "yes")/成功,(>50100, "errmsg")/失败
    """
    try:
        # 检查请求用户numbers必须是平台管理员
        numbers = kwargs.get("numbers", "")
        if not account_is_platform(numbers):
            g_log.warning("not platform %s", numbers)
            return 50111, "no privilege"

        # 必须是已认证商家
        # manager = kwargs.get("manager", "")
        # merchant = user_is_merchant_manager(manager, merchant_identity)
        # if not merchant:
        #     g_log.error("%s is not merchant %s manager", manager, merchant_identity)
        #     return 50112, "not manager"
        # merchant_founder = merchant["merchant_founder"]
        merchant_identity = kwargs.get("merchant_identity", "")
        if not merchant_is_verified(merchant_identity):
            g_log.error("merchant %s not verified", merchant_identity)
            return 50112, "not verified"

        # TODO... 保证金、账户余额、账户余额换积分比率、消费换积分比率检查
        bond = kwargs.get("bond", 0)
        balance_ratio = kwargs.get("balance_ratio", 0)
        value = {"merchant_identity": merchant_identity, "bond": bond, "balance_ratio": balance_ratio, "deleted": 0}

        # 存入数据库
        collection = get_mongo_collection("parameters")
        if not collection:
            g_log.error("get collection parameters failed")
            return 50113, "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["balance"] = 0
            value["consumption_ratio"] = 0
            business_parameters = collection.insert_one(value)

            if not business_parameters:
                g_log.error("update merchant %s parameters failed", merchant_identity)
                return 50114, "update failed"
        g_log.debug("update parameter succeed")

        # 更新记录入库
        collection = get_mongo_collection("parameters_record")
        if not collection:
            g_log.error("get collection parameters record failed")
            return 50115, "get collection parameters record failed"
        quantization = "bond:%s, balance_ratio:%s" % (bond, balance_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 50116, "insert parameters record failed"

        # 更新商家积分总量
        code, message = upper_bound_update(**{"numbers": 10000, "merchant_identity": merchant_identity, "bond": bond,
                                              "ratio": balance_ratio})
        if code != 60300:
            g_log.error("platform update parameters failed, set upper bound failed")
            return 50118, "set upper bound failed"

        return 50100, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 50117, "exception"
Ejemplo n.º 4
0
def merchant_withdrawals(**kwargs):
    """
    商家提现,未认证商家不允许操作
    :param kwargs: {"numbers": 118688982240, "merchant_identity": "", "money": 100}
    :return: (60400, "yes")/成功,(>60400, "errmsg")/失败
    """
    try:
        # 检查要请求用户numbers必须是平台管理员
        numbers = kwargs.get("numbers", "")
        if not account_is_valid_merchant(numbers):
            g_log.warning("not manager %s", numbers)
            return 60411, "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 60412, "not manager"
        # merchant_founder = merchant["merchant_founder"]
        # g_log.debug("merchant %s founder %s", merchant_identity, merchant_founder)

        # 认证用户才可以充值
        if not merchant_is_verified(merchant_identity):
            g_log.error("merchant %s not verified", merchant_identity)
            return 60413, "not verified"

        # 检查今天是否已经提现
        if query_withdrawals_times(merchant_identity) > 0:
            g_log.error("had withdrawals today")
            return 60418, "exceed"

        # 提现金额换成积分值 (提现金额 * 金额积分比例系数)
        money = kwargs.get("money", 0)
        collection = get_mongo_collection("parameters")
        if not collection:
            g_log.error("get collection parameters failed")
            return 60418, "get collection parameters failed"

        business_parameters = collection.find_one({"merchant_identity": merchant_identity})
        if not business_parameters:
            g_log.error("get merchant %s parameters failed", merchant_identity)
            return 60419, "get merchant parameters failed"
        balance = money * business_parameters["balance_ratio"]

        # 存入数据库
        code, message = merchant_credit_update(**{"numbers": numbers, "merchant_identity": merchant_identity,
                                                  "mode": "balance", "supplement": -balance})
        if code != 60400:
            g_log.error("merchant withdrawals failed, %s", message)
            return 60414, "withdrawals failed"
        # last = int(message)
        # g_log.debug("last balance: %d", last)

        if update_withdrawals_times(merchant_identity, 1) == "no":
            g_log.warning("update withdrawals %d times failed", 1)

        # 更新记录入库
        collection = get_mongo_collection("balance_record")
        if not collection:
            g_log.error("get collection balance record failed")
            return 60400, "get collection balance record failed"
            # return 60415, "get collection balance record failed"
        result = collection.insert_one({"merchant_identity": merchant_identity, "time": datetime.now(),
                                        "operator": numbers, "money": money, "type": "withdrawals", "state": "pending"})
        if not result:
            g_log.error("insert withdrawals record failed")
            return 60400, "insert withdrawals record failed"
            # return 60416, "insert withdrawals record failed"
        return 60400, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 60417, "exception"
Ejemplo n.º 5
0
def merchant_recharge(**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 60311, "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 60312, "not manager"
        # merchant_founder = merchant["merchant_founder"]
        # g_log.debug("merchant %s founder %s", merchant_identity, merchant_founder)

        # 认证用户才可以充值
        if not merchant_is_verified(merchant_identity):
            g_log.error("merchant %s not verified", merchant_identity)
            return 60313, "not verified"

        # 充值金额换成积分值 (充值金额 * 金额积分比例系数)
        money = kwargs.get("money", 0)
        collection = get_mongo_collection("parameters")
        if not collection:
            g_log.error("get collection parameters failed")
            return 60318, "get collection parameters failed"

        business_parameters = collection.find_one({"merchant_identity": merchant_identity})
        if not business_parameters:
            g_log.error("get merchant %s parameters failed", merchant_identity)
            return 60319, "get merchant parameters failed"
        balance = money * business_parameters["balance_ratio"]

        # 存入数据库
        code, message = merchant_credit_update(**{"numbers": numbers, "merchant_identity": merchant_identity,
                                                  "mode": "balance", "supplement": balance})
        if code != 60400:
            g_log.error("merchant recharge failed, %s", message)
            return 60314, "recharge failed"
        # last = int(message)
        # g_log.debug("last balance: %d", last)

        trade_no = kwargs.get("trade_no", "")
        # 更新记录入库
        collection = get_mongo_collection("balance_record")
        if not collection:
            g_log.error("get collection balance record failed")
            return 60315, "get collection balance record failed"
        result = collection.insert_one({"merchant_identity": merchant_identity, "time": datetime.now(),
                                        "operator": numbers, "money": money, "type": "recharge", "trade_no": trade_no})
        if not result:
            g_log.error("insert recharge record failed")
            return 60316, "insert recharge record failed"

        # collection = get_mongo_collection("trade_no")
        # if not collection:
        #     g_log.error("get collection trade_no failed")
        #     return 60320, "get collection trade_no failed"
        # result = collection.find_one_and_update({"merchant_identity": merchant_identity, "trade_no": trade_no},
        #                                         {"$set": {"state": "done"}})
        # if not result:
        #     g_log.warning("can not find trade no: %s", trade_no)

        return 60300, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 60317, "exception"