def update(billingItemJson, headers=None, **kwargs):
    try:
        billingItemJson = json.loads(billingItemJson)
        billingItemDict = billingItemJson['billing_item']
        billingItem = BillingItem()
        billingItem.billing_item_id = billingItemDict['billing_item_id']
        billingItemDao = BillingItemDao(billingItem)
        billingItemDao.update(billingItemDict)
        return outSuccess(
            "billing_item",
            getJsonFromObj(billingItemDao.billingItem, BILLINGITEM_OBJECT_KEY))
    except Exception as e:
        LOG.error(str(e))
        LOG.error(traceback.format_exc())
        return outError("更新计费项失败!")
 def list(self, account_id, region_id=None, session=None):
     if not session:
         session = sa.get_session()
     query = session.query(Discount)
     if account_id:
         query = query.filter(Discount.account_id == account_id)
     if region_id:
         billingItem = BillingItem()
         billingItem.region_id = region_id
         billingItemDao = BillingItemDao(billingItem)
         billingItems = billingItemDao.list(session)
         query = query.filter(
             Discount.billing_item_id.in_(billingItem.billing_item_id
                                          for billingItem in billingItems))
     rows = query.all()
     if rows:
         for row in rows:
             row.billing_item
     return rows
def get_price_for_nass(network_type, bandwidth, distance):
    try:
        billingItem = BillingItem()
        billingItemDao = BillingItemDao(billingItem)
        result = billingItemDao.get_price_for_nass(network_type, bandwidth,
                                                   distance)
        return outSuccess("price", result.price * Decimal(bandwidth))
    except Exception as e:
        LOG.error(str(e))
        LOG.error(traceback.format_exc())
        return outError("取得计费项失败!")
def getBillingItem(billing_item_id,
                   region_id,
                   billing_item,
                   headers=None,
                   **kwargs):
    try:
        billingItem = BillingItem()
        if billing_item_id:
            billingItem.billing_item_id = billing_item_id
        if region_id:
            billingItem.region_id = region_id
        if billing_item:
            billingItem.billing_item = billing_item
        billingItemDao = BillingItemDao(billingItem)
        result = billingItemDao.getBillingItem()
        return outSuccess("billing_item",
                          getJsonFromObj(result, BILLINGITEM_OBJECT_KEY))
    except Exception as e:
        LOG.error(str(e))
        LOG.error(traceback.format_exc())
        return outError("取得计费项失败!")
def list(region_id, headers=None, **kwargs):
    '''
        计费项列表
    '''
    try:
        billingItem = BillingItem()
        if region_id:
            billingItem.region_id = region_id
        billingItemDao = BillingItemDao(billingItem)
        result = billingItemDao.list()
        dataResult = []
        if result:
            for billingItem in result:
                dataResult.append(
                    getJsonFromObj(billingItem,
                                   notInDict=BILLINGITEM_OBJECT_KEY))
        return outSuccess("billing_itemList", dataResult)
    except Exception as e:
        LOG.error(str(e))
        LOG.error(traceback.format_exc())
        return outError("取得计费项列表失败!")
 def getDiscountDetail(self, condition=None, session=None):
     if not session:
         session = sa.get_session()
     if condition is None:
         return session.query(Discount)
     query = session.query(Discount)
     if condition:
         billingItem = None
         for (attr, attrValue) in [(key, value)
                                   for (key, value) in condition.items()]:
             if attr == 'account_id':
                 query = query.filter(Discount.account_id == attrValue)
             if attr == 'billing_item_id':
                 query = query.filter(Discount.billing_item_id == attrValue)
             if attr == 'region_id':
                 if not billingItem:
                     billingItem = BillingItem()
                 billingItem.region_id = attrValue
             if attr == 'billing_item':
                 if not billingItem:
                     billingItem = BillingItem()
                 billingItem.billing_item = attrValue
         if billingItem:
             billingItemDao = BillingItemDao(billingItem)
             billingItem = billingItemDao.getBillingItem(session)
             query = query.filter(Discount.billing_item == billingItem)
     row = query.first()
     if row:
         row.billing_item
     return row
    def getQueryByCondition(self, condition=None, session=None):
        '''条件查询'''
        if not session:
            session = sa.get_session()
        if condition is None:
            return session.query(Discount)
        query = session.query(Discount)
        if condition:
            billingItem = None
            for (attr, attrValue) in [(key, value)
                                      for (key, value) in condition.items()]:
                if attr == 'account_id':
                    query = query.filter(Discount.account_id == attrValue)
                if attr == 'billing_item_id':
                    query = query.filter(Discount.billing_item_id == attrValue)
                if attr == 'region_id':
                    if not billingItem:
                        billingItem = BillingItem()
                    billingItem.region_id = attrValue
                if attr == 'billing_item':
                    if not billingItem:
                        billingItem = BillingItem()
                    billingItem.billing_item = attrValue
            if billingItem:
                billingItemDao = BillingItemDao(billingItem)
                billingItem = billingItemDao.getBillingItem(session)
                query = query.filter(Discount.billing_item == billingItem)

#        if likeCondition:
#            for (attr,attrValue) in [(key,value)for (key,value) in likeCondition.items()]:
#                if attr=='username':
#                    query=query.filter(Discount.username.like('%'+attrValue+'%'))
        return query