/**
 * Created by CSL on 2017/5/3.
 * 系数表。
 */

var mongoose = require("mongoose");

var counter = require('../../../public/counter/counter');

var coeListModel = mongoose.model("std_ration_lib_coe_list");

var coeListDAO = function(){};

const compleCoeModel = mongoose.model('complementary_ration_coe_list');

coeListDAO.prototype.getComplementaryCoes = async function (userId, compilationId, callback) {
    try {
        let coeData =  await compleCoeModel.find({userId, compilationId});
        callback(0, coeData);
    } catch (err) {
        callback(1, null);
    }
};

coeListDAO.prototype.getCoe = function (data, callback) {
    coeListModel.findOne({
            "libID": data.libID,
            "ID": data.ID,
            "$or": [{"isDeleted": null}, {"isDeleted": false}]
        },
        function (err, doc) {
            if (err) callback("获取系数明细错误！", null)
            else callback(null, doc);
        })
};

coeListDAO.prototype.getCoeItemsByIDs = function (data, callback) {
    coeListModel.find({
            "libID": data.libID,
            "ID": {"$in":data.coeIDs}
        }, ["libID","ID", "serialNo","name","content","-_id"],
        function (err, doc) {
            if (err) callback("批量获取系数明细错误！", null)
            else callback(0, doc);
        })
};

coeListDAO.prototype.getCoeItemsByNos = function (data, callback) {
    coeListModel.find({
            "libID": data.libID,
            "serialNo": {"$in":data.coeNos}
        }, ["libID","ID","serialNo","name","content","-_id"],
        function (err, doc) {
            if (err) callback("批量获取系数明细错误！", null)
            else callback(0, doc);
        })
};

coeListDAO.prototype.getCoesByLibID = function (libID, callback) {
    coeListModel.find({ "libID": libID },
        function (err, doc) {
            if (err) callback("获取定额库系数表错误", null)
            else callback(0, doc);
        })
};

coeListDAO.prototype.saveToCoeList = function(data, callback) {
    var me = this;
    if (data.addArr.length > 0) {
        me.addItems(data.addArr, callback);
    };

    if (data.deleteArr.length > 0) {
        me.deleteItems(data.deleteArr, callback);
    };

    if (data.updateArr.length > 0) {
        me.updateItems(data.updateArr, callback);
    };
};

coeListDAO.prototype.addItems = function(addArr, callback) {
    if (addArr && addArr.length > 0) {
        counter.counterDAO.getIDAfterCount(counter.moduleName.coeList, addArr.length, function(err, result){
            var maxId = result.sequence_value;
            for (var i = 0; i < addArr.length; i++) {
                var obj = new coeListModel(addArr[i]);
                obj.ID = (maxId - (addArr.length - 1) + i);
                coeListModel.create(obj, function(err) {
                    if (err) {
                        callback(true, "add fail", null);
                    } else {
                        callback(false, "add success", obj.ID);
                    };
                });
            };

        });
    } else {
        callback(true, "no source", null);
    };
};

coeListDAO.prototype.updateItems = function(updateArr, callback) {
    if (updateArr && updateArr.length > 0) {
            for (var i = 0; i < updateArr.length; i++) {
                var obj = updateArr[i];
                coeListModel.update({"libID": obj.libID, "ID": obj.ID}, updateArr[i], function(err) {
                    if (err) {
                        callback(true, "update fail", null);
                    } else {
                        callback(false, "update success", obj.ID);
                    };
                });
            };
    } else {
        callback(true, "no source", null);
    };
};

coeListDAO.prototype.deleteItems = function(deleteArr, callback) {
    if (deleteArr && deleteArr.length > 0) {
        for (var i = 0; i < deleteArr.length; i++) {
            var obj = deleteArr[i];
            coeListModel.remove({"libID": obj.libID, "ID": obj.ID}, function(err) {
                if (err) {
                    callback(true, "delete fail", null);
                } else {
                    callback(false, "delete success", obj.ID);
                };
            });
        };
    } else {
        callback(true, "no source", null);
    };
};

module.exports = new coeListDAO();