Last active
March 21, 2017 04:41
-
-
Save SergProduction/e37709ae25d749e9deb08d64fbb50c0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const copy = require('recursive-copy'); | |
const Excel = require('exceljs') | |
const _uniq = require('lodash/uniq'); | |
const dateInterval = require('./reestrDate')//{val:{}, parse:Function} | |
let sourceDir = path.join(__dirname, 'base/regis') | |
let targetDir = path.join(__dirname, 'result/regis') | |
let listSourceDir = fs.readdirSync(sourceDir) | |
console.log('listSourceDir', listSourceDir) | |
let listTargetDir = fs.readdirSync(targetDir) | |
let logOutId = []; | |
//----------- перебирает все директории(реестры), получает список файлов в каждом | |
function* generateGetReestrAndInFileList(){ | |
for(let i=0; i<listSourceDir.length; i++){ | |
let sourceFileList = path.join(__dirname, 'base/regis', listSourceDir[i]) | |
sourceFileList = fs.readdirSync(sourceFileList) | |
//console.log(sourceFileList) | |
yield { | |
reestr: listSourceDir[i], | |
fileList: sourceFileList | |
} | |
} | |
} | |
//----------- end ----------- | |
let reesAndFileList = generateGetReestrAndInFileList() //получаем итератор - список папок реестра по очереди и список файлов к каждому реестру | |
//----------- перебирает все файлы в папке(реестре) | |
function* generatorGetXlsx(){ | |
let source = reesAndFileList.next() | |
let {fileList} = source.value | |
let {reestr} = source.value | |
//console.log('reestr',reestr,'fileList',fileList) | |
if(source.done) return | |
for(let i=0; i<fileList.length; i++) | |
yield {reestr:reestr, fileName:fileList[i]} | |
} | |
//----------- end ----------- | |
let reesAndxlsx = generatorGetXlsx()//получаем итератор - папка реестра и название фаилов по очереди | |
const ID_LENGTH = 10 //количество символов лицевого счета | |
//----------- перебирает основную бд | |
let mainBaseJ = JSON.parse( fs.readFileSync( path.join(__dirname, 'result/MainBase.json') ).toString() ) | |
function* equalBase(base, ids){ | |
//let id = yield true | |
for(let i=0; i<base.length; i++){ | |
for(let z=0; z<ids.length; z++){ | |
let id; | |
if( Object.prototype.toString.call(ids[z]).slice(8,14) != 'Object' ){ | |
id = { | |
date:{ | |
start: 0, | |
end: 0 | |
}, | |
value: 0 | |
}; | |
} | |
else{ | |
id = ids[z] | |
} | |
let dirName = base[i][1].match(/[а-я]+/i); | |
let fileName = base[i][1].match(/\d+/i); | |
let licChet = base[i][3].match(/\d+/); | |
licChet = licChet ? licChet[0] : 0; | |
let start = +dateInterval.parse(id.date.start) | |
let end = +dateInterval.parse(id.date.end ) | |
let col_8 = +dateInterval.parse(base[i][8] ) | |
let col_4 = +dateInterval.parse(base[i][5] ) | |
let result = []; | |
if( col_8 >= start && | |
col_8 <= end && | |
id.value == licChet ){ | |
let dirName_c = dirName ? dirName[0] : 'other'; | |
let fileName_c = fileName ? (fileName[0] + '.1') : 'other'+i+'_1.JPEG'; | |
result.push({ fileName:fileName_c, dirName:dirName_c }) | |
//console.log('two file !') | |
} | |
if( col_4 >= start && | |
col_4 <= end && | |
id.value == licChet ){ | |
let dirName_cc = dirName ? dirName[0] : 'other'; | |
let fileName_cc = fileName ? (fileName[0] ) : 'other'+i+'.JPEG'; | |
result.push({ fileName:fileName_cc, dirName:dirName_cc }) | |
//console.log('one file !') | |
} | |
if( result.length ){ | |
logOutId.push({id: id.value, dir: fileName[0]+' '+dirName[0]}) | |
yield result | |
} | |
} | |
} | |
} | |
//----------- end | |
let logOut = []; | |
//----------- главное | |
function mainStart(){ | |
let source = reesAndxlsx.next() | |
if( source.done ){ | |
logWrite() | |
logWriteId() | |
return | |
} | |
//let iterBase = equalBase( mainBaseJ ) | |
//iterBase.next() | |
let {reestr} = source.value | |
let {fileName} = source.value | |
let fileNameXlsx = fileName.match(/[^.]+/ )[0] + '.xlsx' | |
console.log(reestr, fileName) | |
let checkFile = fs.existsSync( path.join(__dirname, 'result/regis/', reestr, fileNameXlsx) ) | |
if(!checkFile){ | |
console.warn('NOT FILE', fileName, fileNameXlsx, path.join(__dirname, 'result/regis/', reestr, fileNameXlsx)) | |
console.log('\n') | |
logOut.push({ | |
reestr: reestr, | |
source: fileName, | |
target: fileNameXlsx, | |
error: null | |
}) | |
workbook = null | |
mainStart() | |
return | |
} | |
let pathToXlsx = path.join(__dirname, 'result/regis/', reestr, fileNameXlsx) | |
//console.log(pathToXlsx) | |
var workbook = new Excel.Workbook(); | |
workbook.xlsx.readFile( pathToXlsx ) | |
.then( e => { | |
let worksheet = workbook.getWorksheet(1); | |
let ids = []; | |
let col_ids = worksheet.getColumn(1); | |
col_ids.eachCell((cell, rowNum) => { | |
let text = cell.value != null ? cell.value.richText : [] | |
ids.push( text[0] ? text[0].text : '' ) | |
//console.log( text[0] ? text[0].text : null ) | |
}) | |
ids = _uniq(ids) | |
ids = ids.filter( el => el.length == ID_LENGTH) | |
//ids.filter( el => el == 'T') | |
let d_i = dateInterval.value.filter( el => el.r == reestr )[0] | |
ids = ids.map( el => ({ | |
value:el, | |
date:{ | |
start: d_i.start, | |
end: d_i.end | |
} | |
})) | |
let iterEqualBase = equalBase( mainBaseJ, ids ) | |
let result = iterEqualBase.next() | |
myCopy( result ) | |
function myCopy(iterNext){ | |
logWriteId() | |
//console.log(iterNext.value) | |
let source_c; | |
let target_c; | |
if(iterNext.done){ | |
workbook = null | |
mainStart() | |
return | |
} | |
if(iterNext.value.length == 2){ | |
//iterNext.value[0] | |
let dirName_c = iterNext.value[0].dirName | |
let fileName_c = iterNext.value[0].fileName | |
let dirName_cc = iterNext.value[1].dirName | |
let fileName_cc = iterNext.value[1].fileName | |
//console.log('\n') | |
//console.log(dirName_c, fileName_c, dirName_cc, fileName_cc) | |
//console.log('\n') | |
let fileEnd = pathRes(reestr, fileName, dirName_c, fileName_c) | |
//console.log('\nv1 ',fileEnd,'\n') | |
twoCopy(dirName_cc, fileName_cc) | |
function twoCopy(dirName_cc, fileName_cc){ | |
underCopy(fileEnd.source, fileEnd.target, () => { | |
let fileEnd_cc = pathRes(reestr, fileName, dirName_cc, fileName_cc) | |
underCopy(fileEnd_cc.source, fileEnd_cc.target ) | |
let titlePath = pathRes(reestr, 'T.jpg', dirName_cc, fileName_cc+'T') | |
copyTitle(titlePath.source, titlePath.target) | |
}) | |
} | |
let titlePath = pathRes(reestr, 'T.jpg', dirName_c, fileName_c+'T') | |
copyTitle(titlePath.source, titlePath.target ) | |
} | |
else{ | |
let dirName_c = iterNext.value[0].dirName | |
let fileName_c = iterNext.value[0].fileName | |
let fileEnd = pathRes(reestr, fileName, dirName_c, fileName_c) | |
//console.log('\nv2 ',fileEnd,'\n') | |
underCopy(fileEnd.source, fileEnd.target) | |
let titlePath = pathRes(reestr, 'T.jpg', dirName_c, fileName_c+'T') | |
copyTitle(titlePath.source, titlePath.target ) | |
} | |
function pathRes(sourceDir, sourceFile, dirName, fileName){ | |
let source = path.join(__dirname, 'base/regis', sourceDir, sourceFile) | |
let target = path.join(__dirname, 'result/regis_jpg', dirName, fileName+'.jpg') | |
return {source, target} | |
} | |
function underCopy(source, dest, two){ | |
console.log('underCopy',source, dest) | |
copy(source, dest) | |
.then( result => { | |
console.log('YES COPY') | |
if( !iterNext.done && !two ){ | |
myCopy( iterEqualBase.next() ) | |
} | |
if( !iterNext.done && two ){ | |
two() | |
} | |
}) | |
.catch( error => { | |
if( error.code == 'EEXIST'){ | |
console.log('File exists!') | |
} | |
if( !iterNext.done && !two ){ | |
myCopy( iterEqualBase.next() ) | |
} | |
if( !iterNext.done && two ){ | |
two() | |
//console.log('error!?', error ) | |
} | |
}) | |
} | |
function copyTitle(source, dest){ | |
console.log('copyTitle\n',source, '\n', dest) | |
copy(source, dest) | |
.then( result => { | |
console.log('TITLECOPY') | |
}) | |
.catch( error => { | |
if( error.code == 'EEXIST'){ | |
console.log('TITLE IS EEXIST') | |
} | |
else{ | |
//console.log('TITLE NOT COPY', error) | |
} | |
}) | |
} | |
}//myCopy(){ | |
})//workbook.xlsx.readFile( pathToXlsx ).then( ()=>{ | |
.catch( error => { | |
console.warn('NOT FILE', fileName, fileNameXlsx, path.join(__dirname, 'result/regis/', reestr, fileNameXlsx)) | |
logOut.push({ | |
reestr: reestr, | |
source: fileName, | |
target: fileNameXlsx, | |
error: error | |
}) | |
workbook = null | |
mainStart() | |
}) | |
} | |
mainStart() | |
function logWrite(){ | |
const logStream = fs.createWriteStream( path.join(__dirname, 'logReestr.txt'), {defaultEncoding: 'utf8'}) | |
logOut.forEach( el => { | |
logStream.write( JSON.stringify(el) + '\n') | |
}) | |
logStream.end() | |
} | |
function logWriteId(){ | |
//logOutId.push({id, dir: fileName+' '+dirName}) | |
let data = ''; | |
logOutId.forEach( el => { | |
data += el.dir + ' ; ' + el.id + '\n' | |
}) | |
fs.appendFile( path.join(__dirname, 'logReestrIsId.csv'), data, {encoding:'utf8'}, err => { | |
if(err){ | |
console.log('ERROR, The "data to append" was appended to file!'); | |
} | |
}) | |
//const logStream = fs.createWriteStream( path.join(__dirname, 'logReestrIsId.txt'), {defaultEncoding: 'utf8', flags:'r+'}) | |
//logOutId.forEach( el => { | |
// logStream.write( el.dir + ' ; ' + el.id + '\n') | |
//}) | |
//logStream.end() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment