process-create.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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<string>; //流程名称
  47. desc: FormControl<string>; //流程描述
  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: ['',[Validators.maxLength(500)]],
  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.get('name')?.setValue(this.eduProcess?.get('name') || '')
  88. this.validateForm.get('desc')?.setValue(this.eduProcess?.get('desc') || '')
  89. this.validateForm.get('code')?.setValue(this.eduProcess?.get('code') || '')
  90. this.validateForm.get('num')?.setValue(this.eduProcess?.get('num') || '')
  91. this.validateForm.get('branch')?.setValue(this.eduProcess?.get('branch')?.id || '')
  92. this.validateForm.get('startDate')?.setValue(this.eduProcess?.get('startDate')
  93. ? this.eduProcess?.get('startDate')
  94. : new Date('2024/07/20'))
  95. this.validateForm.get('deadline')?.setValue( this.eduProcess?.get('deadline')
  96. ? this.eduProcess?.get('deadline')
  97. : new Date('2024-09-20'))
  98. // this.validateForm = this.fb.group({
  99. // name: [this.eduProcess?.get('name') || '', [Validators.required]],
  100. // desc: [this.eduProcess?.get('desc') || '',[Validators.maxLength(100)]],
  101. // code: [this.eduProcess?.get('code') || '', [Validators.required]],
  102. // num: [this.eduProcess?.get('num') || ''],
  103. // branch: [
  104. // this.eduProcess?.get('branch')?.id || '',
  105. // [Validators.required],
  106. // ],
  107. // startDate: [
  108. // this.eduProcess?.get('startDate')
  109. // ? this.eduProcess?.get('startDate')
  110. // : new Date('2024/07/20'),
  111. // [Validators.required],
  112. // ],
  113. // deadline: [
  114. // this.eduProcess?.get('deadline')
  115. // ? this.eduProcess?.get('deadline')
  116. // : new Date('2024-09-20'),
  117. // [Validators.required],
  118. // ],
  119. // });
  120. }
  121. this.profileId = this.eduProcess?.get('profileSubmitted')?.id || '';
  122. this.department = this.eduProcess?.get('department')?.id || '';
  123. });
  124. }
  125. async getUnitTypes() {
  126. let query = new Parse.Query('Department');
  127. query.equalTo('branch', undefined);
  128. query.equalTo('parent', undefined);
  129. query.notEqualTo('isDeleted', true);
  130. query.select('name');
  131. query.descending('createdAt')
  132. let r = await query.find();
  133. r.forEach((item) => {
  134. this.unitTypes.push({ id: item.id, name: item.get('name') });
  135. });
  136. }
  137. //根据所选单位类型获取对应单位
  138. async provinceChange(id?: string, val?: string) {
  139. let query = new Parse.Query('Department');
  140. id && query.equalTo('parent', id);
  141. query.select('name', 'branch');
  142. query.limit(100);
  143. val && query.contains('name', val);
  144. let r = await query.find();
  145. this.parentList = r;
  146. }
  147. async submitForm(type: string): Promise<void> {
  148. if (type == 'close') {
  149. this.modal.confirm({
  150. nzTitle: '你确定取消吗?',
  151. nzContent: '',
  152. nzOkText: '是',
  153. nzOkType: 'primary',
  154. nzOkDanger: true,
  155. nzOnOk: () => history.back(),
  156. nzCancelText: '否',
  157. nzOnCancel: () => console.log('Cancel'),
  158. });
  159. return;
  160. }
  161. console.log('submit', this.validateForm.value);
  162. if (this.validateForm.valid) {
  163. let params = this.validateForm.value;
  164. this.saveEduCollection(params);
  165. } else {
  166. this.msg.warning('请填写完整信息');
  167. Object.values(this.validateForm.controls).forEach((control) => {
  168. if (control.invalid) {
  169. control.markAsDirty();
  170. control.updateValueAndValidity({ onlySelf: true });
  171. }
  172. });
  173. }
  174. }
  175. async saveEduCollection(params: any) {
  176. if (!this.eduProcess?.id) {
  177. let obj = Parse.Object.extend('EduProcess');
  178. this.eduProcess = new obj();
  179. }
  180. // this.eduProcess?.set('user', Parse.User.current()?.toPointer());
  181. this.eduProcess?.set('company', {
  182. __type: 'Pointer',
  183. className: 'Company',
  184. objectId: this.tbookSer.company,
  185. });
  186. this.eduProcess?.set('branch', {
  187. __type: 'Pointer',
  188. className: 'Department',
  189. objectId: params.branch,
  190. });
  191. this.department &&
  192. this.eduProcess?.set('department', {
  193. __type: 'Pointer',
  194. className: 'Department',
  195. objectId: this.department,
  196. });
  197. this.eduProcess?.set('name', params.name);
  198. this.eduProcess?.set('desc', params.desc);
  199. this.eduProcess?.set('code', params.code);
  200. params.num && this.eduProcess?.set('num', params.num);
  201. this.eduProcess?.set('startDate', params.startDate);
  202. this.eduProcess?.set('deadline', params.deadline);
  203. if (!this.eduProcess?.get('profileSubmitted')) {
  204. let pid = await this.getProfile();
  205. this.eduProcess?.set('profileSubmitted', {
  206. __type: 'Pointer',
  207. className: 'Profile',
  208. objectId: pid,
  209. });
  210. }
  211. !this.eduProcess?.get('status') && this.eduProcess?.set('status', '200');
  212. this.eduProcess = await this.eduProcess?.save();
  213. this.msg.success(this.isEdit ? '已保存' : '已创建');
  214. this.showProfileFrom = false;
  215. this.router.navigate([
  216. '/nav-admin/manage/process/page',
  217. { id: this.eduProcess?.id },
  218. ]);
  219. }
  220. //选择部门
  221. async showModalDepart() {
  222. if (this.eduProcess?.get('department')) {
  223. this.parentMap = await this.formatNode(
  224. this.eduProcess?.get('department').id
  225. );
  226. } else {
  227. let dp = this.unitTypes.find(
  228. (item) => item.id == this.validateForm.value.branch
  229. );
  230. this.parentMap = [
  231. {
  232. title: dp?.name,
  233. id: dp?.id,
  234. },
  235. ];
  236. }
  237. this.provinceChange(this.validateForm.value.branch);
  238. this.isVisible = true;
  239. }
  240. async formatNode(id: string): Promise<Array<any>> {
  241. let arr = [];
  242. if (id) {
  243. let query = new Parse.Query('Department');
  244. query.equalTo('objectId', id);
  245. query.select('parent', 'name');
  246. let r = await query.first();
  247. if(!r?.get('parent')){
  248. arr.push({
  249. title: r?.get('name'),
  250. id: r?.id,
  251. });
  252. }
  253. arr.unshift(...(await this.formatNode(r?.get('parent')?.id)));
  254. }
  255. return arr;
  256. }
  257. //选择联系人
  258. onShowCheck() {
  259. if(!this.eduProcess?.get('department')?.id){
  260. this.msg.error('请先选择申报单位')
  261. return
  262. }
  263. if(this.profileId)return
  264. this.showProfileFrom = true;
  265. }
  266. //获取联系人
  267. async getProfile(): Promise<string | any> {
  268. let queryParams = {
  269. where: {
  270. $or: [
  271. {
  272. user: {
  273. $inQuery: {
  274. where: {
  275. $or: [
  276. {
  277. department: { $eq: this.department },
  278. },
  279. ],
  280. },
  281. className: '_User',
  282. },
  283. },
  284. },
  285. ],
  286. },
  287. };
  288. let query = Parse.Query.fromJSON('Profile', queryParams);
  289. query.equalTo('identity', '工作联系人');
  290. query.notEqualTo('isDeleted', true);
  291. query.select('user');
  292. let r = await query.first();
  293. if (r?.id) {
  294. return r.id;
  295. }
  296. return;
  297. }
  298. async changeSubmitted(e: Array<string>) {
  299. console.log(e);
  300. this.profileId = e[0];
  301. this.eduProcess?.set(
  302. 'profileSubmitted',
  303. this.profileId
  304. ? {
  305. __type: 'Pointer',
  306. className: 'Profile',
  307. objectId: this.profileId,
  308. }
  309. : null
  310. );
  311. await this.eduProcess?.save();
  312. if(this.profileId){
  313. this.msg.success('成功设置联系人')
  314. }else{
  315. this.msg.success('已删除联系人,请重新选择')
  316. }
  317. this.ngOnInit();
  318. this.showProfileFrom = false
  319. }
  320. onCheck(e: any) {
  321. console.log(e);
  322. }
  323. //选择部门
  324. async onCheckedDepart(e: any, checked?: boolean) {
  325. console.log(e);
  326. this.department = e.id;
  327. }
  328. handleCancel(): void {
  329. console.log('Button cancel clicked!');
  330. this.isVisible = false;
  331. }
  332. async handleOk(): Promise<void> {
  333. this.eduProcess?.set('branch', {
  334. __type: 'Pointer',
  335. className: 'Department',
  336. objectId: this.validateForm.value.branch,
  337. });
  338. this.department &&
  339. this.eduProcess?.set('department', {
  340. __type: 'Pointer',
  341. className: 'Department',
  342. objectId: this.department,
  343. });
  344. await this.eduProcess?.save();
  345. this.ngOnInit();
  346. this.msg.success('申报单位设置成功')
  347. this.isVisible = false;
  348. }
  349. }