import { OrderDocument } from 'orders-rest'; import { NSIInstructionExecutor, UserBean, NsiRestService, NSIInstructionStatus, NSIThematicHeading, NSIInstructionPriority, NSIInstructionDifficulty } from 'oasi-nsi-rest'; import { InstructionExecutor } from '../models/InstructionExecutor'; import * as _ from 'lodash'; import { NewOrderModel } from '../models/NewOrderModel'; import { LoadingStatus } from 'oasi-widgets'; import { IFileInFolderInfo, IFolderInfo } from 'isogd-file-rest'; export class EditOrderController { private _blockUI: angular.blockUI.BlockUIService; private loadingStatus: LoadingStatus = LoadingStatus.LOADING; private creators: InstructionExecutor[]; private theme: any; private priority: any; private difficulty: any; private executors: InstructionExecutor[]; private model: NewOrderModel; private minDate: Date = new Date((new Date).valueOf() - (24 * 60 * 60 * 1000) * 7); private statuses: NSIInstructionStatus[]; private coExecutors: UserBean[] = []; private files: IFileInFolderInfo[] = []; private filesForDownloding: File[] = []; private cancelation: any; private _blockSearching: angular.blockUI.BlockUIService; static $inject = ['toastr', '$timeout', 'blockUI', '$q', 'nsiRestService', '$uibModalInstance', 'document', 'login', 'folderInfo']; constructor(private toastr: Toastr, private $timeout: ng.ITimeoutService, private blockUI: any, private $q: ng.IQService, private nsiRestService: NsiRestService, private $uibModalInstance: angular.ui.bootstrap.IModalServiceInstance, private document: OrderDocument, private login: string, private folderInfo: IFolderInfo) { } ok() { if (this.model.isValid()) { this.model.upatedocumentFromModel(this.document, this.priority, this.difficulty); this.$uibModalInstance.close({ document: this.document, files: this.files, filesForDownloding: this.filesForDownloding }); } else { this.toastr.warning('Не заполнены обязательные поля!'); } } cancel() { this.$uibModalInstance.dismiss('cancel'); } getFileByIds(ids: string[]): IFileInFolderInfo[] { return _.filter(this.folderInfo.files, f => { return _.findIndex(ids, i => { return i === f.versionSeriesGuid; }) !== -1; }); } upload(files: File[]) { this.filesForDownloding = files; } deleteD(file: File) { let index = _.findIndex(this.filesForDownloding, f => { return f === file; }); this.filesForDownloding.splice(index, 1); } delete(file: IFileInFolderInfo) { this.files = []; } $onInit() { this._blockUI = this.blockUI.instances.get('editOrder'); this._blockSearching = this.blockUI.instances.get('coExecutorsSearch'); this.nsiRestService.get('OasiInstructionExecutors').then((resp: NSIInstructionExecutor[]) => { return this.transformInstructionExecutor(resp); }).then(response => { this.executors = response; this.creators = _.filter(this.executors, r => { return r.creator.accountName === this.login; }).concat(_.filter(this.executors, r => { return _.findIndex(r.assistant, e => { return e.accountName === this.login; }) !== -1; })); return this.nsiRestService.get('OasiThematicHeading'); }).then(response => { this.theme = response; return this.nsiRestService.get('OasiInstructionPriority'); }).then(response => { this.priority = response; return this.nsiRestService.get('OasiInstructionDifficulty'); }).then(response => { this.difficulty = response; return this.nsiRestService.get('OasiInstructionStatus'); }).then(response => { this.statuses = response; this.model = new NewOrderModel(); this.files = this.getFileByIds(this.document.instruction.fileId); this.updateModel(); this.loadingStatus = LoadingStatus.SUCCESS; }).catch(error => { this.loadingStatus = LoadingStatus.ERROR; }); } updateModel() { this.model.creator = _.find(this.creators, c => { return c.creator.accountName === this.document.instruction.creator.login; } ); this.model.executor = _.find(this.model.creator.executor, c => { return c.accountName === this.document.instruction.executor.login; } ); this.document.instruction.coExecutor.forEach(ce => { let user = new UserBean(); user.accountName = ce.login; user.post = ce.post; user.displayName = ce.fioFull; this.model.coExecutor.push(user); }); if (this.document.instruction.beginDate) { this.model.beginDate = this.document.instruction.beginDate; } if (this.document.instruction.theme.code ) { this.model.theme = _.find(this.theme, th => { return th.code === this.document.instruction.theme.code; } ); } if (this.document.instruction.priority.code ) { this.model.priority = this.document.instruction.priority.code; } if (this.document.instruction.difficulty.code ) { this.model.difficulty = this.document.instruction.difficulty.code ; } if (this.document.instruction.content) { this.model.content = this.document.instruction.content; } if (this.document.instruction.description) { this.model.description = this.document.instruction.description; } if (this.document.instruction.planDate) { this.model.planDate = this.document.instruction.planDate; } if (this.document.instruction.private !== null) { this.model.private = this.document.instruction.private; } } onCreatorChange() { this.model.executor = null; } public solicitorSearch(search: string, $select: any) { if (search && search.length > 2) { if (this.cancel) { this.$timeout.cancel(this.cancelation); } this.cancelation = this.$timeout(() => { this.nsiRestService.searchUsers({ fio: search }).then(response => { if (response && response.length > 0) { this.coExecutors = response; } }).catch(error => { this.toastr.error('Ошибка при получении информации из справочника!'); }); }, 500); } } transformInstructionExecutor(nsiInstructionExecutors: NSIInstructionExecutor[]): ng.IPromise { let defer: ng.IDeferred = this.$q.defer(); let logins: string[] = []; nsiInstructionExecutors.forEach(e => { logins = logins.concat(e.assistant); logins.push(e.creator); logins = logins.concat(e.executor); }); logins = _.uniq(logins); if (logins.length > 0) { let promises: ng.IPromise[] = []; logins.forEach(l => { promises.push(this.nsiRestService.ldapUser(l)); }); this.$q.all(promises).then(response => { let result: InstructionExecutor[] = _.map(nsiInstructionExecutors, n => { return { creator: _.find(response, r => { return r.accountName === n.creator; }), executor: _.filter(response, r => { return _.findIndex(n.executor, e => { return e === r.accountName; }) !== -1; }), assistant: _.filter(response, r => { return _.findIndex(n.assistant, e => { return e === r.accountName; }) !== -1; }) }; }); defer.resolve(result); }).catch(error => { defer.reject(error); }); } else { defer.resolve([]); } return defer.promise; } }