finish
This commit is contained in:
@@ -2,11 +2,13 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"nCovTrack-Backend/global"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var colNameReg, _ = regexp.Compile(".*column:(.*);?")
|
||||
@@ -124,7 +126,7 @@ func BeforeBatchSave(colMaps *[]map[string]interface{}, user int) {
|
||||
|
||||
func Upsert[T any](colMap map[string]interface{}) (ok bool, rowsAffected int64) {
|
||||
var tx *gorm.DB
|
||||
if colMap["id"] == nil || int(colMap["id"].(float64)) == 0 {
|
||||
if colMap["id"] == nil || getMapId(colMap["id"]) == 0 {
|
||||
tx = global.Db.Model(new(T)).Create(colMap)
|
||||
} else {
|
||||
tx = global.Db.Model(new(T)).Where("id = ?", colMap["id"]).Updates(colMap)
|
||||
@@ -169,7 +171,7 @@ func DeleteById[T any](id int) (ok bool, rowsAffected int64) {
|
||||
if tx.Error != nil {
|
||||
return false, 0
|
||||
}
|
||||
return true, rowsAffected
|
||||
return true, tx.RowsAffected
|
||||
}
|
||||
|
||||
func DropById[T any](id int) {
|
||||
@@ -253,3 +255,17 @@ func CountByOrm(tx *gorm.DB) int64 {
|
||||
tx.Count(&count)
|
||||
return count
|
||||
}
|
||||
|
||||
func getMapId(id interface{}) int {
|
||||
switch id.(type) {
|
||||
case int:
|
||||
return id.(int)
|
||||
case string:
|
||||
id, _ := strconv.Atoi(id.(string))
|
||||
return id
|
||||
case float64:
|
||||
return int(id.(float64))
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user