'use strict';

/**
 *
 *
 * @author Mai
 * @date 2018/4/24
 * @version
 */
const excel = require('node-xlsx');

// 参数可选代号
const paramCodeArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad', 'ae', 'af',
    'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au',
    'av', 'aw', 'ax', 'ay', 'az'];

// 参数绑定类别
const matchType = {
    fixed_id: 1,
    properties: 2,
    node_default: 11,
    parent_default: 12,
    code: 21,
    non_match: -1,
}
const matchTypeStr = [];
matchTypeStr[matchType.fixed_id] = '全局匹配参数';
matchTypeStr[matchType.node_default] = '节点默认参数';
matchTypeStr[matchType.properties] = '属性匹配参数';
matchTypeStr[matchType.code] = '编号匹配参数';
matchTypeStr[matchType.non_match] = '非匹配参数';
const validMatchType = [matchType.properties, matchType.code, matchType.non_match];
const validProperties = {
    loadLength: 'loadLength',
    loadWidth: 'loadWidth'
}

// 参数取值
const matchNum = {
    total_price: 1,
    dgn_quantity1: 2,
    dgn_quantity2: 3,
    quantity: 4,
};
const matchNumStr = [];
matchNumStr[matchNum.total_price] = '合价';
matchNumStr[matchNum.dgn_quantity1] = '设计数量1';
matchNumStr[matchNum.dgn_quantity2] = '设计数量2';
matchNumStr[matchNum.quantity] = '清单数量';

const globalParamNodeId = 0;
const defaultGlobalParamsFile = 'app/const/global_params.xls';
// 默认全局参数
const loadingGlobalParams = function () {
    const params = [];
    const sheets = excel.parse(defaultGlobalParamsFile);
    if (sheets && sheets.length > 0 && sheets[0] !== undefined && sheets[0].data !== undefined) {
        for (const row of sheets[0].data) {
            if (!row[0] || row[0] === '') { continue; }
            const param = {
                template_id: 1,
                node_id: globalParamNodeId,
                param_id: params.length + 1,
                name: row[0],
            };
            param.code = 'g_' + paramCodeArr[param.param_id - 1];
            if (row[1] && row[1] === 'fixed') {
                param.match_type = matchType.fixed_id;
            } else if (row[2] && row[2] !== '') {
                param.match_type = matchType.code;
            } else {
                param.match_type = matchType.non_match;
            }
            param.match_key = row[2];
            if (row[3] && row[3] === '合价') {
                param.match_num = matchNum.total_price;
            } else if (row[3] === '数量1') {
                param.match_num = matchNum.dgn_quantity1;
            } else if (row[3] === '数量2') {
                param.match_num = matchNum.dgn_quantity2;
            } else if (row[3] === '数量') {
                param.match_num = matchNum.quantity;
            }
            params.push(param);
        }
    }
    return params;
};
const defaultGlobalParams = loadingGlobalParams();

// 默认节点参数
const defaultNodeParams = [
    {
        template_id: 1,
        param_id: 1,
        code: 'a',
        name: '本项合价',
        match_type: matchType.node_default,
        match_num: matchNum.total_price,
    }, {
        template_id: 1,
        param_id: 2,
        code: 'b',
        name: '本项数量1',
        match_type: matchType.node_default,
        match_num: matchNum.dgn_quantity1,
    }, {
        template_id: 1,
        param_id: 3,
        code: 'c',
        name: '本项数量2',
        match_type: matchType.node_default,
        match_num: matchNum.dgn_quantity2,
    }, {
        template_id: 1,
        param_id: 4,
        code: 'd',
        name: '本项清单数量',
        match_type: matchType.node_default,
        match_num: matchNum.quantity,
    }, {
        template_id: 1,
        param_id: 1,
        code: 'e',
        name: '父项合价',
        match_type: matchType.parent_default,
        match_num: matchNum.total_price,
    }, {
        template_id: 1,
        param_id: 2,
        code: 'f',
        name: '父项数量1',
        match_type: matchType.parent_default,
        match_num: matchNum.dgn_quantity1,
    }, {
        template_id: 1,
        param_id: 3,
        code: 'g',
        name: '父项数量2',
        match_type: matchType.parent_default,
        match_num: matchNum.dgn_quantity2,
    },
];

module.exports = {
    paramCodeArr,
    matchType,
    matchTypeStr,
    validMatchType,
    validProperties,
    matchNum,
    matchNumStr,
    globalParamNodeId,
    defaultGlobalParams,
    defaultNodeParams
}