Skip to content

Commit f930208

Browse files
committed
stock history page
1 parent cce7752 commit f930208

File tree

8 files changed

+120
-124
lines changed

8 files changed

+120
-124
lines changed

src/controllers/StockHistoryController.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Equal, Like, Between, FindManyOptions} from "typeorm";
21
import { Context } from '@core/koa'
32
import { StockHistory, History, KeyofHistory } from '../entities/mysql/shares/stockHistory'
43
import { Guid } from "../utils/tools";
@@ -92,25 +91,6 @@ class SharesController {
9291
}
9392
}
9493

95-
static async insert(args: Partial<StockHistory>) {
96-
let model = new StockHistory()
97-
model.uuid = Guid()
98-
model.tradeAt = args.tradeAt
99-
model.open = args.open
100-
model.close = args.close
101-
model.high = args.high
102-
model.low = args.low
103-
model.total = args.total
104-
model.totalAmt = args.totalAmt
105-
model.amplitude = args.amplitude
106-
model.rasingRatio = args.rasingRatio
107-
model.rasingPrice = args.rasingPrice
108-
model.exchangeRatio = args.exchangeRatio
109-
const result = await getSharesManager().save(model)
110-
console.log(result.id)
111-
return result.id
112-
}
113-
11494
static async add(data: History) {
11595
let model = new History()
11696
model.stockId = data.stockId
@@ -169,24 +149,24 @@ class SharesController {
169149
async pages(ctx: Context) {
170150
const { code, page = 1, pageSize = 10 } = ctx.query
171151
if(!code) {
172-
ctx.Json({msg: 'code参数缺失', status: 400})
152+
throw new Error('code参数缺失')
173153
}
174-
const [list, total] = await StockService.pages(page, pageSize, code)
154+
const [list, total] = await StockHistoryService.pages(Number(page), Number(pageSize), code)
175155
ctx.Pages({list, total})
176156
}
177157

178158
async updateHistory(ctx: Context) {
179159
const { code } = ctx.fields
180160
const stock = await StockService.getByCode(code)
181-
const [list, total] = await StockHistoryService.pages(1, 10, stock.id)
182-
let lastestDate = 0
183-
if(total > 0) {
184-
lastestDate = list[total - 1].timestamp
185-
const today = new Date
186-
// const
187-
} else {
188-
//
189-
}
161+
// const [list, total] = await StockHistoryService.pages(1, 10, stock.id)
162+
// let lastestDate = 0
163+
// if(total > 0) {
164+
// lastestDate = list[total - 1].timestamp
165+
// const today = new Date
166+
// // const
167+
// } else {
168+
// //
169+
// }
190170
return stock
191171
}
192172
}

src/daos/StockDao.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Equal, Like, Between, FindManyOptions, FindConditions} from "typeorm";
2+
import { Context } from '@core/koa'
3+
import { Stock } from '../entities/mysql/shares/stock'
4+
import { EMarket, EBLock } from '../models/Stocks'
5+
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
6+
7+
8+
9+
class StockDao {
10+
11+
async getByCode(code: string) {
12+
const stock = await getSharesRepository(Stock).findOne({code})
13+
return stock
14+
}
15+
16+
async pages(offset = 1, size = 10, code?: string, name?: string, market?: EMarket, block?: EBLock) {
17+
const where: FindConditions<Stock> = {}
18+
if(code) {
19+
where.code = Like(`%${code}%`)
20+
}
21+
if(name) {
22+
where.name = Like(`%${name}%`)
23+
}
24+
if(market) {
25+
where.market = Equal(market)
26+
}
27+
if(block) {
28+
where.block = Equal(block)
29+
}
30+
const options: FindManyOptions<Stock> = {
31+
skip: (offset > 0 ? (offset - 1) : 0) * size,
32+
take: size,
33+
order: { id: 'ASC'},
34+
where
35+
}
36+
const pages = await getSharesRepository(Stock).findAndCount(options)
37+
return pages
38+
}
39+
}
40+
41+
export default new StockDao

src/daos/StockHistoryDao.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Equal, Like, Between, FindManyOptions} from "typeorm";
2+
import { StockHistory } from '../entities/mysql/shares/stockHistory'
3+
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
4+
5+
6+
7+
class StockHistoryDao {
8+
9+
async pages(offset = 1, size = 10, stockId: number): Promise<[StockHistory[], number]> {
10+
let sqlList = `
11+
Select sh.*, s.code, s.name From stock_history_new sh Left Join stocks s On sh.stockId = s.id
12+
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`
16+
17+
const parameters = []
18+
19+
if(stockId) {
20+
const whereAnd = `
21+
And sh.stockId = ?
22+
`
23+
sqlList += whereAnd
24+
sqlTotal += whereAnd
25+
parameters.push(stockId)
26+
}
27+
sqlList += `
28+
Order By sh.timestamp Desc
29+
Limit ?,?;`
30+
31+
parameters.push((offset > 0 ? (offset - 1) : 0) * size, size)
32+
33+
const list = await getSharesManager().query(sqlList, parameters)
34+
const totalRow = await getSharesManager().query(sqlTotal, parameters)
35+
return [list, totalRow[0].total]
36+
}
37+
}
38+
39+
export default new StockHistoryDao
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { GJRecord } from './GJRecord'
22
import { Stock } from './stock'
3-
import { StockHistory, History } from './stockHistory'
3+
import { History } from './stockHistory'
44

55

66
export const ShareEntities = [
77
GJRecord,
88
Stock,
9-
StockHistory,
109
History
1110
]

src/entities/mysql/shares/stockHistory.ts

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'reflect-metadata'
22
import { Entity, Column, PrimaryColumn} from "typeorm";
3+
import { Stock } from './stock';
34

45
export type KeyofHistory = keyof History
56

@@ -84,48 +85,7 @@ export class History {
8485
net_volume_hk: number
8586
}
8687

87-
@Entity('stock_history_5')
88-
export class StockHistory {
89-
@PrimaryColumn({ unique: true })
90-
id: number
91-
92-
@Column()
93-
uuid: string
94-
95-
@Column()
96-
stockId: number
97-
98-
@Column()
99-
open: number
100-
101-
@Column()
102-
close: number
103-
104-
@Column()
105-
low: number
106-
107-
@Column()
108-
high: number
109-
110-
@Column()
111-
rasingPrice: number
112-
113-
@Column()
114-
rasingRatio: number
115-
116-
@Column()
117-
total: number
118-
119-
@Column()
120-
totalAmt: number
121-
122-
@Column()
123-
exchangeRatio: number
124-
125-
@Column()
126-
amplitude: number
127-
128-
@Column()
129-
tradeAt: number
13088

89+
export type StockHistory = History & Stock & {
90+
tradeAt: string
13191
}

src/routes/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import LogsCtrl from '../controllers/LogsController'
88
import ServerAPI from '../controllers/ServerAPIController'
99
import DoubleColorBallController from '../controllers/DoubleColorBallController'
1010
import SharesCtrl from '../controllers/SharesController'
11+
import StockCtrl from '../controllers/StockController'
12+
import StockHistoryCtrl from '../controllers/StockHistoryController'
1113

1214
const _PROD_ = process.env.NODE_ENV === 'production'
1315

@@ -25,6 +27,8 @@ router
2527
.get('/api/log-errors', LogsCtrl.errorsPages)
2628
.post('/api/batchShares', SharesCtrl.batchInsert)
2729
.post('/api/upload', FileCtrl.upload)
30+
.get('/api/stocks', StockCtrl.pages1)
31+
.get('/api/stockhistory', StockHistoryCtrl.pages)
2832
.get('/graphql', KoaGraphql({
2933
schema: RootSchema,
3034
graphql: _PROD_ ? false : true

src/services/StockHistoryService.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import { Equal, Like, Between, FindManyOptions} from "typeorm";
2-
import { Context } from '@core/koa'
3-
import { History } from '../entities/mysql/shares/stockHistory'
4-
import { Guid } from "../utils/tools";
5-
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
1+
import { StockHistory } from '../entities/mysql/shares/stockHistory'
2+
import StockService from './StockService'
3+
import StockHistoryDao from '../daos/StockHistoryDao'
4+
import { formatDate } from "../utils/tools";
5+
import { DateFormat } from "../types/base";
66

77

88

99
class StockHistoryService {
1010

11-
async pages(offset = 1, size = 10, stockId: number, code?: string, name?: string) {
12-
const options: FindManyOptions<History> = {
13-
skip: (offset > 0 ? (offset - 1) : 0) * size,
14-
take: size,
15-
order: { timestamp: 'DESC' },
16-
where: {
17-
stockId: Equal(stockId)
18-
}
11+
async pages(offset = 1, size = 10, code: string): Promise<[StockHistory[], number]> {
12+
const stock = await StockService.getByCode(code)
13+
if(stock) {
14+
const [list, total] = await StockHistoryDao.pages(offset, size, stock.id)
15+
return [
16+
list.map(item => {
17+
item.tradeAt = formatDate(+item.timestamp, DateFormat.Date)
18+
return item
19+
}),
20+
total
21+
]
22+
} else {
23+
return [[], 0]
1924
}
20-
const pages = await getSharesRepository(History).findAndCount(options)
21-
return pages
2225
}
2326
}
2427

src/services/StockService.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,20 @@ import { Equal, Like, Between, FindManyOptions, FindConditions } from "typeorm";
22
import { Stock } from '../entities/mysql/shares/stock'
33
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
44
import { EMarket, EBLock } from '../models/Stocks'
5+
import StockDao from '../daos/StockDao'
56

67

78
class StockService {
89

910
async getByCode(code: string) {
10-
const stock = await getSharesRepository(Stock).findOne({code})
11+
const stock = await StockDao.getByCode(code);
1112
return stock
1213
}
1314

1415
async pages(offset = 1, size = 10, code?: string, name?: string, market?: EMarket, block?: EBLock) {
15-
const where: FindConditions<Stock> = {}
16-
if(code) {
17-
where.code = Like(`%${code}%`)
18-
}
19-
if(name) {
20-
where.name = Like(`%${name}%`)
21-
}
22-
if(market) {
23-
where.market = Equal(market)
24-
}
25-
if(block) {
26-
where.block = Equal(block)
27-
}
28-
console.log(where, 'where')
29-
const options: FindManyOptions<Stock> = {
30-
skip: (offset > 0 ? (offset - 1) : 0) * size,
31-
take: size,
32-
order: { id: 'ASC'},
33-
where
34-
}
35-
const pages = await getSharesRepository(Stock).findAndCount(options)
16+
const pages = await StockDao.pages(offset, size, code, name, market, block);
3617
return pages
3718
}
38-
39-
async getList(delta = 0, size = 100) {
40-
const options: FindManyOptions<Stock> = {
41-
skip: delta * size,
42-
take: size,
43-
order: { id: 'ASC'},
44-
where: {}
45-
}
46-
const stocks = await getSharesRepository(Stock).find(options)
47-
return stocks
48-
}
4919
}
5020

5121
export default new StockService

0 commit comments

Comments
 (0)