|
@@ -59,6 +59,120 @@ async function crawlDataByCompilation(compilationID, from, to) {
|
|
|
await crawlData(from, to);
|
|
|
}
|
|
|
|
|
|
+// 导入excel数据,格式如下
|
|
|
+// 格式1:
|
|
|
+//地区 分类 编码 名称 规格型号 不含税价 含税价
|
|
|
+//江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
|
+//江北区 木、竹材料及其制品 柏木门套线 60×10 8.76 9.9
|
|
|
+// 格式2:
|
|
|
+//地区 分类 编码 名称 规格型号 不含税价 含税价
|
|
|
+//江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
|
+// 柏木门套线 60×10 8.76 9.9
|
|
|
+// 沥青混凝土 AC-13 982.3 1110
|
|
|
+//
|
|
|
+//北碚区 木、竹材料及其制品 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
|
+async function importExcelData(libID, sheetData) {
|
|
|
+ console.log(sheetData);
|
|
|
+ const libs = await getLibs({ ID: libID });
|
|
|
+ const compilationID = libs[0].compilationID;
|
|
|
+ // 建立区映射表:名称-ID映射、ID-名称映射
|
|
|
+ const areaList = await getAreas(compilationID);
|
|
|
+ const areaMap = {};
|
|
|
+ areaList.forEach(({ ID, name }) => {
|
|
|
+ areaMap[name] = ID;
|
|
|
+ areaMap[ID] = name;
|
|
|
+ });
|
|
|
+ // 建立分类映射表:地区名称@分类名称:ID映射
|
|
|
+ const classMap = {};
|
|
|
+ const classList = await getClassData(libID);
|
|
|
+ classList.forEach(({ ID, areaID, name }) => {
|
|
|
+ const areaName = areaMap[areaID] || '';
|
|
|
+ classMap[`${areaName}@${name}`] = ID;
|
|
|
+ });
|
|
|
+ // 第一行获取行映射
|
|
|
+ const colMap = {};
|
|
|
+ for (let col = 0; col < sheetData[0].length; col++) {
|
|
|
+ const cellText = sheetData[0][col];
|
|
|
+ switch (cellText) {
|
|
|
+ case '地区':
|
|
|
+ colMap.area = col;
|
|
|
+ break;
|
|
|
+ case '分类':
|
|
|
+ colMap.class = col;
|
|
|
+ break;
|
|
|
+ case '编码':
|
|
|
+ colMap.code = col;
|
|
|
+ break;
|
|
|
+ case '名称':
|
|
|
+ colMap.name = col;
|
|
|
+ break;
|
|
|
+ case '规格型号':
|
|
|
+ colMap.specs = col;
|
|
|
+ break;
|
|
|
+ case '单位':
|
|
|
+ colMap.unit = col;
|
|
|
+ break;
|
|
|
+ case '不含税价':
|
|
|
+ colMap.noTaxPrice = col;
|
|
|
+ break;
|
|
|
+ case '含税价':
|
|
|
+ colMap.taxPrice = col;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 提取数据
|
|
|
+ const data = [];
|
|
|
+ let curAreaName;
|
|
|
+ let curClassName;
|
|
|
+ for (let row = 1; row < sheetData.length; row++) {
|
|
|
+ const areaName = sheetData[row][colMap.area] || '';
|
|
|
+ const className = sheetData[row][colMap.class] || '';
|
|
|
+ const code = sheetData[row][colMap.code] || '';
|
|
|
+ const name = sheetData[row][colMap.name] || '';
|
|
|
+ const specs = sheetData[row][colMap.specs] || '';
|
|
|
+ const unit = sheetData[row][colMap.unit] || '';
|
|
|
+ const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
|
|
|
+ const taxPrice = sheetData[row][colMap.taxPrice] || '';
|
|
|
+ if (!code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (areaName && areaName !== curAreaName) {
|
|
|
+ curAreaName = areaName;
|
|
|
+ }
|
|
|
+ if (className && className !== curClassName) {
|
|
|
+ curClassName = className;
|
|
|
+ }
|
|
|
+ const areaID = areaMap[curAreaName];
|
|
|
+ if (!areaID) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ const classID = classMap[`${curAreaName}@${curClassName}`];
|
|
|
+ if (!classID) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ data.push({
|
|
|
+ ID: uuidV1(),
|
|
|
+ compilationID,
|
|
|
+ libID,
|
|
|
+ areaID,
|
|
|
+ classID,
|
|
|
+ period: libs[0].period,
|
|
|
+ code,
|
|
|
+ name,
|
|
|
+ specs,
|
|
|
+ unit,
|
|
|
+ noTaxPrice,
|
|
|
+ taxPrice
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (data.length) {
|
|
|
+ await priceInfoItemModel.remove({ libID });
|
|
|
+ await priceInfoItemModel.insertMany(data);
|
|
|
+ } else {
|
|
|
+ throw 'excel没有有效数据。'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 获取费用定额的地区数据
|
|
|
async function getAreas(compilationID) {
|
|
|
return await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
|
|
@@ -88,7 +202,16 @@ async function deleteAreas(deleteData) {
|
|
|
}
|
|
|
|
|
|
async function getClassData(libID, areaID) {
|
|
|
- return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
|
|
|
+ if (libID && areaID) {
|
|
|
+ return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
|
|
|
+ }
|
|
|
+ if (libID) {
|
|
|
+ return await priceInfoClassModel.find({ libID }, '-_id').lean();
|
|
|
+ }
|
|
|
+ if (areaID) {
|
|
|
+ return await priceInfoClassModel.find({ areaID }, '-_id').lean();
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
async function getPriceData(classIDList) {
|
|
@@ -170,6 +293,7 @@ module.exports = {
|
|
|
updateLib,
|
|
|
deleteLib,
|
|
|
crawlDataByCompilation,
|
|
|
+ importExcelData,
|
|
|
getAreas,
|
|
|
updateAres,
|
|
|
insertAreas,
|