Skip to content

Commit 606b150

Browse files
committed
fix
1 parent 898adc0 commit 606b150

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

src/controllers/StockHistoryController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ class SharesController {
148148

149149
async pages(ctx: Context) {
150150
const { code, page = 1, pageSize = 10 } = ctx.query
151-
if(!code) {
152-
throw new Error('code参数缺失')
153-
}
151+
// if(!code) {
152+
// throw new Error('code参数缺失')
153+
// }
154154
const [list, total] = await StockHistoryService.pages(Number(page), Number(pageSize), code)
155155
ctx.Pages({list, total})
156156
}

src/daos/StockDao.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class StockDao {
1414
return stock
1515
}
1616

17-
async getCodeByIds(ids: number[]) {
18-
const stock = await getSharesRepository(Stock).findByIds(ids)
19-
return stock
17+
async getByIds(ids: number[]) {
18+
const stocks = await getSharesRepository(Stock).findByIds(ids, {
19+
select: ['id', 'code', 'name']
20+
})
21+
return stocks
2022
}
2123

2224
async getByCode(code: string) {

src/daos/StockHistoryDao.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@ class StockHistoryDao {
1919
return lastestTrade
2020
}
2121

22-
async pages(offset = 1, size = 10, stockId: number): Promise<[StockHistory[], number]> {
23-
let sqlList = `
24-
Select sh.*, s.code, s.name From stock_history_new sh Left Join stocks s On sh.stockId = s.id
25-
Where 1 = 1`
26-
let sqlTotal = `Select count(sh.id) total From stock_history_new sh Where 1 = 1`
22+
async pages(offset = 1, size = 10, stockId?: number): Promise<[StockHistory[], number]> {
23+
let sqlList = `Select
24+
id,
25+
stockId,
26+
volume,
27+
open,
28+
high,
29+
low,
30+
close,
31+
chg,
32+
percent,
33+
turnoverrate,
34+
amount,
35+
pe,
36+
pb,
37+
ps,
38+
pcf,
39+
market_capital From stock_history_new sh`
40+
let sqlTotal = `Select count(sh.id) total From stock_history_new sh`
2741

2842
const parameters = []
2943

src/services/StockHistoryService.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { StockHistory } from '../entities/mysql/shares/stockHistory'
2+
import { Stock } from '../entities/mysql/shares/stock'
23
import StockService from './StockService'
34
import StockHistoryDao from '../daos/StockHistoryDao'
45
import { formatDate } from "../utils/tools";
@@ -14,19 +15,32 @@ class StockHistoryService {
1415
}
1516

1617
async pages(offset = 1, size = 10, code: string): Promise<[StockHistory[], number]> {
17-
const stock = await StockService.getByCode(code)
18-
if(stock) {
19-
const [list, total] = await StockHistoryDao.pages(offset, size, stock.id)
18+
// const stock = await StockService.getByCode(code)
19+
// if(stock) {
20+
const [list, total] = await StockHistoryDao.pages(offset, size)
21+
const ids = list.map(item => item.stockId).reduce<number[]>((pre, cur) => {
22+
if(!pre.includes(cur)) {
23+
pre.push(cur)
24+
}
25+
return pre
26+
}, [])
27+
const stocks = await StockService.getByIds(ids)
28+
const stockNameObj = stocks.reduce<Record<number, Stock>>((pre, cur) => {
29+
return {...pre, ...{[cur.id]: cur}}
30+
}, {})
2031
return [
2132
list.map(item => {
2233
item.tradeAt = formatDate(+item.timestamp, DateFormat.Date)
34+
const stock = stockNameObj[item.stockId]
35+
item.name = stock.name
36+
item.code = stock.code
2337
return item
2438
}),
2539
total
2640
]
27-
} else {
28-
return [[], 0]
29-
}
41+
// } else {
42+
// return [[], 0]
43+
// }
3044
}
3145
}
3246

src/services/StockService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import StockHistoryDao from '../daos/StockHistoryDao'
77

88
class StockService {
99

10+
async getByIds(ids: number[]) {
11+
const stocks = await StockDao.getByIds(ids);
12+
return stocks
13+
}
14+
1015
async getCode(id: number) {
1116
const stock = await StockDao.getCode(id);
1217
const lastestTrade = await StockHistoryDao.getLastestTrade(stock.id)

0 commit comments

Comments
 (0)