/**
 * Created by Tony on 2021/10/8.
 */

let fs = require('fs');

let data1 = fs.readFileSync('D:/GitHome/ConstructionOperation/tmp/testRationData_1633514079281.js');
let data2 = fs.readFileSync('D:/GitHome/ConstructionOperation/tmp/testRationCoeData.js');
let data3 = fs.readFileSync('D:/GitHome/ConstructionOperation/tmp/testGLJData.js');

//----------------------------------------

let rationItemArr = JSON.parse(data1);
let coeItemArr = JSON.parse(data2);
let gljItemArr = JSON.parse(data3);

let gljCache = {};
let coeCache = {};
const prefix = '_'

for (let gljItem of gljItemArr) {
    gljCache[prefix + gljItem.code] = gljItem;
}


for (let gljItem of coeItemArr) {
    coeCache[prefix + gljItem.ID] = gljItem;
}

let newData = [];

// 先测量最多有多少coes
let maxCoes = 0;
let fixedCoes = [];
for (let idx = 0; idx < coeItemArr.length; idx++) {
    let item = coeItemArr[idx];
    let dtlMax = 0;
    let dtlFixedCoes = [];
    for (let coe of item.coes) {
        if (coe.coeType !== '人工' && coe.coeType !== '机械' && coe.coeType !== '材料' && coe.coeType !== '定额') {
            dtlMax++;
        } else {
            dtlFixedCoes.push(coe);
        }
    }
    fixedCoes.push(dtlFixedCoes);
    maxCoes = Math.max(dtlMax, maxCoes);
}

let firstStr = 'ID|Name|Type|Content|G|L|J';
//for (let idx = 0; idx < maxCoes - 1; idx++) {
for (let idx = 1; idx <= 10; idx++) {
    let str = '';
    str = `|GLJ${idx}|Count${idx}|Type${idx}`;
    firstStr += str;
};
newData.push(firstStr);

function _getAmountByCoeType(coeObjs, coeItem) {
    let rst = [];
    let coeG = 0, coeL = 0, coeJ = 0;
    for (let coeObj of coeObjs) {
        switch (coeObj.coeType) {
            case '定额':
                coeG = coeObj.amount;
                coeL = coeObj.amount;
                coeJ = coeObj.amount;
                break;
            case '人工' :
                coeG = coeObj.amount;
                break;
            case '单个工料机' :
                let gcKey = prefix + coeObj.gljCode;
                if (!gljCache.hasOwnProperty(gcKey)) {
                    console.log('code1: ' + ' - ' + coeItem.ID + ' - ' + coeObj.coeType);
                } else {
                    if (gljCache[gcKey].gljType > 200 && gljCache[gcKey].gljType < 300) {
                        coeL = coeObj.amount;
                    } else if (gljCache[gcKey].gljType > 300 && gljCache[gcKey].gljType < 400) {
                        coeJ = coeObj.amount;
                    } else if (gljCache[gcKey].gljType <= 2) {
                        coeG = coeObj.amount;
                    } else {
                        console.log('不明材料1: ' + ' - ' + gcKey + ' - ' + gljCache[gcKey].gljType);
                    }
                }
                break;
            case '机械' :
                coeJ = coeObj.amount;
                break;
            case '材料' :
                coeL = coeObj.amount;
                break;
            default :
                console.log('不明coeType1: ' + ' - ' + coeObj.coeType);
                break;
        }
    }
    rst.push(coeG);
    rst.push(coeL);
    rst.push(coeJ);
    return rst;
}

// 先输出coe list
for (let idx = 0; idx < coeItemArr.length; idx++) {
    let coeItem = coeItemArr[idx];
    // coeCache[prefix + gljItem.ID] = gljItem;
    let firstCoe = coeItem.coes[0];
    let firstCoeType = firstCoe.operator;
    let cRst =_getAmountByCoeType(fixedCoes[idx], coeItem);
    let firstCoeG = cRst[0];
    let firstCoeL = cRst[1];
    let firstCoeJ = cRst[2];
    let str = `${coeItem.ID}|${coeItem.name}|${firstCoeType}|${coeItem.content}|${firstCoeG}|${firstCoeL}|${firstCoeJ}`;

    /*
    for (let idx = 0; idx < maxCoes - 1; idx++) {
        if (idx < coeItem.coes.length - 1) {
            // 判断coeItem.coes[idx+1].gljCode，有一下几种情况：udefined, 空串, 其他正常值
            let dtlRst =_getAmountByCoeType(coeItem.coes[idx+1], coeItem);
            if (coeItem.coes[idx+1].gljCode === undefined || coeItem.coes[idx+1].gljCode === '') {
                // str += `,${gljCache[gcKey].ID},${coeItem.coes[idx+1].amount},${coeItem.coes[idx+1].operator}`;
                console.log('不明额外coeType2: ' + ' - ' + coeItem.ID + coeItem.coes[idx+1].coeType);
                str += `,-1,${coeItem.coes[idx+1].amount},${coeItem.coes[idx+1].operator}`;
            } else {
                //正常值
                let gcKey = prefix + coeItem.coes[idx+1].gljCode;
                if (!gljCache.hasOwnProperty(gcKey)) {
                    console.log('code2: ' + coeItem.ID + ' - ' + gcKey);
                }
                str += `,${gljCache[gcKey].ID},${coeItem.coes[idx+1].amount},${coeItem.coes[idx+1].operator}`;
            }
        } else {
            str += ',,,';
        }
    }
    */
    for (let idx = 1; idx <= 10; idx++) {
        str += '|||';
    }
    newData.push(str);
}
let ttlStr = newData.join('\n');

let regExp = new RegExp('"', "gm");
ttlStr = ttlStr.replace(regExp, '');
// fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/子目换算List文件_${(new Date()).getTime()}.txt`, ttlStr, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
//     if(err) throw err;
// });

// 再输出ration - coe
firstStr = 'SectionID|RationID|CoeID|Order|GroupIdx|Param';
newData = [];
newData.push(firstStr);
for (let item of rationItemArr) {
    for (let coeItem of item.rationCoeList) {
        let str = `${item.sectionId}|${item.ID}|${coeItem.ID}|${coeItem.no}||`;
        newData.push(str);
    }
}
ttlStr = newData.join('\n');
ttlStr = ttlStr.replace(regExp, '');

fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/定额_子目换算Data文件_${(new Date()).getTime()}.txt`, ttlStr, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
    if(err) throw err;
});

