feat: tranfer data

This commit is contained in:
clz'lab 2022-11-18 17:11:52 +08:00
parent f2b955c7fc
commit ba9adfe799
3 changed files with 83 additions and 2 deletions

View File

@ -0,0 +1,37 @@
import aiohttp
import asyncio
mongo_url = 'http://www.fadinglight.cn:8088/list'
mysql_url = 'http://localhost:8080/api/v1/bill/'
async def getMongoBills():
async with aiohttp.ClientSession() as session:
async with session.get(mongo_url) as response:
data = await response.json()
return data
async def postBillsToMysql(bills):
async with aiohttp.ClientSession() as session:
async with session.post(mysql_url, json=bills) as response:
data = await response.json()
return data
def dealOneBill(bill):
bill.setdefault('type', 0)
return dict(
money=bill['money'],
cls=bill['cls'],
label=bill['label'],
date=bill['date'],
options=bill['options'],
type="consume" if bill['type'] == 0 else "income",
)
async def main():
data = await getMongoBills()
bills = data['data']
bills = list(map(dealOneBill, bills))
respData = await postBillsToMysql(bills)
print(respData)
asyncio.run(main())

View File

@ -0,0 +1,38 @@
import aiohttp
import asyncio
mongo_url = 'http://www.fadinglight.cn:8088/class'
mysql_url = 'http://localhost:8080/api/v1/label/'
async def getMongoBills():
async with aiohttp.ClientSession() as session:
async with session.get(mongo_url) as response:
data = await response.json()
return data
async def postBillsToMysql(bills):
async with aiohttp.ClientSession() as session:
async with session.post(mysql_url, json=bills) as response:
data = await response.json()
return data
def dealOneBill(bill):
bill.setdefault('type', 0)
return dict(
money=bill['money'],
cls=bill['cls'],
label=bill['label'],
date=bill['date'],
options=bill['options'],
type="consume" if bill['type'] == 0 else "income",
)
async def main():
data = await getMongoBills()
labels = data['data']
print(labels)
# bills = list(map(dealOneBill, bills))
# respData = await postBillsToMysql(bills)
# print(respData)
asyncio.run(main())

View File

@ -11,6 +11,12 @@ import org.jetbrains.exposed.sql.transactions.transaction
class BillServiceImpl : BillService {
private fun formatSingletonNumber(n: String) = if (n.length == 1) {
"0$n"
} else {
n
}
private fun resultRowToBill(row: ResultRow) = Bill(
id = row[Bills.id].value,
type = BillType.valueOf(row[Bills.type]),
@ -23,14 +29,14 @@ class BillServiceImpl : BillService {
override suspend fun getManyBills(year: String, month: String): List<Bill> = transaction {
Bills
.select(Bills.date like "%${year}-${month}%")
.select(Bills.date like "%${year}-${formatSingletonNumber(month)}%")
.map(::resultRowToBill)
}
override suspend fun getManyBills(year: String, month: String, day: String): List<Bill> = transaction {
Bills
.select(Bills.date eq "${year}-${month}-${day}")
.select(Bills.date eq "${year}-${formatSingletonNumber(month)}-${formatSingletonNumber(day)}")
.map(::resultRowToBill)
}