feat: tranfer data
This commit is contained in:
parent
f2b955c7fc
commit
ba9adfe799
37
scripts/bills_mongo2mysql.py
Normal file
37
scripts/bills_mongo2mysql.py
Normal 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())
|
38
scripts/labels_mongo2mysql.py
Normal file
38
scripts/labels_mongo2mysql.py
Normal 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())
|
|
@ -11,6 +11,12 @@ import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
|
||||||
class BillServiceImpl : BillService {
|
class BillServiceImpl : BillService {
|
||||||
|
private fun formatSingletonNumber(n: String) = if (n.length == 1) {
|
||||||
|
"0$n"
|
||||||
|
} else {
|
||||||
|
n
|
||||||
|
}
|
||||||
|
|
||||||
private fun resultRowToBill(row: ResultRow) = Bill(
|
private fun resultRowToBill(row: ResultRow) = Bill(
|
||||||
id = row[Bills.id].value,
|
id = row[Bills.id].value,
|
||||||
type = BillType.valueOf(row[Bills.type]),
|
type = BillType.valueOf(row[Bills.type]),
|
||||||
|
@ -23,14 +29,14 @@ class BillServiceImpl : BillService {
|
||||||
|
|
||||||
override suspend fun getManyBills(year: String, month: String): List<Bill> = transaction {
|
override suspend fun getManyBills(year: String, month: String): List<Bill> = transaction {
|
||||||
Bills
|
Bills
|
||||||
.select(Bills.date like "%${year}-${month}%")
|
.select(Bills.date like "%${year}-${formatSingletonNumber(month)}%")
|
||||||
.map(::resultRowToBill)
|
.map(::resultRowToBill)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override suspend fun getManyBills(year: String, month: String, day: String): List<Bill> = transaction {
|
override suspend fun getManyBills(year: String, month: String, day: String): List<Bill> = transaction {
|
||||||
Bills
|
Bills
|
||||||
.select(Bills.date eq "${year}-${month}-${day}")
|
.select(Bills.date eq "${year}-${formatSingletonNumber(month)}-${formatSingletonNumber(day)}")
|
||||||
.map(::resultRowToBill)
|
.map(::resultRowToBill)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user