Skip to content

Commit 898adc0

Browse files
committed
detail data
1 parent db6044f commit 898adc0

File tree

9 files changed

+80
-11
lines changed

9 files changed

+80
-11
lines changed

src/controllers/StockController.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ const requestApi = async(code: string) => {
5353

5454
class StockController {
5555

56-
async getByCode(code: string) {
57-
const stock = await getSharesRepository(Stock).findOne({code})
58-
return stock
56+
async getStock(ctx: Context) {
57+
const {id} = ctx.params
58+
const stockDetail = await StockService.getCode(id)
59+
ctx.Json({data: stockDetail})
5960
}
6061

62+
// async getByCode(code: string) {
63+
// const stock = await getSharesRepository(Stock).findOne({code})
64+
// return stock
65+
// }
66+
6167
async pages1(ctx: Context) {
6268
const { page = 1, pageSize = 10, code, name, market, block } = ctx.query
6369
const [list, total] = await StockService.pages(page, pageSize, code, name, market, block)

src/core/koa.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface Context extends KoaContext {
1515
limit: number
1616
}
1717

18+
params: AnyObject
19+
1820
// response
1921
Json?: <T = any>(res: T | ResponseData<T> | (() => T)) => ResponseData<T>
2022
Pages?: <T = any>(res: ReturnPage<T>) => ResponseData<T[]>

src/daos/StockDao.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ import { getSharesManager, getSharesRepository } from '../database/dbUtils';
88

99
class StockDao {
1010

11+
12+
async getCode(id: number) {
13+
const stock = await getSharesRepository(Stock).findOne({id})
14+
return stock
15+
}
16+
17+
async getCodeByIds(ids: number[]) {
18+
const stock = await getSharesRepository(Stock).findByIds(ids)
19+
return stock
20+
}
21+
1122
async getByCode(code: string) {
1223
const stock = await getSharesRepository(Stock).findOne({code})
1324
return stock

src/daos/StockHistoryDao.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { Equal, Like, Between, FindManyOptions} from "typeorm";
2-
import { StockHistory } from '../entities/mysql/shares/stockHistory'
2+
import { StockHistory, History } from '../entities/mysql/shares/stockHistory'
33
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
44

55

66

77
class StockHistoryDao {
88

9+
async getLastestTrade(stockId: number) {
10+
const lastestTrade = await getSharesRepository(History).findOne({
11+
select: ['timestamp'],
12+
where: {
13+
stockId: Equal(stockId)
14+
},
15+
order: {
16+
timestamp: 'DESC'
17+
}
18+
})
19+
return lastestTrade
20+
}
21+
922
async pages(offset = 1, size = 10, stockId: number): Promise<[StockHistory[], number]> {
1023
let sqlList = `
1124
Select sh.*, s.code, s.name From stock_history_new sh Left Join stocks s On sh.stockId = s.id
1225
Where 1 = 1`
13-
let sqlTotal = `
14-
Select count(sh.id) total From stock_history_new sh Left Join stocks s On sh.stockId = s.id
15-
Where 1 = 1`
26+
let sqlTotal = `Select count(sh.id) total From stock_history_new sh Where 1 = 1`
1627

1728
const parameters = []
1829

src/entities/mysql/shares/stock.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@ export class Stock {
2424
@Column()
2525
amount: number // 单手成交数量
2626

27+
}
28+
29+
30+
export class StockDetail extends Stock {
31+
constructor(arg: Stock) {
32+
super()
33+
this.id = arg.id
34+
this.name = arg.name
35+
this.code = arg.code
36+
this.market = arg.market
37+
this.block = arg.block
38+
this.amount = arg.amount
39+
this.lastestTradeAt = 0
40+
}
41+
42+
43+
private lastestTradeAt = 0
44+
45+
get _lastestTradeAt() {
46+
return this.lastestTradeAt
47+
}
48+
set _lastestTradeAt(val) {
49+
this.lastestTradeAt = +val
50+
}
2751
}

src/middlewares/catch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as Koa from 'koa'
1+
import { Context } from '@core/koa'
22
import LogCtrl from '../controllers/LogsController'
33

4-
export default async (ctx: Koa.Context, next: () => Promise<any>) => {
4+
export default async (ctx: Context, next: () => Promise<any>) => {
55
// console.log('ctx-----------', ctx.header)
66
const start = Date.now()
77
try {

src/routes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ router
2828
.post('/api/batchShares', SharesCtrl.batchInsert)
2929
.post('/api/upload', FileCtrl.upload)
3030
.get('/api/stocks', StockCtrl.pages1)
31+
.get('/api/stocks/:id', StockCtrl.getStock)
3132
.get('/api/stockhistory', StockHistoryCtrl.pages)
3233
.get('/graphql', KoaGraphql({
3334
schema: RootSchema,

src/services/StockHistoryService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { DateFormat } from "../types/base";
88

99
class StockHistoryService {
1010

11+
async getLastestTrade(stockId: number) {
12+
const lastestTrade = await StockHistoryDao.getLastestTrade(stockId)
13+
return lastestTrade
14+
}
15+
1116
async pages(offset = 1, size = 10, code: string): Promise<[StockHistory[], number]> {
1217
const stock = await StockService.getByCode(code)
1318
if(stock) {

src/services/StockService.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { Equal, Like, Between, FindManyOptions, FindConditions } from "typeorm";
2-
import { Stock } from '../entities/mysql/shares/stock'
3-
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
2+
import { Stock, StockDetail } from '../entities/mysql/shares/stock'
43
import { EMarket, EBLock } from '../models/Stocks'
54
import StockDao from '../daos/StockDao'
5+
import StockHistoryDao from '../daos/StockHistoryDao'
66

77

88
class StockService {
99

10+
async getCode(id: number) {
11+
const stock = await StockDao.getCode(id);
12+
const lastestTrade = await StockHistoryDao.getLastestTrade(stock.id)
13+
const detail = new StockDetail(stock)
14+
console.log(detail, '---------')
15+
detail._lastestTradeAt = lastestTrade ? lastestTrade.timestamp : null
16+
return detail
17+
}
18+
1019
async getByCode(code: string) {
1120
const stock = await StockDao.getByCode(code);
1221
return stock

0 commit comments

Comments
 (0)