textbook-content.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
  2. import { CommonCompModule } from '../../../../services/common.modules';
  3. import { NzSelectModule } from 'ng-zorro-antd/select';
  4. import { ReactiveFormsModule } from '@angular/forms';
  5. import { NzRadioModule } from 'ng-zorro-antd/radio';
  6. import { NzMessageService } from 'ng-zorro-antd/message';
  7. import { NzGridModule } from 'ng-zorro-antd/grid';
  8. import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
  9. import { NzTableModule } from 'ng-zorro-antd/table';
  10. import { NzModalService } from 'ng-zorro-antd/modal';
  11. import Parse from 'parse';
  12. import {
  13. FormControl,
  14. FormGroup,
  15. NonNullableFormBuilder,
  16. Validators,
  17. } from '@angular/forms';
  18. import { textbookServer } from '../../../../services/textbook';
  19. import { CreatedService } from '../../../../services/created.service';
  20. import { ContentComponent } from '../create/content/content.component';
  21. import { NzCollapseModule } from 'ng-zorro-antd/collapse';
  22. import { ViewChildren, QueryList } from '@angular/core';
  23. interface course {
  24. date: Date | any;
  25. wordage: number | any;
  26. num: number | any;
  27. sumNum: number | any;
  28. accolade: string;
  29. }
  30. @Component({
  31. selector: 'app-textbook-content',
  32. imports: [
  33. NzCollapseModule,
  34. CommonCompModule,
  35. ReactiveFormsModule,
  36. NzSelectModule,
  37. NzRadioModule,
  38. NzGridModule,
  39. NzCheckboxModule,
  40. NzTableModule,
  41. ContentComponent,
  42. ],
  43. standalone: true,
  44. templateUrl: './textbook-content.component.html',
  45. styleUrls: ['./textbook-content.component.scss'],
  46. })
  47. export class TextbookContentComponent implements OnInit {
  48. @Input('eduTextbook') eduTextbook: any;
  49. @Input('maxWidth') maxWidth: number = 0;
  50. @Output() state: EventEmitter<any> = new EventEmitter<any>();
  51. @Output() save: EventEmitter<any> = new EventEmitter<any>();
  52. validateForm: FormGroup<{
  53. innovateExplain: FormControl<string>; //申报教材特色及创新
  54. influence: FormControl<string>; //申报教材应用情况及社会影响力
  55. }> = this.fb.group({
  56. innovateExplain: ['', [Validators.required]],
  57. influence: ['', [Validators.required]],
  58. });
  59. //申报教材建设历程
  60. courses: Array<course> = [
  61. {
  62. date: '', //出版时间
  63. wordage: '', //字数
  64. num: '', //重印次数
  65. sumNum: '', //本版总印数
  66. accolade: '', //获奖励情况
  67. },
  68. ];
  69. constructor(
  70. public tbookSer: textbookServer,
  71. private fb: NonNullableFormBuilder,
  72. private modal: NzModalService,
  73. private msg: NzMessageService,
  74. private creatSev: CreatedService
  75. ) {}
  76. loading = false;
  77. /**扩展表记录 */
  78. eduTextbookVolumeList: Array<any> = [];
  79. /**获取扩展表记录 */
  80. async getEduTextbookVolumeList() {
  81. if (this.typeNumber && this.typeNumber > 12) {
  82. this.typeNumber = 12;
  83. }
  84. console.log(this.type, this.typeNumber);
  85. let query = new Parse.Query('EduTextbookVolume');
  86. query.equalTo('eduTextbook', this.eduTextbook?.id);
  87. query.select('objectId');
  88. query.ascending('createdAt'); //小到大
  89. query.notEqualTo('isDeleted', true);
  90. if (this.type == '全册') {
  91. query.limit(this.typeNumber);
  92. } else {
  93. query.limit(1);
  94. }
  95. let list = await query.find();
  96. console.log(list);
  97. if (this.type == '全册') {
  98. this.eduTextbookVolumeList = new Array(this.typeNumber).fill({
  99. objectId: '',
  100. });
  101. for (let i in list) {
  102. this.eduTextbookVolumeList[i] = list[i];
  103. }
  104. } else {
  105. this.eduTextbookVolumeList[0] = list[0];
  106. console.log(this.eduTextbookVolumeList);
  107. this.eduTextbookVolumeId = list[0]?.id;
  108. }
  109. }
  110. /** 册数*/
  111. typeNumber: any;
  112. /** 申报类型*/
  113. type: any;
  114. /** 单册时该册id*/
  115. eduTextbookVolumeId: any;
  116. ngAfterViewInit(): void {
  117. this.typeNumber = this.eduTextbook?.get('typeNumber');
  118. this.type = this.eduTextbook?.get('type');
  119. this.getEduTextbookVolumeList();
  120. this.creatSev.getEduTextbookVolumeList(this.eduTextbook.id);
  121. }
  122. ngOnInit() {
  123. if (this.eduTextbook.id) {
  124. this.creatSev.type = this.eduTextbook.get('type');
  125. this.creatSev.typeNumber = this.eduTextbook.get('typeNumber');
  126. this.validateForm = this.fb.group({
  127. innovateExplain: [
  128. this.eduTextbook.get('innovateExplain'),
  129. [Validators.required],
  130. ],
  131. influence: [this.eduTextbook.get('influence'), [Validators.required]],
  132. });
  133. console.log(this.validateForm.value);
  134. this.courses = this.eduTextbook.get('courses') || this.courses;
  135. }
  136. }
  137. async submitForm(event?: string): Promise<void> {
  138. let params: any = this.validateForm.value;
  139. console.log(params);
  140. if (event == 'pre') {
  141. this.state.emit({ type: 'pre' });
  142. return
  143. }
  144. console.log(this.validateForm.value);
  145. // let coursesVrifly = !this.courses.some(item=>Object.values(item).some(val=> val == '' || val == undefined))
  146. let coursesVrifly = true
  147. if (this.validateForm.valid) {
  148. let params: any = this.validateForm.value;
  149. if (event == 'next') {
  150. if(!coursesVrifly){
  151. this.msg.warning('申报教材建设历程填写不完整')
  152. return
  153. }
  154. await this.saveEduTextbook(params, true);
  155. this.state.emit({ type: 'next', textBook: this.eduTextbook });
  156. }
  157. } else {
  158. if (event == 'save') {
  159. let params: any = this.validateForm.value;
  160. await this.saveEduTextbook(params, (this.validateForm.valid && coursesVrifly));
  161. this.modal.success({
  162. nzTitle: '保存成功',
  163. nzContent: '<p>已保存并且至空间</p>',
  164. nzOnOk: () => console.log('Info OK'),
  165. });
  166. return;
  167. }
  168. if (event == 'next') {
  169. let params: any = this.validateForm.value;
  170. await this.saveEduTextbook(params, false);
  171. this.state.emit({ type: 'next', textBook: this.eduTextbook });
  172. }
  173. // Object.values(this.validateForm.controls).forEach((control) => {
  174. // if (control.invalid) {
  175. // control.markAsDirty();
  176. // control.updateValueAndValidity({ onlySelf: true });
  177. // }
  178. // });
  179. // this.msg.warning('请填写完整信息');
  180. return;
  181. }
  182. // let coursesVrifly = !this.courses.some(item=>Object.values(item).some(val=> val == '' || val == undefined))
  183. if (event == 'save') {
  184. await this.saveEduTextbook(params, this.validateForm.valid);
  185. this.modal.success({
  186. nzTitle: '保存成功',
  187. nzContent: '<p>已保存并且至空间</p>',
  188. nzOnOk: () => console.log('Info OK'),
  189. });
  190. return;
  191. }
  192. if (event == 'next') {
  193. await this.saveEduTextbook(params, this.validateForm.valid);
  194. this.state.emit({ type: 'next', textBook: this.eduTextbook });
  195. return;
  196. }
  197. // if (this.validateForm.valid) {
  198. // let params: any = this.validateForm.value;
  199. // if (event == 'next') {
  200. // // if(!coursesVrifly){
  201. // // this.msg.warning('申报教材建设历程填写不完整')
  202. // // return
  203. // // }
  204. // await this.saveEduTextbook(params, true);
  205. // this.state.emit({ type: 'next', textBook: this.eduTextbook });
  206. // }
  207. // } else {
  208. // if (event == 'save') {
  209. // let params: any = this.validateForm.value;
  210. // await this.saveEduTextbook(params, this.validateForm.valid);
  211. // this.modal.success({
  212. // nzTitle: '保存成功',
  213. // nzContent: '<p>已保存并且至空间</p>',
  214. // nzOnOk: () => console.log('Info OK'),
  215. // });
  216. // return;
  217. // }
  218. // if (event == 'next') {
  219. // let params: any = this.validateForm.value;
  220. // await this.saveEduTextbook(params, false);
  221. // this.state.emit({ type: 'next', textBook: this.eduTextbook });
  222. // }
  223. // // Object.values(this.validateForm.controls).forEach((control) => {
  224. // // if (control.invalid) {
  225. // // control.markAsDirty();
  226. // // control.updateValueAndValidity({ onlySelf: true });
  227. // // }
  228. // // });
  229. // // this.msg.warning('请填写完整信息');
  230. // }
  231. // if (event == 'save') {
  232. // let params = this.validateForm.value;
  233. // await this.saveEduTextbook(params, this.validateForm.valid);
  234. // this.modal.success({
  235. // nzTitle: '保存成功',
  236. // nzContent: '<p>已保存并且至空间</p>',
  237. // nzOnOk: () => console.log('Info OK'),
  238. // });
  239. // }
  240. }
  241. changeCode() {}
  242. getCode(e: any) {}
  243. //添加作者信息
  244. onPush(idx: number) {
  245. this.courses.splice(idx + 1, 0, {
  246. date: '',
  247. wordage: '',
  248. num: '',
  249. sumNum: '',
  250. accolade: '',
  251. });
  252. }
  253. //删除作者信息
  254. onDel(idx: number) {
  255. if (this.courses?.length == 1) return;
  256. this.courses.splice(idx, 1);
  257. }
  258. @ViewChildren(ContentComponent) children: QueryList<ContentComponent> | any;
  259. /**上传分册数据 */
  260. async saveEduTextbookVolume() {
  261. let arr = []; //存储返回的数组id
  262. let isVrifly = true; //默认都通过,若一项填写未完成,则不通过
  263. return Promise.all(
  264. this.children.map(async (comp: any) => {
  265. let req = await comp.submitForm();
  266. // console.log(req)
  267. if (!req) {
  268. isVrifly = false;
  269. }
  270. return isVrifly;
  271. })
  272. ).then((data) => {
  273. console.log(isVrifly);
  274. return isVrifly;
  275. });
  276. }
  277. saveLoading: boolean = false;
  278. async saveEduTextbook(params: any, isComplete: boolean) {
  279. console.log(params);
  280. if (!this.eduTextbook) {
  281. this.msg.error('请先创建教材');
  282. return;
  283. }
  284. if (this.saveLoading) return;
  285. this.saveLoading = true;
  286. try {
  287. //如果填写未完整,仅保存,状态修改待完善101
  288. let complete = await this.saveEduTextbookVolume();
  289. isComplete = isComplete && complete;
  290. if (this.eduTextbook.get('status') == '102' && !isComplete) {
  291. this.eduTextbook?.set('status', '101');
  292. this.eduTextbook.set('complete', false);
  293. } else if (!this.eduTextbook.get('status')) {
  294. this.eduTextbook?.set('status', '101');
  295. }
  296. this.eduTextbook?.set('user', Parse.User.current()?.toPointer());
  297. this.eduTextbook?.set('company', {
  298. __type: 'Pointer',
  299. className: 'Company',
  300. objectId: this.tbookSer.company,
  301. });
  302. params.innovateExplain &&
  303. this.eduTextbook?.set('innovateExplain', params.innovateExplain);
  304. params.influence && this.eduTextbook?.set('influence', params.influence);
  305. // this.eduTextbook?.set('courses', this.courses);
  306. await this.eduTextbook?.save();
  307. this.saveLoading = false;
  308. if (!isComplete) {
  309. this.msg.warning('保存成功,但存在未填写完成项');
  310. }
  311. return;
  312. } catch (err) {
  313. console.warn('保存错误:', err);
  314. this.saveLoading = false;
  315. this.msg.error('保存出错');
  316. }
  317. }
  318. }