team-assign.component.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { Component, Input, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormsModule } from '@angular/forms';
  4. import { FmodeObject, FmodeParse } from 'fmode-ng/parse';
  5. import { ProductSpaceService, Project } from '../../services/product-space.service';
  6. const Parse = FmodeParse.with('nova');
  7. @Component({
  8. selector: 'app-team-assign',
  9. standalone: true,
  10. imports: [CommonModule, FormsModule],
  11. templateUrl: './team-assign.component.html',
  12. styleUrls: ['./team-assign.component.scss'],
  13. changeDetection: ChangeDetectionStrategy.OnPush
  14. })
  15. export class TeamAssignComponent implements OnInit {
  16. @Input() project: FmodeObject | null = null;
  17. @Input() canEdit: boolean = true; // 可选:未传入时默认允许编辑
  18. @Input() currentUser: FmodeObject | null = null; // 可选:未传入时为 null
  19. // 项目组(Department)列表
  20. departments: FmodeObject[] = [];
  21. selectedDepartment: FmodeObject | null = null;
  22. // 项目组成员(Profile)列表
  23. departmentMembers: FmodeObject[] = [];
  24. selectedDesigner: FmodeObject | null = null;
  25. // 已分配的项目团队成员
  26. projectTeams: FmodeObject[] = [];
  27. // 设计师分配对话框
  28. showAssignDialog: boolean = false;
  29. assigningDesigner: FmodeObject | null = null;
  30. selectedSpaces: string[] = [];
  31. editingTeam: FmodeObject | null = null; // 当前正在编辑的团队对象
  32. // 加载状态
  33. loadingMembers: boolean = false;
  34. loadingTeams: boolean = false;
  35. loadingSpaces: boolean = false;
  36. saving: boolean = false;
  37. // 空间数据
  38. projectSpaces: Project[] = [];
  39. constructor(
  40. private productSpaceService: ProductSpaceService,
  41. private cdr: ChangeDetectorRef
  42. ) {}
  43. async ngOnInit() {
  44. await this.loadData();
  45. }
  46. async loadData() {
  47. if (!this.project) return;
  48. try {
  49. // 初始化已选择的项目组与设计师(若项目已有)
  50. const department = this.project.get('department');
  51. if (department) {
  52. this.selectedDepartment = department;
  53. await this.loadDepartmentMembers(department);
  54. }
  55. const assignee = this.project.get('assignee');
  56. if (assignee) {
  57. this.selectedDesigner = assignee;
  58. }
  59. // 加载项目组列表
  60. const deptQuery = new Parse.Query('Department');
  61. deptQuery.include('leader');
  62. deptQuery.equalTo('type', 'project');
  63. deptQuery.equalTo('company', localStorage.getItem('company'));
  64. deptQuery.notEqualTo('isDeleted', true);
  65. deptQuery.ascending('name');
  66. this.departments = await deptQuery.find();
  67. // 加载项目团队
  68. await this.loadProjectTeams();
  69. // 加载项目空间
  70. await this.loadProjectSpaces();
  71. } catch (err) {
  72. console.error('加载团队分配数据失败:', err);
  73. } finally {
  74. this.cdr.markForCheck();
  75. }
  76. }
  77. async loadProjectSpaces(): Promise<void> {
  78. if (!this.project) return;
  79. try {
  80. this.loadingSpaces = true;
  81. const projectId = this.project.id || '';
  82. this.projectSpaces = await this.productSpaceService.getProjectProductSpaces(projectId);
  83. } catch (err) {
  84. console.error('加载项目空间失败:', err);
  85. } finally {
  86. this.loadingSpaces = false;
  87. this.cdr.markForCheck();
  88. }
  89. }
  90. async loadProjectTeams() {
  91. if (!this.project) return;
  92. try {
  93. this.loadingTeams = true;
  94. const query = new Parse.Query('ProjectTeam');
  95. query.equalTo('project', this.project.toPointer());
  96. query.include('profile');
  97. query.notEqualTo('isDeleted', true);
  98. this.projectTeams = await query.find();
  99. } catch (err) {
  100. console.error('加载项目团队失败:', err);
  101. } finally {
  102. this.loadingTeams = false;
  103. }
  104. }
  105. async selectDepartment(department: FmodeObject) {
  106. this.selectedDepartment = department;
  107. this.selectedDesigner = null;
  108. this.departmentMembers = [];
  109. await this.loadDepartmentMembers(department);
  110. }
  111. async loadDepartmentMembers(department: FmodeObject) {
  112. const departmentId = department?.id;
  113. if (!departmentId) return [];
  114. try {
  115. this.loadingMembers = true;
  116. const query = new Parse.Query('Profile');
  117. query.equalTo('department', departmentId);
  118. query.equalTo('roleName', '组员');
  119. query.notEqualTo('isDeleted', true);
  120. query.ascending('name');
  121. this.departmentMembers = await query.find();
  122. // 将组长置顶展示
  123. const leader = department?.get('leader');
  124. if (leader) {
  125. this.departmentMembers.unshift(leader);
  126. }
  127. return this.departmentMembers;
  128. } catch (err) {
  129. console.error('加载项目组成员失败:', err);
  130. } finally {
  131. this.loadingMembers = false;
  132. }
  133. return [];
  134. }
  135. selectDesigner(designer: FmodeObject) {
  136. // 检查是否已分配
  137. const isAssigned = this.projectTeams.some(team => team.get('profile')?.id === designer.id);
  138. if (isAssigned) {
  139. alert('该设计师已分配到此项目');
  140. return;
  141. }
  142. this.assigningDesigner = designer;
  143. this.selectedSpaces = [];
  144. // 如果只有一个空间,默认选中
  145. if (this.projectSpaces.length === 1) {
  146. const only = this.projectSpaces[0];
  147. this.selectedSpaces = [only.name];
  148. }
  149. this.showAssignDialog = true;
  150. }
  151. editAssignedDesigner(team: FmodeObject) {
  152. const designer = team.get('profile');
  153. if (!designer) return;
  154. this.assigningDesigner = designer;
  155. this.editingTeam = team;
  156. const currentSpaces = team.get('data')?.spaces || [];
  157. this.selectedSpaces = [...currentSpaces];
  158. this.showAssignDialog = true;
  159. }
  160. toggleSpaceSelection(spaceName: string) {
  161. const index = this.selectedSpaces.indexOf(spaceName);
  162. if (index > -1) {
  163. this.selectedSpaces.splice(index, 1);
  164. } else {
  165. this.selectedSpaces.push(spaceName);
  166. }
  167. }
  168. async confirmAssignDesigner() {
  169. if (!this.assigningDesigner || !this.project) return;
  170. if (this.selectedSpaces.length === 0) {
  171. alert('请至少选择一个空间场景');
  172. return;
  173. }
  174. try {
  175. this.saving = true;
  176. if (this.editingTeam) {
  177. // 更新现有团队成员的空间分配
  178. const data = this.editingTeam.get('data') || {};
  179. data.spaces = this.selectedSpaces;
  180. data.updatedAt = new Date();
  181. data.updatedBy = this.currentUser?.id;
  182. this.editingTeam.set('data', data);
  183. await this.editingTeam.save();
  184. alert('更新成功');
  185. } else {
  186. // 创建新的 ProjectTeam
  187. const ProjectTeam = Parse.Object.extend('ProjectTeam');
  188. const team = new ProjectTeam();
  189. team.set('project', this.project.toPointer());
  190. team.set('profile', this.assigningDesigner.toPointer());
  191. team.set('role', '组员');
  192. team.set('data', {
  193. spaces: this.selectedSpaces,
  194. assignedAt: new Date(),
  195. assignedBy: this.currentUser?.id
  196. });
  197. await team.save();
  198. // 加入群聊(静默执行)
  199. await this.addMemberToGroupChat(this.assigningDesigner.get('userId'));
  200. alert('分配成功');
  201. }
  202. await this.loadProjectTeams();
  203. this.showAssignDialog = false;
  204. this.assigningDesigner = null;
  205. this.selectedSpaces = [];
  206. this.editingTeam = null;
  207. } catch (err) {
  208. console.error(this.editingTeam ? '更新失败:' : '分配设计师失败:', err);
  209. alert(this.editingTeam ? '更新失败' : '分配失败');
  210. } finally {
  211. this.saving = false;
  212. }
  213. }
  214. cancelAssignDialog() {
  215. this.showAssignDialog = false;
  216. this.assigningDesigner = null;
  217. this.selectedSpaces = [];
  218. this.editingTeam = null;
  219. }
  220. async addMemberToGroupChat(userId: string) {
  221. if (!userId) return;
  222. try {
  223. const groupChat = (this as any).groupChat;
  224. if (!groupChat) return;
  225. const chatId = groupChat.get('chat_id');
  226. if (!chatId) return;
  227. if (typeof (window as any).ww !== 'undefined') {
  228. await (window as any).ww.updateEnterpriseChat({
  229. chatId: chatId,
  230. userIdsToAdd: [userId]
  231. });
  232. }
  233. } catch (err) {
  234. console.warn('添加群成员失败:', err);
  235. }
  236. }
  237. getMemberSpaces(team: FmodeObject): string {
  238. const spaces = team.get('data')?.spaces || [];
  239. return spaces.join('、') || '未分配';
  240. }
  241. getDesignerWorkload(designer: FmodeObject): string {
  242. return '3个项目';
  243. }
  244. }