import { type ConvertItemOptions, type ConvertTable, DataConverter } from "@imengyu/js-request-transform"; interface CommonModelConfig { idKey?: string; convertTable?: ConvertTable; } export interface NewCommonModel { new(): T; } export type ValidCommonModel = T extends CommonModel ? NewCommonModel : undefined; export class CommonModel { public _tableName: string; public _config: CommonModelConfig; public constructor(tableName: string, config: CommonModelConfig) { this._tableName = tableName; this._config = config; this._config.convertTable = { ...this._config.convertTable, createdAt: { clientSide: 'date', serverSide: 'string' }, updatedAt: { clientSide: 'date', serverSide: 'string' }, deletedAt: { clientSide: 'date', serverSide: 'string' }, } } createdAt = new Date(); updatedAt : Date | null = null; deletedAt : Date | null = null; public setValue(key: keyof this, value: any) { (this as any)[key] = value; return this; } public setValues(at: { [key in keyof this]?: any; }) { for (const key in at) this.setValue(key, at[key]); return this; } public fromServerSide(data: any) { const self = this as Record; const options : ConvertItemOptions = { direction: 'client', defaultDateFormat: 'YYYY-MM-DD HH:mm:ss', policy: 'default', } for (const key in data) { if (key.startsWith('_')) continue; const convert = this._config.convertTable?.[key]; if (convert) { let value = data[key]; if (convert instanceof Array) for (const convertItem of convert) value = DataConverter.convertDataItem(value, key, convertItem, options, `${key}`, this._tableName); else value = DataConverter.convertDataItem(value, key, convert, options, `${key}`, this._tableName) self[key] = value; } else { self[key] = data[key]; } } return this; } public toServerSide() { const self = this as Record; const options : ConvertItemOptions = { direction: 'server', defaultDateFormat: 'YYYY-MM-DD HH:mm:ss', policy: 'default', } const result : Record = {}; for (const key in self) { if (key.startsWith('_')) continue; const convert = this._config.convertTable?.[key]; if (convert) { let value = self[key]; if (convert instanceof Array) for (const convertItem of convert) value = DataConverter.convertDataItem(value, key, convertItem, options, `${key}`, this._tableName); else value = DataConverter.convertDataItem(value, key, convert, options, `${key}`, this._tableName); result[key] = value; } else { result[key] = self[key]; } } return result; } } /** * 通用分页返回 */ export class CommonPageResult implements ICommonPageResult { public items : T[]; public pageIndex : number; public pageSize : number; public allCount : number; public allPage : number; public empty : boolean; public constructor(model: ValidCommonModel|undefined, data : any[], pageIndex : number, pageSize : number, allCount : number) { this.items = data.map((item) => { return model ? new model().fromServerSide(item) as T : item as T; }); this.pageIndex = pageIndex; this.pageSize = pageSize; this.allCount = allCount; this.allPage = Math.floor(allCount / pageSize) || 0; this.empty = data.length == 0; } } /** * 通用分页返回结构 */ export interface ICommonPageResult { items : T[]; pageIndex : number; pageSize : number; allCount : number; allPage : number; empty : boolean; }