/**
 * Created by CSL on 2017/5/3.
 * 系数表。
 */
//modiyied by zhong on 2017/9/21
const mongoose = require('mongoose');
const coeListModel = mongoose.model('std_ration_lib_coe_list');
let counter = require('../../../public/counter/counter');
const rationModel = mongoose.model('std_ration_lib_ration_items');
import async from 'async';

let coeListDAO = function(){};

coeListDAO.prototype.getCoeReference = async function (rationRepId, coeID) {
    return await rationModel.find({rationRepId, 'rationCoeList.ID': coeID}, '-_id code').sort({code: 1});
};

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(null, 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(null, doc);
        })
};

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


coeListDAO.prototype.saveToCoeList = function(data, callback) {
    let 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);
    }*/
    if (data.addArr.length > 0 && data.updateArr.length > 0) {
        async.parallel([
            function (cb) {
                me.addItems(data.addArr, cb);
            },
            function (cb) {
                me.updateItems(data.updateArr, cb);
            }
        ], function (err, result) {
            callback(err, 'mixed', result);
        });
    }
    else if(data.addArr.length > 0){
        me.addItems(data.addArr, callback);
    }
    else if (data.updateArr.length > 0) {
        me.updateItems(data.updateArr, callback);
    }
    else if (data.deleteArr.length > 0) {
        me.deleteItems(data.deleteArr, callback);
    }
};

coeListDAO.prototype.addItems = function(addArr, callback) {
    if (addArr && addArr.length > 0) {
        counter.counterDAO.getIDAfterCount(counter.moduleName.coeList, addArr.length, function(err, result){
            let maxId = result.sequence_value;
            let functions = [];
            for (let i = 0; i < addArr.length; i++) {
                let obj = new coeListModel(addArr[i]);
                obj.ID = (maxId - (addArr.length - 1) + i);
                functions.push((function (obj) {
                    return function (cb) {
                        coeListModel.create(obj, function(err) {
                            if (err) {
                                cb(true, null);
                            } else {
                                cb(false, obj);
                            };
                        });
                    }
                })(obj));
            }
            async.parallel(functions, function (err, result) {
                if(err) callback(true, 'add fail', null);
                else callback(false, 'addSc', result);
            })
        });
    } else {
        callback(true, "no source", null);
    }
};

coeListDAO.prototype.updateItems = function(updateArr, callback) {
    if (updateArr && updateArr.length > 0) {
        let functions = [];
        for (let i = 0; i < updateArr.length; i++) {
            let obj = updateArr[i];
            let needSet = {};
            for(let attr in obj){
                if(attr !== 'libID' || attr !== 'ID'){
                    needSet[attr] = obj[attr];
                }
            }
            functions.push((function (obj) {
                return function (cb) {
                    coeListModel.update({"libID": obj.libID, "ID": obj.ID}, needSet, function(err) {
                        if (err) {
                            cb(true);
                        } else {
                            cb(false);
                        }
                    });
                }
            })(obj));
        }
        async.parallel(functions, function (err, result) {
            if(err) callback(true, 'update fail', null);
            else callback(false, 'updateSc', null);
        })
    } else {
        callback(true, "no source", null);
    }
};

coeListDAO.prototype.deleteItems = function(deleteArr, callback) {
    if (deleteArr && deleteArr.length > 0) {
        let functions = [];
        for (let i = 0; i < deleteArr.length; i++) {
            let obj = deleteArr[i];
            functions.push((function (obj) {
                return function (cb) {
                    coeListModel.remove({"libID": obj.libID, "ID": obj.ID}, function(err) {
                        if (err) {
                            cb(true, null);
                        } else {
                            cb(false, obj.ID);
                        }
                    });
                }
            })(obj));
        }
        async.parallel(functions, function (err, result) {
            if(err) callback(true, 'delete fail', null);
            else callback(false, 'sc', result);
        })
    } else {
        callback(true, "no source", null);
    }
};

module.exports = new coeListDAO();