Beispiel #1
0
def upload_token_retrieve_debug(kind, numbers, merchant_identity=""):
    """
    debug环境生成上传凭证
    指定客户头像路径c/avatar/numbers, 商家logo路径m/logo/numbers, 商家活动路径ma/poster/numbers
    :param kind: 资源类型
    :param numbers: 账号
    :return: (upload_token, key)
    """
    q = Auth(g_access_key, g_secret_key)

    kind_to_key = {"c_avatar": "c/avatar", "m_logo": "m/logo", "m_qrcode": "m/qrcode", "a_poster": "a/poster"}
    key = "%s/%s/%s" % (kind_to_key.get(kind, "dummy"), numbers, datetime.now().strftime('%b%d%y%H%M%S'))
    policy = {"scope": g_bucket_name_debug + ":" + key, "mimeLimit": "image/*"}
    upload_token = q.upload_token(g_bucket_name_debug, key=key, expires=3600, policy=policy)
    g_log.debug("debug upload token, [key:%s, policy:%s, token:%s]", key, policy, upload_token)

    # 修改资料图片路径
    if kind == "c_avatar":
        # 平台修改客户头像资料
        if not account_is_valid_consumer(numbers):
            g_log.warning("%s invalid consumer account", numbers)
            return 70111, "invalid consumer account"
        # code, message = consumer_update_with_numbers(numbers, avatar=key)
        # if code != 20400:
        #     g_log.warning("update consumer %s avatar %s failed", numbers, key)
        #     return 70112, "update consumer avatar failed"
    elif kind == "m_logo":
        # 平台修改商家logo资料
        if not account_is_valid_merchant(numbers):
            return 70113, "invalid merchant account"
        if not merchant_identity:
            return 70114, "missing merchant identity"
        # code, message = merchant_update_with_numbers(numbers, merchant_identity, logo=key)
        # if code != 30400:
        #     g_log.warning("update merchant logo %s failed", key)
        #     return 70115, "update merchant logo failed"
    elif kind == "m_qrcode":
        if not account_is_valid_merchant(numbers):
            return 70113, "invalid merchant account"
        if not merchant_identity:
            return 70114, "missing merchant identity"
    elif kind == "a_poster":
        # 平台修改活动图片资料
        if not account_is_valid_merchant(numbers):
            return 70116, "invalid merchant account"
        if not merchant_identity:
            return 70117, "missing merchant identity"
    else:
        g_log.debug("unsupported resource kind %s", kind)
        return 70118, "unsupported resource kind"

    return 70100, (upload_token, key)
Beispiel #2
0
def upload_token_retrieve_online(kind, numbers, merchant_identity=""):
    """
    线上环境生成上传凭证
    平台回调完成,生成资源下载key,并存入平台数据库
    回调返回数据结构
    {
        "key": "key",
        "payload": {
            {"success":true,"name":"key"}
        }
    }
    :param kind: 上传的资源类型
    :param numbers: 账号
    :return:
    """
    # 7牛环境初始化
    q = Auth(g_access_key, g_secret_key)

    # 回调url
    kind_to_path = {"c_avatar": "consumer", "m_logo": "merchant", "a_poster": "activity"}
    callback_url = "%s/%s" % (g_qiniu_callback_url, kind_to_path.get(kind, "dummy"))

    # 回调post参数
    kind_to_type = {"c_avatar": "update_avatar", "m_logo": "update_logo", "a_poster": "update_poster"}
    callback_body_type = kind_to_type.get(kind, "dummy")
    callback_body = {"type": callback_body_type, "numbers": numbers, "merchant": merchant_identity, "hash": "$(etags)"}

    policy = {"callbackUrl": callback_url, "callbackBody": callback_body, "callbackBodyType": "application/json",
              "callbackFetchKey": 1, "mimeLimit": "image/*"}

    upload_token = q.upload_token(g_bucket_name, expires=3600, policy=policy)
    g_log.debug("debug upload token, [policy:%s, token:%s]", policy, upload_token)

    # 修改资料图片路径
    if kind == "c_avatar":
        # 平台修改客户头像资料
        if not account_is_valid_consumer(numbers):
            g_log.warning("%s invalid consumer account", numbers)
            return 70117, "invalid consumer account"
    elif kind == "m_logo":
        # 平台修改商家logo资料
        if not account_is_valid_merchant(numbers):
            return 70118, "invalid consumer account"
    elif kind == "a_poster":
        # 平台修改活动图片资料
        if not account_is_valid_merchant(numbers):
            return 70119, "invalid consumer account"
    else:
        g_log.debug("unsupported resource kind %s", kind)
        return 70120, "unsupported resource kind"

    return 70100, upload_token
Beispiel #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"
Beispiel #4
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"
Beispiel #5
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"
Beispiel #6
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"
Beispiel #7
0
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"
Beispiel #8
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"
Beispiel #9
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"
Beispiel #10
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"
Beispiel #11
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"
Beispiel #12
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"
Beispiel #13
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"