Browse Source

project properties & test

TonyKang 8 years ago
parent
commit
c6705c9b73

+ 105 - 0
modules/main/facade/prj_properties_facade.js

@@ -1,3 +1,108 @@
 /**
  * Created by Tony on 2017/8/3.
  */
+let mongoose = require("mongoose");
+let prj_prop_mdl = mongoose.model("project_property");
+
+module.exports = {
+    createPrjProperties: createPrjProperties,
+    removeProperty : removeProperty,
+    updateWhole: updateWhole,
+    updateOneProperty: updateOneProperty,
+    addOrChangeProperty: addOrChangeProperty,
+    removeOneProperty: removeOneProperty,
+
+    createPrjPropertiesWithCallBack: createPrjPropertiesWithCallBack,
+    removePropertyWithCallBack: removePropertyWithCallBack,
+    updateWholeWithCallBack: updateWholeWithCallBack,
+    updateOnePropertyWithCallBack: updateOnePropertyWithCallBack,
+    addOrChangePropertyWithCallBack: addOrChangePropertyWithCallBack,
+    removeOnePropertyWithCallBack: removeOnePropertyWithCallBack
+};
+
+async function createPrjProperties(prjId, dftItems) {
+    let data = {};
+    data.projectID = prjId;
+    data.properties = dftItems;
+    let saveObj = new prj_prop_mdl(data);
+    let rst = await  saveObj.save();
+    return rst;
+};
+
+async function removeProperty(prjId) {
+    let rst = await  prj_prop_mdl.remove({'projectID': prjId});
+    return rst;
+};
+
+async function updateWhole(prjPropObj) {
+    let rst = null;
+    if (prjPropObj && prjPropObj.hasOwnProperty('projectID') && prjPropObj.hasOwnProperty('properties') && (prjPropObj.properties instanceof Array)) {
+        rst = await  prj_prop_mdl.update({'projectID': prjPropObj.projectID}, prjPropObj);
+    }
+    return rst;
+}
+
+async function updateOneProperty(prjId, key, newValue) {
+    let rst = await  prj_prop_mdl.update({"projectID": prjId, "properties.key": key}, {"$set": {"properties.$.value": newValue}});
+    return rst;
+}
+
+async function addOrChangeProperty(prjId, newItem) {
+    let rst = null;
+    rst = await  prj_prop_mdl.findOne({"projectID": prjId, "properties.key": newItem.key});
+    //console.log('find result: ' + rst);
+    if (rst === null || rst.length === 0) {
+        rst = await  prj_prop_mdl.update({"projectID": prjId}, {"$push": {"properties": newItem}});
+        //console.log('push result: ' + rst);
+    } else {
+        rst = await  prj_prop_mdl.update({"projectID": prjId, "properties.key": newItem.key}, {"$set": {"properties.$.value": newItem.value}});
+        //console.log('update result: ' + rst);
+    }
+    return rst;
+};
+
+async  function removeOneProperty(prjId, key) {
+    let rst = prj_prop_mdl.update({"projectID": prjId}, {"$pull": {"properties": {"key": key}}});
+    return rst;
+}
+
+
+function createPrjPropertiesWithCallBack(prjId, dftItems, cb) {
+    let data = {};
+    data.projectID = prjId;
+    data.properties = dftItems;
+    let saveObj = new prj_prop_mdl(data);
+    saveObj.save(cb);
+};
+
+function removePropertyWithCallBack(prjId, cb) {
+    prj_prop_mdl.remove({'projectID': prjId}, cb);
+}
+
+function updateWholeWithCallBack(prjPropObj, cb) {
+    if (prjPropObj && prjPropObj.hasOwnProperty('projectID') && prjPropObj.hasOwnProperty('properties') && (prjPropObj.properties instanceof Array)) {
+        prj_prop_mdl.update({'projectID': prjPropObj.projectID}, prjPropObj, cb);
+    }
+}
+
+function updateOnePropertyWithCallBack(prjId, key, newValue, cb) {
+    prj_prop_mdl.update({"projectID": prjId, "properties.key": key}, {"$set": {"properties.$.value": newValue}}, cb);
+}
+
+function addOrChangePropertyWithCallBack(prjId, newItem, cb) {
+    prj_prop_mdl.findOne({"projectID": prjId, "properties.key": newItem.key}, function(err, result){
+        if (err === null) {
+            if (result === null) {
+                console.log('no result');
+                prj_prop_mdl.update({"projectID": prjId}, {"$push": {"properties": newItem}}, cb);
+            } else {
+                console.log('has result');
+                prj_prop_mdl.update({"projectID": prjId, "properties.key": newItem.key}, {"$set": {"properties.$.value": newItem.value}}, cb);
+            }
+        }
+    });
+}
+
+function removeOnePropertyWithCallBack(prjId, key, cb) {
+    prj_prop_mdl.update({"projectID": prjId}, {"$pull": {"properties": {"key": key}}}, cb);
+}

+ 12 - 11
modules/main/models/prj_properties.js

@@ -62,17 +62,18 @@
 //
 // module.exports = new projectPropertiesDAO();
 
-var mongoose = require('mongoose'),
-    Schema = mongoose.Schema;
+let mongoose = require('mongoose');
+// let propertyDetailSchema = new mongoose.Schema({
+//     key: String,
+//     dispName: String,
+//     value: String
+// });
 
-var prj_property = new Schema({
+let prj_property = new mongoose.Schema({
     projectID: Number,
-    items: [
-        {
-            key: String,
-            value: String
-        }
-    ]
-},{versionKey:false});
+    properties: Array
+}, {versionKey: false});
+
+let prj_prop_mdl = mongoose.model('project_property', prj_property, 'prj_properties');
 
-mongoose.model('prj_properties', prj_property, 'prj_properties');
+module.exports = prj_prop_mdl;

+ 1 - 1
modules/reports/models/rpt_tpl_data.js

@@ -11,7 +11,7 @@ let rptTplPrjSchema = new Schema({
     "NextSiblingID": Number,
     "name": String,         //项目名称
     "location": String,     //工程地点
-    "constructor": String,  //建设单位
+    "constructCompany": String,  //建设单位
     "supervisor": String,   //监理
     "auditor": String       //审核
 });

+ 145 - 0
test/unit/main/prj_properties_test.js

@@ -0,0 +1,145 @@
+/**
+ * Created by Tony on 2017/8/4.
+ */
+let test = require('tape');
+let mongoose = require("mongoose");
+let dbm = require("../../../config/db/db_manager");
+require('../../../modules/main/models/prj_properties');
+let ppFacade = require('../../../modules/main/facade/prj_properties_facade');
+
+//let randomPrjId = Math.round(Math.random() * 100);
+let randomPrjId = 35;
+dbm.connect();
+
+// test('测试 - 创建dummy项目属性: ', function (t) {
+//     let items = [];
+//     items.push({key: "contractNum", dispName: "合同号", value: "天字第一号"});
+//     items.push({key: "appType", dispName: "工程专业", value: "建筑工程"});
+//     items.push({key: "constructType", dispName: "工程类别", value: "住宅"});
+//     items.push({key: "startDate", dispName: "建设日期", value: "2017-07-31"});
+//     items.push({key: "owner", dispName: "建设单位", value: "珠海纵横创新"});
+//     items.push({key: "designer", dispName: "设计单位", value: "珠海设计局"});
+//     items.push({key: "builder", dispName: "施工单位", value: "中建十局"});
+//
+//     let results = ppFacade.createPrjProperties(randomPrjId, items);
+//     results.then(function(rst) {
+//         console.log('rst.projectID: ' + rst.projectID);
+//         for (let prop of rst.properties) {
+//             console.log('property ' + prop.dispName + ': ' + prop.value);
+//         }
+//         t.pass('just pass!');
+//         t.end();
+//     });
+// });
+
+// test('测试 - 整个更新项目属性: ', function (t) {
+//     let items = [];
+//     items.push({key: "contractNum", dispName: "合同号", value: "人字第三号"});
+//     items.push({key: "appType", dispName: "工程专业", value: "建筑工程"});
+//     items.push({key: "constructType", dispName: "工程类别", value: "住宅"});
+//     items.push({key: "startDate", dispName: "建设日期", value: "2017-08-01"});
+//     items.push({key: "owner", dispName: "建设单位", value: "珠海纵横创新"});
+//     items.push({key: "designer", dispName: "设计单位", value: "珠海设计局"});
+//     items.push({key: "builder", dispName: "施工单位", value: "中建十局"});
+//
+//     let udtObj = {"projectID": 35, "properties": items};
+//
+//     let rst = ppFacade.updateWhole(udtObj);
+//     if (rst) {
+//         rst.then(function(results){
+//             for (let prop in results) {
+//                 console.log(prop + ': ' + results[prop]);
+//             }
+//             t.pass('just pass!');
+//             t.end();
+//         });
+//     } else {
+//         console.log('update failed!');
+//         t.pass('just pass!');
+//         t.end();
+//     }
+// });
+
+// test('测试 - 新增项目属性: ', function (t) {
+//     let newItem = {key: "auditor", dispName: "监理单位", value: "珠海德信"};
+//     let results = ppFacade.addOrChangeProperty(35, newItem);
+//     if (results instanceof Promise) {
+//         results.then(function (rst) {
+//             for (let prop in rst) {
+//                 console.log("property " + prop + ' : ' + rst[prop]);
+//             }
+//         })
+//     } else {
+//         for (let prop in results) {
+//             console.log("property " + prop + ' : ' + results[prop]);
+//         }
+//     }
+//
+//     t.pass('just pass!');
+//     t.end();
+//
+// });
+
+// test('测试 - 删除全体项目属性: ', function (t) {
+//     let rst = ppFacade.removeProperty(35);
+//     if (rst) {
+//         if (rst instanceof Promise) {
+//             rst.then(function(results){
+//                 for (let prop in results) {
+//                     console.log(prop + ': ' + results[prop]);
+//                 }
+//                 t.pass('just pass with remove1!');
+//                 t.end();
+//             });
+//         } else {
+//             for (let prop in results) {
+//                 console.log(prop + ': ' + results[prop]);
+//             }
+//             t.pass('just pass with remove2!');
+//             t.end();
+//         }
+//     } else {
+//         t.pass('just pass but not remove!');
+//         t.end();
+//     }
+// });
+
+// test('测试 - 更新项目属性: ', function (t) {
+//     let rst = ppFacade.updateOneProperty(35, "builder", "中建十一局");
+//     if (rst) {
+//         rst.then(function(results){
+//             for (let prop in results) {
+//                 console.log(prop + ': ' + results[prop]);
+//             }
+//             t.pass('just pass with update!');
+//             t.end();
+//         });
+//     } else {
+//         t.pass('just pass but not update!');
+//         t.end();
+//     }
+// });
+
+// test('测试 - 移除一项属性: ', function (t) {
+//     let rst = ppFacade.removeOneProperty(35, "auditor");
+//     if (rst) {
+//         rst.then(function(results){
+//             for (let prop in results) {
+//                 console.log(prop + ': ' + results[prop]);
+//             }
+//             t.pass('just pass with remove!');
+//             t.end();
+//         });
+//     } else {
+//         t.pass('just pass but not remove!');
+//         t.end();
+//     }
+// });
+
+test('close the connection', function (t) {
+    setTimeout(function () {
+        mongoose.disconnect();
+        t.pass('closing db connection');
+        t.end();
+    }, 1500);
+})