CommonModel.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { type ConvertItemOptions, type ConvertTable, DataConverter } from "@imengyu/js-request-transform";
  2. interface CommonModelConfig {
  3. idKey?: string;
  4. convertTable?: ConvertTable;
  5. }
  6. export interface NewCommonModel<T extends CommonModel> {
  7. new(): T;
  8. }
  9. export type ValidCommonModel<T> = T extends CommonModel ? NewCommonModel<T> : undefined;
  10. export class CommonModel {
  11. public _tableName: string;
  12. public _config: CommonModelConfig;
  13. public constructor(tableName: string, config: CommonModelConfig) {
  14. this._tableName = tableName;
  15. this._config = config;
  16. this._config.convertTable = {
  17. ...this._config.convertTable,
  18. createdAt: { clientSide: 'date', serverSide: 'string' },
  19. updatedAt: { clientSide: 'date', serverSide: 'string' },
  20. deletedAt: { clientSide: 'date', serverSide: 'string' },
  21. }
  22. }
  23. createdAt = new Date();
  24. updatedAt : Date | null = null;
  25. deletedAt : Date | null = null;
  26. public setValue(key: keyof this, value: any) {
  27. (this as any)[key] = value;
  28. return this;
  29. }
  30. public setValues(at: {
  31. [key in keyof this]?: any;
  32. }) {
  33. for (const key in at)
  34. this.setValue(key, at[key]);
  35. return this;
  36. }
  37. public fromServerSide(data: any) {
  38. const self = this as Record<string, any>;
  39. const options : ConvertItemOptions = {
  40. direction: 'client',
  41. defaultDateFormat: 'YYYY-MM-DD HH:mm:ss',
  42. policy: 'default',
  43. }
  44. for (const key in data) {
  45. if (key.startsWith('_'))
  46. continue;
  47. const convert = this._config.convertTable?.[key];
  48. if (convert) {
  49. let value = data[key];
  50. if (convert instanceof Array)
  51. for (const convertItem of convert)
  52. value = DataConverter.convertDataItem(value, key, convertItem, options, `${key}`, this._tableName);
  53. else
  54. value = DataConverter.convertDataItem(value, key, convert, options, `${key}`, this._tableName)
  55. self[key] = value;
  56. } else {
  57. self[key] = data[key];
  58. }
  59. }
  60. return this;
  61. }
  62. public toServerSide() {
  63. const self = this as Record<string, any>;
  64. const options : ConvertItemOptions = {
  65. direction: 'server',
  66. defaultDateFormat: 'YYYY-MM-DD HH:mm:ss',
  67. policy: 'default',
  68. }
  69. const result : Record<string, any> = {};
  70. for (const key in self) {
  71. if (key.startsWith('_'))
  72. continue;
  73. const convert = this._config.convertTable?.[key];
  74. if (convert) {
  75. let value = self[key];
  76. if (convert instanceof Array)
  77. for (const convertItem of convert)
  78. value = DataConverter.convertDataItem(value, key, convertItem, options, `${key}`, this._tableName);
  79. else
  80. value = DataConverter.convertDataItem(value, key, convert, options, `${key}`, this._tableName);
  81. result[key] = value;
  82. } else {
  83. result[key] = self[key];
  84. }
  85. }
  86. return result;
  87. }
  88. }
  89. /**
  90. * 通用分页返回
  91. */
  92. export class CommonPageResult<T> implements ICommonPageResult<T> {
  93. public items : T[];
  94. public pageIndex : number;
  95. public pageSize : number;
  96. public allCount : number;
  97. public allPage : number;
  98. public empty : boolean;
  99. public constructor(model: ValidCommonModel<T>|undefined, data : any[], pageIndex : number, pageSize : number, allCount : number) {
  100. this.items = data.map((item) => {
  101. return model ? new model().fromServerSide(item) as T : item as T;
  102. });
  103. this.pageIndex = pageIndex;
  104. this.pageSize = pageSize;
  105. this.allCount = allCount;
  106. this.allPage = Math.floor(allCount / pageSize) || 0;
  107. this.empty = data.length == 0;
  108. }
  109. }
  110. /**
  111. * 通用分页返回结构
  112. */
  113. export interface ICommonPageResult<T> {
  114. items : T[];
  115. pageIndex : number;
  116. pageSize : number;
  117. allCount : number;
  118. allPage : number;
  119. empty : boolean;
  120. }