/**
 * Created by jimiz on 2017/4/1.
 */
var mongoose = require("mongoose");
var db = require("../db/project_db");
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 unitPriceFile = require('../../glj/models/unit_price_file')

var projectGLJSchema = new Schema({
    ID: Number,
    projectID: Number,
    type: String,
    amount: String, // Decimal
    rationPrice: String, //Decimal
    adjustPrice: String, //Decimal
    price: String, //Decimal
    isInterim: Boolean, // 是否暂估
    provideType: Number, // 供货方式
    partyAAmount: String, // Decimal 甲供数量
    deliveryType: String, // 交货方式
    deliveryPlace: String, // 送达地点
    noTender: Boolean,
    deleteInfo: deleteSchema
});

var projectGLJ = db.model("projectGLJ", projectGLJSchema, "projectGLJ");

var projectGLJDAO = function(){};

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

// 工料机汇总的工料机不在这里新增，只能由add方法新增，删除需要在前端projectGLJ判断：1、是新工料机；2、没有定额引用，才可以删除
projectGLJDAO.prototype.save = function(projectID, datas, callback){
    var functions = [];
    var data;

    function saveOne(data) {
        return function (cb) {

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

    async.parallel(functions, callback);
};

projectGLJDAO.prototype.add = function(projectID, count, callback){
    unitPriceFile.add(count, callback);
};

projectGLJDAO.prototype.syncData = function(projectID, datas, callback){

};

module.exports = new projectGLJDAO();