public Sub FeldolgozTermekJSON(jsonText As String) As String
Private res As String = ""
Private cikkcsop As String = "cikkcsop_temp"
Private cikkt As String = "cikkt_temp"
Try
' 1. Parse JSON
Dim parser As JSONParser
parser.Initialize(jsonText)
Dim root As Map = parser.NextObject
Dim updatetype As String = root.GetDefault("updatetype", "replace") ' alapértelmezés: nem fűz
Dim data As List = root.Get("data")
If updatetype = "replace" Then
' 2. Létrehoz ideiglenes táblákat
CreateTempTables
Else
'update esetén az eredeti táblákban dolgozom
cikkcsop = "cikkcsop"
cikkt = "cikkt"
End If
' 3. Végigmegy az itemgroupokon
Private csopazon As Int = 1
Private cikkid As Int = 1
For Each entry As Map In data
Dim itemgroups As List = entry.Get("itemgroup")
For Each group As Map In itemgroups
Dim groupname As String = group.Get("name")
If updatetype = "update" Then
'meg kell keresnem a groupname-t a cikkcsopban
Private csazon As Object = SQL1.ExecQuerySingleResult2($"
SELECT azon
FROM cikkcsop
WHERE lower(megnevezes) = ?"$,Array As String(groupname.ToLowerCase))
If IsNumber(csazon) Then
csopazon = csazon
Log($"Létező cikkcsoport: ${groupname}, azon:${csopazon}"$)
Else
'nincs ilyen cikkcsoport
csopazon = SQL1.ExecQuerySingleResult($"
select max(coalesce(cast(azon as integer),0))+1
from cikkcsop
"$)
Log($"Új cikkcsoport: ${groupname}, azon:${csopazon}"$)
' Beszúrás a cikkcsop-ba
SQL1.ExecNonQuery2($"INSERT INTO ${cikkcsop} (azon, megnevezes) VALUES (?, ?)"$, Array As String(csopazon, groupname))
End If
Else
' Beszúrás a cikkcsop_temp-be
SQL1.ExecNonQuery2($"INSERT INTO ${cikkcsop} (azon, megnevezes) VALUES (?, ?)"$, Array As String(csopazon, groupname))
End If
' Items
Dim items As List = group.Get("items")
For Each item As Map In items
Private ujme As String = mertekegysegvalt(item.GetDefault("unit","OWN"),False)
Private ujmentak As String = mertekegysegvalt(item.GetDefault("NTAKunit","DARAB"),True)
If Codebase.afakodok.GetDefault(item.Get("VAT"),"0") = "0" Then
DobKivetelt("Unknown VAT code:" & item.GetDefault("VAT","???"))
End If
Private insMap As Map
insMap.Initialize
Private whereMap As Map
whereMap.Initialize
If updatetype = "update" Then
'meg kell keresnem a cikkszámot a cikktben
Private cazon As Object = SQL1.ExecQuerySingleResult2($"
SELECT azon
FROM cikkt
WHERE cikkszam = ?"$,Array As String(item.Get("id")))
If IsNumber(cazon) Then
'van már ilyen cikkszám, update lesz
whereMap.Put("cikkszam",item.Get("id"))
Else
'nincs ilyen cikksszám, insert lesz
cikkid = SQL1.ExecQuerySingleResult($"
select max(coalesce(cast(azon as integer),0))+1
from cikkt
"$)
End If
Else
'teljes terméktörzscsere, insert lesz
End If
insMap.Put("csopazon",csopazon)
insMap.Put("cikkszam",item.Get("id"))
insMap.Put("megnevezes",item.Get("name"))
insMap.Put("vtszkodtip",item.Get("navcode").As(Map).Get("type"))
insMap.Put("vtsz",item.Get("navcode").As(Map).Get("code"))
insMap.Put("me",ujme)
insMap.Put("bruttoar",item.Get("pricebrutto"))
insMap.Put("afakod",item.Get("VAT"))
insMap.Put("vonalkod",item.Get("barcode"))
insMap.Put("azon",cikkid)
insMap.Put("fokatjson",item.Get("NTAKmaingroup"))
insMap.Put("alkatjson",item.Get("NTAKsubgroup"))
insMap.Put("ntakme",ujmentak)
insMap.Put("ntakszorzo",item.Get("NTAKmultiplier"))
If whereMap.Size = 0 Then
Private l As List
l.Initialize
l.Add(insMap)
DBUtils.InsertMaps(SQL1,cikkt,L)
Else
DBUtils.UpdateRecord2(SQL1,cikkt,insMap,whereMap)
End If
cikkid = cikkid + 1
Next
csopazon = csopazon + 1
Next
Next
If updatetype = "replace" Then
' 4. Ha ideiglenes táblák feltöltése sikeres, véglegesítés
ReplaceOriginalTables
End If
Log("Sikeres terméktörzs betöltés és csere!")
Catch
If feldhiba.Length > 0 Then
Else
feldhiba = LastException.Message
End If
Log("Hiba a terméktörzs JSON feldolgozás során: " & feldhiba)
res = "Error in processing items JSON: " & feldhiba
End Try
Return res
End Sub
Sub CreateTempTables
'SQL1.BeginTransaction
SQL1.ExecNonQuery("DROP TABLE IF EXISTS cikkcsop_temp")
SQL1.ExecNonQuery("DROP TABLE IF EXISTS cikkt_temp")
MasolatTablaLetrehozasa("cikkcsop")
MasolatTablaLetrehozasa("cikkt")
End Sub
Sub ReplaceOriginalTables
'SQL1.BeginTransaction
Try
SQL1.ExecNonQuery("DELETE FROM cikkcsop")
SQL1.ExecNonQuery("DELETE FROM cikkt")
Log($"Insert előtt:"$ )
Log($"Cikkcsop rekordszám: ${SQL1.ExecQuerySingleResult("SELECT count(*) FROM cikkcsop")}"$ )
Log($"Cikkt rekordszám: ${SQL1.ExecQuerySingleResult("SELECT count(*) FROM cikkt")}"$ )
SQL1.ExecNonQuery("INSERT INTO cikkcsop SELECT * FROM cikkcsop_temp")
SQL1.ExecNonQuery("INSERT INTO cikkt SELECT * FROM cikkt_temp")
SQL1.ExecNonQuery("DROP TABLE cikkcsop_temp")
SQL1.ExecNonQuery("DROP TABLE cikkt_temp")
Log($"Insert után:"$ )
Log($"Cikkcsop rekordszám: ${SQL1.ExecQuerySingleResult("SELECT count(*) FROM cikkcsop")}"$ )
Log($"Cikkt rekordszám: ${SQL1.ExecQuerySingleResult("SELECT count(*) FROM cikkt")}"$ )
'SQL1.TransactionSuccessful
Catch
Log("Hiba véglegesítés közben: " & LastException)
End Try
'SQL1.EndTransaction
End Sub