import { DocumentIsogdWrapper } from 'isogd-document-rest'; import { SearchResult, SolarRestService, SearchExtData } from 'isogd-solar-rest'; import { DocumentLinkRestService, IDocumentLink } from 'isogd-document-link-rest'; import * as _ from 'lodash'; import { SearchResultDocumentEx } from './search-candidates'; import { SortingType } from './search-result-sorting'; import { IPagination } from './search-pagination'; import { ISearchParams } from './search-filter'; import * as widgets from "oasi-widgets"; import { LinkedDocumentService } from '../../services/LinkedDocumentService'; import { DocumentIsogd, DocumentRestService } from 'isogd-document-rest'; import { NsiRestService } from 'oasi-nsi-rest'; export interface ILinkWithDocument { link: IDocumentLink; document: DocumentIsogd; checked: boolean; } class LinkedDocumentsEditController { static $inject = ['$filter', 'solarRestService', 'linkedDocumentService', '$timeout', 'toastr', 'blockUI', 'documentLinkRestService', 'documentRestService', 'nsiRestService', '$q']; private document: DocumentIsogd; private linksWithDocument: ILinkWithDocument[] = []; private linksWithDocumentApprove: ILinkWithDocument[] = []; private linksWithDocumentReview: ILinkWithDocument[] = []; private linksWithDocumentReject: ILinkWithDocument[] = []; private chapters: any[]; private linkdocTypes: any[]; private linkdocStatuses: any[]; private documentTypes: any[]; private statuses: any[]; private loadingStatus: widgets.LoadingStatus = widgets.LoadingStatus.LOADING; private myBlockUI: any; private searchParams: ISearchParams; private sortings: SortingType[]; private pagination: IPagination; private searchResult: SearchResultDocumentEx[] = []; constructor(private $filter: ng.IFilterService, private solarRestService: SolarRestService, private linkedDocumentService: LinkedDocumentService, private $timeout: ng.ITimeoutService, private toastr: Toastr, private blockUI: any, private documentLinksRestService: DocumentLinkRestService, private documentRestService: DocumentRestService, private nsiRestService: NsiRestService, private $q: ng.IQService) { this.searchParams = { search: "", documentType: "", documentNumber: "", documentDate: { startDate: null, endDate: null }, searchType: "", docStatuse: null }; this.pagination = { currentPage: 1, totalItems: 0, disable: false }; this.sortings = [ new SortingType('documentDate', 'desc', 'по дате'), new SortingType('documentNumber', 'desc', 'по номеру') ]; } $onDestroy() { this.myBlockUI.reset(); } $onInit() { this.myBlockUI = this.blockUI.instances.get('linkedDocumentEditor'); this.nsiRestService.get("LinkdocTypes").then(response => { this.linkdocTypes = response; return this.nsiRestService.get("LinkdocStatuses") }).then(response => { this.linkdocStatuses = response; return this.nsiRestService.get("OldChapters"); }).then(response => { this.chapters = response; return this.nsiRestService.get("DocumentTypes"); }).then(response => { this.documentTypes = response; return this.nsiRestService.get("DocumentStatus"); }).then(response => { this.statuses = response; return this.documentLinksRestService.getAll(this.document.documentID); }).then(response => { let links = response; if (links.length > 0) { let promises: ng.IPromise[] = []; links.forEach(i => { promises.push(this.documentRestService.getDocument(this.getLinkedDocId(i))); }); this.$q.all(promises).then(response => { let linkedDocuments: DocumentIsogd[] = _.map(response, d => { return d.document; }); links.forEach(l => { linkedDocuments.forEach(d => { if (this.getLinkedDocId(l) === d.documentID) { this.linksWithDocument.push({ link: l, document: d, checked: false }); } }); }); this.linksWithDocumentApprove = _.filter(this.linksWithDocument, r => { return r.link.status === 'approve'; }); this.linksWithDocumentReview = _.filter(this.linksWithDocument, r => { return r.link.status === 'review'; }); this.linksWithDocumentReject = _.filter(this.linksWithDocument, r => { return r.link.status === 'reject'; }); this.linksWithDocumentApprove = _.orderBy(this.linksWithDocumentApprove, function (e) { return e.document.documentRequisites.documentDate }, ['asc']); this.linksWithDocumentReview = _.orderBy(this.linksWithDocumentReview, function (e) { return e.document.documentRequisites.documentDate }, ['asc']); this.linksWithDocumentReject = _.orderBy(this.linksWithDocumentReject, function (e) { return e.document.documentRequisites.documentDate }, ['asc']); this.loadingStatus = widgets.LoadingStatus.SUCCESS; }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; }); } else { this.loadingStatus = widgets.LoadingStatus.SUCCESS; } }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; console.log(error); }); } update() { this.myBlockUI.start(); this.$timeout(1500).then(response => { this.documentLinksRestService.getAll(this.document.documentID).then(response => { let links = response; if (links.length > 0) { let promises: ng.IPromise[] = []; links.forEach(i => { promises.push(this.documentRestService.getDocument(this.getLinkedDocId(i))); }); this.$q.all(promises).then(response => { let linkedDocuments: DocumentIsogd[] = _.map(response, d => { return d.document; }); this.linksWithDocument = []; links.forEach(l => { linkedDocuments.forEach(d => { if (this.getLinkedDocId(l) === d.documentID) { this.linksWithDocument.push({ link: l, document: d, checked: false }); } }); }); this.linksWithDocumentApprove = _.filter(this.linksWithDocument, r => { return r.link.status === 'approve'; }); this.linksWithDocumentReview = _.filter(this.linksWithDocument, r => { return r.link.status === 'review'; }); this.linksWithDocumentReject = _.filter(this.linksWithDocument, r => { return r.link.status === 'reject'; }); this.linksWithDocumentApprove = _.orderBy(this.linksWithDocumentApprove, function (e) { return e.document.documentRequisites.documentDate }, ['asc']); this.linksWithDocumentReview = _.orderBy(this.linksWithDocumentReview, function (e) { return e.document.documentRequisites.documentDate }, ['asc']); this.linksWithDocumentReject = _.orderBy(this.linksWithDocumentReject, function (e) { return e.document.documentRequisites.documentDate }, ['asc']); this.myBlockUI.stop(); }).catch(error => { this.myBlockUI.stop(); console.log(error); }); } else { this.myBlockUI.stop(); } }).catch(error => { this.myBlockUI.stop(); console.log(error); }); }); } updateAndFind() { this.myBlockUI.start(); this.$timeout(1500).then(response => { this.documentLinksRestService.getAll(this.document.documentID).then(response => { let links = response; if (links.length > 0) { let promises: ng.IPromise[] = []; links.forEach(i => { promises.push(this.documentRestService.getDocument(this.getLinkedDocId(i))); }); this.$q.all(promises).then(response => { let linkedDocuments: DocumentIsogd[] = _.map(response, d => { return d.document; }); this.linksWithDocument = []; links.forEach(l => { linkedDocuments.forEach(d => { if (this.getLinkedDocId(l) === d.documentID) { this.linksWithDocument.push({ link: l, document: d, checked: false }); } }); }); this.linksWithDocumentApprove = _.filter(this.linksWithDocument, r => { return r.link.status === 'approve'; }); this.linksWithDocumentReview = _.filter(this.linksWithDocument, r => { return r.link.status === 'review'; }); this.linksWithDocumentReject = _.filter(this.linksWithDocument, r => { return r.link.status === 'reject'; }); if (this.searchParams.searchType !== "") { let promise: ng.IPromise = null; if (this.searchParams.searchType === "common") { promise = this.solarRestService.searchCommon({ page: this.pagination.currentPage - 1, sort: this.sortingsToString(), text: this.searchParams.search, reestr: [], documentRegistrationCode: ['REGISTERED', 'QUEUE', 'REFUSED', 'DELETED', 'DOUBLE', 'CHANGE_QUEUE', 'CHANGE_REFUSED', 'CHANGE_REGISTERED', 'CHANGE_DELETED'] }); } else if (this.searchParams.searchType === "ext") { promise = this.solarRestService.searchExt(this.updateData()); } this.$timeout(1500).then(response => { promise.then(response => { this.searchResult = response.docs; this.searchResult.forEach(r => { let link = this.findLinkedDocumentById(r.documentId) r.checked = false; r.linkStatus = link !== null ? link.link.status : "approve"; r.linkType = link !== null ? link.link.linkType : "link"; r.hasLink = link !== null; }); this.pagination.totalItems = response.numFound; this.pagination.currentPage = response.start / response.pageSize + 1; this.myBlockUI.stop(); }).catch(error => { console.log(error); this.myBlockUI.stop(); }); }); } else { this.myBlockUI.stop(); } }).catch(error => { this.myBlockUI.stop(); console.log(error); }); } else { this.myBlockUI.stop(); } }).catch(error => { this.myBlockUI.stop(); console.log(error); }); }); } getLinkedDocId(link: IDocumentLink): string { return (link.doc1Id === this.document.documentID) ? link.doc2Id : link.doc1Id; } find() { if (this.searchParams.searchType !== "") { let promise: ng.IPromise = null; this.myBlockUI.start(); if (this.searchParams.searchType === "common") { promise = this.solarRestService.searchCommon({ page: this.pagination.currentPage - 1, sort: this.sortingsToString(), reestr: [], text: this.searchParams.search, documentRegistrationCode: ['REGISTERED', 'QUEUE', 'REFUSED', 'DELETED', 'DOUBLE', 'CHANGE_QUEUE', 'CHANGE_REFUSED', 'CHANGE_REGISTERED', 'CHANGE_DELETED'] }); } else if (this.searchParams.searchType === "ext") { promise = this.solarRestService.searchExt(this.updateData()); } this.$timeout(1500).then(response => { promise.then(response => { this.searchResult = response.docs; this.searchResult.forEach(r => { let link = this.findLinkedDocumentById(r.documentId) r.checked = false; r.linkStatus = link !== null ? link.link.status : "approve"; r.linkType = link !== null ? link.link.linkType : "link"; r.hasLink = link !== null; }); this.pagination.totalItems = response.numFound; this.pagination.currentPage = response.start / response.pageSize + 1; this.myBlockUI.stop(); }).catch(error => { console.log(error); this.myBlockUI.stop(); }); }); } } sortingsToString(): string { let result = ""; this.sortings.forEach((s, i) => { result += s.name + ' ' + s.value; if (i < this.sortings.length - 1) { result += ","; } }); return result; } updateData(): SearchExtData { let result = new SearchExtData(); result.page = this.pagination.currentPage - 1; result.sort = this.sortingsToString(); result.reestr = []; result.documentRegistrationCode = ['REGISTERED', 'QUEUE', 'REFUSED', 'DELETED', 'DOUBLE', 'CHANGE_QUEUE', 'CHANGE_REFUSED', 'CHANGE_REGISTERED', 'CHANGE_DELETED'] if (this.searchParams.docStatuse) { result.documentStatusCode = this.searchParams.docStatuse.statusCode; } if (this.searchParams.documentType !== "") { result.reestr = [this.searchParams.documentType]; } if (this.searchParams.documentNumber !== "") { result.documentNumber = this.searchParams.documentNumber; } if (this.searchParams.documentDate.startDate !== null) { result.documentDateFrom = this.$filter('date')(this.searchParams.documentDate.startDate, 'yyyy-MM-dd'); } if (this.searchParams.documentDate.endDate !== null) { result.documentDateTo = this.$filter('date')(this.searchParams.documentDate.endDate, 'yyyy-MM-dd'); } return result; } findLinkedDocumentById(id): ILinkWithDocument { let result = null; this.linksWithDocument.forEach(l => { if (l.document.documentID === id) { result = l; } }); return result; } } export const LinkedDocumentsEditComponent: angular.IComponentOptions = { controller: LinkedDocumentsEditController, template: require('./linked-documents-edit.html'), bindings: { document: '<' } };