108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package coryCommon
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||
"reflect"
|
||
"strconv"
|
||
)
|
||
|
||
func New() *coryCom {
|
||
return &coryCom{}
|
||
}
|
||
|
||
type coryCom struct{}
|
||
|
||
// CreateByOrUpdateBy 专门用来反射结构体中的创建人和更新人,然后返回回去,避免重复造轮子去写重复代码
|
||
/**
|
||
*使用实例代码
|
||
* by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
|
||
* infoRes := by.(model.BusCompanyInfoRes)
|
||
* res = &infoRes
|
||
*
|
||
*其中 updateByFieldVal.Set(reflect.ValueOf(updateByValue.Interface().(*gvar.Var).String())) 必须类型一致
|
||
*/
|
||
func (s *coryCom) CreateByOrUpdateBy(ctx context.Context, data interface{}) (dataRes interface{}) {
|
||
// 使用反射获取 data 的值和类型信息
|
||
val := reflect.ValueOf(data)
|
||
typ := reflect.TypeOf(data)
|
||
|
||
// 判断 data 是否为指针类型,并获取实际值
|
||
if val.Kind() == reflect.Ptr {
|
||
val = val.Elem()
|
||
typ = typ.Elem()
|
||
}
|
||
|
||
flagCreate := true
|
||
flagUpdate := true
|
||
// 获取 createBy 字段在结构体中的索引
|
||
createByField, ok := typ.FieldByName("CreateBy")
|
||
if !ok {
|
||
flagCreate = false
|
||
//return data // 如果结构体中不存在 createBy 字段,则直接返回原始值
|
||
createByField2, ok2 := typ.FieldByName("CreatedBy")
|
||
if ok2 {
|
||
createByField = createByField2
|
||
flagCreate = true
|
||
}
|
||
}
|
||
updateByField, ok := typ.FieldByName("UpdateBy")
|
||
if !ok {
|
||
flagUpdate = false
|
||
//return data // 如果结构体中不存在 createBy 字段,则直接返回原始值
|
||
updateByField2, ok2 := typ.FieldByName("UpdatedBy")
|
||
if ok2 {
|
||
updateByField = updateByField2
|
||
flagCreate = true
|
||
}
|
||
}
|
||
if flagCreate {
|
||
// 判断 createBy 字段的类型是否为 string
|
||
if createByField.Type.Kind() != reflect.String {
|
||
// 如果 createBy 字段的类型不是 string,请根据需要进行相应的处理或返回错误
|
||
// 此处假设 createBy 必须为 string 类型,如果不是则返回错误
|
||
return errors.New("createBy字段类型不匹配")
|
||
}
|
||
// 获取原始的 createBy 字段的值并获取创建人
|
||
createId := val.FieldByIndex(createByField.Index).String()
|
||
ve := SelectByString(ctx, IsNumeric(createId), createId)
|
||
// 设置 createBy 字段的值为指定参数
|
||
createByFieldVal := val.FieldByIndex(createByField.Index)
|
||
createByFieldVal.SetString(ve)
|
||
}
|
||
if flagUpdate {
|
||
// 判断 updateBy 字段的类型是否为 string
|
||
if updateByField.Type.Kind() != reflect.String {
|
||
// 如果 createBy 字段的类型不是 string,请根据需要进行相应的处理或返回错误
|
||
// 此处假设 createBy 必须为 string 类型,如果不是则返回错误
|
||
return errors.New("updateBy字段类型不匹配")
|
||
}
|
||
// 获取原始的 createBy 字段的值并获取创建人
|
||
updateId := val.FieldByIndex(updateByField.Index).String()
|
||
ve := SelectByString(ctx, IsNumeric(updateId), updateId)
|
||
// 设置 createBy 字段的值为指定参数
|
||
updateByFieldVal := val.FieldByIndex(updateByField.Index)
|
||
updateByFieldVal.SetString(ve)
|
||
}
|
||
// 返回修改后的结构体
|
||
return val.Interface()
|
||
}
|
||
|
||
func IsNumeric(str string) bool {
|
||
_, err := strconv.ParseFloat(str, 64)
|
||
return err == nil
|
||
}
|
||
|
||
// SelectByString 根据字符串查询到具体人对应的名称
|
||
func SelectByString(ctx context.Context, flag bool, str string) (ve string) {
|
||
if flag {
|
||
value, _ := dao.SysUser.Ctx(ctx).Fields("user_nickname").Where("id", str).Value()
|
||
ve = value.String()
|
||
} else {
|
||
value, _ := dao.BusConstructionUser.Ctx(ctx).Fields("user_name").Where("openid", str).Value()
|
||
ve = value.String()
|
||
}
|
||
return
|
||
}
|