employee.service.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Injectable } from '@angular/core';
  2. import { AdminDataService } from './admin-data.service';
  3. import { FmodeObject } from 'fmode-ng/core';
  4. /**
  5. * 员工管理数据服务 (Profile表)
  6. */
  7. @Injectable({
  8. providedIn: 'root'
  9. })
  10. export class EmployeeService {
  11. constructor(private adminData: AdminDataService) {}
  12. /**
  13. * 查询员工列表
  14. */
  15. async findEmployees(options?: {
  16. roleName?: string;
  17. departmentId?: string;
  18. keyword?: string;
  19. skip?: number;
  20. limit?: number;
  21. }): Promise<FmodeObject[]> {
  22. return await this.adminData.findAll('Profile', {
  23. include: ['department'],
  24. skip: options?.skip || 0,
  25. limit: options?.limit || 100,
  26. descending: 'createdAt',
  27. additionalQuery: query => {
  28. if (options?.roleName) {
  29. query.equalTo('roleName', options.roleName);
  30. }
  31. if (options?.departmentId) {
  32. query.equalTo('department', {
  33. __type: 'Pointer',
  34. className: 'Department',
  35. objectId: options.departmentId
  36. });
  37. }
  38. if (options?.keyword) {
  39. const kw = options.keyword.trim();
  40. if (kw) {
  41. query.matches('name', new RegExp(kw, 'i'));
  42. }
  43. }
  44. }
  45. });
  46. }
  47. /**
  48. * 统计员工数量
  49. */
  50. async countEmployees(roleName?: string): Promise<number> {
  51. return await this.adminData.count('Profile', query => {
  52. if (roleName) {
  53. query.equalTo('roleName', roleName);
  54. }
  55. });
  56. }
  57. /**
  58. * 根据ID获取员工
  59. */
  60. async getEmployee(objectId: string): Promise<FmodeObject | null> {
  61. return await this.adminData.getById('Profile', objectId, ['department']);
  62. }
  63. /**
  64. * 创建员工 (注意: 员工从企微同步,实际可能不需要创建功能)
  65. */
  66. async createEmployee(data: {
  67. name: string;
  68. mobile?: string;
  69. userId?: string;
  70. roleName: string;
  71. departmentId?: string;
  72. data?: any;
  73. }): Promise<FmodeObject> {
  74. const employeeData: any = {
  75. name: data.name,
  76. mobile: data.mobile || '',
  77. userId: data.userId || '',
  78. roleName: data.roleName
  79. };
  80. if (data.departmentId) {
  81. employeeData.department = {
  82. __type: 'Pointer',
  83. className: 'Department',
  84. objectId: data.departmentId
  85. };
  86. }
  87. if (data.data) {
  88. employeeData.data = data.data;
  89. }
  90. const employee = this.adminData.createObject('Profile', employeeData);
  91. return await this.adminData.save(employee);
  92. }
  93. /**
  94. * 更新员工
  95. */
  96. async updateEmployee(
  97. objectId: string,
  98. updates: {
  99. name?: string;
  100. mobile?: string;
  101. roleName?: string;
  102. departmentId?: string;
  103. isDisabled?: boolean;
  104. data?: any;
  105. }
  106. ): Promise<FmodeObject | null> {
  107. const employee = await this.getEmployee(objectId);
  108. if (!employee) {
  109. return null;
  110. }
  111. if (updates.name !== undefined) {
  112. employee.set('name', updates.name);
  113. }
  114. if (updates.mobile !== undefined) {
  115. employee.set('mobile', updates.mobile);
  116. }
  117. if (updates.roleName !== undefined) {
  118. employee.set('roleName', updates.roleName);
  119. }
  120. if (updates.departmentId !== undefined) {
  121. employee.set('department', {
  122. __type: 'Pointer',
  123. className: 'Department',
  124. objectId: updates.departmentId
  125. });
  126. }
  127. if (updates.isDisabled !== undefined) {
  128. employee.set('isDisabled', updates.isDisabled);
  129. }
  130. if (updates.data !== undefined) {
  131. const currentData = employee.get('data') || {};
  132. employee.set('data', { ...currentData, ...updates.data });
  133. }
  134. return await this.adminData.save(employee);
  135. }
  136. /**
  137. * 禁用/启用员工
  138. */
  139. async toggleEmployee(objectId: string, isDisabled: boolean): Promise<boolean> {
  140. const employee = await this.getEmployee(objectId);
  141. if (!employee) {
  142. return false;
  143. }
  144. employee.set('isDisabled', isDisabled);
  145. await this.adminData.save(employee);
  146. return true;
  147. }
  148. /**
  149. * 转换为JSON
  150. */
  151. toJSON(employee: FmodeObject): any {
  152. const json = this.adminData.toJSON(employee);
  153. // 处理部门关联
  154. if (json.department && typeof json.department === 'object') {
  155. json.departmentName = json.department.name || '';
  156. json.departmentId = json.department.objectId;
  157. }
  158. return json;
  159. }
  160. /**
  161. * 批量转换
  162. */
  163. toJSONArray(employees: FmodeObject[]): any[] {
  164. return employees.map(e => this.toJSON(e));
  165. }
  166. }