admin-data.service.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { Injectable } from '@angular/core';
  2. import { FmodeObject, FmodeParse, FmodeQuery } from 'fmode-ng/core';
  3. /**
  4. * 管理系统数据服务基类
  5. * 提供统一的数据访问接口,所有数据操作都指向 cDL6R1hgSi 映三色帐套
  6. */
  7. @Injectable({
  8. providedIn: 'root'
  9. })
  10. export class AdminDataService {
  11. private readonly COMPANY_ID = 'cDL6R1hgSi'; // 映三色帐套ID
  12. private Parse: any;
  13. constructor() {
  14. this.Parse = FmodeParse.with("nova")
  15. }
  16. /**
  17. * 获取公司指针
  18. */
  19. getCompanyPointer(): any {
  20. return {
  21. __type: 'Pointer',
  22. className: 'Company',
  23. objectId: this.COMPANY_ID
  24. };
  25. }
  26. /**
  27. * 创建查询并自动添加公司和非删除过滤
  28. */
  29. createQuery(className: string): FmodeQuery {
  30. const query = new this.Parse.Query(className);
  31. query.equalTo('company', this.getCompanyPointer());
  32. query.notEqualTo('isDeleted', true);
  33. return query;
  34. }
  35. /**
  36. * 创建新对象
  37. */
  38. createObject(className: string, data: any): FmodeObject {
  39. const obj = new this.Parse.Object(className);
  40. obj.set('company', this.getCompanyPointer());
  41. obj.set('isDeleted', false);
  42. // 设置其他字段
  43. Object.keys(data).forEach(key => {
  44. if (key !== 'company' && key !== 'isDeleted') {
  45. obj.set(key, data[key]);
  46. }
  47. });
  48. return obj;
  49. }
  50. /**
  51. * 软删除对象
  52. */
  53. async softDelete(obj: FmodeObject): Promise<FmodeObject> {
  54. obj.set('isDeleted', true);
  55. return await obj.save();
  56. }
  57. /**
  58. * 批量软删除
  59. */
  60. async softDeleteBatch(objects: FmodeObject[]): Promise<FmodeObject[]> {
  61. objects.forEach(obj => obj.set('isDeleted', true));
  62. return await this.Parse.Object.saveAll(objects);
  63. }
  64. /**
  65. * 查询统计数量
  66. */
  67. async count(className: string, additionalQuery?: (query: FmodeQuery) => void): Promise<number> {
  68. const query = this.createQuery(className);
  69. if (additionalQuery) {
  70. additionalQuery(query);
  71. }
  72. return await query.count();
  73. }
  74. /**
  75. * 查询列表
  76. */
  77. async findAll(
  78. className: string,
  79. options?: {
  80. include?: string[];
  81. limit?: number;
  82. skip?: number;
  83. descending?: string;
  84. ascending?: string;
  85. additionalQuery?: (query: FmodeQuery) => void;
  86. }
  87. ): Promise<FmodeObject[]> {
  88. const query = this.createQuery(className);
  89. if (options?.include) {
  90. options.include.forEach(field => query.include(field));
  91. }
  92. if (options?.limit) {
  93. query.limit(options.limit);
  94. }
  95. if (options?.skip) {
  96. query.skip(options.skip);
  97. }
  98. if (options?.descending) {
  99. query.descending(options.descending);
  100. }
  101. if (options?.ascending) {
  102. query.ascending(options.ascending);
  103. }
  104. if (options?.additionalQuery) {
  105. options.additionalQuery(query);
  106. }
  107. return await query.find();
  108. }
  109. /**
  110. * 根据ID获取对象
  111. */
  112. async getById(className: string, objectId: string, include?: string[]): Promise<FmodeObject | null> {
  113. try {
  114. const query = this.createQuery(className);
  115. if (include) {
  116. include.forEach(field => query.include(field));
  117. }
  118. return await query.get(objectId);
  119. } catch (error) {
  120. console.error(`获取${className} ${objectId}失败:`, error);
  121. return null;
  122. }
  123. }
  124. /**
  125. * 保存对象
  126. */
  127. async save(obj: FmodeObject): Promise<FmodeObject> {
  128. return await obj.save();
  129. }
  130. /**
  131. * 批量保存
  132. */
  133. async saveAll(objects: FmodeObject[]): Promise<FmodeObject[]> {
  134. return await this.Parse.Object.saveAll(objects);
  135. }
  136. /**
  137. * 将Parse对象转换为普通对象
  138. */
  139. toJSON(obj: FmodeObject): any {
  140. let json = obj.toJSON();
  141. return json;
  142. }
  143. /**
  144. * 批量转换
  145. */
  146. toJSONArray(objects: FmodeObject[]): any[] {
  147. return objects.map(obj => this.toJSON(obj));
  148. }
  149. }