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

// testRationData_1633422499669.js

let fs = require('fs');

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

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

let rationItemArr = JSON.parse(data);

// console.log(rationItemArr);

function _getIDCode(code) {
    let rst = '';
    if (code !== null && code !== undefined) {
        let codes = code.split('-');
        if (codes.length === 3) {
            let suppls = ['', '', ''];

            let regExp = new RegExp('D', "gm");
            codes[0] = codes[0].replace(regExp, '');

            if (codes[0].length < 2) {
                for (let idx = codes[0].length; idx < 2; idx++) {
                    suppls[0] = suppls[0] + '0';
                }
            }
            if (codes[1].length < 2) {
                for (let idx = codes[1].length; idx < 2; idx++) {
                    suppls[1] = suppls[1] + '0';
                }
            }
            if (codes[2].length < 3) {
                for (let idx = codes[2].length; idx < 3; idx++) {
                    suppls[2] = suppls[2] + '0';
                }
            }
            for (let si = 0; si < codes.length; si++) {
                rst += (suppls[si] + codes[si]);
            }
        } else {
            //
        }
    }
    return rst;
}

let newSectionData = [];
let firstStr = 'ID|IDCode|Code|Name|Unit|BasePrice|SectionID|Caption|FeeType|Name2|Unit2|UUID,';
newSectionData.push(firstStr);

for (let item of rationItemArr) {

    let str = `${item.ID}|${_getIDCode(item.code)}|${item.code}|${item.name}|${item.unit}|${item.basePrice}|${item.sectionId}|${item.caption}|${item.feeType}||||`;

    newSectionData.push(str);
}

// let ttlSQL = JSON.stringify(newSectionData);
// let ttlSQL = newSectionData.join('');
let ttlSQL = newSectionData.join('\n');

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

//fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/章节树临时Data文件_${(new Date()).getTime()}.js`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
// fs.writeFile(`D:/GitHome/ConstructionOperation/tmp/定额临时Data文件_${(new Date()).getTime()}.txt`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
fs.writeFile(`D:/GitHome/YangHuOperation/tmp/定额临时Data文件_${(new Date()).getTime()}.txt`, ttlSQL, { 'flag': 'a', 'encoding': 'utf-8' }, function(err){
    if(err) throw err;
});

// */

