瀏覽代碼

工料机汇总页面切换单价文件数据操作

olym 8 年之前
父節點
當前提交
5cbbe20a0e

+ 40 - 2
modules/glj/controllers/glj_controller.js

@@ -308,9 +308,47 @@ class GLJController extends BaseController {
      * @param {object} response
      * @return {void}
      */
-    changeUnitPriceFile(request, response) {
-        let unitPriceModel = new UnitPriceModel();
+    async changeUnitPriceFile(request, response) {
+        let projectId = request.body.project_id;
+        let changeUnitPriceId = request.body.change_id;
+        let responseData = {
+            err: 0,
+            msg: ''
+        };
+        try {
+            let currentUnitPriceId = await ProjectModel.getUnitPriceFileId(projectId);
 
+            // 获取即将更改的单价文件信息
+            let unitPriceFileModel = new UnitPriceFileModel();
+            let targetUnitPriceFile = await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId});
+            if (targetUnitPriceFile === null) {
+                throw '没有找到对应的单价文件';
+            }
+
+            // 查找对应单价文件的项目工料机数据
+            let unitPriceModel = new UnitPriceModel();
+            let copyResult = await unitPriceModel.copyNotExist(currentUnitPriceId, changeUnitPriceId);
+            // 复制成功后更改project数据
+            if (!copyResult) {
+                throw '复制数据失败';
+            }
+
+            let changeUnitPriceFileInfo = {
+                id: targetUnitPriceFile.id,
+                name: targetUnitPriceFile.name
+            };
+            let result = ProjectModel.changeUnitPriceFileInfo(projectId, changeUnitPriceFileInfo);
+            if (!result) {
+                throw '切换单价文件失败!';
+            }
+
+        } catch (error) {
+            console.log(error);
+            responseData.err = 1;
+            responseData.msg = error;
+        }
+
+        response.json(responseData);
     }
 
     /**

+ 48 - 0
modules/glj/models/unit_price_model.js

@@ -213,6 +213,54 @@ class UnitPriceModel extends BaseModel {
         return result.ok !== undefined && result.ok === 1;
     }
 
+    /**
+     * 复制单价文件数据
+     *
+     * @param {Number} currentUnitPriceId
+     * @param {Number} changeUnitPriceId
+     * @return {Promise}
+     */
+    async copyNotExist(currentUnitPriceId, changeUnitPriceId) {
+        let result = false;
+        // 首先查找原单价文件id下的数据
+        let currentUnitList = await this.findDataByCondition({unit_price_file_id: currentUnitPriceId}, null, false);
+        if (currentUnitList === null) {
+            return result;
+        }
+        // 过滤mongoose格式
+        currentUnitList = JSON.stringify(currentUnitList);
+        currentUnitList = JSON.parse(currentUnitList);
+
+        let codeList = [];
+        for (let tmp of currentUnitList) {
+            if (codeList.indexOf(tmp.code) >= 0) {
+                continue;
+            }
+            codeList.push(tmp.code);
+        }
+
+        // 查找即将更替的单价文件是否存在对应的工料机数据
+        let condition = {unit_price_file_id: changeUnitPriceId, code: {"$in": codeList}};
+        let targetUnitList = await this.findDataByCondition(condition, null, false, 'code');
+
+        // 如果没有重叠的数据则原有的数据都复制一份
+        let insertData = [];
+        for (let tmp of currentUnitList) {
+            if (targetUnitList !== null && targetUnitList[tmp.code] !== undefined) {
+                continue;
+            }
+            // 删除原有id信息
+            delete tmp._id;
+            delete tmp.id;
+            tmp.unit_price_file_id = changeUnitPriceId;
+            insertData.push(tmp);
+        }
+
+        return insertData.length > 0 ? this.add(currentUnitList) : true;
+
+    }
+
+
 }
 
 export default UnitPriceModel;

+ 1 - 0
modules/glj/routes/glj_router.js

@@ -18,6 +18,7 @@ router.post('/update', gljController.init, gljController.updateData);
 router.post('/get-ratio', gljController.init, gljController.getRatio);
 router.post('/delete-ratio', gljController.init, gljController.deleteMixRatio);
 router.post('/get-project-info', gljController.init, gljController.getProjectInfo);
+router.post('/change-file', gljController.init, gljController.changeUnitPriceFile);
 
 router.get('/test', gljController.init, gljController.test);
 router.get('/testModify', gljController.init, gljController.testModify);

+ 17 - 0
modules/pm/models/project_model.js

@@ -316,6 +316,23 @@ ProjectsDAO.prototype.getUserProjectData = async function(userId) {
     return projectList;
 };
 
+/**
+ * 更改项目属性中的单价文件
+ *
+ * @param {Number} projectId
+ * @param {Object} changeInfo
+ * @return {Promise}
+ */
+ProjectsDAO.prototype.changeUnitPriceFileInfo = async function(projectId, changeInfo) {
+    projectId = parseInt(projectId);
+    if (isNaN(projectId) || projectId <= 0) {
+        return false;
+    }
+    let result = await Projects.update({ID: projectId}, {"property.unitPriceFile": changeInfo});
+
+    return result.ok === 1;
+};
+
 module.exports ={
     project: new ProjectsDAO(),
     projType: projectType

+ 1 - 1
web/building_saas/glj/html/glj_index.html

@@ -90,7 +90,7 @@
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
-                <a href="" class="btn btn-primary" id="change-file-confirm">确定</a>
+                <a href="javascript:void(0);" class="btn btn-primary" id="change-file-confirm">确定</a>
             </div>
         </div>
     </div>

+ 26 - 2
web/building_saas/glj/js/project_glj.js

@@ -106,15 +106,39 @@ $(document).ready(function () {
 
     // 单价文件切换确认
     $("#change-file-confirm").click(function() {
+        if (isChanging) {
+            return false;
+        }
         let type = $("input[name='change-type']:checked").val();
         type = parseInt(type);
+        let changeUnitPriceId = 0;
         if (type === 0) {
             // 从本项目中选择
-
+            changeUnitPriceId = $("#self-file").val();
         } else {
             // 从其他项目中复制
-
+            changeUnitPriceId = $("#other-file").val();
         }
+        $.ajax({
+            url: '/glj/change-file',
+            type: 'post',
+            data: {project_id: scUrlUtil.GetQueryString('project'), change_id: changeUnitPriceId},
+            error: function() {
+                isChanging = false;
+            },
+            beforeSend: function() {
+                isChanging = true;
+            },
+            success: function(response) {
+                isChanging = false;
+                if (response.err === 1) {
+                    let msg = response.msg !== undefined ? response.msg : '未知错误';
+                    alert(msg);
+                    return false;
+                }
+                $('#change-dj').modal("hide");
+            }
+        });
     });
 
     // 是否主动更改数据