소스 검색

feat: 拷贝计价规则

vian 2 년 전
부모
커밋
f499772166

+ 12 - 0
modules/users/controllers/compilation_controller.js

@@ -755,6 +755,18 @@ class CompilationController extends BaseController {
 
     }
 
+    async copyValuation(req, res) {
+        const compilationModel = new CompilationModel();
+        const { compilationID, valuationType, orgValuationID, newName } = JSON.parse(req.body.data);
+        try {
+            await compilationModel.copyValuation(compilationID, valuationType, orgValuationID, newName);
+            res.json({ error: 0, message: '复制成功', data: null });
+        } catch (err) {
+            res.json({ error: 1, message: String(err), data: null });
+        }
+
+    }
+
     async addEngineer(request,response){
         let engineeringLibModel = new EngineeringLibModel();
         try {

+ 31 - 0
modules/users/models/compilation_model.js

@@ -8,6 +8,8 @@
 import mongoose from "mongoose";
 import BaseModel from "../../common/base/base_model";
 import  uuidV1  from 'uuid/v1';
+const engineeringModel = mongoose.model('engineering_lib');
+const compilationModel = mongoose.model('compilation');
 
 class CompilationModel extends BaseModel {
 
@@ -457,6 +459,35 @@ class CompilationModel extends BaseModel {
         return await this.updateById(compilationId, {categoryID: category});
     }
 
+    // 拷贝计价规则
+    async copyValuation(compilationID, valuationType, orgValuationID, newName) {
+        const objectId = mongoose.Types.ObjectId(compilationID);
+        const compilationData = await compilationModel.findOne({_id: objectId }).lean();
+        const orgValuation = compilationData[valuationType].find(item => item.id === orgValuationID);
+        if (!orgValuation) {
+            throw '不存在对应计价规则';
+        }
+        const newValuation = {
+            ...orgValuation,
+            id: uuidV1(),
+            enable: false,
+            name: newName
+        };
+        await compilationModel.update({ _id:  objectId }, { $push: { [valuationType]: newValuation } });
+        await this.copyEngineeringList(orgValuationID, newValuation.id);
+    }
+    
+
+    // 拷贝工程专业
+    async copyEngineeringList(orgValuationID, newValuationID) {
+        const engineeringList = await engineeringModel.find({ valuationID: orgValuationID }, '-_id').lean();
+        console.log(engineeringList);
+        const newEngineeringList = engineeringList.map(item => ({ ...item, valuationID: newValuationID }));
+        if (newEngineeringList.length) {
+            await engineeringModel.insertMany(newEngineeringList);
+        }
+    }
+
 }
 
 export default CompilationModel;

+ 1 - 0
modules/users/routes/compilation_route.js

@@ -34,6 +34,7 @@ module.exports = function (app) {
     router.post('/template/:section/:id/:engineering/update', compilationController.auth, compilationController.init, compilationController.updateBillsTemplate);
     router.post('/addEngineer', compilationController.auth, compilationController.init, compilationController.addEngineer);
     router.post('/copyRationLibs', compilationController.auth, compilationController.init, compilationController.copyRationLibs);
+    router.post('/copyValuation', compilationController.auth, compilationController.init, compilationController.copyValuation);
 
     router.post('/changeCategory', compilationController.auth, compilationController.init, compilationController.changeCategory);
 

+ 32 - 0
web/users/js/compilation.js

@@ -217,6 +217,38 @@ $(document).ready(function() {
         }
     });
 
+    // 复制计价规则
+    let orgValuationID;
+    let valuationType;
+    $('.copy-bill-valuation').click(function() {
+        valuationType = 'bill_valuation';
+        orgValuationID = $(this).data('id');
+        console.log(orgValuationID);
+        $('#copy-valuation-modal').modal('show');
+    });
+    $('.copy-ration-valuation').click(function() {
+        valuationType = 'ration_valuation';
+        orgValuationID = $(this).data('id');
+        console.log(orgValuationID);
+        $('#copy-valuation-modal').modal('show');
+    });
+
+    $('#copy-valuation-confirm').click(async function() {
+        const newName = $('#valuation-name').val();
+        console.log(newName);
+        if (!newName) {
+            alert('请输入计价规则');
+            return;
+        }
+        const compilationID =  window.location.search.replace('?id=', '');
+        $.bootstrapLoading.start();
+        await ajaxPost('/compilation/copyValuation', { compilationID, valuationType, orgValuationID, newName });
+        $.bootstrapLoading.end();
+        window.location.reload();
+
+
+    })
+
     // 拖动排序
     const dragSelector = '.ration_tr[draggable=true]';
     const rationBodySelector = '#ration_tbody';

+ 2 - 0
web/users/views/compilation/index.html

@@ -55,6 +55,7 @@
                             <input type="checkbox" data-id="<%= bill.id %>" class="fileType" id="<%= bill.id %>_submission" <% if (bill.fileTypes && bill.fileTypes.includes(1)) { %> checked <% } %>  /> 预算      
                         </td>
                         <td>
+                            <a class="copy-bill-valuation" data-id="<%= bill.id %>" href="javascript:void(0);" class="btn btn-sm">拷贝</a>
                             <a href="/compilation/valuation/bill/<%= bill.id %>" class="btn btn-sm">编辑</a>
                             <a onclick="$('#del').attr('selectedId', '<%= bill.id %>')" href="#" data-id="<%= bill.id %>" data-toggle="modal" data-target="#del" class="btn btn-sm text-danger">删除</a>
                             <!--<a href="/compilation/valuation/bill/delete/<%= bill.id %>" class="btn btn-sm text-danger">删除</a>-->
@@ -99,6 +100,7 @@
                             
                         </td>
                         <td>
+                            <a class="copy-ration-valuation" data-id="<%= ration.id %>" href="javascript:void(0);" class="btn btn-sm">拷贝</a>
                             <a href="/compilation/valuation/ration/<%= ration.id %>" class="btn btn-sm">编辑</a>
                             <a href="/compilation/valuation/ration/delete/<%= ration.id %>" class="btn btn-sm text-danger">删除</a>
                         </td>

+ 23 - 0
web/users/views/compilation/modal.html

@@ -456,4 +456,27 @@
     </div>
 </div>
 
+<div class="modal fade in" id="copy-valuation-modal" data-backdrop="static">
+    <div class="modal-dialog" role="document">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h5 class="modal-title">复制计价规则</h5>
+                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                    <span aria-hidden="true">×</span>
+                </button>
+            </div>
+            <div class="modal-body">
+                <div class="form-group">
+                    <label>计价规则</label>
+                    <input class="form-control" placeholder="请输入新的计价规则" id="valuation-name">
+                </div>
+            </div>
+            <div class="modal-footer" style="justify-content: center">
+                <button type="button" class="btn btn-primary" data-dismiss="modal" id="copy-valuation-confirm">是</button>
+                <button type="button" class="btn btn-primary" data-dismiss="modal">否</button>
+            </div>
+        </div>
+    </div>
+</div>
+
 <script type="text/javascript" src="/web/users/js/col_setting.js"></script>