/**
 * 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/testGLJData.js');

// let mixedData = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testDataResult_广西公路日常养护预算指标(2021).js');
let mixedData = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testDataResult_广西公路养护预算定额(2021).js');
let data = JSON.parse(mixedData);
let gljItemArr = data.gljList;

// let data1 = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testRationData_广西公路日常养护预算指标(2021).js');
let data1 = fs.readFileSync('D:/GitHome/YangHuOperation/tmp/testRationData_广西公路养护预算定额(2021).js');
let rationItemArr = JSON.parse(data1);

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

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

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

let pmKeysCache = {};

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

let newData = [];
let firstStr = 'RationID|GLJID|Amount|Proportion|Type';
newData.push(firstStr);

for (let item of rationItemArr) {

    for (let rgljItem of item.rationGljList) {
        if (parseFloat(rgljItem.consumeAmt) > 0) {
            let str = `${item.ID}|${rgljItem.gljId}|${rgljItem.consumeAmt}|${(rgljItem.proportion !== undefined)?rgljItem.proportion:0}|${gljCache[prefix + rgljItem.gljId].gljType}`;
            // if (pmKeysCache.hasOwnProperty(item.ID + prefix + rgljItem.gljId)) {
            //     pmKeysCache[item.ID + prefix + rgljItem.gljId]++;
            //     console.log('dup key: ' + (item.ID + prefix + rgljItem.gljId));
            // } else {
            //     pmKeysCache[item.ID + prefix + rgljItem.gljId] = 1;
            // }
            newData.push(str);
        }
    }
}

let ttlStr = newData.join('\n');

let regExp = new RegExp('"', "gm");
ttlStr = ttlStr.replace(regExp, '');

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


