示例#1
0
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"
示例#2
0
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"
示例#3
0
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"
示例#4
0
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"
示例#5
0
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"
示例#6
0
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"
示例#7
0
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"
示例#8
0
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"
示例#9
0
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"
示例#10
0
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"
示例#11
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"
示例#12
0
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"
示例#13
0
def consumption_ratio_update(**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 50411, "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 50412, "not manager"
        merchant_founder = merchant["merchant_founder"]
        g_log.debug("merchant %s founder %s", merchant_identity, merchant_founder)

        # if not merchant_is_verified(merchant_founder, merchant_identity):
        #     g_log.error("merchant %s not verified", merchant_identity)
        #     return 50413, "not verified"

        # 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 50413, "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 50414, "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 50414, "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 50415, "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 50416, "insert parameters record failed"
        return 50400, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 50417, "exception"
示例#14
0
def activity_update_with_numbers(numbers, **kwargs):
    """
    更新活动资料
    :param numbers: 用户电话号码
    :param kwargs: {"numbers": "18688982240", "title": "good taste", "introduce": "ego cogito ergo sum",
                    "poster": "a/18688982240/x87cd", "credit": 100, "activity_identity": "xij923f0a8m",
                    "expire_time": "2016-12-12"}
    :return: (20400, "yes")/成功,(>20400, "errmsg")/失败
    """
    try:
        # 检查合法账号
        if not account_is_valid_merchant(numbers):
            g_log.warning("invalid merchant account %s", numbers)
            return 70411, "invalid phone number"

        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 70412, "not manager"
        activity_identity = kwargs.get("activity_identity")

        value = {}
        # 标题不能超过32字节,超过要截取前32字节
        title = kwargs.get("title")
        if title:
            if len(title) > 32:
                g_log.warning("too long nickname %s", title)
                title = title[0:32]
            value["title"] = title

        credit = kwargs.get("credit", 0)
        if credit:
            value["credit"] = credit

        # 活动介绍不能超过512字节,超过要截取前512字节
        introduce = kwargs.get("introduce")
        if introduce:
            if len(introduce) > 512:
                g_log.warning("too long introduce %s", introduce)
                introduce = introduce[0:512]
            value["introduce"] = introduce

        # TODO...检查
        poster = kwargs.get("poster")
        if poster:
            value["poster"] = poster

        expire_time = kwargs.get("expire_time", "")
        if expire_time:
            value["expire_time"] = datetime.strptime(expire_time, "%Y-%m-%d")

        g_log.debug("update activity material: %s", value)
        # 存入数据库
        collection = get_mongo_collection("activity")
        if not collection:
            g_log.error("get collection activity failed")
            return 70413, "get collection activity failed"
        activity = collection.find_one_and_update({"merchant_identity": merchant_identity,
                                                   "_id": ObjectId(activity_identity), "deleted": 0}, {"$set": value})
        if not activity:
            g_log.error("activity %s exist", numbers)
            return 70414, "activity not exist"
        return 70400, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 70415, "exception"
示例#15
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"
示例#16
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"
示例#17
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"
示例#18
0
def merchant_credit_update_batch(**kwargs):
    """
    商家积分变更
    积分类型:可发行积分总量、已发行积分、积分互换IN & OUT、用户消费积分、账户余额变更
    mode=["may_issued", "issued", "interchange_in", "interchange_out", "consumption", "balance"]
    :param kwargs: {"numbers": 11868898224, "merchant_identity": "", "items": [("may_issued", 1000), ...]}
    :return:
    """
    try:
        # 检查请求用户numbers必须是平台管理员
        numbers = kwargs.get("numbers", "")
        merchant_identity = kwargs.get("merchant_identity", "")
        if numbers != "10000":
            if not account_is_valid_merchant(numbers):
                g_log.warning("not manager %s", numbers)
                return 60421, "no privilege"
            # 必须是已认证商家,在补充可发行积分总量时已经做过验证,此处省略

            merchant = user_is_merchant_manager(numbers, merchant_identity)
            if not merchant:
                g_log.error("%s is not merchant %s manager", numbers, merchant_identity)
                return 60422, "not manager"
            merchant_founder = merchant["merchant_founder"]
            g_log.debug("merchant %s founder %s", merchant_identity, merchant_founder)

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

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

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

        # 更新记录入库
        collection = get_mongo_collection("flow_record")
        if not collection:
            g_log.error("get collection flow record failed")
            return 60426, "get collection flow record failed"
        quantization = "&".join(["mode:" + mode + ", supplement:" + str(supplement) for (mode, supplement) in items])
        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, "yes"
    except Exception as e:
        g_log.error("%s", e)
        return 60427, "exception"