| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { type ConvertItemOptions, type ConvertTable, DataConverter } from "@imengyu/js-request-transform";
- interface CommonModelConfig {
- idKey?: string;
- convertTable?: ConvertTable;
- }
- export interface NewCommonModel<T extends CommonModel> {
- new(): T;
- }
- export type ValidCommonModel<T> = T extends CommonModel ? NewCommonModel<T> : 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<string, any>;
- 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<string, any>;
- const options : ConvertItemOptions = {
- direction: 'server',
- defaultDateFormat: 'YYYY-MM-DD HH:mm:ss',
- policy: 'default',
- }
- const result : Record<string, any> = {};
- 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<T> implements ICommonPageResult<T> {
- public items : T[];
- public pageIndex : number;
- public pageSize : number;
- public allCount : number;
- public allPage : number;
- public empty : boolean;
- public constructor(model: ValidCommonModel<T>|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<T> {
- items : T[];
- pageIndex : number;
- pageSize : number;
- allCount : number;
- allPage : number;
- empty : boolean;
- }
|