feat: management && notify

This commit is contained in:
fallen-angle
2022-05-04 20:06:21 +08:00
parent e58bafd0d3
commit 7598280fc1
28 changed files with 1226 additions and 143 deletions

View File

@@ -60,17 +60,60 @@ func MapJ2c[T any](jsonMap map[string]interface{}, ignoreNil bool) (colMap map[s
return colMap
}
func MapsJ2c[T any](jsonMaps []map[string]interface{}, ignoreNil bool) (colMaps []map[string]interface{}) {
for _, jsonMap := range jsonMaps {
colMap := MapJ2c[T](jsonMap, ignoreNil)
colMaps = append(colMaps, colMap)
}
return colMaps
}
func MapC2j[T any](colMap map[string]interface{}, ignoreNil bool) (jsonMap map[string]interface{}) {
tName := reflect.TypeOf(new(T)).Elem().Name()
tC2jMap := c2jMap[tName]
if tC2jMap == nil {
panic(tName + " is not init registered int j2cMap")
}
jsonMap = make(map[string]interface{})
for k, v := range colMap {
//TODO 无法转换
if jsonKey := tC2jMap[k]; jsonKey != "" && (!ignoreNil || v != nil) {
jsonMap[jsonKey] = v
}
}
return jsonMap
}
func MapsC2j[T any](jsonMaps []map[string]interface{}, ignoreNil bool) (colMaps []map[string]interface{}) {
for _, jsonMap := range jsonMaps {
colMap := MapC2j[T](jsonMap, ignoreNil)
colMaps = append(colMaps, colMap)
}
return colMaps
}
// BeforeSave need to set some field while insert or update
func BeforeSave(colMap map[string]interface{}, user int) {
if colMap["id"] == nil {
if colMap["id"] == nil || int(colMap["id"].(float64)) == 0 {
colMap["create_time"] = time.Now()
if user != -1 {
colMap["create_user"] = user
} else {
colMap["create_user"] = 0
}
}
colMap["modify_time"] = time.Now()
if user != -1 {
colMap["modify_user"] = user
} else {
colMap["modify_user"] = 0
}
}
func BeforeBatchSave(colMaps *[]map[string]interface{}, user int) {
for _, colMap := range *colMaps {
BeforeSave(colMap, user)
}
}
@@ -81,7 +124,7 @@ func BeforeSave(colMap 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 {
if colMap["id"] == nil || int(colMap["id"].(float64)) == 0 {
tx = global.Db.Model(new(T)).Create(colMap)
} else {
tx = global.Db.Model(new(T)).Where("id = ?", colMap["id"]).Updates(colMap)
@@ -93,6 +136,15 @@ func Upsert[T any](colMap map[string]interface{}) (ok bool, rowsAffected int64)
return true, tx.RowsAffected
}
func BatchInsert[T any](colMaps []map[string]interface{}) (ok bool, rowsAffected int64) {
tx := global.Db.Model(new(T)).Create(&colMaps)
if tx.Error != nil {
fmt.Println(tx.Error)
return false, 0
}
return true, tx.RowsAffected
}
func Update[T any](queryMap []map[string]interface{}, updateMap map[string]interface{}) (ok bool, rowsAffected int64) {
tx := global.Db.Model(new(T))
for _, e := range queryMap {
@@ -120,6 +172,10 @@ func DeleteById[T any](id int) (ok bool, rowsAffected int64) {
return true, rowsAffected
}
func DropById[T any](id int) {
global.Db.Model(new(T)).Delete("id = ?", id)
}
func List[T any](queryMap []map[string]interface{}) *[]map[string]interface{} {
tx := global.Db.Model(new(T))
for _, e := range queryMap {