#
#	dquerymfile_TSTULSWRF.txt
#
@group				TEST
@querytitle 		Test von updateLSWRFsummary
@querydescription	testet die Subroutine updateLSWRFsummary mit fixem sumbuff inhalt
@input_encoding		iso-8859-15
@querytype			function
@function			seqtrans.seqtrans
@title				Test von updateLSWRFsummary abgeschlossen
@init.qexpression
	this.options.maxrecsteps = 1000000;

~query.tsteps

#
#	0:
#
pre_qexpression
	let rawdata = fs.readFileSync('C:\\ta3j\\prg\\pmtools\\ccdb\\app\\temp\\TEMPFILE_20211222191505278002611_20211222012802_SUMDUMP.JSON');
	this.sumbuffer = JSON.parse(rawdata);
	logger.debug("TSTULSWRF.sumbuffer: "+aux.objTxt(this.sumbuffer,99,999999999));
gosub	updateLSWRFsummary

#
#	1:
#
end

#
# ------------------------------------------------------------------------------
#
#	<<TSTEPNUM:78>>:		SUBROUTINE:			updateLSWRFsummary
#							DESCRIPTION:		persist counters ffrom this.sumbuffer.lswrf to table svcllswrfcount
#
#	first, check if table svcllswrfcount is here at all
#
updateLSWRFsummary:
sql_mysql		select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='svcllswrfcount'
result_varname	lrfcires

#
#	<<TSTEPNUM:79>>:
#
if		(this.lrfcires.getRowCount()!=0)
goto	have_lrfctable

#
#	<<TSTEPNUM:80>>:	create svcllswrfcount table
#
sql_mysql		create table svcllswrfcount (LSWID varchar(32), DOKDATNUM varchar(8), RUECKFRAGETYPCODE varchar(10), RFCOUNT DECIMAL(19))

#
#	<<TSTEPNUM:81>>:	iterate over all LSWID/DATNUM/RFTYP counters
#
have_lrfctable:
pre_qexpression
	this.local.lswrf = this.sumbuffer.lswrf;
gosub		objectDoForEach
params.obj	~this.local.lswrf
params.sub	procmeldlswid
then_return

#
# ------------------------------------------------------------------------------
#
#	SUBROUTINE:		procmeldlswid
#	INPUT:			p0		-	LSW-ID of software which counters shall be persisted
#					p1		-	sub-structure (1 attribute for each day) of counter data known about this LSWID
#	DESCRIPTION:	persist counters for LSWID <p0>
#
#	<<TSTEPNUM:82>>:	invoke iteration about all days in the range stored in <p1>
#	
procmeldlswid:
pre_qexpression
	this.local.lswid = this.local.params.p0;
	this.local.lswrec = this.local.params.p1;
gosub		objectDoForEach
params.obj	~this.local.lswrec
params.sub	procmeldlswidtag
params.par2	~this.local.lswid
then_return

#
# ------------------------------------------------------------------------------
#
#	SUBROUTINE:			procmeldlswidtag
#	DESCRIPTION:		persist RFTYP counters for LSWID <p2> on day <p0>
#
#	<<TSTEPNUM:83>>:
#
procmeldlswidtag:
pre_qexpression
	this.local.datnum = this.local.params.p0;
	this.local.tagrec = this.local.params.p1;
	this.local.lswid  = this.local.params.p2;
gosub		objectDoForEach
params.sub	procmeldlswidtagrf
params.obj	~this.local.tagrec
params.par2	~this.local.lswid
params.par3	~this.local.datnum
then_return

#
# ------------------------------------------------------------------------------
#
#	SUBROUTINE:		objectDoForEach
#	INPUT:			obj			-	Object to iterate over - each attribute of <obj>
#					sub			-	name of subroutine to be invoked with each attribute of <obj>
#									<sub> must have 3 parameters: "p0","p1", "p2", "p3"
#									p0 receives the name of the attribute
#									p1 receives the attribute
#									p2 receives the value of <par2>
#									p3 receives the value of <par3>
#					par2		-	value of the 2nd parameter 
#					par3		-	value of the 3rd parameter
#	DESCRIPTION:	iterates over all attributes of <obj> and invokes <sub> for each attribute
#
#	<<TSTEPNUM:84>>: get parameters, setup iteration
#
objectDoForEach:
qexpression
	/* this.debug = true; */
	this.local.obj  = this.local.params.obj;
	this.local.sub	= this.local.params.sub;
	this.local.par2 = this.local.params.par2;
	this.local.par3 = this.local.params.par3;
	this.local.attribindex = 0;
	this.local.attribs = Object.keys(this.local.obj);
	logger.debug("objectDoForEach.sub="+this.local.sub+".BEGIN.data: "+aux.objTxt(this.local,999,999999999));

#
#	<<TSTEPNUM:85>>:	check if more attributes to be processed
#
nextObjectDoForEach:
if				(this.local.attribindex>=this.local.attribs.length)
return

#
#	<<TSTEPNUM:86>>:	invoke the processing subroutine
#
pre_qexpression
	this.local.attribname = this.local.attribs[this.local.attribindex];
	this.local.attrib = this.local.obj[this.local.attribname];
	logger.debug("objectDoForEach.sub="+this.local.sub+".LOOPHEAD.index="+this.local.attribindex+".attribname=\""+this.local.attribname+"\"");
gosub			@@sub@@
params.p0		~this.local.attribname
params.p1		~this.local.attrib
params.p2		~this.local.par2
params.p3		~this.local.par3

#
#	<<TSTEPNUM:87>>:	bump index, rinse and repeat
#
pre_qexpression
	this.local.attribindex++;
	logger.debug("objectDoForEach.sub="+this.local.sub+".LOOPFOOT.index="+this.local.attribindex+".attribname=\""+this.local.attribname+"\"");
goto			nextObjectDoForEach

#
# ------------------------------------------------------------------------------
#
#	SUBROUTINE:			procmeldlswidtagrf
#	INPUT:				p0		-	RFTYP-code
#						p1		-	counter record object
#						p2		-	LSWID  - ID of LSW for which this counter is valid
#						p3		-	DATNUM - day for which this counter is valid
#
#	<<TSTEPNUM:88>>:	is there already a persistant counter for LSWID/DATNUM/RFTYP
#
procmeldlswidtagrf:
pre_qexpression
	proc: {
		this.local.rftyp = this.local.params.p0;
		this.local.counter = this.local.params.p1;
		this.local.lswid = this.local.params.p2;
		this.local.datnum = this.local.params.p3;
		if (!this.local.counter.hasOwnProperty("ANZAHL_RF")) {
			this.errcoll.collect(null,"LSWID="+this.local.lswid+"/DATNUM="+this.local.datnum+"/RFTYP="+this.local.rftyp+" counter has no ANZAHL_RF",this.local);
			break proc;
		}
		this.local.rftcount = this.local.counter.ANZAHL_RF;
	}
sql				select LSWID,DOKDATNUM,RUECKFRAGETYPCODE,RFCOUNT from svcllswrfcount where LSWID=::lswid:: and DOKDATNUM=::datnum:: and RUECKFRAGETYPCODE=::rftyp::
named_params	lswid,datnum,rftyp
result_varname	rfcres
post_qexpression
	proc: {
		if (!this.rfcres) {
			this.errcoll.collect(null,"no result reading LSWDATRFCNT",this.local);
			break proc;
		}
		this.local.rfcrecnum = this.rfcres.getRowCount();
		if (this.local.rfcrecnum>1) {
			this.local.rfcres = this.rfcres;
			this.errcoll.collect(null,"LSWDTRFCNT has more than one record",this.local);
			break proc;
		}
		if (this.local.rfcrecnum==1) {
			this.local.oldrftcount = this.rfcres.getCellValue(0,3);
		}
	}

#
#	<<TSTEPNUM:89>>:	if already there goto update
#
if		(this.local.rfcrecnum>0)
goto	update_lswdatrfcnt

#
#	<<TSTEPNUM:90>>:	counter record not yet there, create it
#
sql				insert into svcllswrfcount(LSWID,DOKDATNUM,RUECKFRAGETYPCODE,RFCOUNT) values(::lswid::,::datnum::,::rftyp::,::rftcount::)
named_params	lswid,datnum,rftyp,rftcount
then_return

#
#	<<TSTEPNUM:91>>:	counter already there, update the record by incrementing the count value
#
update_lswdatrfcnt:
pre_qexpression
	this.local.newrftcount = this.local.oldrftcount + this.local.rftcount;
sql				update 	svcllswrfcount set RFCOUNT=::newrftcount:: where LSWID=::lswid:: and DOKDATNUM=::datnum:: and RUECKFRAGETYPCODE=::rftyp::
named_params	lswid,datnum,rftyp,newrftcount
then_return

