62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { DateTime } from 'luxon'
|
|
import { BaseModel, column, belongsTo } from '@adonisjs/lucid/orm'
|
|
import type { BelongsTo } from '@adonisjs/lucid/types/relations'
|
|
import Product from '#models/product'
|
|
|
|
export interface SupplierPricePoint {
|
|
price: number
|
|
date?: string
|
|
source?: string
|
|
[k: string]: any
|
|
}
|
|
|
|
export interface EbayData {
|
|
sold: Array<Record<string, any>>
|
|
sale: Array<Record<string, any>>
|
|
}
|
|
|
|
/**
|
|
* Snapshot dữ liệu mỗi lần lấy về để đưa AI gợi ý giá.
|
|
* Lưu nguyên đầu vào (supplier + eBay) phục vụ audit & re-run.
|
|
*/
|
|
export default class History extends BaseModel {
|
|
@column({ isPrimary: true })
|
|
declare id: number
|
|
|
|
@column()
|
|
declare username: string
|
|
|
|
@column()
|
|
declare productId: number
|
|
|
|
/** [{ price, date, source, ... }, ...] — lịch sử giá nguồn (ERP, supplier). */
|
|
@column({
|
|
columnName: 'data_sources',
|
|
prepare: (v) => JSON.stringify(v ?? []),
|
|
consume: (v) => (typeof v === 'string' ? JSON.parse(v) : v),
|
|
})
|
|
declare dataSources: SupplierPricePoint[]
|
|
|
|
/** { sold: [...], sale: [...] } — dữ liệu eBay (đã bán / đang bán). */
|
|
@column({
|
|
columnName: 'data_ebay',
|
|
prepare: (v) => JSON.stringify(v ?? { sold: [], sale: [] }),
|
|
consume: (v) => (typeof v === 'string' ? JSON.parse(v) : v),
|
|
})
|
|
declare dataEbay: EbayData
|
|
|
|
/** Kết quả AI tại thời điểm đó (tùy chọn). */
|
|
@column({
|
|
columnName: 'ai_result',
|
|
prepare: (v) => (v === null || v === undefined ? null : JSON.stringify(v)),
|
|
consume: (v) => (typeof v === 'string' ? JSON.parse(v) : v),
|
|
})
|
|
declare aiResult: Record<string, any> | null
|
|
|
|
@column.dateTime({ autoCreate: true, columnName: 'time' })
|
|
declare time: DateTime
|
|
|
|
@belongsTo(() => Product)
|
|
declare product: BelongsTo<typeof Product>
|
|
}
|