/**
 * Created by jimiz on 2017/4/1.
 */
var mongoose = require("mongoose");
var db = require("../db/project_db");
var subSchema = require("./bills_sub_schemas");
var deleteSchema = require('../../../public/models/delete_schema');
var Schema = mongoose.Schema;
var async = require("async");
var counter = require("../../../public/counter/counter.js");
var consts = require('./project_consts');
var projectConsts = consts.projectConst;
var commonConsts = consts.commonConst;

var billsSchema = new Schema({
    ID: Number,
    ParentID: Number,
    NextSiblingID: Number,
    projectID: Number,
    serialNo: Number,
    chapterID: Number,
    code: String,
    fullCode: String,
    name: String,
    unit: String,
    quantity: String, // Decimal
    programID: Number,
    comments: String,
    // 调价
    xs_Labour: String, // Decimal
    xs_Material: String, // Decimal
    xs_Machine: String, // Decimal
    xs_FeeRate: String, // Decimal
    xs_LabourPrice: String, // Decimal
    xs_MaterialPrice: String, // Decimal
    xs_MachinePrice: String, // Decimal
    isTender_Labour: Boolean,
    isTender_Material: Boolean,
    isTender_Machine: Boolean,
    tenderTargetPrice: String, // Decimal
    tenderTargetUnitPrice: String, // Decimal
    // 费用字段
    fees: [subSchema.feesSchema],
    // 标记字段
    flags: [subSchema.flagsSchema],
    deleteInfo: deleteSchema
});

var bills = db.model("bills", billsSchema);

var billsDAO = function(){};

billsDAO.prototype.getData = function(projectID, callback){
    bills.find({'$or': [{projectID: projectID, deleteInfo: null}, {projectID: projectID, 'deleteInfo.deleted': {$in: [null, false]}}]}, '-_id', function(err, datas){
        if (!err) {
            callback(0, projectConsts.BILLS, datas);
        } else {
            callback(1, projectConsts.BILLS, null);
        };
    });
};

// get All Project Bills, include deleted
billsDAO.prototype.getProjectBills = function (projectId, callback) {
    if (callback) {
        bills.find({projectID: projectId}, '-_id').exec()
            .then(function (result, err) {
                if (err) {
                    callback(1, '找不到模板', null);
                } else {
                    callback(0, '', template);
                }
            });
        return null;
    } else {
        return bills.find({projectID: projectId}, '-_id').exec();
    }
}

billsDAO.prototype.AddBillsFromTemplate = function (datas, callback) {
    bills.collection.insert(datas, callback);
};

billsDAO.prototype.save = function(datas, callback){
    var functions = [];
    var data;

    function saveOne(doc) {
        return function (cb) {
            switch (doc.updateType) {
                case commonConsts.UT_UPDATE:
                    bills.update({ID: doc.ID}, doc, cb);
                    break;
                case commonConsts.UT_CREATE:
                    bills.create(doc, cb);
                    break;
                case commonConsts.UT_DELETE:
                    /* 假删除
                    var item = new bills(doc);
                    item.remove(cb);
                    */
            }
        }
    }
    for (var i = 0; i < datas.length; i++){
        data = datas[i];
        functions.push(saveOne(data));
    }

    async.parallel(functions, callback);
};

billsDAO.prototype.getItemTemplate = function(callback){
    var data = new bills;
    /* to do: 需要根据标准配置库填充fees和flags字段，是否需要更多的参数？ */
    callback(0, '', data);
};

billsDAO.prototype.allocIDs = function(IDStep, callback){
    counter.counterDAO.getIDAfterCount(counter.moduleName.bills, IDStep, function(err, highID){
        var lowID = highID - IDStep + 1;
        callback(0, '', {lowID: lowID, highID: highID});
    });
};

module.exports = new billsDAO();