58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package gmodel
|
|
|
|
import (
|
|
"context"
|
|
"fusenapi/utils/handler"
|
|
"reflect"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// TODO: 使用model的属性做你想做的
|
|
|
|
func (p *FsUserMaterialModel) CreateOrUpdate(ctx context.Context, req *FsUserMaterial) (resp *FsUserMaterial, err error) {
|
|
rowBuilder := p.db.Table(p.name).WithContext(ctx)
|
|
if req.Id > 0 {
|
|
err = rowBuilder.Save(req).Error
|
|
} else {
|
|
err = rowBuilder.Create(req).Error
|
|
}
|
|
return req, err
|
|
}
|
|
|
|
func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsUserMaterial, error) {
|
|
var resp []*FsUserMaterial
|
|
// 过滤
|
|
if filterMap != nil {
|
|
rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap))
|
|
}
|
|
|
|
// 排序
|
|
if orderBy != "" {
|
|
var fieldsMap = make(map[string]struct{})
|
|
s := reflect.TypeOf(&FsUserMaterial{}).Elem() //通过反射获取type定义
|
|
for i := 0; i < s.NumField(); i++ {
|
|
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
|
|
}
|
|
rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap))
|
|
}
|
|
|
|
result := rowBuilder.WithContext(ctx).Find(&resp)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
} else {
|
|
return resp, nil
|
|
}
|
|
}
|
|
|
|
func (m *FsUserMaterialModel) RowSelectBuilder(selectData []string) *gorm.DB {
|
|
var rowBuilder = m.db.Table(m.name)
|
|
|
|
if selectData != nil {
|
|
rowBuilder = rowBuilder.Select(selectData)
|
|
} else {
|
|
rowBuilder = rowBuilder.Select("*")
|
|
}
|
|
return rowBuilder
|
|
}
|