示例#1
0
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"
示例#2
0
def parameters_record_retrieve_all(numbers):
    """
    查找所有商家的操作纪录
    :param numbers:
    :return:
    """
    try:
        if not account_is_platform(numbers):
            g_log.error("%s not platform", numbers)
            return 50615, "no privilege"

        # 广播查找所有商家的经营参数更新纪录
        collection = get_mongo_collection("parameters_record")
        if not collection:
            g_log.error("get collection parameters record failed")
            return 50616, "get collection parameters record failed"
        records = collection.find({}).sort("merchant_identity")
        if not records:
            g_log.error("retrieve parameters record failed")

        return 50600, records
    except Exception as e:
        g_log.error("%s", e)
        return 50617, "exception"
示例#3
0
def merchant_credit_flow_retrieve_all(numbers):
    """
    查找所有商家的操作纪录
    :param numbers:
    :return:
    """
    try:
        if not account_is_platform(numbers):
            g_log.error("%s not platform", numbers)
            return 60116, "no privilege"

        # 广播查找所有商家的积分详情
        collection = get_mongo_collection("flow")
        if not collection:
            g_log.error("get collection flow failed")
            return 60117, "get collection flow failed"
        records = collection.find({"deleted": 0}).sort("merchant_identity")
        if not records:
            g_log.error("retrieve flow failed")

        return 60100, records
    except Exception as e:
        g_log.error("%s", e)
        return 60118, "exception"
示例#4
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"
示例#5
0
def merchant_credit_update(**kwargs):
    """
    商家积分变更
    积分类型:可发行积分总量、已发行积分、积分互换IN & OUT、用户消费积分、账户余额变更
    mode=["may_issued", "issued", "interchange_in", "interchange_out", "consumption", "balance"]
    :param kwargs: {"numbers": 11868898224, "merchant_identity": "", "mode": may_issued, "supplement": 1000}
    :return:
    """
    try:
        # 检查请求用户numbers必须是平台管理员或者商家管理员
        numbers = kwargs.get("numbers", "")
        if not account_is_platform(numbers) and not account_is_valid_merchant(numbers):
            g_log.warning("not manager and not platform, %s", numbers)
            return 60411, "no privilege"

        # 必须是已认证商家,在补充可发行积分总量时已经做过验证,此处省略
        merchant_identity = kwargs.get("merchant_identity", "")
        if not account_is_platform(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 60412, "not manager"
            merchant_founder = merchant["merchant_founder"]
            g_log.debug("merchant %s founder %s", merchant_identity, merchant_founder)

        mode = kwargs.get("mode", "")
        modes = ["may_issued", "issued", "interchange_in", "interchange_out", "consumption", "balance"]
        if mode not in modes:
            g_log.error("not supported mode %s", mode)
            return 60413, "not supported mode"
        # TODO... 积分检查
        supplement = kwargs.get("supplement", 0)
        value = {"merchant_identity": merchant_identity, mode: supplement, "deleted": 0}

        # 存入数据库
        collection = get_mongo_collection("flow")
        if not collection:
            g_log.error("get collection flow failed")
            return 60414, "get collection flow failed"
        flow = collection.find_one_and_update({"merchant_identity": merchant_identity, "deleted": 0},
                                              {"$inc": {mode: supplement}}, return_document=ReturnDocument.BEFORE)

        # 第一次更新,则插入一条
        if not flow:
            g_log.debug("insert new flow")
            flow = collection.insert_one(value)
            if not flow:
                g_log.error("update merchant %s %s credit failed", merchant_identity, mode)
                return 60415, "update failed"
        g_log.debug("update merchant %s credit succeed", mode)
        # last = flow[mode]    # 更新前的值

        # 更新记录入库
        collection = get_mongo_collection("flow_record")
        if not collection:
            g_log.error("get collection flow record failed")
            return 60416, "get collection flow record failed"
        quantization = "mode:%s, supplement:%d" % (mode, supplement)
        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 60400, last
        return 60400, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 60417, "exception"