Files
jx-callback/business/model/dao/dao_utils.go
gazebo 4bfb7c235a - rf.
2018-09-14 11:21:45 +08:00

95 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package dao
import (
"reflect"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/model"
)
func NormalFilterMapByStructObject(mapData map[string]interface{}, obj interface{}) (valid map[string]interface{}, invalid map[string]interface{}) {
// 这里必须用首字母小写因为是用于访问map是用于访问map是需要完全匹配的
return jxutils.FilterMapByStructObject(mapData, obj, []string{"id", "createdAt", "updatedAt", "finishedAt", "deletedAt", "syncStatus", "lastOperator"})
}
func NormalMakeMapByStructObject(mapData map[string]interface{}, obj interface{}, userName string) (retVal map[string]interface{}) {
retVal, _ = NormalFilterMapByStructObject(mapData, obj)
if len(retVal) > 0 {
WrapUpdateULEntity(retVal, userName)
}
return retVal
}
func NormalMakeMapByFieldList(mapData map[string]interface{}, fields []string, userName string) (retVal map[string]interface{}) {
retVal, _ = jxutils.FilterMapByFieldList(mapData, fields)
if len(retVal) > 0 {
WrapUpdateULEntity(retVal, userName)
}
return retVal
}
func checkAndGetStructValue(item interface{}) *reflect.Value {
value := reflect.ValueOf(item)
if value.Kind() == reflect.Ptr {
value = value.Elem()
} else {
panic("item ust be ptr type")
}
return &value
}
func WrapAddIDCULEntity(item interface{}, lastOperator string) interface{} {
now := time.Now()
if mapData, ok := item.(map[string]interface{}); ok {
mapData[model.FieldID] = 0
mapData[model.FieldCreatedAt] = now
mapData[model.FieldUpdatedAt] = now
mapData[model.FieldLastOperator] = lastOperator
} else {
value := checkAndGetStructValue(item)
nowValue := reflect.ValueOf(now)
value.FieldByName(model.FieldID).SetInt(0)
value.FieldByName(model.FieldCreatedAt).Set(nowValue)
value.FieldByName(model.FieldUpdatedAt).Set(nowValue)
value.FieldByName(model.FieldLastOperator).SetString(lastOperator)
}
return item
}
func WrapAddIDCULDEntity(item interface{}, lastOperator string) interface{} {
if mapData, ok := item.(map[string]interface{}); ok {
mapData[model.FieldDeletedAt] = utils.DefaultTimeValue
} else {
value := checkAndGetStructValue(item)
value.FieldByName(model.FieldDeletedAt).Set(reflect.ValueOf(utils.DefaultTimeValue))
}
return WrapAddIDCULEntity(item, lastOperator)
}
func WrapUpdateULEntity(item interface{}, lastOperator string) interface{} {
now := time.Now()
if mapData, ok := item.(map[string]interface{}); ok {
mapData[model.FieldUpdatedAt] = now
mapData[model.FieldLastOperator] = lastOperator
} else {
value := checkAndGetStructValue(item)
nowValue := reflect.ValueOf(now)
value.FieldByName(model.FieldUpdatedAt).Set(nowValue)
value.FieldByName(model.FieldLastOperator).SetString(lastOperator)
}
return item
}
func GenQuestionMarks(count int) string {
marks := ""
if count > 0 {
marks = "?"
for i := 1; i < count; i++ {
marks += ", ?"
}
}
return marks
}