????
Current Path : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/tailieu/ |
Current File : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/tailieu/vanban.js |
var Database = require('../../models/Database'); var {existsSync} = require('fs'); var fsp = require('fs/promises'); var moment = require('moment'); var path = require('path'); var hosoCtrl = require('./hoso').create(); const pdfParser = require('pdf-parse'); const { scanPath, savePath } = require('../../appconfig'); module.exports = class VanBanController extends Database { constructor(params) { super(params) } static create(params) { return new VanBanController(params); } async getDocuments(fileId) { if (fileId) { var query = 'SELECT * FROM Document WHERE FileId = @fileId ORDER BY FileId,DocOrdinal'; return await this.select(query, { fileId: fileId }); } else { return [] } } async addDocument(user, doc) { let file = (await this.select('SELECT UUID,FileCode FROM [File] WHERE FileCode = @fileCode AND [State] = 1', { fileCode: doc.file }))[0]; if (!file) throw new Error(`Không tồn tại hồ sơ với mã [${doc.file}]`); let foundDocs = await this.select('SELECT UUID FROM Document WHERE FileId = @file AND DocOrdinal = @ordinal', { file: file.UUID, ordinal: parseInt(doc.ordinal) }); if (foundDocs.length) throw new Error(`Đã tồn tại văn bản thứ ${doc.ordinal} trong hồ sơ`); let scanDocPath = path.join(scanPath, doc.dirpath, doc.docname); let pageAmount = null; if (existsSync(scanDocPath)) { let pdfData = await pdfParser(await fsp.readFile(scanDocPath)); pageAmount = pdfData.numpages; } let input = { file: file.UUID, ordinal: doc.ordinal, type: doc.type || null, number: doc.number ? doc.number.padStart(2, '0') : null, notation: doc.notation || null, issuedDate: moment.utc(doc.date, 'DD/MM/YYYY').isValid() ? moment.utc(doc.date, 'DD/MM/YYYY').toDate() : null, organ: doc.organname || null, subject: doc.subject, language: doc.language || null, pageAmount: pageAmount, sheetAmount: doc.sheetamount || null, sheetIndex: doc.sheetindex || null, desc: doc.desc || null, inforsign: doc.inforsign || null, keyword: doc.keyword || null, mode: doc.mode || 1, level: doc.level || 1, autograph: doc.autograph || null, format: doc.format || null, createdby: user.UUID, docCode: file.FileCode + '.' + doc.ordinal.padStart(2, '0') } let query = 'INSERT INTO Document([FileId],[DocOrdinal],[DocType],[CodeNumber],[CodeNotation],[IssuedDate],[OrganName],[Subject],[Language],[PageAmount],[SheetAmount],[SheetIndex],[Description],[InforSign],[Keyword],[ModeId],[ConfidenceLevelId],[Autograph],[Format],[CreatedBy],[DocCode]) VALUES (@file,@ordinal,@type,@number,@notation,@issuedDate,@organ,@subject,@language,@pageAmount,@sheetamount,@sheetindex,@desc,@inforsign,@keyword,@mode,@level,@autograph,@format,@createdby,@docCode)'; await this.query(query, input); try { let dir = path.join(savePath, file.FileCode); if (!existsSync(dir)) await fsp.mkdir(dir, {recursive: true}); await fsp.rename(scanDocPath, path.join(dir, input.docCode + '.pdf')); return 'Thêm văn bản thành công'; } catch (err) { console.log(err); return 'Thêm văn bản thành công nhưng không thể chép tập tin!' } } async editDocument(user, doc, pdf) { var returnDoc = (await this.select('SELECT Document.*,[File].FileCode FROM Document INNER JOIN [File] ON [File].UUID = Document.FileId WHERE Document.UUID = @uuid', { uuid: doc.id }))[0]; if (!returnDoc) throw new Error('Văn bản không tồn tại trong hệ thống'); var file = await hosoCtrl.canEdit(user, returnDoc.FileId); if (!file) throw new Error('Tài khoản của bạn không có quyền chỉnh sửa văn bản! Xin vui lòng liên hệ quản trị hệ thống'); if ((await this.select('SELECT * FROM Document WHERE FileId = @fileId AND DocOrdinal = @ordinal AND UUID != @uuid', { fileId: returnDoc.FileId, ordinal: doc.ordinal || returnDoc.DocOrdinal, uuid: doc.id })).length) throw new Error(`Đã tồn tại văn bản khác có số thứ tự ${parseInt(doc.ordinal) || returnDoc.DocOrdinal} trong hồ sơ`); var updates = []; var pageAmount = returnDoc.PageAmount; var oldPath = ''; if (pageAmount) oldPath = path.join(savePath, returnDoc.FileCode, returnDoc.DocCode + '.pdf'); if (pdf) { var pdfData = await pdfParser(await fsp.readFile(pdf.path)); pageAmount = pdfData.numpages; oldPath = pdf.path; } var docCode = `${returnDoc.FileCode}.${doc.ordinal.padStart(2, '0') || returnDoc.DocOrdinal.toString().padStart(2, '0')}`; //các cột cần sửa var input = { uuid: doc.id }; if (typeof doc.number !== 'undefined') { updates.push('CodeNumber = @number'); input['number'] = doc.number || null } if (typeof doc.notation !== 'undefined') { updates.push('CodeNotation = @notation'); input['notation'] = doc.notation || null } if (typeof doc.type !== 'undefined') { updates.push('DocType = @type'); input['type'] = doc.type || null } if (typeof doc.organ !== 'undefined') { updates.push('OrganName = @organ'); input['organ'] = doc.organ || null } if (typeof doc.subject !== 'undefined') { updates.push('Subject = @subject'); input['subject'] = doc.subject } if (typeof doc.desc !== 'undefined') { updates.push('Description = @desc'); input['desc'] = doc.desc || null } if (typeof doc.autograph !== 'undefined') { updates.push('Autograph = @autograph'); input['autograph'] = doc.autograph || null } if (typeof doc.keyword !== 'undefined') { updates.push('Keyword = @keyword'); input['keyword'] = doc.keyword || null } if (typeof doc.ordinal !== 'undefined') { updates.push('DocOrdinal = @ordinal'); input['ordinal'] = parseInt(doc.ordinal) } if (typeof doc.sheetamount !== 'undefined') { updates.push('SheetAmount = @sheetamount'); input['sheetamount'] = parseInt(doc.sheetamount) || null } if (typeof doc.sheetindex !== 'undefined') { updates.push('SheetIndex = @sheetindex'); input['sheetindex'] = doc.sheetindex || null } if (typeof doc.language !== 'undefined') { updates.push('Language = @language'); input['language'] = doc.language || null } if (typeof doc.format !== 'undefined') { updates.push('Format = @format'); input['format'] = doc.format || null } if (typeof doc.mode !== 'undefined') { updates.push('ModeId = @mode'); input['mode'] = doc.mode || 1 } if (typeof doc.level !== 'undefined') { updates.push('ConfidenceLevelId = @level'); input['level'] = doc.level || 1 } if (typeof doc.date !== 'undefined') { updates.push('IssuedDate = @issuedDate'); input['issuedDate'] = moment.utc(doc.date, 'DD/MM/YYYY').isValid() ? moment.utc(doc.date, 'DD/MM/YYYY').toDate() : null } if (docCode != returnDoc.DocCode) { updates.push('DocCode = @docCode'); input['docCode'] = docCode; } updates.push('PageAmount = @pageAmount'); input['pageAmount'] = pageAmount; if (!updates.length) return null; var query = `UPDATE Document SET ${updates.join(',')} WHERE UUID = @uuid`; var result = await this.query(query, input); var newPath = path.join(savePath, returnDoc.FileCode, docCode + '.pdf'); if (oldPath && oldPath != newPath) await fsp.rename(oldPath, newPath); return result; } async deleteDocument(user, docId) { var recordset = await this.select('SELECT Document.*,[File].FileCode FROM Document INNER JOIN [File] ON [File].UUID = Document.FileId WHERE Document.UUID = @uuid', { uuid: docId }); var doc = recordset[0]; var result = await hosoCtrl.canDelete(user, doc.FileId); if (!result) throw new Error('Tài khoản hiện tại không có quyền xóa văn bản trên'); var data = await this.query('DELETE FROM Document WHERE UUID = @uuid', { uuid: docId }); if (doc.PageAmount) { try { await fsp.unlink(path.join(savePath, doc.FileCode, doc.DocCode + '.pdf')); console.log(`Đã xóa tập tin ${doc.DocCode + '.pdf'}`) } catch (err) { console.log(`Xóa tập tin ${doc.DocCode + '.pdf'} thất bại - ${err.message}`) } } return data; } // Ký số async signDocument(docId, file) { await this.query('UPDATE Document SET Signed = 1 WHERE UUID = @docId', { docId: docId }); let doc = (await this.select('SELECT Document.UUID,DocCode,FileCode FROM Document INNER JOIN [File] ON [File].UUID = Document.FileId WHERE Document.UUID = @docId', { docId: docId }))[0]; let signPath = path.join(savePath, doc['FileCode'], doc['DocCode'] + '.signed.pdf'); await fsp.rename(file.path, signPath); return doc; } searchDocument(_user, _req) { } }