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

let billsData = require('../../main/models/bills');
let projCounter = require('../../main/models/proj_counter_model');
let projSetting = require('../../main/models/proj_setting_model');
let async = require('async');

import BillsTemplateModel from "../models/templates/bills_template_model";
import EngineeringLibModel from "../../users/models/engineering_lib_model";

module.exports = {
    copyTemplateData: async function (property, newProjID, callback) {
        async.parallel([
            async function (cb) {
                // 获取清单模板数据
                let billsTemplateModel = new BillsTemplateModel();
                let templateData = JSON.stringify(await billsTemplateModel.getTemplateDataForNewProj(property.valuation, property.engineering));
                let billsDatas = JSON.parse(templateData);
                billsDatas.forEach(function (template) {
                    template.projectID = newProjID;
                });
                billsData.insertData(billsDatas, callback);
            },
            function (cb) {
                projCounter.insertData({"projectID": newProjID}, cb);
            },
            async function (cb) {
                let engineeringModel = new EngineeringLibModel();
                let engineering = await engineeringModel.getEngineering(property.engineering_id);
                projSetting.insertData({"projectID": newProjID, main_tree_col: engineering.main_tree_col}, cb);
            }
        ], (err) => callback(err));
    }
};