/**
 * Created by Mai on 2017/4/24.
 */

let billsData = require('../../main/models/bills');
let rationData = require('../../main/models/ration');
let projCounter = require('../../main/models/proj_counter_model');
let projSetting = require('../../main/models/proj_setting_model');
// let volumePriceData = require('../../volume_price/models/volume_price_model');
let async = require('async');

module.exports = {
    copyProjectData: function (srcProjID, newProjID, callback) {
        var fun = [];
        var copyData = function (model) {
            return (cb) => {
                model.getQueryData({"projectID": srcProjID}, '-_id').then(function (results, err) {
                    if (results.length > 0) {
                        let datas = [];
                        results.forEach(function (result) {
                            result._doc.projectID = newProjID;
                            datas.push(result._doc);
                        });
                        return model.insertData(datas, cb);
                    } else {
                        cb(err);
                    }
                }).catch(cb);
            };
        };
        fun.push(copyData(billsData));
        fun.push(copyData(rationData));
        fun.push(copyData(projCounter));
        fun.push(copyData(projSetting));
        // fun.push(copyData(volumePriceData));
        async.parallel(fun, (err) => callback(err));
    }
};