#
#				ITSV GmbH
#		CCDB - Command and Control Database
#
#	FILE:					dquerymfile_KFOPP008002.txt
#	DESCRIPTION:			DQUERY definition for DQUERY KFOPP008002
#							the query executes Step 008.002 of KFOPP - KFOQSDB ZPV Partner prefill
#							create new LE-Partner entries form table CCKFONEWPARTNERS
#							if partner entries already exist, they are inserted into CCKFONEWPARTNERCONFLICTS
#							if there are any partner conflicts, no data is inserted at all
#
@querytyitle			KFOPP-008: neue Partner aus ZPV in KFOQSDB eintragen
@querydescription		trägt neue LE-Partner aus Tabelle CCKFONEWPARTNERS in die KFOQSDB-Instanz eintragen
@group					KFOQSDBPARPREFILL
@attributenames			inscfgname:kfodbconfigname:{{inscfgnameoptions}}
@inscfgnameoptions	{
	labeltext:			"KFOQSDB-Datenbank",
	typedesc:			"KFOQSDB-Datenbank, in die die neuen Partner eingetragen werden sollen" }
@querytype				tseq
@sql_select				select * from CCKFONEWPARTNERS
#
# definition of fields for "Partner" table
#
@PFLIST	{
	fields:	[	{ name:	"PartnerBundesland" },
				{ name:	"PartnerName" },
				{ name:	"VpNr" },
				{ name:	"version" },
				{ name: "countryCode" } ] }

~query.tsteps

#
# <<TSTEPNUM:0>>: check initialize
#
if		(this.init_done)
goto	record_proper

#
# <<TSTEPNUM:1>>: initialize
#
qexpression
	proc: {
		if (!this.query.inscfgname) {
			this.errcoll.collect(null,"query parameter \"inscfgname\" is not given",{ query: this.query });
			break proc;
		}
		this.inscfgname = this.query.inscfgname;
		this.recsprocessed = 0;
		this.recsinserted = 0;
		this.recstoprocess = this.records_to_process;
		this.logswritten = 0;
		let fnames = new Array();
		for (let fi=0; fi<this.query.PFLIST.fields.length; fi++) {
			fnames.push(this.query.PFLIST.fields[fi].name);
		}
		this.pfnames = "\"" + fnames.join("\",\"") + "\"";		/* list of quoted names */
		this.pvnames = fnames.join(",");						/* list of unquoted names */
		this.pftnames = "::" + fnames.join("::,::") + "::";		/* list of bind variable templates */
		logger.debug(this.phead("pfnames=")+" >>> "+this.pfnames+" <<< ");
		logger.debug(this.phead("pvnames=")+" >>> "+this.pvnames+" <<< ");
		logger.debug(this.phead("pftnames=")+" >>> "+this.pftnames+" <<< ");
	}

#
# <<TSTEPNUM:2>>: make backup of "Partner" table
#
pre_qexpression
	this.backuptablename = "Partner_backup_"+aux.uniqueNowID();
sql		create table "@@backuptablename@@" as select * from "Partner"
cfgname	@@inscfgname@@
post_qexpression
	this.init_done = true;

#
# <<TSTEPNUM:3>>: create logging table
#
pre_qexpression
	this.logtablename = "CCKFOPPLOGTABLE_"+aux.uniqueNowID();
sql create table @@logtablename@@  (LOGDATA TEXT)

#
# <<TSTEPNUM:4>>: process record - read in KFOQSDB if already present
#
record_proper:
pre_qexpression
	if ((this.recnum%10)==0) {
		logger.debug(this.phead("PROGRESS.RECNUM=")+this.recnum);
	}
sql				select * from "Partner" where "VpNr"=::VPNR::
cfgname			@@inscfgname@@
named_params	VPNR
result_varname	cpres
post_qexpression
	proc: {
		this.logdiffbundesland = false;
		let rc = this.cpres.getRowCount();
		if (rc>1) {
			this.ppush([this.phead("VPNRALEADYMULTIPLE("+this.cdata.VPNR+")"),"multiple records already there for VPNR="+this.cdata.VPNR,"",{ cpres: this.cpres }]);
			this.skip = true;
			this.do_not_commit = true;
		} else if (rc>0) {
			this.duplogrecord = [this.phead("VPNRALREADYTHERE("+this.cdata.VPNR+")"),"VPNR_IN_KFOQSDB="+this.cdata.VPNR,this.cdata,this.cpres,"",""];
			this.ppush(this.duplogrecord);
			let cpr = this.cpres.getRowObject(0);
			if (cpr.PartnerBundesland!=this.cdata.LEPKFOBUNDESLAND) {
				this.difflogrecord = [this.phead("VPNRALREADYTHERE("+this.cdata.VPNR+")_DIFF_BUNDESLAND"),"KFOQSDB_BUNDESLAND="+cpr.PartnerBundesland,"NEW_BUNDESLAND="+this.cdata.LEPKFOBUNDESLAND,""]
				this.ppush(this.difflogrecord);
				this.logdiffbundesland = true;
				/* this.do_not_commit = true; */ /* conflicting bundesland is logged, but data is kept */
			}
			this.skip = true;
		} else {
			this.skip = false;
		}
	}

#
# <<TSTEPNUM:5>>: check if different bundesland shall be logged
#
if		(!this.logdiffbundesland)
goto	after_logdiffbundesland

#
# <<TSTEPNUM:5>>: log different bundesland
#
pre_qexpression
	this.difbdllogtext = aux.objTxt({ logtype: "Duplicate VPNR, Differenz Bundesland", duplog: this.duplogrecord, difflog: this.difflogrecord});
sql				insert into @@logtablename@@(LOGDATA) values(::difbdllogtext::)
named_params	difbdllogtext
post_qexpression
	this.logswritten++;

#
# <<TSTEPNUM:4>>: check if this record should be skipped
#
after_logdiffbundesland:
if		(this.skip)
goto	after_insert

#
# <<TSTEPNUM:5>>: insert one record in "Partner"
#
pre_qexpression
	this.cdata.PartnerBundesland	= this.cdata.LEPKFOBUNDESLAND;
	this.cdata.PartnerName			= this.cdata.LEPARTNERNAME;
	this.cdata.VpNr					= this.cdata.VPNR;
	this.cdata.version 				= 0;
	this.cdata.countryCode			= this.cdata.LEPARTNERSTAAT;
sql				insert into "Partner"(@@pfnames@@) values(@@pftnames@@)
cfgname			@@inscfgname@@
named_params	@@pvnames@@
post_qexpression
	this.recsinserted++;

#
# <<TSTEPNUM:6>>: was this the last record?
#
after_insert:
pre_qexpression
	this.recsprocessed++;
if			(!this.is_last_record)
goto		record_end

#
# <<TSTEPNUM:7>>: shall we rollback?
#
qexpression
	if (this.do_not_commit) {
		this.force_rollback = true;
		this.ppush([this.phead("WILLROLLBACK"),"Data inconsistencies, will roll back all changes to \"Partner\"","",""]);
	} else {
		this.force_rollback = false;
	}
	this.ppush([this.phead("PARBACKUP"),"PARTNER_BACKUP_TABLE="+this.backuptablename,"",""]);
	this.ppush([this.phead("LOGSWRITTEN"),"LOGS_WRITTEN="+this.logswritten,"TO_TABLE="+this.logtablename,""]);
	this.ppush([this.phead("FINISHED"),"RECORDS_TO_PROCESS="+this.recstoprocess,"RECORDS_PROCESSED="+this.recsprocessed,"RECORDS_CREATED="+this.recsinserted]);

#
# <<TSTEPNUM:8>>: end of record processing
#
record_end:
end
