Selaa lähdekoodia

feat: 导入关键字表

vian 1 vuosi sitten
vanhempi
commit
252aa84b82

+ 10 - 1
modules/price_info_lib/controllers/index.js

@@ -171,7 +171,16 @@ class PriceInfoController extends BaseController {
                     throw 'excel没有对应数据。';
                 }
                 // 提取excel数据并入库
-                importType === 'originalData' ? await facade.importExcelData(libID, sheet[0].data) : await facade.importKeyData(libID, areaID, sheet[0].data, sheet[1].data);
+                if (importType === 'originalData') {
+                    // 导入原始数据
+                    await facade.importExcelData(libID, sheet[0].data);
+                } else if (importType === 'mainSub') {
+                    // 导入主从表
+                    await facade.importMainSubData(libID, areaID, sheet[0].data, sheet[1].data)
+                } else if (importType === 'keys') {
+                    // 导入关键字
+                    await facade.importKeyData(libID, areaID, sheet[0].data)
+                }
                 // 删除文件
                 if (uploadFullName && fs.existsSync(uploadFullName)) {
                     fs.unlink(uploadFullName);

+ 42 - 96
modules/price_info_lib/facade/index.js

@@ -242,7 +242,7 @@ async function importExcelData(libID, sheetData) {
 主表:主从对应码	别名编码	材料名称	规格	单位	含税价(元)	除税价(元)	月份备注	计算式
 副表:主从对应码	关键字	单位	关键字效果	组别	选项号
  */
-async function importKeyData(libID, areaID, mainData, subData) {
+async function importMainSubData(libID, areaID, mainData, subData) {
     const lib = await priceInfoLibModel.findOne({ ID: libID }).lean();
     if (!lib) {
         throw new Error('库不存在');
@@ -326,106 +326,51 @@ async function importKeyData(libID, areaID, mainData, subData) {
     }
 }
 
-/* async function importExcelData(libID, 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;
-        }
+// 仅导入关键字excel
+async function importKeyData(libID, areaID, subData) {
+    const lib = await priceInfoLibModel.findOne({ ID: libID }).lean();
+    if (!lib) {
+        throw new Error('库不存在');
     }
-    // 提取数据
-    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) {
+    /* const zh = await priceInfoAreaModel.findOne({ name: { $regex: '珠海' } }).lean();
+    if (!zh) {
+        throw new Error('该库不存在珠海地区');
+    } */
+    const area = await priceInfoAreaModel.findOne({ ID: areaID }).lean();
+    if (!area) {
+        throw new Error('不存在该地区');
+    }
+    // 主从对应码 - 关键字数组映射
+    const keywordMap = {};
+    for (let row = 1; row < subData.length; row++) {
+        const rowData = subData[row];
+        const keywordItem = {
+            code: rowData[0] ? String(rowData[0]) : '',
+            keyword: rowData[1] || '',
+            unit: rowData[2] || '',
+            coe: rowData[3] || '',
+            group: rowData[4] || '',
+            optionCode: rowData[5] || '',
+        };
+        if (!keywordItem.code) {
             continue;
         }
-        data.push({
-            ID: uuidV1(),
-            compilationID,
-            libID,
-            areaID,
-            classID,
-            period: libs[0].period,
-            code,
-            name,
-            specs,
-            unit,
-            noTaxPrice,
-            taxPrice
-        });
+        (keywordMap[keywordItem.code] || (keywordMap[keywordItem.code] = [])).push(keywordItem);
     }
-    if (data.length) {
-        await priceInfoItemModel.remove({ libID });
-        await priceInfoItemModel.insertMany(data);
-    } else {
-        throw 'excel没有有效数据。'
+
+    const priceItems = await priceInfoItemModel.find({ libID: lib.ID, areaID, period: lib.period, compilationID: lib.compilationID }).lean();
+
+    const bulks = [];
+    priceItems.forEach(item => {
+        if (item.code && keywordMap[item.code]) {
+            bulks.push({ updateOne: { filter: { ID: item.ID }, update: { $set: { keywordList: keywordMap[item.code] || [] } } } });
+        }
+    });
+    if (bulks.length) {
+        await priceInfoItemModel.bulkWrite(bulks);
     }
-} */
+}
+
 
 // 获取费用定额的地区数据
 async function getAreas(compilationID) {
@@ -788,6 +733,7 @@ module.exports = {
     processChecking,
     crawlDataByCompilation,
     importExcelData,
+    importMainSubData,
     importKeyData,
     getAreas,
     updateAres,

+ 8 - 2
web/maintain/price_info_lib/html/main.html

@@ -29,7 +29,8 @@
                                     <th width="160">添加时间</th>
                                     <th width="70">操作</th>
                                     <th width="150">原始数据</th>
-                                    <th width="70">关键字</th>
+                                    <th width="60">主从表</th>
+                                    <th width="60">关键字</th>
                                 </tr>
                             </thead>
                             <tbody id="showArea">
@@ -59,6 +60,11 @@
                                     </td>
                                     <td>
                                         <a class="btn btn-secondary btn-sm import-data lock-btn-control disabled"
+                                            onclick='handleImportClick("<%= lib.ID%>", "mainSub")' href="javacript:void(0);"
+                                            title="导入主从表"><i class="fa fa-sign-in fa-rotate-90"></i>导入</a>
+                                    </td>
+                                    <td>
+                                        <a class="btn btn-secondary btn-sm import-data lock-btn-control disabled"
                                             onclick='handleImportClick("<%= lib.ID%>", "keys")' href="javacript:void(0);"
                                             title="导入关键字"><i class="fa fa-sign-in fa-rotate-90"></i>导入</a>
                                     </td>
@@ -205,7 +211,7 @@
     <div class="modal-dialog" role="document">
         <div class="modal-content">
             <div class="modal-header">
-                <h5 class="modal-title">导入数据</h5>
+                <h5 class="modal-title" id="import-title">导入数据</h5>
                 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                     <span aria-hidden="true">×</span>
                 </button>

+ 8 - 1
web/maintain/price_info_lib/js/main.js

@@ -143,12 +143,19 @@ const setAreaOption = ($parent, $sub, areaMap) => {
 
 let importType = 'originalData';
 
+const importTitleMap = {
+    originalData: '原始数据',
+    mainSub: '主从数据',
+    keys: '关键字数据',
+};
+
 // 点击导入按钮
 async function handleImportClick(libID, type) {
     try {
         setCurLib(libID);
         importType = type;
-        if (importType === 'keys') {
+        $('#import-title').text(`导入${importTitleMap[type] || '数据'}`);
+        if (['mainSub', 'keys'].includes(importType)) {
             $('#import-area').show();
             const areaMap = await getAreaMap();
             setAreaOption($('#import-parent-area'), $('#import-sub-area'), areaMap);