浏览代码

版本管理中的新增与删除

caiaolin 8 年之前
父节点
当前提交
6462de6984

+ 1 - 0
modules/common/helper/mongoose_helper.js

@@ -32,6 +32,7 @@ class MongooseHelper {
     findOne(conditions, fields = null) {
         let self = this;
         return new Promise(function (resolve, reject) {
+            conditions = self._convertId(conditions);
             self.model.findOne(conditions, fields, function (error, data) {
                 if (error) {
                     reject(null);

+ 56 - 0
modules/users/controllers/version_controller.js

@@ -123,6 +123,62 @@ class VersionController extends BaseController {
         response.json(responseData);
     }
 
+    /**
+     * 新增标准清单/定额库
+     *
+     * @param {object} request
+     * @param {object} response
+     * @return {void}
+     */
+    async addLib(request, response) {
+        let responseData = {
+            err: 0,
+            msg: ''
+        };
+        try {
+            let versionModel = new VersionModel();
+            let addResult = await versionModel.addLib(request.body);
+            if (!addResult) {
+                throw '新增失败';
+            }
+        } catch (error) {
+            responseData.err = 1;
+            responseData.msg = error;
+        }
+
+        response.json(responseData);
+    }
+
+    /**
+     * 删除对应的标准清单/定额库
+     *
+     * @param {object} request
+     * @param {object} response
+     * @return {void}
+     */
+    async deleteLib(request, response) {
+        let id = request.body.id;
+        let model = request.body.model;
+        let deleteId = request.body.delete_id;
+        let responseData = {
+            err: 0,
+            msg: ''
+        };
+        try {
+            let versionModel = new VersionModel();
+            let result = await versionModel.deleteLib(id, deleteId, model);
+
+            if (!result) {
+                throw '删除失败';
+            }
+        } catch (error) {
+            responseData.err = 1;
+            responseData.msg = error;
+        }
+
+        response.json(responseData);
+    }
+
 }
 
 export default VersionController;

+ 88 - 0
modules/users/models/version_model.js

@@ -65,6 +65,94 @@ class VersionModel extends BaseModel {
         return result;
     }
 
+    /**
+     * 新增标准清单/定额库
+     *
+     * @param {Object} postData
+     * @return {Promise}
+     */
+    async addLib(postData) {
+        if (postData.id === undefined || postData.model === undefined) {
+            throw '参数错误';
+        }
+        let model = postData.model;
+        let id = postData.id;
+        let standardBillId = postData.standard_bill_id;
+        let standardBill = postData.standard_bill;
+        let rationLibId = postData.ration_lib_id;
+        let rationLib = postData.ration_lib;
+
+        switch (model) {
+            case 'bill':
+                if (standardBillId === undefined || standardBill === undefined) {
+                    throw '参数错误';
+                }
+                break;
+            case 'ration':
+                if (rationLibId === undefined || rationLib === undefined) {
+                    throw '参数错误';
+                }
+                break;
+        }
+
+        let versionData = await this.findDataByCondition({_id: id});
+        if (versionData === null || versionData.standard_bill === undefined) {
+            throw '没有找到对应的数据';
+        }
+
+        let updateData = {};
+        let field = model === 'bill' ? 'standard_bill' : 'ration_lib';
+        let tmpData = {
+            id: model === 'bill' ? standardBillId : rationLibId,
+            name: model === 'bill' ? standardBill : rationLib
+        };
+        versionData[field].push(tmpData);
+        updateData[field] = versionData[field];
+
+        let result = await this.db.update({_id: id}, updateData);
+
+        return result.ok === undefined ? false : result.ok;
+    }
+
+    /**
+     * 删除对应的标准清单库/定额库
+     *
+     * @param {Number} id
+     * @param {Number} libId
+     * @param {String} model
+     * @return {Promise}
+     */
+    async deleteLib(id, libId, model) {
+        let versionData = await this.findDataByCondition({_id: id});
+        if (versionData === null || versionData.standard_bill === undefined) {
+            throw '没有找到对应的数据';
+        }
+
+        let field = model === 'bill' ? 'standard_bill' : 'ration_lib';
+        if (versionData[field].length <= 0) {
+            throw '没有对应的库数据';
+        }
+
+        let own = false;
+        for (let index in versionData[field]) {
+            if (versionData[field][index].id === libId) {
+                versionData[field].splice(index, 1);
+                own = true;
+                break;
+            }
+        }
+
+        if (!own) {
+            throw '没有对应的库数据';
+        }
+        let updateData = {};
+        updateData[field] = versionData[field];
+        let result = await this.db.update({_id: id}, updateData);
+
+        return result.ok === undefined ? false : result.ok;
+    }
+
+
 }
 
 export default VersionModel;

+ 2 - 0
modules/users/routes/version_route.js

@@ -15,6 +15,8 @@ module.exports =function (app) {
     // action定义区域
     router.get('/', versionController.auth, versionController.init, versionController.index);
     router.post('/add', versionController.auth, versionController.init, versionController.add);
+    router.post('/addLib', versionController.auth, versionController.init, versionController.addLib);
+    router.post('/deleteLib', versionController.auth, versionController.init, versionController.deleteLib);
 
     app.use("/version", router);
 };

+ 93 - 9
web/users/js/version.js

@@ -7,19 +7,37 @@
  */
 $(document).ready(function() {
     let isAdding = false;
+    let model = '';
+
     // 初始化数据
     initVersion();
 
     // 新增版本
     $("#add-version").click(function() {
         try {
-            let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData();
-
+            let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData(model);
+            let id = $("#version-id").val();
+
+            let postData = {};
+            let url = '/version/add';
+            switch (model) {
+                case 'all':
+                    postData = {model: model, name: name, standard_bill_id: standardBill, ration_lib_id: rationLib,
+                        standard_bill: standardBillString, ration_lib: rationLibString};
+                    break;
+                case 'bill':
+                    url = '/version/addLib';
+                    postData = {model: model, standard_bill_id: standardBill, standard_bill: standardBillString, id: id};
+                    break;
+                case 'ration':
+                    url = '/version/addLib';
+                    postData = {model: model, ration_lib_id: rationLib, ration_lib: rationLibString, id: id};
+                    break;
+            }
             $.ajax({
-                url: '/version/add',
+                url: url,
                 type: 'post',
-                data: {name: name,standard_bill_id: standardBill, ration_lib_id: rationLib,
-                    standard_bill: standardBillString, ration_lib: rationLibString},
+                data: postData,
                 error: function() {
                     isAdding = false;
                 },
@@ -69,6 +87,71 @@ $(document).ready(function() {
         targetSelector.html(html);
     });
 
+    // 添加
+    $(".add-version").click(function() {
+        model = $(this).data('model');
+        switch (model) {
+            case 'all':
+                $("#name-area").show();
+                $("#bill-area").show();
+                $("#ration-area").show();
+                $("#add-version-title").text('添加新版本');
+                break;
+            case 'bill':
+                $("#name-area").hide();
+                $("#bill-area").show();
+                $("#ration-area").hide();
+                $("#add-version-title").text('添加标准清单');
+                break;
+            case 'ration':
+                $("#name-area").hide();
+                $("#bill-area").hide();
+                $("#ration-area").show();
+                $("#add-version-title").text('添加定额库');
+                break;
+        }
+
+        $("#addversion").modal('show');
+    });
+
+    // 移除操作
+    let isDeleting = false;
+    $(".remove-version").click(function() {
+        let model = $(this).data('model');
+        let id = $("#version-id").val();
+        let deleteId = $(this).data('id');
+        deleteId = parseInt(deleteId);
+
+        if (model === undefined || model === '' || isNaN(deleteId)) {
+            return false;
+        }
+        if (isDeleting) {
+            return false;
+        }
+
+        $.ajax({
+            url: '/version/deleteLib',
+            type: 'post',
+            data: {id: id, model: model, delete_id: deleteId},
+            error: function() {
+                isDeleting = false;
+            },
+            beforeSend: function() {
+                isDeleting = true;
+            },
+            success: function(response) {
+                isDeleting = false;
+                if (response.err === 0) {
+                    window.location.reload();
+                } else {
+                    let msg = response.msg === undefined ? '未知错误' : response.msg;
+                    alert(msg);
+                }
+            }
+        });
+
+    });
+
 });
 
 /**
@@ -103,22 +186,23 @@ function initVersion() {
 /**
  * 校验数据
  *
+ * @param {String} model
  * @return {Array}
  */
-function getAndValidData() {
+function getAndValidData(model) {
     let name = $("input[name='version_name']").val();
     let standardBill = $("select[name='standard_bill']").val();
     let rationLib = $("select[name='ration_lib']").val();
 
-    if (name === '') {
+    if (name === '' && model === 'all') {
         throw '版本名字不能为空';
     }
 
-    if (standardBill === '' || standardBill === undefined) {
+    if ((model === 'all' || model === 'bill') && (standardBill === '' || standardBill === undefined)) {
         throw '请选择标准清单库';
     }
 
-    if (rationLib === '' || rationLib === undefined) {
+    if ((model === 'all' || model === 'ration') && (rationLib === '' || rationLib === undefined)) {
         throw '请选择定额库';
     }
 

+ 4 - 4
web/users/views/layout/layout.html

@@ -105,14 +105,14 @@
         <div class="modal-content">
             <div class="modal-header">
                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
-                <h4 class="modal-title">添加新版本</h4>
+                <h4 class="modal-title" id="add-version-title">添加新版本</h4>
             </div>
             <div class="modal-body">
-                <div class="form-group">
+                <div class="form-group" id="name-area">
                     <label>版本名称</label>
                     <input class="form-control" placeholder="" name="version_name">
                 </div>
-                <div class="form-group">
+                <div class="form-group" id="bill-area">
                     <label>标准清单</label>
                     <div class="row">
                         <div class="col-xs-4">
@@ -127,7 +127,7 @@
                         </div>
                     </div>
                 </div>
-                <div class="form-group">
+                <div class="form-group" id="ration-area">
                     <label>定额库</label>
                     <div class="row">
                         <div class="col-xs-4">

+ 7 - 6
web/users/views/version/index.html

@@ -1,7 +1,7 @@
 <div class="panel-sidebar">
 	<div class="panel-title">
 		<div class="title-bar">
-			<h2><a class="pull-right" title="添加新版本" href="javascript:void(0)" data-toggle="modal" data-target="#addversion"><span class="glyphicon glyphicon-plus"></span></a>版本管理</h2>
+			<h2><a class="pull-right add-version" title="添加新版本" href="javascript:void(0)" data-model="all"><span class="glyphicon glyphicon-plus"></span></a>版本管理</h2>
 		</div>
 	</div>
 	<div class="scrollbar-auto">
@@ -28,20 +28,21 @@
 					<label>标准清单</label>
 					<div id="bill-list">
 						<% selectedVersion.standard_bill.forEach(function (bill){ %>
-						<p class="form-control-static"><a class="pull-right text-danger" tlte="移除"><span class="glyphicon glyphicon-remove"></span></a><%= bill.name %></p>
+						<p class="form-control-static"><a class="pull-right text-danger remove-version" data-model="bill" data-id="<%= bill.id %>" title="移除"><span class="glyphicon glyphicon-remove"></span></a><%= bill.name %></p>
 						<% }) %>
 					</div>
-					<a class="btn btn-link btn-sm" href="javascript:void(0)" data-toggle="modal" data-target="#addqingdan">添加</a>
+					<a class="btn btn-link btn-sm add-version" href="javascript:void(0)" data-model="bill">添加</a>
 				</div>
 				<div class="form-group">
 					<label>定额库</label>
 					<div id="ration-list">
-						<% selectedVersion.ration_lib.forEach(function (bill){ %>
-						<p class="form-control-static"><a class="pull-right text-danger" tlte="移除"><span class="glyphicon glyphicon-remove"></span></a><%= bill.name %></p>
+						<% selectedVersion.ration_lib.forEach(function (ration){ %>
+						<p class="form-control-static"><a class="pull-right text-danger remove-version" data-model="ration" title="移除" data-id="<%= ration.id %>"><span class="glyphicon glyphicon-remove"></span></a><%= ration.name %></p>
 						<% }) %>
 					</div>
-					<a href="#" class="btn btn-link btn-sm">添加</a>
+					<a href="#" class="btn btn-link btn-sm add-version" data-model="ration">添加</a>
 				</div>
+				<input type="hidden" name="version-id" value="<%= selectedVersion._id %>" id="version-id">
 			</div>
 			<!--<div class="col-md-4">-->
 				<!--<div class="form-group">-->