明树Git Lab

Commit 813abdf1 authored by zfp1's avatar zfp1

update

parent 879382cf
Pipeline #103859 passed with stage
in 4 seconds
const { buildTree } = require("../utils");
const errorMessage = require("../utils/errorMessage");
const { Op, where } = require('sequelize');
const { Op } = require('sequelize');
/**
......@@ -9,30 +9,30 @@ const { Op, where } = require('sequelize');
async function createResource(req, res, next) {
try {
const body = req.body;
// await DB.Resources.bulkCreate(
// [
// { "key": "k1a92f", "name": "公交车" },
// { "key": "u3b81x", "name": "地铁" },
// { "key": "p9c44m", "name": "高铁" },
// { "key": "n7d22t", "name": "出租车" },
// { "key": "q4e93z", "name": "轻轨" },
// { "key": "t6f81k", "name": "磁悬浮列车" },
// { "key": "r8g55w", "name": "轮渡" },
// { "key": "s2h19p", "name": "共享单车" },
// { "key": "w9j73v", "name": "电动车" },
// { "key": "a5k33q", "name": "自驾车" },
// { "key": "y1l87u", "name": "长途客车" },
// { "key": "d3m41b", "name": "飞机" },
// { "key": "f8n22h", "name": "直升机" },
// { "key": "c4p66r", "name": "城际铁路" },
// { "key": "m2q99s", "name": "轨道交通" },
// { "key": "j7r55d", "name": "大巴车" },
// { "key": "z3s13e", "name": "顺风车" },
// { "key": "e9t80y", "name": "三轮车" },
// { "key": "h5u28i", "name": "地面有轨电车" },
// { "key": "b6v71o", "name": "高速公路客运" }
// ]
// )
// await DB.Resources.bulkCreate(
// [
// { "key": "k1a92f", "name": "公交车" },
// { "key": "u3b81x", "name": "地铁" },
// { "key": "p9c44m", "name": "高铁" },
// { "key": "n7d22t", "name": "出租车" },
// { "key": "q4e93z", "name": "轻轨" },
// { "key": "t6f81k", "name": "磁悬浮列车" },
// { "key": "r8g55w", "name": "轮渡" },
// { "key": "s2h19p", "name": "共享单车" },
// { "key": "w9j73v", "name": "电动车" },
// { "key": "a5k33q", "name": "自驾车" },
// { "key": "y1l87u", "name": "长途客车" },
// { "key": "d3m41b", "name": "飞机" },
// { "key": "f8n22h", "name": "直升机" },
// { "key": "c4p66r", "name": "城际铁路" },
// { "key": "m2q99s", "name": "轨道交通" },
// { "key": "j7r55d", "name": "大巴车" },
// { "key": "z3s13e", "name": "顺风车" },
// { "key": "e9t80y", "name": "三轮车" },
// { "key": "h5u28i", "name": "地面有轨电车" },
// { "key": "b6v71o", "name": "高速公路客运" }
// ]
// )
const ret = await DB.Resources.create(body);
return res.sendData(ret);
} catch (error) {
......@@ -43,25 +43,25 @@ async function createResource(req, res, next) {
async function updateResource(req, res, next) {
try {
const body = req.body;
if (!body.id) {
if (!body.id) {
return res.sendError(errorMessage.resourceNotFound)
}
const ret = await DB.Resources.update(body, { where: { id: body.id } });
return res.sendData(ret);
} catch (error) {
next(error);
next(error);
}
}
async function deleteResource(req, res, next) {
try {
if (!req.body.id) {
return res.sendError(errorMessage.resourceNotFound);
return res.sendError(errorMessage.resourceNotFound);
}
const ret = await DB.Resources.update({ del: 1 }, { where: { id: req.body.id } });
return res.sendData(ret);
} catch (error) {
next(error);
next(error);
}
}
......@@ -73,13 +73,13 @@ async function listResource(req, res, next) {
//翻页
let offset = (page - 1) * limit;
if (req.body.name) {
search.where = {
[Op.or]: [
//根据名字模糊搜索
{ name: { [Op.like]: `%${req.body.name}%` } }
],
del: 0,
}
search.where = {
[Op.or]: [
//根据名字模糊搜索
{ name: { [Op.like]: `%${req.body.name}%` } }
],
del: 0,
}
}
search.limit = limit;
search.offset = offset;
......@@ -100,11 +100,11 @@ async function createResourceInfo(req, res, next) {
const body = req.body;
if (!body.resourceId) {
return res.sendError(errorMessage.resourceNotFound)
}
}
const resource = await DB.Resources.findOne({ where: { id: body.resourceId } });
if (!resource) {
return res.sendError(errorMessage.resourceNotFound)
}
}
const ret = await DB.ResourcesInfo.create(body);
return res.sendData(ret);
} catch (error) {
......@@ -115,25 +115,25 @@ async function createResourceInfo(req, res, next) {
async function updateResourceInfo(req, res, next) {
try {
const body = req.body;
if (!body.id) {
if (!body.id) {
return res.sendError(errorMessage.resourceNotFound)
}
const ret = await DB.ResourcesInfo.update(body, { where: { id: body.id } });
return res.sendData(ret);
} catch (error) {
next(error);
next(error);
}
}
async function deleteResourceInfo(req, res, next) {
try {
if (!req.body.id) {
return res.sendError(errorMessage.resourceNotFound);
return res.sendError(errorMessage.resourceNotFound);
}
const ret = await DB.ResourcesInfo.update({ del: 1 }, { where: { id: req.body.id } });
return res.sendData(ret);
} catch (error) {
next(error);
next(error);
}
}
......@@ -160,35 +160,35 @@ async function listResourceInfo(req, res, next) {
console.log("ret", ret)
if (resource.type === 1) {
return res.sendData(ret);
} else if (resource.type === 2) {
} else if (resource.type === 2) {
//ret为json数据数组
const treeData = buildTree(ret);
return res.sendData(treeData);
}
}
} catch (error) {
next(error);
}
};
async function saveAllResourceInfo(req, res, next) {
try {
const { resourceId, data = [] } = req.body;
if (!resourceId || !Array.isArray(data)) {
if (!resourceId || !Array.isArray(data)) {
return res.sendError(errorMessage.resourceNotFound);
}
const resource = await DB.Resources.findOne({
where: { id: resourceId },
raw: true
});
if (!resource) {
if (!resource) {
return res.sendError(errorMessage.resourceNotFound);
}
if (resource.type === 1) {
if (resource.type === 1) {
const existingRecords = await DB.ResourcesInfo.findAll({
where: { del: 0, resourceId: resourceId },
raw: true
......@@ -202,14 +202,14 @@ async function saveAllResourceInfo(req, res, next) {
const toUpdate = [];
const incomingIds = new Set();
for (const item of data) {
for (const item of data) {
if (item.resourceId != resourceId) {
console.log(item.value, "不属于当前资源,跳过");
continue;
}
console.log("每条aaa记录", item);
const record = {
...item,
//绑定ID
......@@ -218,7 +218,7 @@ async function saveAllResourceInfo(req, res, next) {
};
//传进来有id,数据库能查到的为更新
if (item.id != null && existMap.has(item.id)) {
if (item.id != null && existMap.has(item.id)) {
toUpdate.push(record);
incomingIds.add(item.id);
} else {
......@@ -235,18 +235,18 @@ async function saveAllResourceInfo(req, res, next) {
console.log('本次更新:', toUpdate.length);
console.log('本次软删:', toDeleteIds);
if (toCreate.length > 0) {
if (toCreate.length > 0) {
await DB.ResourcesInfo.bulkCreate(toCreate);
}
if (toUpdate.length > 0) {
for (const item of toUpdate) {
if (toUpdate.length > 0) {
for (const item of toUpdate) {
await DB.ResourcesInfo.update(item, {
where: { id: item.id }
});
}
}
if (toDeleteIds.length > 0) {
await DB.ResourcesInfo.destroy(
{ del: 1 },
......@@ -259,7 +259,7 @@ async function saveAllResourceInfo(req, res, next) {
raw: true
});
return res.sendData(ret);
return res.sendData(ret);
}
return res.sendData([]);
......@@ -270,6 +270,27 @@ async function saveAllResourceInfo(req, res, next) {
}
}
async function listResourceAll(req, res, next) {
try {
let datas = await DB.Resources.findAll(
{
where: {del: 0},
include: [
{
model: DB.ResourcesInfo,
as: 'resourcesInfos',
attributes: ['key', 'value']
}
]
}
)
return res.sendData(datas)
} catch (error) {
next(error);
}
}
module.exports = {
createResource,
updateResource,
......@@ -282,5 +303,6 @@ module.exports = {
deleteResourceInfo,
listResourceInfo,
saveAllResourceInfo
saveAllResourceInfo,
listResourceAll,
}
\ No newline at end of file
......@@ -4,12 +4,13 @@
* 2. 上传文件导入数据 解析数据并存入数据库
* 3.
*/
const ExcelJS = require('exceljs');
const path = require('path');
const errorMessage = require("../utils/errorMessage");
async function getExcelTemplate(req, res, next) {
try {
let { tampName, projectId, startYear, endYear } = req.body;
let { tampName, projectId, startYear, endYear } = req.query;
const workbook = new ExcelJS.Workbook();
if (tampName === 'xmtzzjll') { // 项目投资资金流量表
if (!startYear || !endYear) {
......@@ -17,21 +18,21 @@ async function getExcelTemplate(req, res, next) {
return res.sendError(errorMessage.paramsError);
}
// 1. 读取空模板文件
const inputFilePath = path.join(__dirname, '../public/templates/项目投资资金流量表.xlsx');
const inputFilePath = path.join(__dirname, '../public/template/项目投资资金流量表.xlsx');
// 2. 处理excel 文件,动态生成表格
await workbook.xlsx.readFile(inputFilePath);
const worksheet = await workbook.getWorksheet(1);
// 2.1 处理表头
let count = endYear - startYear;
let count = Number(endYear) - Number(startYear);
let columns = [];
for (let index = 0; index <= count; index++) {
columns.push({ name: Number(startYear + index) });
columns.push({ name: Number(Number(startYear) + index) });
}
// 2.2 处理数据
let rows = Array(5).fill([]); // 5 是模板有5行
if (!projectId) {
let tzzjlls = await ProjectTzzjll.findAll({
let rows = Array(5).fill(Array(columns.length).fill(0)); // 5 是模板有5行
if (projectId) {
let tzzjlls = await DB.ProjectTzzjll.findAll({
where: { projectId: projectId, del: 0 },
raw: true,
});
......@@ -84,7 +85,7 @@ async function getExcelTemplate(req, res, next) {
const buffer = await workbook.xlsx.writeBuffer();
// 发送文件并结束响应
res.end(buffer);
} else if(tampName === 'tjjh'){ // 投决计划模板{
} else if (tampName === 'tjjh') { // 投决计划模板{
if (!startYear || !endYear) {
errorMessage.paramsError.message = "当前模板开始年份和结束年份不能为空";
return res.sendError(errorMessage.paramsError);
......@@ -182,8 +183,90 @@ function thvc(a) {
return [xjlr, xjlc, sdsqjxjll, tzsds, sdshjxjll];
}
async function importExcelTempData(req, res, next) {
try {
let { tampName, projectId } = req.body;
if (tampName == 'xmtzzjll') {
// if (!projectId) {
// errorMessage.paramsError.message = "项目ID不能为空";
// return res.sendError(errorMessage.paramsError);
// }
const workbook = new ExcelJS.Workbook();
workbook.calcProperties.fullCalcOnLoad = true;
await workbook.xlsx.load(req.file.buffer, {
ignoreNodes:['relationships', 'styles', 'calcChain', 'drawings', 'core']
});
const worksheet = await workbook.getWorksheet(1);
let headers = [], data = [];
worksheet.eachRow((row, rowIndex) => {
let colText = [];
row.eachCell((col, colIndex) => {
console.log(col.result, col.value, "--", col.text,"-", col.toString());
if (rowIndex == 3 && colIndex > 3) {
console.log(col);
headers.push(col.text);
}
if (rowIndex > 3) {
colText.push(col.value);
}
});
if (rowIndex > 3) {
data.push(colText);
}
});
return res.sendData({ headers, data });
} else if (tampName === 'tjjh') {
if (!projectId) {
errorMessage.paramsError.message = "项目ID不能为空";
return res.sendError(errorMessage.paramsError);
}
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.load(req.file.buffer);
const worksheet = await workbook.getWorksheet(1);
let years = [], data = [];
worksheet.eachRow((row, rowIndex) => {
if (rowIndex === 3) {
years = row.values.slice(4);
}
if (rowIndex > 3) {
data.push(row.values.slice(4));
}
});
const fieldMap = {
1: 'xjlr', // 现金流入
2: 'xjlc', // 现金流出
3: 'sdsqjxjll', // 所得税前净现金流量
4: 'tzsds', // 调整所得税
5: 'sdshjxjll' // 所得税后净现金流量
};
let retData = [];
years.map((year, yearIndex) => {
const yearData = { year };
for (let rowIndex = 1; rowIndex <= data.length; rowIndex++) {
const fieldKey = fieldMap[rowIndex];
if (fieldKey) {
const value = data[rowIndex - 1][yearIndex];
yearData[fieldKey] = parseFloat(value) || 0;
}
}
retData.push(yearData);
});
await ProjectTzzjll.destroy({
where: { projectId: projectId },
});
await ProjectTzzjll.bulkCreate(retData);
}
} catch (error) {
next(error)
}
}
async function importExcelTempData2(req, res, next) {
try {
let { tampName, projectId } = req.body;
if (tampName == 'xmtzzjll') {
......@@ -227,8 +310,8 @@ async function importExcelTempData(req, res, next) {
where: { projectId: projectId },
});
await ProjectTzzjll.bulkCreate(retData);
} else if(tampName === 'tjjh') {
if (!projectId) {
} else if (tampName === 'tjjh') {
if (!projectId) {
errorMessage.paramsError.message = "项目ID不能为空";
return res.sendError(errorMessage.paramsError);
}
......
......@@ -62,6 +62,7 @@ function checkPath(path) {
const allowedPatterns = [
/^\/user\/login$/,
/^\/user\/regist$/,
/^\/template\/getExcelTemplate$/,
/^\/file\/download\/\d+$/,
/^\/file\/show\/\d+$/,
];
......
......@@ -32,7 +32,8 @@
"multer": "1.4.5-lts.1",
"mysql2": "^3.15.3",
"node-cron": "3.0.3",
"sequelize": "^6.37.7"
"sequelize": "^6.37.7",
"xlsx": "^0.18.5"
},
"devDependencies": {
"cross-env": "^7.0.3",
......
......@@ -22,5 +22,8 @@ router.post('/listResourceInfo', resourceController.listResourceInfo);
router.post('/saveAllResourceInfo', resourceController.saveAllResourceInfo);
router.post('/listResourceAll', resourceController.listResourceAll);
module.exports = router;
\ No newline at end of file
......@@ -13,6 +13,7 @@ const templateController = require('../controller/templateController');
* 获取项目相关excel 模板下载
*/
router.post('/getExcelTemplate', templateController.getExcelTemplate);
router.get('/getExcelTemplate', templateController.getExcelTemplate);
......
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