Untitled

 avatar
unknown
plain_text
a year ago
18 kB
3
Indexable
module.exports = app => {
    const menu = {
        parentMenu: app.parentMenu.sdh,
        menus: {
            7638: {
                title: 'Công trình cán bộ hướng dẫn', link: '/user/sau-dai-hoc/cong-trinh-cbhd', parentKey: 7543
            }
        }
    };
    app.permission.add(
        { name: 'sdhCongTrinhCBHD:read', menu },
        { name: 'sdhCongTrinhCBHD:write' },
        { name: 'sdhCongTrinhCBHD:delete' },
        { name: 'sdhCongTrinhCBHD:export' }
    );

    app.get('/user/sau-dai-hoc/cong-trinh-cbhd', app.permission.check('sdhCongTrinhCBHD:read'), app.templates.admin);

    // app.permissionHooks.add('staff', 'addRoleSdhCongTrinhCBHD', (user, staff) => new Promise(resolve => {
    //     if (staff.maDonVi && staff.maDonVi == '37') {
    //         app.permissionHooks.pushUserPermission(user, 'sdhCongTrinhCBHD:write', 'sdhCongTrinhCBHD:read', 'sdhCongTrinhCBHD:delete');
    //         resolve();
    //     }
    //     else resolve();
    // }));

    //APIs----------------------------------------------------------------------------------------------------------------------------------
    app.get('/api/sdh/cong-trinh-cbhd/item/:id', app.permission.check('sdhCongTrinhCBHD:read'), async (req, res) => {
        try {
            const { id } = req.params;
            const item = await app.model.sdhDmCongTrinhCanBoHuongDan.get({ id });
            res.send({ item });
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.get('/api/sdh/cong-trinh-cbhd/:shcc', app.permission.check('sdhCongTrinhCBHD:read'), async (req, res) => {
        try {
            const { shcc } = req.params;
            const item = await app.model.sdhDmCongTrinhCanBoHuongDan.getAll({ shcc });
            res.send({ item });
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.put('/api/sdh/cong-trinh-cbhd', app.permission.check('sdhCongTrinhCBHD:write'), async (req, res) => {
        try {
            const { data, id } = req.body;
            data.modifier = req.session.user.email;
            data.timeModified = Date.now();
            const item = await app.model.sdhDmCongTrinhCanBoHuongDan.update({ id }, data);
            res.send({ item });
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.post('/api/sdh/cong-trinh-cbhd', app.permission.check('sdhCongTrinhCBHD:write'), async (req, res) => {
        try {
            const { data } = req.body;
            data.modifier = req.session.user.email;
            data.timeModified = Date.now();
            const item = await app.model.sdhDmCongTrinhCanBoHuongDan.create(data);
            res.send({ item });
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.delete('/api/sdh/cong-trinh-cbhd', app.permission.check('sdhCongTrinhCBHD:delete'), async (req, res) => {
        try {
            const { id } = req.body;
            await app.model.sdhDmCongTrinhCanBoHuongDan.delete({ id });
            res.end();
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.get('/api/sdh/cong-trinh-cbhd/download-template', app.permission.check('sdhCongTrinhCBHD:export'), async (req, res) => {
        const workBook = app.excel.create();
        const ws = workBook.addWorksheet('CongTrinhCBHD');
        ws.columns = [
            { header: 'Mã cán bộ', key: 'shcc', width: 20 },
            { header: 'Tên công trình', key: 'tenCongTrinh', width: 20 },
            { header: 'Ngày đăng', key: 'ngayDang', width: 20 },
            { header: 'Tạp chí', key: 'tapChi', width: 20 },
            { header: 'Trang', key: 'trang', width: 20 },
            { header: 'Chỉ số', key: 'chiSo', width: 20 },
            { header: 'Điểm', key: 'diem', width: 20 },
        ];
        ws.getRow(1).alignment = { ...ws.getRow(1).alignment, vertical: 'middle', wrapText: true };
        ws.getRow(1).font = {
            name: 'Times New Roman',
            family: 4,
            size: 12,
            bold: true,
            color: { argb: 'FF000000' }
        };

        ws.getRow(2).font = {
            name: 'Times New Roman',
            family: 4,
            size: 12,
        };

        const rowIndex = 2;
        ws.getCell('A' + rowIndex).value = '\'004.5003 (Vui lòng để 1 dấu \' ở trước như trong mẫu)';
        ws.getCell('B' + rowIndex).value = 'Công Trình Xịn';
        ws.getCell('C' + rowIndex).value = '14/02/2021';
        ws.getCell('D' + rowIndex).value = 'TechMagazine';
        ws.getCell('E' + rowIndex).value = 10;
        ws.getCell('F' + rowIndex).value = 10;
        ws.getCell('G' + rowIndex).value = 10;

        ws.getCell('A' + rowIndex).alignment = { horizontal: 'center' };
        ws.getCell('B' + rowIndex).alignment = { horizontal: 'center' };
        ws.getCell('C' + rowIndex).alignment = { horizontal: 'center' };
        ws.getCell('D' + rowIndex).alignment = { horizontal: 'right' };
        ws.getCell('E' + rowIndex).alignment = { horizontal: 'center' };
        ws.getCell('F' + rowIndex).alignment = { horizontal: 'center' };
        ws.getCell('G' + rowIndex).alignment = { horizontal: 'center' };

        let fileName = 'CongTrinhCBHD.xlsx';
        app.excel.attachment(workBook, res, fileName);
    });


    //Hook upload
    app.uploadHooks.add('SdhCongTrinhCBHDData', (req, fields, files, params, done) =>
        app.permission.has(req, async () => await sdhCongTrinhCBHDImportData(fields, files, done), done, 'sdhCongTrinhCBHD:write')
    );

    const sdhCongTrinhCBHDImportData = async (fields, files, done) => {
        let worksheet = null;
        if (fields.userData && fields.userData[0] && fields.userData[0] == 'SdhCongTrinhCBHDData' && files.SdhCongTrinhCBHDData && files.SdhCongTrinhCBHDData.length) {
            const srcPath = files.SdhCongTrinhCBHDData[0].path;
            let workbook = app.excel.create();
            workbook = await app.excel.readFile(srcPath);
            if (workbook) {
                app.fs.deleteFile(srcPath);
                worksheet = workbook.getWorksheet(1);
                if (worksheet) {
                    const items = [];
                    const falseItems = [];
                    let index = 2;
                    try {
                        while (true) {
                            if (!worksheet.getCell('A' + index).value) {
                                done({ items, falseItems });
                                break;
                            } else {
                                const shcc = worksheet.getCell('A' + index).value;
                                const tenCongTrinh = worksheet.getCell('B' + index).value;
                                const ngayDangText = worksheet.getCell('C' + index).value;
                                const tapChi = worksheet.getCell('D' + index).value;
                                const trang = worksheet.getCell('E' + index).value;
                                const diem = worksheet.getCell('F' + index).value?.toString().trim() || '';
                                const chiSo = worksheet.getCell('G' + index).value?.toString().trim() || '';
                                const row = { shcc, tenCongTrinh, tapChi, trang, diem, chiSo };
                                if (tenCongTrinh && ngayDangText && tapChi) {
                                    if (typeof (ngayDangText) == Object) {
                                        row.ngayDang = new Date(ngayDangText).getTime();
                                    } else {
                                        const ngayDang = ngayDangText.split('/');
                                        if (ngayDang.length != 3) {
                                            falseItems.push({ ...row, ghiChuExcel: 'Ngày đăng không hợp lệ' });
                                            continue;
                                        }
                                        row.ngayDang = new Date(ngayDang[2], ngayDang[1] - 1, ngayDang[0]).setHours(0, 0, 0, 0);
                                    }

                                    if (isNaN(row.ngayDang)) {
                                        falseItems.push({ ...row, ghiChuExcel: 'Ngày đăng không hợp lệ' });
                                        continue;
                                    }

                                    const canBo = await app.model.tchcCanBo.getGiangVien('', app.utils.stringify({ shcc })).then(res => res.rows);

                                    if (canBo.length) {
                                        const hoTen = (canBo[0].ho ?? '') + ' ' + (canBo[0].ten ?? '');
                                        const tmpRow = { ...row, hoTen };
                                        items.push(tmpRow);
                                    }
                                    else {
                                        const tmpRow = { ...row, ghiChuExcel: 'Mã số cán bộ không tồn tại' };
                                        falseItems.push(tmpRow);
                                    }
                                } else {
                                    const tmpRow = { ...row, ghiChuExcel: 'Thiếu thông tin nhập vào' };
                                    falseItems.push(tmpRow);
                                }
                                index++;
                            }
                        }
                    } catch (error) {
                        app.consoleError(error);
                        done({ error });
                    }
                } else {
                    done({ error: 'No worksheet!' });
                }
            } else done({ error: 'No workbook!' });

        }
    };

    app.get('/api/sdh/cong-trinh-cbhd/all', app.permission.check('sdhCongTrinhCBHD:read'), async (req, res) => {
        try {
            const items = await app.model.sdhDmCongTrinhCanBoHuongDan.getAllColumn().then(rs => rs.rows);
            res.send({ items });

        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.post('/api/sdh/cong-trinh-cbhd/create-multiple', app.permission.check('sdhCongTrinhCBHD:write'), async (req, res) => {
        try {
            const { data } = req.body;
            await Promise.all(data.map(item => app.model.sdhDmCongTrinhCanBoHuongDan.create({
                ...item,
                'modifier': req.session.user.email,
                'timeModified': Date.now(),
            })));
            res.send({ success: true });
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.get('/api/sdh/cong-trinh-cbhd/download-excel', app.permission.check('sdhCongTrinhCBHD:export'), async (req, res) => {
        try {
            // Lấy tất cả dữ liệu từ bảng sdhDmCongTrinhCanBoHuongDan
            const data = await app.model.sdhDmCongTrinhCanBoHuongDan.getAllColumn();            // Tạo file Excel từ dữ liệu
            const list = data.rows;
            const workBook = app.excel.create();
            const ws = workBook.addWorksheet('CongTrinhCBHD');

            ws.columns = [
                { header: 'STT', key: 'stt', width: 5 },
                { header: 'MSCB', key: 'shcc', width: 10 },
                { header: 'Tên cán bộ', key: 'tenCanBo', width: 30 },
                { header: 'Tên công trình', key: 'tenCongTrinh', width: 30 },
                { header: 'Ngày đăng', key: 'ngayDang', width: 15 },
                { header: 'Tạp chí', key: 'tapChi', width: 20 },
                { header: 'Trang', key: 'trang', width: 10 },
                { header: 'Điểm', key: 'diem', width: 10 },
                { header: 'Chỉ số', key: 'chiSo', width: 10 },
                { header: 'Người sửa đổi', key: 'modifier', width: 20 },
                { header: 'Thời gian sửa đổi', key: 'timeModified', width: 20 },
            ];

            ws.getRow(1).alignment = { ...ws.getRow(1).alignment, vertical: 'middle', wrapText: true };

            ws.getRow(1).font = {
                name: 'Times New Roman',
                family: 4,
                size: 12,
                bold: true,
                color: { argb: 'FF000000' }
            };

            list.forEach((item, index) => {
                const rowIndex = index + 2;

                ws.getRow(rowIndex).alignment = { ...ws.getRow(1).alignment, vertical: 'middle', wrapText: true };
                ws.getRow(rowIndex).font = { name: 'Times New Roman' };

                ws.getCell('A' + rowIndex).value = index + 1;
                ws.getCell('B' + rowIndex).value = item.shcc;
                ws.getCell('C' + rowIndex).value = item.hoTen;
                ws.getCell('D' + rowIndex).value = item.tenCongTrinh;
                ws.getCell('E' + rowIndex).value = app.date.dateTimeFormat(new Date(item.ngayDang), 'dd/mm/yyyy');
                ws.getCell('F' + rowIndex).value = item.tapChi;
                ws.getCell('G' + rowIndex).value = item.trang;
                ws.getCell('H' + rowIndex).value = item.diem;
                ws.getCell('I' + rowIndex).value = item.chiSo;
                ws.getCell('J' + rowIndex).value = item.modifier;
                ws.getCell('K' + rowIndex).value = app.date.dateTimeFormat(new Date(parseInt(item.timeModified)), 'dd/mm/yyyy HH:MM:ss');

                ws.getCell('A' + rowIndex).alignment = { horizontal: 'center' };
                ws.getCell('D' + rowIndex).alignment = { horizontal: 'center' };
                ws.getCell('F' + rowIndex).alignment = { horizontal: 'center' };
                ws.getCell('G' + rowIndex).alignment = { horizontal: 'right' };
                ws.getCell('J' + rowIndex).alignment = { horizontal: 'center' };
            });

            let fileName = `CongTrinhCBHD_${Date.now()}.xlsx`;
            app.excel.attachment(workBook, res, fileName);
        } catch (error) {
            app.consoleError(error);
            res.send({ error });
        }
    });

    app.get('/api/sdh/cong-trinh-cbhd/tuyensinh/download-excel', app.permission.check('sdhCongTrinhCBHD:export'), async (req, res) => {
        try {
            const data = await app.model.sdhTsCongTrinhCbhd.getAll();
            const workBook = app.excel.create();
            const ws = workBook.addWorksheet('CongTrinhCBHD');

            ws.columns = [
                { header: 'SHCC', key: 'Cbhd', width: 10 },
                { header: 'Tên công trình', key: 'ten', width: 30 },
                { header: 'Ngày đăng', key: 'ngayDang', width: 15 },
                { header: 'Tạp chí', key: 'tenTapChi', width: 20 },
                { header: 'Trang', key: 'trang', width: 10 },
                { header: 'Chỉ Số', key: 'chiSo', width: 10 },
                { header: 'Điểm', key: 'diem', width: 10 },
            ];

            ws.getRow(1).alignment = { ...ws.getRow(1).alignment, vertical: 'middle', wrapText: true };

            ws.getRow(1).font = {
                name: 'Times New Roman',
                family: 4,
                size: 12,
                bold: true,
                color: { argb: 'FF000000' }
            };

            data.forEach((item, index) => {
                const rowIndex = index + 2;
                ws.getRow(rowIndex).alignment = { ...ws.getRow(1).alignment, vertical: 'middle', wrapText: true };
                ws.getRow(rowIndex).font = { name: 'Times New Roman' };

                ws.getCell('A' + rowIndex).value = item.idCbhd;
                ws.getCell('B' + rowIndex).value = item.ten;
                ws.getCell('C' + rowIndex).value = app.date.dateTimeFormat(new Date(item.ngayDang), 'dd/mm/yyyy');
                ws.getCell('D' + rowIndex).value = item.tenTapChi;
                ws.getCell('E' + rowIndex).value = item.trang;
                ws.getCell('F' + rowIndex).value = item.chiSo;
                ws.getCell('G' + rowIndex).value = item.diem;

                ws.getCell('A' + rowIndex).alignment = { horizontal: 'center' };
                ws.getCell('D' + rowIndex).alignment = { horizontal: 'center' };
                ws.getCell('F' + rowIndex).alignment = { horizontal: 'center' };
                ws.getCell('G' + rowIndex).alignment = { horizontal: 'right' };
            });
            let fileName = `CongTrinhTuyenSinhCBHD_${Date.now()}.xlsx`;
            app.excel.attachment(workBook, res, fileName);
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });

    app.get('/api/sdh/cong-trinh-cbhd/page/:pageNumber/:pageSize', app.permission.check('sdhCongTrinhCBHD:read'), async (req, res) => {
        try {
            const { pageNumber, pageSize } = req.params;
            let { filter = {}, condition } = req.query || {};
            const searchTerm = condition ?? '';

            const { rows: list, pagenumber, pagesize, pagetotal, totalitem } = await app.model.sdhDmCongTrinhCanBoHuongDan.searchPage(Number(pageNumber), Number(pageSize), app.utils.stringify(filter), searchTerm);

            res.send({ page: { list, pageNumber: pagenumber, pageSize: pagesize, pageTotal: pagetotal, totalItem: totalitem } });
        } catch (error) {
            app.consoleError(req, error);
            res.send({ error });
        }
    });
};
Editor is loading...
Leave a Comment