/**
 * Created by jimiz on 2017/5/19.
 */
// var mongoose = require("mongoose");
// var db = require("../db/project_db");
// var subSchema = require("./bills_sub_schemas");
// var Schema = mongoose.Schema;
// var deleteSchema = require('../../../public/models/delete_schema');
// var consts = require('./project_consts');
// var projectConsts = consts.projectConst;
// var commonConsts = consts.commonConst;
//
// var projectPropertiesSchema = new Schema({
//     ID: Number,
//     unitPriceFileID: Number,
//     unitPriceFileVer: Number,
//     deleteInfo: deleteSchema
// });
//
// var projectProperties = db.model("prj_properties", projectPropertiesSchema, "prj_properties");
//
// var projectPropertiesDAO = function(){};
//
// projectPropertiesDAO.prototype.getData = function(projectID, callback){
//     projectProperties.find({'$or': [{ID: projectID, deleteInfo: null}, {ID: projectID, 'deleteInfo.deleted': {$in: [null, false]}}]}, '-_id', function(err, datas){
//         if (!err) {
//             callback(0, projectConsts.PROPERTIES, datas);
//         } else {
//             callback(1, '', null);
//         };
//     });
// };
//
// projectPropertiesDAO.prototype.save = function(datas, callback){
//     var functions = [];
//     var data;
//
//     function saveOne(data) {
//         return function (cb) {
//             switch (data.updateType) {
//                 case commonConsts.UT_UPDATE:
//                     projectProperties.update({ID: data.ID}, data, cb);
//                     break;
//                 case commonConsts.UT_CREATE:
//                     projectProperties.create(data, cb);
//                     break;
//                 case commonConsts.UT_DELETE:
//                 /* 假删除
//                  var item = new GLJ(doc);
//                  item.remove(cb);
//                  */
//             }
//         }
//     }
//     for (var i = 0; i < datas.length; i++){
//         data = datas[i];
//         functions.push(saveOne(data));
//     }
//
//     async.parallel(functions, callback);
// };
//
// module.exports = new projectPropertiesDAO();

let mongoose = require('mongoose');
let propertyDetailSchema = new mongoose.Schema({
    key: String,
    dispName: String,
    value: String
});

let prj_property = new mongoose.Schema({
    projectID: Number,
    properties: Array  //原则上是应该设置好对象的属性，但在实际应用中发现mongoose保存的时候会自动增加objectId（_id）字段，无语。。。
    // properties: [{
    //     key: String,
    //     dispName: String,
    //     value: String
    // }]
}, {versionKey: false});

//预储存的项目属性，在项目创建的时候copy一份到项目中
let cfg_property = new mongoose.Schema({
    rationLibID: Number,      //这个对应标准定额库ID
    descr: String,      //描述
    properties: [propertyDetailSchema]
}, {versionKey: false});

let prj_prop_mdl = mongoose.model('project_property', prj_property, 'prj_properties');
let std_prj_prop_mdl = mongoose.model('cfg_prj_property', cfg_property, 'std_cfg_prj_properties');

module.exports = {
    prj_prop_mdl: prj_prop_mdl,
    std_prj_prop_mdl: std_prj_prop_mdl
};
