process-create.component.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { Component, Input, OnInit } from '@angular/core';
  2. import { FormsModule, ReactiveFormsModule } from '@angular/forms';
  3. import { CommonCompModule } from '../../../../services/common.modules';
  4. import { Router, ActivatedRoute } from '@angular/router';
  5. import { NzSelectModule } from 'ng-zorro-antd/select';
  6. import Parse from 'parse';
  7. import {
  8. FormControl,
  9. FormGroup,
  10. NonNullableFormBuilder,
  11. Validators,
  12. } from '@angular/forms';
  13. import { textbookServer } from '../../../../services/textbook';
  14. import { NzMessageService } from 'ng-zorro-antd/message';
  15. import { NzModalService } from 'ng-zorro-antd/modal';
  16. import { SubmittedComponent } from '../../components/submitted/submitted.component';
  17. import { ProfileComponent } from '../../components/profile/profile.component';
  18. import { MatButtonModule } from '@angular/material/button';
  19. import { differenceInCalendarDays, setHours } from 'date-fns';
  20. import { NzRadioModule } from 'ng-zorro-antd/radio';
  21. @Component({
  22. selector: 'app-process-create',
  23. templateUrl: './process-create.component.html',
  24. styleUrls: ['./process-create.component.scss'],
  25. imports: [
  26. CommonCompModule,
  27. FormsModule,
  28. ReactiveFormsModule,
  29. NzSelectModule,
  30. SubmittedComponent,
  31. MatButtonModule,
  32. ProfileComponent,
  33. NzRadioModule,
  34. ],
  35. standalone: true,
  36. })
  37. export class ProcessCreateComponent implements OnInit {
  38. timeDefaultValue = setHours(new Date(), 0);
  39. eduProcess: Parse.Object | undefined;
  40. showProfileFrom: boolean = false;
  41. @Input('isEdit') isEdit: boolean = false; //当前是否作为编辑子组件
  42. profileId: string = '';
  43. isVisible: boolean = false;
  44. searchValue: string = ''; //搜索部门内容
  45. validateForm: FormGroup<{
  46. name: FormControl<Array<string> | any>; //流程名称
  47. desc: FormControl<Array<string> | any>; //流程描述
  48. code: FormControl<string | any>; //流程唯一标识
  49. num: FormControl<number | any>; //报送配额
  50. branch: FormControl<Array<string> | any>; //所属分类
  51. startDate: FormControl<Date>; //开始时间
  52. deadline: FormControl<Date>; //结束时间
  53. }> = this.fb.group({
  54. name: ['', [Validators.required]],
  55. desc: ['',],
  56. code: ['', [Validators.required]],
  57. num: ['',],
  58. branch: ['', [Validators.required]],
  59. startDate: [new Date('2024/07/20'), [Validators.required]],
  60. deadline: [new Date('2024-09-20'), [Validators.required]],
  61. });
  62. department: string = ''; //所属单位
  63. unitTypes: Array<any> = []; //单位类型
  64. getNumlength(): number {
  65. return this.validateForm.value.num.toString().length;
  66. }
  67. parentMap: Array<any> = [];
  68. parentList: Array<any> = []; //单位列表
  69. constructor(
  70. private activeRoute: ActivatedRoute,
  71. private router: Router,
  72. public tbookSer: textbookServer,
  73. private fb: NonNullableFormBuilder,
  74. private msg: NzMessageService,
  75. private modal: NzModalService
  76. ) {}
  77. ngOnInit() {
  78. this.activeRoute.paramMap.subscribe(async (params) => {
  79. let id = params.get('id');
  80. await this.getUnitTypes();
  81. if (id) {
  82. this.isEdit = true;
  83. let query = new Parse.Query('EduProcess');
  84. query.include('branch', 'department','profileSubmitted');
  85. query.equalTo('objectId', id);
  86. this.eduProcess = await query.first();
  87. this.validateForm = this.fb.group({
  88. name: [this.eduProcess?.get('name') || '', [Validators.required]],
  89. desc: [this.eduProcess?.get('desc') || '',],
  90. code: [this.eduProcess?.get('code') || '', [Validators.required]],
  91. num: [this.eduProcess?.get('num') || ''],
  92. branch: [
  93. this.eduProcess?.get('branch')?.id || '',
  94. [Validators.required],
  95. ],
  96. startDate: [
  97. this.eduProcess?.get('startDate')
  98. ? this.eduProcess?.get('startDate')
  99. : new Date('2024/07/20'),
  100. [Validators.required],
  101. ],
  102. deadline: [
  103. this.eduProcess?.get('deadline')
  104. ? this.eduProcess?.get('deadline')
  105. : new Date('2024-09-20'),
  106. [Validators.required],
  107. ],
  108. });
  109. }
  110. this.profileId = this.eduProcess?.get('profileSubmitted')?.id || '';
  111. this.department = this.eduProcess?.get('department')?.id || '';
  112. });
  113. }
  114. async getUnitTypes() {
  115. let query = new Parse.Query('Department');
  116. query.equalTo('branch', undefined);
  117. query.equalTo('parent', undefined);
  118. query.notEqualTo('isDeleted', true);
  119. query.select('name');
  120. query.descending('createdAt')
  121. let r = await query.find();
  122. r.forEach((item) => {
  123. this.unitTypes.push({ id: item.id, name: item.get('name') });
  124. });
  125. }
  126. //根据所选单位类型获取对应单位
  127. async provinceChange(id?: string, val?: string) {
  128. let query = new Parse.Query('Department');
  129. id && query.equalTo('parent', id);
  130. query.select('name', 'branch');
  131. query.limit(100);
  132. val && query.contains('name', val);
  133. let r = await query.find();
  134. this.parentList = r;
  135. }
  136. async submitForm(type: string): Promise<void> {
  137. if (type == 'close') {
  138. this.modal.confirm({
  139. nzTitle: '你确定取消吗?',
  140. nzContent: '',
  141. nzOkText: '是',
  142. nzOkType: 'primary',
  143. nzOkDanger: true,
  144. nzOnOk: () => history.back(),
  145. nzCancelText: '否',
  146. nzOnCancel: () => console.log('Cancel'),
  147. });
  148. return;
  149. }
  150. console.log('submit', this.validateForm.value);
  151. if (this.validateForm.valid) {
  152. let params = this.validateForm.value;
  153. this.saveEduCollection(params);
  154. } else {
  155. this.msg.warning('请填写完整信息');
  156. Object.values(this.validateForm.controls).forEach((control) => {
  157. if (control.invalid) {
  158. control.markAsDirty();
  159. control.updateValueAndValidity({ onlySelf: true });
  160. }
  161. });
  162. }
  163. }
  164. async saveEduCollection(params: any) {
  165. if (!this.eduProcess?.id) {
  166. let obj = Parse.Object.extend('EduProcess');
  167. this.eduProcess = new obj();
  168. }
  169. // this.eduProcess?.set('user', Parse.User.current()?.toPointer());
  170. this.eduProcess?.set('company', {
  171. __type: 'Pointer',
  172. className: 'Company',
  173. objectId: this.tbookSer.company,
  174. });
  175. this.eduProcess?.set('branch', {
  176. __type: 'Pointer',
  177. className: 'Department',
  178. objectId: params.branch,
  179. });
  180. this.department &&
  181. this.eduProcess?.set('department', {
  182. __type: 'Pointer',
  183. className: 'Department',
  184. objectId: this.department,
  185. });
  186. this.eduProcess?.set('name', params.name);
  187. this.eduProcess?.set('desc', params.desc);
  188. this.eduProcess?.set('code', params.code);
  189. params.num && this.eduProcess?.set('num', params.num);
  190. this.eduProcess?.set('startDate', params.startDate);
  191. this.eduProcess?.set('deadline', params.deadline);
  192. if (!this.eduProcess?.get('profileSubmitted')) {
  193. let pid = await this.getProfile();
  194. this.eduProcess?.set('profileSubmitted', {
  195. __type: 'Pointer',
  196. className: 'Profile',
  197. objectId: pid,
  198. });
  199. }
  200. !this.eduProcess?.get('status') && this.eduProcess?.set('status', '200');
  201. this.eduProcess = await this.eduProcess?.save();
  202. this.msg.success(this.isEdit ? '已保存' : '已创建');
  203. this.showProfileFrom = false;
  204. this.router.navigate([
  205. '/nav-admin/manage/process/page',
  206. { id: this.eduProcess?.id },
  207. ]);
  208. }
  209. //选择部门
  210. async showModalDepart() {
  211. if (this.eduProcess?.get('department')) {
  212. this.parentMap = await this.formatNode(
  213. this.eduProcess?.get('department').id
  214. );
  215. } else {
  216. let dp = this.unitTypes.find(
  217. (item) => item.id == this.validateForm.value.branch
  218. );
  219. this.parentMap = [
  220. {
  221. title: dp?.name,
  222. id: dp?.id,
  223. },
  224. ];
  225. }
  226. this.provinceChange(this.validateForm.value.branch);
  227. this.isVisible = true;
  228. }
  229. async formatNode(id: string): Promise<Array<any>> {
  230. let arr = [];
  231. if (id) {
  232. let query = new Parse.Query('Department');
  233. query.equalTo('objectId', id);
  234. query.select('parent', 'name');
  235. let r = await query.first();
  236. if(!r?.get('parent')){
  237. arr.push({
  238. title: r?.get('name'),
  239. id: r?.id,
  240. });
  241. }
  242. arr.unshift(...(await this.formatNode(r?.get('parent')?.id)));
  243. }
  244. return arr;
  245. }
  246. //选择联系人
  247. onShowCheck() {
  248. if(!this.eduProcess?.get('department')?.id){
  249. this.msg.error('请先选择申报单位')
  250. return
  251. }
  252. if(this.profileId)return
  253. this.showProfileFrom = true;
  254. }
  255. //获取联系人
  256. async getProfile(): Promise<string | any> {
  257. let queryParams = {
  258. where: {
  259. $or: [
  260. {
  261. user: {
  262. $inQuery: {
  263. where: {
  264. $or: [
  265. {
  266. department: { $eq: this.department },
  267. },
  268. ],
  269. },
  270. className: '_User',
  271. },
  272. },
  273. },
  274. ],
  275. },
  276. };
  277. let query = Parse.Query.fromJSON('Profile', queryParams);
  278. query.equalTo('identity', '工作联系人');
  279. query.notEqualTo('isDeleted', true);
  280. query.select('user');
  281. let r = await query.first();
  282. if (r?.id) {
  283. return r.id;
  284. }
  285. return;
  286. }
  287. async changeSubmitted(e: Array<string>) {
  288. console.log(e);
  289. this.profileId = e[0];
  290. this.eduProcess?.set(
  291. 'profileSubmitted',
  292. this.profileId
  293. ? {
  294. __type: 'Pointer',
  295. className: 'Profile',
  296. objectId: this.profileId,
  297. }
  298. : null
  299. );
  300. await this.eduProcess?.save();
  301. if(this.profileId){
  302. this.msg.success('成功设置联系人')
  303. }else{
  304. this.msg.success('已删除联系人,请重新选择')
  305. }
  306. this.ngOnInit();
  307. this.showProfileFrom = false
  308. }
  309. onCheck(e: any) {
  310. console.log(e);
  311. }
  312. //选择部门
  313. async onCheckedDepart(e: any, checked?: boolean) {
  314. console.log(e);
  315. this.department = e.id;
  316. }
  317. handleCancel(): void {
  318. console.log('Button cancel clicked!');
  319. this.isVisible = false;
  320. }
  321. async handleOk(): Promise<void> {
  322. this.eduProcess?.set('branch', {
  323. __type: 'Pointer',
  324. className: 'Department',
  325. objectId: this.validateForm.value.branch,
  326. });
  327. this.department &&
  328. this.eduProcess?.set('department', {
  329. __type: 'Pointer',
  330. className: 'Department',
  331. objectId: this.department,
  332. });
  333. await this.eduProcess?.save();
  334. this.ngOnInit();
  335. this.msg.success('申报单位设置成功')
  336. this.isVisible = false;
  337. }
  338. }