Sfoglia il codice sorgente

feat: 信息价地区增加排序

vian 1 anno fa
parent
commit
915b929541

+ 38 - 38
modules/price_info_lib/facade/index.js

@@ -312,7 +312,7 @@ async function importKeyData(libID, mainData, subData) {
             taxPrice: rowData[5] || '',
             noTaxPrice: rowData[6] || '',
             dateRemark: rowData[7] || '',
-            expString: rowData[8] ||  '',
+            expString: rowData[8] || '',
             keywordList: keywordMap[code] || [],
         }
         priceItems.push(priceItem);
@@ -430,10 +430,10 @@ async function getAreas(compilationID) {
 
 async function updateAres(updateData) {
     const bulks = [];
-    updateData.forEach(({ ID, name }) => bulks.push({
+    updateData.forEach(({ ID, field, value }) => bulks.push({
         updateOne: {
             filter: { ID },
-            update: { name }
+            update: { [field]: value }
         }
     }));
     if (bulks.length) {
@@ -538,79 +538,79 @@ async function editClassData(updateData) {
 }
 
 //计算指标平均值
-function calcIndexAvg (period, areaID,compilationID,preCodeMap){
-    const newData = []; 
-    for(const code in preCodeMap){
+function calcIndexAvg(period, areaID, compilationID, preCodeMap) {
+    const newData = [];
+    for (const code in preCodeMap) {
         const indexArr = preCodeMap[code];
         let total = 0;
 
-        for(const index of indexArr){
-            total  =   scMathUtil.roundForObj(total + index,2); 
+        for (const index of indexArr) {
+            total = scMathUtil.roundForObj(total + index, 2);
         }
-        const avg = scMathUtil.roundForObj(total/indexArr.length,2); 
-        newData.push({ID:uuidV1(),code,period,areaID,compilationID,index:avg})
+        const avg = scMathUtil.roundForObj(total / indexArr.length, 2);
+        newData.push({ ID: uuidV1(), code, period, areaID, compilationID, index: avg })
     }
     return newData
 }
 
 //一个月里有classCode相同,但是价格不同的情况,取平均值
-function getClassCodePriceAvgMap  (items){
+function getClassCodePriceAvgMap(items) {
     const classCodeMap = {};
-    for(const b of items){
-        classCodeMap[b.classCode]?classCodeMap[b.classCode].push(b):classCodeMap[b.classCode]=[b];
+    for (const b of items) {
+        classCodeMap[b.classCode] ? classCodeMap[b.classCode].push(b) : classCodeMap[b.classCode] = [b];
     }
 
-    for(const classCode in classCodeMap){
+    for (const classCode in classCodeMap) {
         const baseItems = classCodeMap[classCode];
         const item = baseItems[0];
-        if(baseItems.length > 1){
+        if (baseItems.length > 1) {
             let sum = 0;
-            for(const b of baseItems){
-                sum += parseFloat(b.noTaxPrice);       
-            }      
-            classCodeMap[classCode] = {code:item.code,name:item.name,price:scMathUtil.roundForObj(sum/baseItems.length,2)};
-        }else{
-            classCodeMap[classCode] = {code:item.code,name:item.name,price:parseFloat(item.noTaxPrice)}
+            for (const b of baseItems) {
+                sum += parseFloat(b.noTaxPrice);
+            }
+            classCodeMap[classCode] = { code: item.code, name: item.name, price: scMathUtil.roundForObj(sum / baseItems.length, 2) };
+        } else {
+            classCodeMap[classCode] = { code: item.code, name: item.name, price: parseFloat(item.noTaxPrice) }
         }
 
     }
 
     return classCodeMap
-  
+
 }
 
-async function calcPriceIndex(libID,period, areaID,compilationID){
-    const baseItems = await priceInfoItemModel.find({areaID,period:'2022年-01月'}).lean();//以珠海 22年1月的数据为基准
-    const currentItems =  await priceInfoItemModel.find({areaID,period}).lean();
+async function calcPriceIndex(libID, period, areaID, compilationID) {
+    const baseItems = await priceInfoItemModel.find({ areaID, period: '2022年-01月' }).lean();//以珠海 22年1月的数据为基准
+    const currentItems = await priceInfoItemModel.find({ areaID, period }).lean();
     const preCodeMap = {};//编码前4位-指数映射
     const baseAvgMap = getClassCodePriceAvgMap(baseItems);
     const currentAvgMap = getClassCodePriceAvgMap(currentItems);
-    
+
     let message = '';
 
-    for(const classCode in currentAvgMap){
+    for (const classCode in currentAvgMap) {
         const c = currentAvgMap[classCode];
-        const preCode = c.code.substr(0,4);
+        const preCode = c.code.substr(0, 4);
         let index = 1;
         const baseItem = baseAvgMap[classCode];
-        const tem = {index,classCode,name:c.name,code:c.code};
-        
-        if(baseItem && baseItem.price){//一个月份里有多个值时,先取平均再计算
-            index =  scMathUtil.roundForObj(c.price/baseItem.price,2);
+        const tem = { index, classCode, name: c.name, code: c.code };
+
+        if (baseItem && baseItem.price) {//一个月份里有多个值时,先取平均再计算
+            index = scMathUtil.roundForObj(c.price / baseItem.price, 2);
             tem.baseName = baseItem.name;
         }
         tem.index = index;
-        if(Math.abs(index - 1) > 0.2){
+        if (Math.abs(index - 1) > 0.2) {
             const string = `classCode:${tem.classCode},编号:${tem.code},基础名称:${tem.baseName},当前库中名称:${tem.name},指数:${tem.index};\n`;
-            message +=string;
+            message += string;
             console.log(string)
-        } 
+        }
 
-      preCodeMap[preCode]?preCodeMap[preCode].push(index):preCodeMap[preCode]=[index];
+        preCodeMap[preCode] ? preCodeMap[preCode].push(index) : preCodeMap[preCode] = [index];
     }
-    const newIndexData = calcIndexAvg(period, areaID,compilationID,preCodeMap)
+    const newIndexData = calcIndexAvg(period, areaID, compilationID, preCodeMap)
     //删除旧数据
-    await priceInfoIndexModel.deleteMany({areaID,period});
+    await priceInfoIndexModel.deleteMany({ areaID, period });
     //插入新数据
     await priceInfoIndexModel.insertMany(newIndexData);
     return message;

+ 1 - 1
web/maintain/price_info_lib/html/edit.html

@@ -24,7 +24,7 @@
 
         </nav>
     </div>
-    <div class="wrapper">
+    <div class="wrapper" style="overflow: hidden;">
         <div class="main">
             <div class="left">
                 <div class="top" id="area-spread"></div>

+ 35 - 20
web/maintain/price_info_lib/js/index.js

@@ -59,7 +59,10 @@ const locked = lockUtil.getLocked();
 const AREA_BOOK = (() => {
     const cache = areaList;
     const setting = {
-        header: [{ headerName: '地区', headerWidth: $('#area-spread').width(), dataCode: 'name', dataType: 'String', hAlign: 'center', vAlign: 'center' }]
+        header: [
+            { headerName: '序号', headerWidth: 60, dataCode: 'serialNo', dataType: 'Number', hAlign: 'center', vAlign: 'center' },
+            { headerName: '地区', headerWidth: $('#area-spread').width() - 80, dataCode: 'name', dataType: 'String', hAlign: 'center', vAlign: 'center' },
+        ]
     };
     // 初始化表格
     const workBook = initSheet($('#area-spread')[0], setting);
@@ -78,21 +81,33 @@ const AREA_BOOK = (() => {
     // 编辑处理
     async function handleEdit(changedCells) {
         const updateData = [];
+        let reSort = false;
         changedCells.forEach(({ row, col }) => {
+            const field = setting.header[col].dataCode;
+            let value = sheet.getValue(row, col);
+            if (field === 'serialNo') {
+                reSort = true;
+                value = +value;
+            }
             updateData.push({
                 row,
+                field,
+                value,
                 ID: cache[row].ID,
-                name: sheet.getValue(row, col)
             });
         });
         try {
             await ajaxPost('/priceInfo/editArea', { updateData }, TIME_OUT);
-            updateData.forEach(({ row, name }) => cache[row].name = name);
+            updateData.forEach(({ row, field, value }) => cache[row][field] = value);
+            if (reSort) {
+                cache.sort((a, b) => a.serialNo - b.serialNo);
+                showData(sheet, cache, setting.header);
+            }
         } catch (err) {
             // 恢复各单元格数据
             sheetCommonObj.renderSheetFunc(sheet, () => {
-                changedCells.forEach(({ row }) => {
-                    sheet.setValue(cache[row].name);
+                changedCells.forEach(({ row, col, field }) => {
+                    sheet.setValue(row, col, cache[row][field]);
                 });
             });
         }
@@ -645,19 +660,19 @@ const CLASS_BOOK = (() => {
 
 
 
-    $calcPriceIndex.click(_.debounce(async()=>{
+    $calcPriceIndex.click(_.debounce(async () => {
         $.bootstrapLoading.start();
         try {
-        const data = await ajaxPost('/priceInfo/calcPriceIndex', { libID, period:curLibPeriod,compilationID }, TIME_OUT);
-          //alert(data);
-          
-          if(data){
-              const htmlStr = data.replace(/\n/gm,'<br>'); //replaceAll('\n','<br>',data);
-              $("#result-info-body").html(htmlStr);
-              $("#result-info").modal('show');
-          }else{
-              alert('计算完成!')
-          }  
+            const data = await ajaxPost('/priceInfo/calcPriceIndex', { libID, period: curLibPeriod, compilationID }, TIME_OUT);
+            //alert(data);
+
+            if (data) {
+                const htmlStr = data.replace(/\n/gm, '<br>'); //replaceAll('\n','<br>',data);
+                $("#result-info-body").html(htmlStr);
+                $("#result-info").modal('show');
+            } else {
+                alert('计算完成!')
+            }
 
 
         } catch (error) {
@@ -700,7 +715,7 @@ const KEYWORD_BOOK = (() => {
     }
 
     return {
-        showKeywordData 
+        showKeywordData
     }
 })();
 
@@ -708,8 +723,8 @@ const KEYWORD_BOOK = (() => {
 const PRICE_BOOK = (() => {
     const setting = {
         header: [
-            { headerName: '编码', headerWidth: 100, dataCode: 'code', dataType: 'String', hAlign: 'left', vAlign: 'center' ,formatter: "@"},
-            { headerName: '别名编码', headerWidth: 70, dataCode: 'classCode', dataType: 'String', hAlign: 'left', vAlign: 'center' ,formatter: "@"},
+            { headerName: '编码', headerWidth: 100, dataCode: 'code', dataType: 'String', hAlign: 'left', vAlign: 'center', formatter: "@" },
+            { headerName: '别名编码', headerWidth: 70, dataCode: 'classCode', dataType: 'String', hAlign: 'left', vAlign: 'center', formatter: "@" },
             { headerName: '名称', headerWidth: 200, dataCode: 'name', dataType: 'String', hAlign: 'left', vAlign: 'center' },
             { headerName: '规格型号', headerWidth: 120, dataCode: 'specs', dataType: 'String', hAlign: 'left', vAlign: 'center' },
             { headerName: '单位', headerWidth: 80, dataCode: 'unit', dataType: 'String', hAlign: 'center', vAlign: 'center' },
@@ -740,7 +755,7 @@ const PRICE_BOOK = (() => {
         $.bootstrapLoading.start();
         try {
             cache = await ajaxPost('/priceInfo/getPriceData', { classIDList }, TIME_OUT);
-            cache = _.sortBy(cache,'classCode');
+            cache = _.sortBy(cache, 'classCode');
             showData(sheet, cache, setting.header, 5);
             const row = sheet.getActiveRowIndex();
             const keywordList = cache[row] && cache[row].keywordList || [];