From ba9adfe7991cab4f6fb0e236ecfc988cdb175e46 Mon Sep 17 00:00:00 2001 From: clz'lab Date: Fri, 18 Nov 2022 17:11:52 +0800 Subject: [PATCH] feat: tranfer data --- scripts/bills_mongo2mysql.py | 37 ++++++++++++++++++ scripts/labels_mongo2mysql.py | 38 +++++++++++++++++++ .../fadinglight/services/BillServiceImpl.kt | 10 ++++- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 scripts/bills_mongo2mysql.py create mode 100644 scripts/labels_mongo2mysql.py diff --git a/scripts/bills_mongo2mysql.py b/scripts/bills_mongo2mysql.py new file mode 100644 index 0000000..1f63760 --- /dev/null +++ b/scripts/bills_mongo2mysql.py @@ -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()) \ No newline at end of file diff --git a/scripts/labels_mongo2mysql.py b/scripts/labels_mongo2mysql.py new file mode 100644 index 0000000..3a7e2ec --- /dev/null +++ b/scripts/labels_mongo2mysql.py @@ -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()) \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt index ffc2b15..fa312c0 100644 --- a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt @@ -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 = 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 = transaction { Bills - .select(Bills.date eq "${year}-${month}-${day}") + .select(Bills.date eq "${year}-${formatSingletonNumber(month)}-${formatSingletonNumber(day)}") .map(::resultRowToBill) }