|
@@ -320,8 +320,9 @@ class FinalizedDataService(
|
|
|
if (temporaryDataByCode.any { a -> a.key == keyCode }) {
|
|
if (temporaryDataByCode.any { a -> a.key == keyCode }) {
|
|
|
temporaryDataByCode[keyCode]
|
|
temporaryDataByCode[keyCode]
|
|
|
} else {
|
|
} else {
|
|
|
- var dataUid = try {
|
|
|
|
|
- val fd = if (table == "trunk") " AND (subscribed_no != 'new trunk' OR subscribed_no IS NULL) " else " "
|
|
|
|
|
|
|
+ var dataUid = try {
|
|
|
|
|
+ val fd =
|
|
|
|
|
+ if (table == "trunk") " AND (subscribed_no != 'new trunk' OR subscribed_no IS NULL) " else " "
|
|
|
apiService.transaction { em ->
|
|
apiService.transaction { em ->
|
|
|
val result = em.createNativeQuery("SELECT $select FROM $tableName WHERE $field = :$field $fd")
|
|
val result = em.createNativeQuery("SELECT $select FROM $tableName WHERE $field = :$field $fd")
|
|
|
.setParameter(field, value.toString())
|
|
.setParameter(field, value.toString())
|
|
@@ -334,11 +335,13 @@ class FinalizedDataService(
|
|
|
null
|
|
null
|
|
|
}
|
|
}
|
|
|
if (tableName == "corcos" && dataUid == null && migrationSettingService.autoCreateCorcos) {
|
|
if (tableName == "corcos" && dataUid == null && migrationSettingService.autoCreateCorcos) {
|
|
|
- dataUid = insertDataService.insertDataWithNativeQuery(tableName, "command", mutableMapOf(
|
|
|
|
|
- "command" to value,
|
|
|
|
|
- "name" to value,
|
|
|
|
|
- "description" to "auto create corcos"
|
|
|
|
|
- ))
|
|
|
|
|
|
|
+ dataUid = insertDataService.insertDataWithNativeQuery(
|
|
|
|
|
+ tableName, "command", mutableMapOf(
|
|
|
|
|
+ "command" to value,
|
|
|
|
|
+ "name" to value,
|
|
|
|
|
+ "description" to "auto create corcos"
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
temporaryDataByCode[keyCode] = dataUid
|
|
temporaryDataByCode[keyCode] = dataUid
|
|
|
dataUid
|
|
dataUid
|
|
@@ -527,7 +530,8 @@ class FinalizedDataService(
|
|
|
finalMapEntity,
|
|
finalMapEntity,
|
|
|
functionAfter = { uid, isUpdate ->
|
|
functionAfter = { uid, isUpdate ->
|
|
|
// create budget
|
|
// create budget
|
|
|
- if (BudgetUserType.entries.any { a -> a.name == table.uppercase() }) {
|
|
|
|
|
|
|
+// if (BudgetUserType.entries.any { a -> a.name == table.uppercase() }) {
|
|
|
|
|
+ if (table == "phone_user") {
|
|
|
insertDataService.insertDataWithNativeQuery(
|
|
insertDataService.insertDataWithNativeQuery(
|
|
|
"budget", uniqueTable = null, mutableMapOf(
|
|
"budget", uniqueTable = null, mutableMapOf(
|
|
|
"userType" to BudgetUserType.valueOf(table.uppercase()).ordinal,
|
|
"userType" to BudgetUserType.valueOf(table.uppercase()).ordinal,
|
|
@@ -569,19 +573,21 @@ class FinalizedDataService(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private fun pattern(format: String) = DateTimeFormatter.ofPattern(format)
|
|
|
|
|
+
|
|
|
private fun buildRawData(data: MutableMap<String, Any?>): List<Any?> {
|
|
private fun buildRawData(data: MutableMap<String, Any?>): List<Any?> {
|
|
|
val seg1 = "CDR"
|
|
val seg1 = "CDR"
|
|
|
val seg2 = data["pbx_id"]?.let { findCodeByUid("pbx", it) ?: "" } ?: ""
|
|
val seg2 = data["pbx_id"]?.let { findCodeByUid("pbx", it) ?: "" } ?: ""
|
|
|
val seg3 = data["direction"]?.let { Direction.entries[it.toString().toInt()].code } ?: ""
|
|
val seg3 = data["direction"]?.let { Direction.entries[it.toString().toInt()].code } ?: ""
|
|
|
|
|
|
|
|
val startOfCall = data["startOfCall"]?.toString()?.let {
|
|
val startOfCall = data["startOfCall"]?.toString()?.let {
|
|
|
- LocalDateTime.parse(it, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"))
|
|
|
|
|
|
|
+ LocalDateTime.parse(it, pattern(migrationSettingService.patternDatetime))
|
|
|
}
|
|
}
|
|
|
val endOfCall = startOfCall?.plusSeconds(data["duration"]?.toString()?.toLong() ?: 0)
|
|
val endOfCall = startOfCall?.plusSeconds(data["duration"]?.toString()?.toLong() ?: 0)
|
|
|
|
|
|
|
|
- val seg4 = startOfCall?.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) ?: ""
|
|
|
|
|
- val seg5 = startOfCall?.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) ?: ""
|
|
|
|
|
- val seg6 = endOfCall?.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) ?: ""
|
|
|
|
|
|
|
+ val seg4 = startOfCall?.format(pattern(migrationSettingService.rawDataDatetime)) ?: ""
|
|
|
|
|
+ val seg5 = startOfCall?.format(pattern(migrationSettingService.rawDataDatetime)) ?: ""
|
|
|
|
|
+ val seg6 = endOfCall?.format(pattern(migrationSettingService.rawDataDatetime)) ?: ""
|
|
|
val seg7 = ""
|
|
val seg7 = ""
|
|
|
val seg8 = data["duration"]
|
|
val seg8 = data["duration"]
|
|
|
val seg9 = data["accessNumber"] ?: ""
|
|
val seg9 = data["accessNumber"] ?: ""
|
|
@@ -634,6 +640,7 @@ class FinalizedDataService(
|
|
|
val buildRawData = buildRawData(data)
|
|
val buildRawData = buildRawData(data)
|
|
|
val rawData = buildRawData.joinToString(",")
|
|
val rawData = buildRawData.joinToString(",")
|
|
|
|
|
|
|
|
|
|
+ data["endOfCall"] = buildRawData[5]
|
|
|
data["rawData"] = rawData
|
|
data["rawData"] = rawData
|
|
|
getCallTo(buildRawData[12].toString())?.forEach { (t, u) -> data[t] = u }
|
|
getCallTo(buildRawData[12].toString())?.forEach { (t, u) -> data[t] = u }
|
|
|
//
|
|
//
|
|
@@ -789,8 +796,29 @@ class InsertDataService(
|
|
|
fields.add(t)
|
|
fields.add(t)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- val fieldKey = fieldUnique?.split(";")?.mapNotNull { m -> mapData[m]?.toString() }?.joinToString(";")
|
|
|
|
|
- val uidFromDb = temporaryDataByCode["$table;uid;$fieldKey"]
|
|
|
|
|
|
|
+ val uidFromDb = if (table == "calltransaction") null else {
|
|
|
|
|
+ fieldUnique?.split(";")?.let { fieldKey ->
|
|
|
|
|
+ val keyName = fieldKey.mapNotNull { m -> finalMap[m.replace("_uid", "_id")] }
|
|
|
|
|
+ .joinToString(";")
|
|
|
|
|
+ temporaryDataByCode["$table;uid;$keyName"]
|
|
|
|
|
+// val uidUpdate = temporaryDataByCode["$table;uid;$keyName"]
|
|
|
|
|
+// if (table == "trunk" && uidUpdate == null) {
|
|
|
|
|
+//// println("find trunk by name")
|
|
|
|
|
+// val keyName = fieldKey.mapNotNull { m ->
|
|
|
|
|
+// finalMap[m.replace("code", "name").replace("_uid", "_id")]?.let {
|
|
|
|
|
+// if (it.contains("Auto ")) null else it
|
|
|
|
|
+// }
|
|
|
|
|
+// }.joinToString(";")
|
|
|
|
|
+//// println("keyName $table;uid;$keyName")
|
|
|
|
|
+// temporaryDataByCode["$table;uid;$keyName"]
|
|
|
|
|
+// } else uidUpdate
|
|
|
|
|
+ }
|
|
|
|
|
+// val fieldKey = fieldUnique?.split(";")?.mapNotNull { m ->
|
|
|
|
|
+// finalMap[m.replace("_uid", "_id")]
|
|
|
|
|
+// }?.joinToString(";")
|
|
|
|
|
+// println("update key: $table;uid;$fieldKey")
|
|
|
|
|
+// temporaryDataByCode["$table;uid;$fieldKey"]
|
|
|
|
|
+ }
|
|
|
val uid = uidFromDb ?: ULID.random()
|
|
val uid = uidFromDb ?: ULID.random()
|
|
|
val isUpdate = uidFromDb != null
|
|
val isUpdate = uidFromDb != null
|
|
|
|
|
|
|
@@ -808,10 +836,34 @@ class InsertDataService(
|
|
|
if (isUpdate) {
|
|
if (isUpdate) {
|
|
|
fields.remove("uid")
|
|
fields.remove("uid")
|
|
|
}
|
|
}
|
|
|
- val query =
|
|
|
|
|
- if (table == "calltransaction") "INSERT INTO $table (${fields.joinToString() { it.camelToSnake() }}) " +
|
|
|
|
|
- "VALUES (${fields.joinToString() { ":$it" }})"
|
|
|
|
|
- else {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // prepare final data
|
|
|
|
|
+// println("map: ${Util.mapper.writeValueAsString(finalMap)}")
|
|
|
|
|
+// if (table == "trunk") {
|
|
|
|
|
+// val code = finalMap["code"]!!
|
|
|
|
|
+// val name = finalMap["name"]!!
|
|
|
|
|
+// if (name.startsWith("Auto ")) {
|
|
|
|
|
+// finalMap["name"] = code
|
|
|
|
|
+// } else {
|
|
|
|
|
+// finalMap["code"] = name
|
|
|
|
|
+// }
|
|
|
|
|
+//// println("final map: ${Util.mapper.writeValueAsString(finalMap)}")
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+// println("uidFromDb: $uidFromDb")
|
|
|
|
|
+
|
|
|
|
|
+ val query = when {
|
|
|
|
|
+ table == "calltransaction" -> {
|
|
|
|
|
+ "INSERT INTO $table (${fields.joinToString() { it.camelToSnake() }}) " +
|
|
|
|
|
+ "VALUES (${fields.joinToString() { ":$it" }})"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ uidFromDb != null -> {
|
|
|
|
|
+ "UPDATE $table SET ${fields.joinToString(",\n") { "${it.camelToSnake()} = :${it}" }} " +
|
|
|
|
|
+ "WHERE uid = :uid "
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ else -> {
|
|
|
fieldUnique?.let { fu ->
|
|
fieldUnique?.let { fu ->
|
|
|
val uniqueOn = fu.split(";").joinToString(" AND ") { m -> "t.$m = s.$m" }
|
|
val uniqueOn = fu.split(";").joinToString(" AND ") { m -> "t.$m = s.$m" }
|
|
|
"""
|
|
"""
|
|
@@ -829,21 +881,52 @@ class InsertDataService(
|
|
|
} ?: ("INSERT INTO $table (${fields.joinToString() { it.camelToSnake() }}) " +
|
|
} ?: ("INSERT INTO $table (${fields.joinToString() { it.camelToSnake() }}) " +
|
|
|
"VALUES (${fields.joinToString() { ":$it" }})")
|
|
"VALUES (${fields.joinToString() { ":$it" }})")
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// val query =
|
|
|
|
|
+// if (table == "calltransaction") "INSERT INTO $table (${fields.joinToString() { it.camelToSnake() }}) " +
|
|
|
|
|
+// "VALUES (${fields.joinToString() { ":$it" }})"
|
|
|
|
|
+// else {
|
|
|
|
|
+// fieldUnique?.let { fu ->
|
|
|
|
|
+// val uniqueOn = fu.split(";").joinToString(" AND ") { m -> "t.$m = s.$m" }
|
|
|
|
|
+// """
|
|
|
|
|
+// MERGE INTO $table WITH (HOLDLOCK) AS t
|
|
|
|
|
+// USING (VALUES (${fields.joinToString() { ":$it" }}))
|
|
|
|
|
+// AS s(${fields.joinToString() { it.camelToSnake() }})
|
|
|
|
|
+// ON $uniqueOn
|
|
|
|
|
+// WHEN MATCHED THEN
|
|
|
|
|
+// UPDATE SET
|
|
|
|
|
+// ${fields.joinToString(",\n") { "t.${it.camelToSnake()} = s.${it.camelToSnake()}" }}
|
|
|
|
|
+// WHEN NOT MATCHED THEN
|
|
|
|
|
+// INSERT (${fields.joinToString { it.camelToSnake() }})
|
|
|
|
|
+// VALUES (${fields.joinToString { "s.${it.camelToSnake()}" }});
|
|
|
|
|
+//""".trimIndent()
|
|
|
|
|
+// } ?: ("INSERT INTO $table (${fields.joinToString() { it.camelToSnake() }}) " +
|
|
|
|
|
+// "VALUES (${fields.joinToString() { ":$it" }})")
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
|
|
|
|
|
val sqlNative = apiService.em.createNativeQuery(query)
|
|
val sqlNative = apiService.em.createNativeQuery(query)
|
|
|
|
|
|
|
|
- if (table != "calltransaction" && !isUpdate) {
|
|
|
|
|
|
|
+ val useUidParam = if (isUpdate) true else table != "calltransaction"
|
|
|
|
|
+ if (useUidParam) {
|
|
|
sqlNative.setParameter("uid", uid)
|
|
sqlNative.setParameter("uid", uid)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
structure?.let { sqlNative.setParameter("structure", structure) }
|
|
structure?.let { sqlNative.setParameter("structure", structure) }
|
|
|
finalMap.forEach { (t, u) -> sqlNative.setParameter(t.replace("_id", "_uid"), u) }
|
|
finalMap.forEach { (t, u) -> sqlNative.setParameter(t.replace("_id", "_uid"), u) }
|
|
|
- sqlNative.executeUpdate()
|
|
|
|
|
-
|
|
|
|
|
- functionAfter?.invoke(uid, isUpdate)
|
|
|
|
|
|
|
|
|
|
- return uid
|
|
|
|
|
|
|
+ return try {
|
|
|
|
|
+ sqlNative.executeUpdate()
|
|
|
|
|
+ functionAfter?.invoke(uid, isUpdate)
|
|
|
|
|
+ uid
|
|
|
|
|
+ } catch (e: Exception) {
|
|
|
|
|
+// println("failed insert data")
|
|
|
|
|
+// println("data: ${Util.mapper.writeValueAsString(finalMap)}")
|
|
|
|
|
+ null
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// return uid
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fun createTableCallTransactionTemporary() {
|
|
fun createTableCallTransactionTemporary() {
|