/**
 * 计价规则标准库业务逻辑
 *
 * @author CaiAoLin
 * @date 2017/8/31
 * @version
 */
import BaseModel from "../../common/base/base_model";
import EngineeringLibSchema from "./schema/engineering_lib";

class EngineeringLibModel extends BaseModel {
    /**
     * 构造函数
     *
     * @return {void}
     */
    constructor() {
        let parent = super();
        parent.model = EngineeringLibSchema;
        parent.init();
    }

    /**
     * 获取标准库数据
     *
     * @param {Object} data
     * @return {Promise}
     */
    async getLib(data) {
        if (data.length <= 0) {
            return data;
        }

        let engineeringTemp = {};
        let engineeringLibIdList = [];
        // 整理出数据库需要查找的id，一次查找
        for(let valuation of data) {
            if (valuation === null) {
                continue;
            }
            for(let engineering of valuation.engineering_list) {
                if (engineeringTemp[valuation._id] === undefined) {
                    engineeringTemp[valuation._id] = [engineering.engineering_id];
                }else {
                    engineeringTemp[valuation._id].push(engineering.engineering_id);
                }
                engineeringLibIdList.push(engineering.engineering_id);
            }
        }
        // console.log(data);
        // console.log(engineeringTemp);

        // 查找对应的id数据
        let condition = {_id: {"$in": engineeringLibIdList}};
        let engineeringLibList = await this.findDataByCondition(condition, null, false, '_id');

        // 组合计价规则id对应专业工程标准库数据
        let engineeringLib = {};
        for(let index in engineeringTemp) {
            if (engineeringTemp[index].length <= 0) {
                continue;
            }
            for(let libId of engineeringTemp[index]) {
                if (engineeringLibList[libId] === undefined) {
                    continue;
                }
                if (engineeringLib[index] === undefined) {
                    engineeringLib[index] = [engineeringLibList[libId] ];
                }else {
                    engineeringLib[index].push(engineeringLibList[libId] );
                }
            }
        }

        // 替换计价规则数据
        let valuationData = JSON.parse(JSON.stringify(data));
        for(let valuation of valuationData) {
            if (valuation === null) {
                continue;
            }
            for(let engineering of valuation.engineering_list) {
                if (engineering === null || engineeringLib[valuation._id] === undefined) {
                    continue;
                }
                for (let tmp of engineeringLib[valuation._id]) {
                    if (tmp._id.toString() === engineering.engineering_id.toString()) {
                        engineering.lib = tmp;
                    }
                }
            }
        }
        return valuationData;
    };

    async getEngineering(engineering_id) {
        let condition = {_id: engineering_id};
        let engineering = await this.findDataByCondition(condition);

        return engineering;
    };

}

module.exports = EngineeringLibModel;
