明树Git Lab

Commit d8f9f363 authored by zfp1's avatar zfp1

update

parent 565e33e9
...@@ -24,7 +24,7 @@ async function getProjectFields(req, res, next) { ...@@ -24,7 +24,7 @@ async function getProjectFields(req, res, next) {
xmjs: '项目结束/暂停(备案资料)', xmjs: '项目结束/暂停(备案资料)',
}; };
for (const key in rawAttributes) { for (const key in rawAttributes) {
let element =_.pick(rawAttributes[key],['comment','_mark', 'fieldName']); let element = _.pick(rawAttributes[key], ['comment', '_mark', 'fieldName']);
rawAttributes[key] = element; rawAttributes[key] = element;
} }
...@@ -82,7 +82,7 @@ async function deleteProject(req, res, next) { ...@@ -82,7 +82,7 @@ async function deleteProject(req, res, next) {
async function listProject(req, res, next) { async function listProject(req, res, next) {
try { try {
let search = {where: {del: 0}}; let search = { where: { del: 0 } };
let page = req.body.page || 1; let page = req.body.page || 1;
let limit = req.body.pageSize || 10; let limit = req.body.pageSize || 10;
let offset = (page - 1) * limit; let offset = (page - 1) * limit;
...@@ -105,7 +105,16 @@ async function listProject(req, res, next) { ...@@ -105,7 +105,16 @@ async function listProject(req, res, next) {
} }
async function exportExcel(req, res, next) { /**
* 当数据量比较大的情况,导出分两步走,一步建任务,二步利用定时任务执行任务
* 当前系统数据量较小,暂考虑使用一步导出
* @param {*} req
* @param {*} res
* @param {*} next
* @returns
*/
async function exportExcel_bak(req, res, next) {
try { try {
let search = {}; //根据参数情况处理search let search = {}; //根据参数情况处理search
let obj = { let obj = {
...@@ -122,6 +131,51 @@ async function exportExcel(req, res, next) { ...@@ -122,6 +131,51 @@ async function exportExcel(req, res, next) {
} }
} }
async function exportExcel(req, res, next) {
try {
let fields = req.body.fields || [];// 传入可选导出字段
let name = `项目导出${Date.now().getTime()}`;
let worksheet = workbook.addWorksheet(name);
let attributes = DB.Project.rawAttributes;
//1. 处理excel的表头
let columns = [];
for (const key in attributes) {
const element = attributes[key];
if (fields.length == 0 || fields.includes(key)) { //没有传入可选字段 或者可选包含该字段
if (element && element.comment) {
// 有comment的导出,其他需要标注 不需要导出的用comment1
columns.push({ header: element.comment, key });
}
}
}
worksheet.columns = columns;
//2. 处理数据 其他筛选条件有待处理TODO:
let list = await DB.Project.findAll({ where: { del: 0 }, attributes: fields.length > 0 ? fields : [] });
for (let index = 0; index < list.length; index++) {
const element = list[index];
worksheet.addRow(element);
}
//3. 写入文件
let path = sysConfig.file.storagePath + '/' + moment().format('YYYYMMDD');
if (!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true });
}
let excelUrl = path + '/' + name + '.xlsx'
//4. 写入数据到存储位置
await workbook.xlsx.writeFile(excelUrl);
let fileInfo = await DB.File.create({
originalname: name + '.xlsx',
mimetype: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
filename: name + '.xlsx',
path: excelUrl,
// nginxpath: _.last(String(path).split('/mnt/vdb1')),
type: 2,
});
return res.sendData(fileInfo);
} catch (error) {
next(error);
}
}
module.exports = { module.exports = {
getProjectFields, getProjectFields,
......
...@@ -9,98 +9,98 @@ ...@@ -9,98 +9,98 @@
// 气象 // // 气象
const httpRequest = require('../utils/httpRequest'); // const httpRequest = require('../utils/httpRequest');
const deviceModule = require('../module/deviceModule'); // const deviceModule = require('../module/deviceModule');
const collectIPSModule = require('../module/collectIPSModule'); // const collectIPSModule = require('../module/collectIPSModule');
const _ = require('lodash'); // const _ = require('lodash');
const moment = require('moment'); // const moment = require('moment');
const ExcelJS = require('exceljs'); // const ExcelJS = require('exceljs');
const fs = require('fs'); // const fs = require('fs');
// exportExcel() // // exportExcel()
async function exportExcel() { // async function exportExcel() {
let info = await DB.ExcelRecord.findOne({ type: 2, status: 1, del: 0 }); // let info = await DB.ExcelRecord.findOne({ type: 2, status: 1, del: 0 });
console.log(info); // console.log(info);
// if (info && info._id && info.modleName) { // if (info && info._id && info.modleName) {
// let workbook = new ExcelJS.Workbook(); // let workbook = new ExcelJS.Workbook();
// let name = `${info._id}` // let name = `${info._id}`
// let worksheet = workbook.addWorksheet(name); // let worksheet = workbook.addWorksheet(name);
// let columns = []; // let columns = [];
// if (!_.isEmpty(DB[info.modleName] && DB[info.modleName].schema && DB[info.modleName].schema.obj)) { // if (!_.isEmpty(DB[info.modleName] && DB[info.modleName].schema && DB[info.modleName].schema.obj)) {
// for (const key in DB[info.modleName].schema.obj) { // for (const key in DB[info.modleName].schema.obj) {
// const element = DB[info.modleName].schema.obj[key]; // const element = DB[info.modleName].schema.obj[key];
// if (element && element.comment) { // if (element && element.comment) {
// // 有comment的导出,其他需要标注 不需要导出的用comment1 // // 有comment的导出,其他需要标注 不需要导出的用comment1
// columns.push({ header: element.comment, key }); // columns.push({ header: element.comment, key });
// } // }
// } // }
// } // }
// console.log(columns.length, "-=-=-", name); // console.log(columns.length, "-=-=-", name);
// if (columns.length > 0) { // if (columns.length > 0) {
// // TODO: 可以优化根据count翻页 暂时 // // TODO: 可以优化根据count翻页 暂时
// worksheet.columns = columns; // worksheet.columns = columns;
// let list = await DB[info.modleName].find(info.search || { del: 0 }); // let list = await DB[info.modleName].find(info.search || { del: 0 });
// for (let index = 0; index < list.length; index++) { // for (let index = 0; index < list.length; index++) {
// const element = list[index]; // const element = list[index];
// worksheet.addRow(element); // worksheet.addRow(element);
// } // }
// console.log("-----------------------------------",list.length,"---------------------------------------------") // console.log("-----------------------------------",list.length,"---------------------------------------------")
// if (list.length > 0) { // if (list.length > 0) {
// let path = sysConfig.file.storagePath + '/' + moment().format('YYYYMMDD'); // let path = sysConfig.file.storagePath + '/' + moment().format('YYYYMMDD');
// if (!fs.existsSync(path)) { // if (!fs.existsSync(path)) {
// fs.mkdirSync(path, { recursive: true }); // fs.mkdirSync(path, { recursive: true });
// } // }
// let excelUrl = path + '/' + name + '.xlsx' // let excelUrl = path + '/' + name + '.xlsx'
// // 1. 写入数据到存储位置 // // 1. 写入数据到存储位置
// await workbook.xlsx.writeFile(excelUrl); // await workbook.xlsx.writeFile(excelUrl);
// // 2. 创建文件信息 // // 2. 创建文件信息
// let fileInfo = await DB.File.create({ // let fileInfo = await DB.File.create({
// originalname: name + '.xlsx', // originalname: name + '.xlsx',
// mimetype: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // mimetype: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// filename: name + '.xlsx', // filename: name + '.xlsx',
// path: excelUrl, // path: excelUrl,
// nginxpath: _.last(String(path).split('/mnt/vdb1')), // nginxpath: _.last(String(path).split('/mnt/vdb1')),
// type: 2, // type: 2,
// }); // });
// // 3. 更新任务结果 // // 3. 更新任务结果
// await DB.ExcelRecord.findOneAndUpdate({_id: info._id}, {status: 2, excelUrl: fileInfo._id}) // await DB.ExcelRecord.findOneAndUpdate({_id: info._id}, {status: 2, excelUrl: fileInfo._id})
// } else { // } else {
// await DB.ExcelRecord.findOneAndUpdate({_id: info._id}, {status: 3}); // await DB.ExcelRecord.findOneAndUpdate({_id: info._id}, {status: 3});
// } // }
// } // }
// } // }
// let workbook = new Excel.Workbook(); // // let workbook = new Excel.Workbook();
// // 添加一个工作表 // // // 添加一个工作表
// let worksheet = workbook.addWorksheet('我的工作表'); // // let worksheet = workbook.addWorksheet('我的工作表');
// // 添加表头 // // // 添加表头
// worksheet.columns = [ // // worksheet.columns = [
// { header: '姓名', key: 'name' }, // // { header: '姓名', key: 'name' },
// { header: '年龄', key: 'age' }, // // { header: '年龄', key: 'age' },
// { header: '职业', key: 'occupation' } // // { header: '职业', key: 'occupation' }
// ]; // // ];
// // 添加数据行 // // // 添加数据行
// worksheet.addRow({ name: '张三', age: 30, occupation: '软件工程师' }); // // worksheet.addRow({ name: '张三', age: 30, occupation: '软件工程师' });
// worksheet.addRow({ name: '李四', age: 25, occupation: '产品经理' }); // // worksheet.addRow({ name: '李四', age: 25, occupation: '产品经理' });
// // 写入文件到磁盘 // // // 写入文件到磁盘
// await workbook.xlsx.writeFile('我的Excel.xlsx'); // // await workbook.xlsx.writeFile('我的Excel.xlsx');
} // }
module.exports = { // module.exports = {
exportExcel, // exportExcel,
} // }
......
...@@ -15,14 +15,14 @@ const config = require('../config'); ...@@ -15,14 +15,14 @@ const config = require('../config');
TODO:需要处理 多个server启动时,定时任务启动多次问题。 TODO:需要处理 多个server启动时,定时任务启动多次问题。
*/ */
const cron = () => { const cron = () => {
if (sysConfig && sysConfig.cron && sysConfig.cron.excel) { // if (sysConfig && sysConfig.cron && sysConfig.cron.excel) {
console.log("excel:", new Date()); // console.log("excel:", new Date());
new nodeCron.schedule('1 */1 * * * *', async () => { // new nodeCron.schedule('1 */1 * * * *', async () => {
console.log(new Date().getTime()) // console.log(new Date().getTime())
await excelCron.exportExcel(); // await excelCron.exportExcel();
}, { timezone: "Asia/Shanghai" }); // }, { timezone: "Asia/Shanghai" });
} // }
} }
......
...@@ -13,6 +13,8 @@ const Position = require("./model/system/position"); ...@@ -13,6 +13,8 @@ const Position = require("./model/system/position");
// 业务模型 // 业务模型
const Project = require("./model/project/project"); const Project = require("./model/project/project");
const Resources = require("./model/project/resources");
const ResourcesInfo = require("./model/project/resourcesInfo");
/** /**
* 业务表 * 业务表
...@@ -32,6 +34,8 @@ global.DB = { ...@@ -32,6 +34,8 @@ global.DB = {
File, File,
Position, Position,
Project, Project,
Resources,
ResourcesInfo,
} }
...@@ -98,3 +102,8 @@ Position.belongsToMany(User, { ...@@ -98,3 +102,8 @@ Position.belongsToMany(User, {
otherKey: 'userId', otherKey: 'userId',
as: 'users' as: 'users'
}); });
Resources.hasMany(ResourcesInfo, { foreignKey: 'resourceId', as: 'resourcesInfos' });
ResourcesInfo.belongsTo(Resources, { foreignKey: 'resourceId' });
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
const { DataTypes } = require('sequelize');
const sequelize = require('../index');
// 建设规模
const projectConScale = sequelize.define('projectConScale', {
// 定义字段
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
comment: "指标名称"
},
num: {
type: DataTypes.DECIMAL(20, 4),
comment: "数量"
},
unit: {
type: DataTypes.STRING,
comment: "单位"
},
bcsm: {
type: DataTypes.STRING,
comment: "补充说明"
},
projectId: {
type: DataTypes.INTEGER,
comment: "所属项目ID",
references: {
model: DB.Project,
key: 'id'
}
},
del: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: "0 正常 1 删除"
},
}, {
tableName: 'system_project_con_scale', // 指定表名(如果与模型名不同)
timestamps: true, // 是否自动添加 createdAt 和 updatedAt 字段(覆盖全局设置)
});
// 同步模型到数据库(创建表)
projectConScale.sync({
// force: false,
// force: true ,//会删除已存在表并重新创建
// alter: true
})
.then(() => {
console.log('projectConScale 表同步成功');
});
module.exports = projectConScale;
\ No newline at end of file
// models/ProjectStage.js
const { DataTypes } = require('sequelize'); const { DataTypes } = require('sequelize');
const sequelize = require('../index'); const sequelize = require('../index');
......
const { DataTypes } = require('sequelize');
const sequelize = require('../index');
//资源库表 记录所有的资源库
const Resources = sequelize.define('Resources', {
// 定义字段
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
comment: "资源库名称"
},
description: {
type: DataTypes.STRING,
comment: "资源库描述"
},
type: {
type: DataTypes.INTEGER,
comment: "资源库类型",
description: "1-平行化资源库 2-树形化资源库",
defaultValue: 1
},
del: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: "0 正常 1 删除"
},
}, {
tableName: 'system_resources', // 指定表名(如果与模型名不同)
timestamps: true, // 是否自动添加 createdAt 和 updatedAt 字段(覆盖全局设置)
});
// 同步模型到数据库(创建表)
Resources.sync({
// force: false,
force: true ,//会删除已存在表并重新创建
// alter: true
})
.then(() => {
console.log('Resources 表同步成功');
});
module.exports = Resources;
\ No newline at end of file
const { DataTypes } = require('sequelize');
const sequelize = require('../index');
//资源库详情表 记录所有的资源库详细信息
const ResourcesInfo = sequelize.define('ResourcesInfo', {
// 定义字段
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
key: {
type: DataTypes.STRING,
comment: "资源库字段key"
},
value: {
type: DataTypes.STRING,
comment: "资源库字段value"
},
parentId: {
type: DataTypes.INTEGER,
comment: "父级ID"
},
parentIds: {
type: DataTypes.STRING(500),
},
order: {
type: DataTypes.INTEGER,
comment: "排序字段",
defaultValue: 0
},
resourceId: {
type: DataTypes.INTEGER,
comment: "所属资源库ID",
},
del: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: "0 正常 1 删除"
},
}, {
tableName: 'system_resources_info', // 指定表名(如果与模型名不同)
timestamps: true, // 是否自动添加 createdAt 和 updatedAt 字段(覆盖全局设置)
});
// 同步模型到数据库(创建表)
ResourcesInfo.sync({
// force: false,
force: true ,//会删除已存在表并重新创建
// alter: true
})
.then(() => {
console.log('ResourcesInfo 表同步成功');
});
module.exports = ResourcesInfo;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment