def consumptionList(account_id, started_at, ended_at, region_id, resource_type, page_no=1, page_size=15, headers=None, **kwargs): ''' 查询消费记录 ''' try: consumptionDao = ConsumptionDao() result = consumptionDao.getAmountTotal(account_id, started_at, ended_at, region_id, resource_type, page_no, page_size) dataResult = [] if result: for consumption in result: dataResult.append(consumption) dataResult = totalAmount(dataResult) return outSuccess( "consumptionList", pagePackage('consumptions', dataResult, page_no=result.no, page_size=result.page_size, total=result.total)) except Exception as e: LOG.error(str(e)) LOG.error(traceback.format_exc()) return outError("取得消费记录列表失败!")
def getConsumptionSummary(account_id, started_at, ended_at): '''账户消费明细汇总''' try: consumptionDao = ConsumptionDao() result = consumptionDao.getConsumptionSummary(account_id, started_at, ended_at) return outSuccess("consumptionsummary", result) except Exception as e: LOG.error(str(e)) return outError("取得账户消费汇总失败!")
def subAccountConsumptionList(account_id, started_at, ended_at, condition, page_no=1, page_size=15): '''子账户消费列表''' try: account = Account() account.account_id = account_id accountDao = AccountDao(account) accountDao.detail() rows, count = accountDao.subAccountList(accountDao.account.user_id, condition, page_no, page_size) result = [] if datetime2Str(started_at) == change2UTC( datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m') + '-01', format='%Y-%m-%d') and datetime2Str( datetime2Str(ended_at, outformat='%Y-%m-%d %H'), informat='%Y-%m-%d %H') == change2UTC( datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H'), format='%Y-%m-%d %H'): if rows: accountDao = AccountDao() for row in rows: account = Account() account.account_id = row['account_id'] accountDao.account = account accountDao.detail() row['amount_total'] = accountDao.account.current_month_amount row['standard_amount_total'] = accountDao.account.current_month_standard_amount result.append(row) else: if rows: consumptionDao = ConsumptionDao() for row in rows: total = consumptionDao.getAmountSummary( row['account_id'], started_at, ended_at) result.append(dict(row, **total)) return outSuccess( 'subAccountConsumptionList', pagePackage("accounts", result, page_no=page_no, page_size=page_size, total=count)) except Exception as e: LOG.error(str(e)) LOG.error(traceback.format_exc()) return outError("取得子账户消费列表失败!")
def generateBill(started_at=None, ended_at=None, last=None, current=None): '''生成账单''' if isinstance(started_at, str): started_at = datetime.datetime.strptime(started_at, '%Y-%m-%d %H:%M:%S') if isinstance(ended_at, str): ended_at = datetime.datetime.strptime(ended_at, '%Y-%m-%d %H:%M:%S') accountDao = AccountDao() consumptionDao = ConsumptionDao() # rows = accountDao.list(condition={"status":"normal"}) rows = accountDao.list() billDao = BillDao() for row in rows: account_id = row.account_id if billDao.checkBill(account_id, started_at, ended_at): continue result = consumptionDao.getAmountTotal_new(account_id, started_at, ended_at) if result: result = totalAmount_bill(result) bill = Bill() bill.account_id = account_id bill.started_at = started_at bill.ended_at = ended_at bill.no = generation() bill.bill_items = [] bill.amount = 0 bill.gift_amount = 0 bill.standard_amount = 0 for bill_item_data in result: billItem = BillItem() getObjFromJson(billItem, bill_item_data) if bill_item_data['amount_total'] is None: bill_item_data['amount_total'] = 0 if bill_item_data['standard_total'] is None: bill_item_data['standard_total'] = 0 if bill_item_data['gift_total'] is None: bill_item_data['gift_total'] = 0 bill.amount += float(bill_item_data['amount_total']) bill.standard_amount += float(bill_item_data['standard_total']) bill.gift_amount += float(bill_item_data['gift_total']) billItem.amount = round(float(bill_item_data['amount_total']), 2) billItem.standard_amount = round( float(bill_item_data['standard_total']), 2) billItem.gift_amount = round( float(bill_item_data['gift_total']), 2) bill.bill_items.append(billItem) bill.amount = round(bill.amount, 2) bill.type = 'cloud' billDao.bill = bill billDao.add()
def consumptionForecast(account_id, headers=None, **kwargs): # try: consumptionDao = ConsumptionDao() result = consumptionDao.getForecast(account_id) result_cdn = consumptionDao.getCDNForecast(account_id) result_naas = consumptionDao.getNAASForcast(account_id) dataResult = [] if result_cdn: for cdn in result_cdn: result.append(cdn) if result_naas: for naas in result_naas: result.append(naas) dataResult = forecast(result) return outSuccess('forecast', createForecast(dataResult))
def generateRebateBill(started_at=None, ended_at=None, last=None, current=None): if isinstance(started_at, str): started_at = datetime.strptime(started_at, '%Y-%m-%d %H:%M:%S') if isinstance(ended_at, str): ended_at = datetime.strptime(ended_at, '%Y-%m-%d %H:%M:%S') accountDao = AccountDao() consumptionDao = ConsumptionDao() rebatebillDao = RebateBillDao() rows = accountDao.getAllProjectAdminAccount() if rows: for row in rows: account_id = row.account_id user_id = row.user_id if rebatebillDao.checkRebateBill(account_id, started_at, ended_at): continue subaccounts = accountDao.subAccountList(user_id, page_size=50000)[0] if subaccounts: rebatebill = RebateBill() rebatebill.started_at = started_at rebatebill.ended_at = ended_at rebatebill.account_id = account_id rebatebill_rebate_amount = 0 rebatebill_subaccount_amount = 0 rebatebill_subaccount_gift_amount = 0 rebatebill.rebate_subbills = [] rebatebill.no = generation() for subaccount in subaccounts: temp_account_id = subaccount['account_id'] result = consumptionDao.getRebateSubBill( temp_account_id, started_at, ended_at) if result: rebatesubbill = RebateSubBill() rebatesubbill.rebate_bill_items = [] for item in result: result2 = consumptionDao.getrebatebillitem( temp_account_id, started_at, ended_at) if result2: for item2 in result2: rebatebillitem = RebateBillItem() rebatebillitem.started_at = started_at rebatebillitem.ended_at = ended_at rebatebillitem.amount = round( item2['amount_total'], 2) rebatebillitem.rebate_amount = round( item2['rebate_amount_total'], 2) rebatebillitem.resource_id = item2[ 'resource_id'] rebatebillitem.resource_type = item2[ 'resource_type'] rebatebillitem.resource_name = item2[ 'resource_name'] rebatebillitem.region_id = item2[ 'region_id'] rebatebillitem.gift_amount = round( item2['gift_amount_total'], 2) if item2['gift_amount_total'] else 0 rebatesubbill.rebate_bill_items.append( rebatebillitem) rebatebill_rebate_amount += float( item['rebate_amount_total'] ) if item['rebate_amount_total'] else 0 rebatebill_subaccount_amount += float( item['amount_total'] ) if item['amount_total'] else 0 rebatebill_subaccount_gift_amount += float( item['gift_amount_total'] ) if item['gift_amount_total'] else 0 rebatesubbill.account_id = temp_account_id rebatesubbill.started_at = started_at rebatesubbill.ended_at = ended_at rebatesubbill.rebate_amount = round( item['rebate_amount_total'], 2) rebatesubbill.amount = round( item['amount_total'], 2) rebatesubbill.gift_amount = round( item['gift_amount_total'], 2) rebatebill.rebate_subbills.append(rebatesubbill) rebatebill.rebate_amount = round(rebatebill_rebate_amount, 2) rebatebill.subaccount_amount = round( rebatebill_subaccount_amount, 2) rebatebill.subaccount_gift_amount = round( rebatebill_subaccount_gift_amount, 2) rebatebillDao.rebateBill = rebatebill rebatebillDao.add()