fusenapi/model/gmodel/fs_user_material_logic.go

149 lines
4.4 KiB
Go
Raw Normal View History

2023-08-02 10:14:53 +00:00
package gmodel
2023-08-03 06:38:17 +00:00
import (
"context"
2023-09-04 10:54:03 +00:00
"fmt"
2023-08-03 09:37:25 +00:00
"fusenapi/utils/handlers"
2023-08-03 06:38:17 +00:00
"reflect"
"gorm.io/gorm"
)
2023-08-02 10:14:53 +00:00
// TODO: 使用model的属性做你想做的
2023-09-07 08:25:29 +00:00
type RelaFsUserMaterial struct {
FsUserMaterial
// 关联字段填充
ResourceInfo *RelaFsResource `json:"resource_info" gorm:"foreignkey:resource_id;references:resource_id"`
2023-09-07 08:54:49 +00:00
MetaDataMap map[string]interface{} `json:"metadata_map" gorm:"-"`
2023-09-07 08:25:29 +00:00
}
2023-08-02 10:14:53 +00:00
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
}
2023-08-03 06:38:17 +00:00
func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsUserMaterial, error) {
var resp []*FsUserMaterial
// 过滤
if filterMap != nil {
2023-08-03 09:37:25 +00:00
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
2023-09-07 08:25:29 +00:00
}
// 排序
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(handlers.OrderCheck(orderBy, fieldsMap))
}
result := rowBuilder.WithContext(ctx).Find(&resp)
if result.Error != nil {
return nil, result.Error
} else {
return resp, nil
}
}
2023-09-08 03:47:53 +00:00
func (m *FsUserMaterialModel) FindOne(ctx context.Context, rowBuilder *gorm.DB) (*FsUserMaterial, error) {
var resp *FsUserMaterial
result := rowBuilder.WithContext(ctx).First(&resp)
if result.Error != nil {
return nil, result.Error
} else {
return resp, nil
}
}
2023-09-07 08:25:29 +00:00
2023-09-08 09:36:11 +00:00
func (m *FsUserMaterialModel) FindOneData(ctx context.Context, rowBuilder *gorm.DB) (*RelaFsUserMaterial, error) {
var resp *RelaFsUserMaterial
result := rowBuilder.WithContext(ctx).First(&resp)
if result.Error != nil {
return nil, result.Error
} else {
return resp, nil
}
}
2023-09-07 08:25:29 +00:00
func (m *FsUserMaterialModel) FindList(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*RelaFsUserMaterial, error) {
var resp []*RelaFsUserMaterial
// 过滤
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
2023-08-03 06:38:17 +00:00
}
// 排序
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{}{}
}
2023-08-03 09:37:25 +00:00
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
2023-08-03 06:38:17 +00:00
}
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
}
2023-08-07 09:19:55 +00:00
// 获取最新记录
func (m *FsUserMaterialModel) FindLatestOne(ctx context.Context, userId int64, guestId int64) (resp FsUserMaterial, err error) {
2023-09-04 10:54:03 +00:00
var cond string
if userId != 0 {
cond = fmt.Sprintf("user_id = %d", userId)
2023-09-05 02:53:18 +00:00
} else if guestId != 0 {
2023-09-04 10:54:03 +00:00
cond = fmt.Sprintf("guest_id = %d", guestId)
2023-09-05 02:53:18 +00:00
} else {
cond = fmt.Sprintf("user_id = %d and guest_id = %d", userId, guestId)
2023-09-04 10:54:03 +00:00
}
2023-08-14 09:31:57 +00:00
db := m.db.WithContext(ctx).Model(&FsUserMaterial{}).
2023-09-04 10:54:03 +00:00
Where(cond).
2023-08-14 09:31:57 +00:00
Order("id DESC")
2023-08-07 09:19:55 +00:00
err = db.Take(&resp).Error
return resp, err
}
2023-08-10 04:30:53 +00:00
func (m *FsUserMaterialModel) FindOneById(ctx context.Context, id int64) (resp *FsUserMaterial, err error) {
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).Where("id = ?", id).Take(&resp).Error
return resp, err
}
2023-08-14 09:31:57 +00:00
func (m *FsUserMaterialModel) GetListByUser(ctx context.Context, userId, guestId int64, limit int) (resp *FsUserMaterial, err error) {
2023-09-04 10:54:03 +00:00
var cond string
if userId != 0 {
cond = fmt.Sprintf("user_id = %d", userId)
2023-09-05 02:53:18 +00:00
} else if guestId != 0 {
2023-09-04 10:54:03 +00:00
cond = fmt.Sprintf("guest_id = %d", guestId)
2023-09-05 02:53:18 +00:00
} else {
cond = fmt.Sprintf("user_id = %d and guest_id = %d", userId, guestId)
2023-09-04 10:54:03 +00:00
}
2023-08-14 09:31:57 +00:00
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).
2023-09-04 10:54:03 +00:00
Where(cond).Order("id DESC").Limit(limit).Find(&resp).Error
2023-08-14 09:31:57 +00:00
return resp, err
}
2023-09-20 10:06:21 +00:00
func (m *FsUserMaterialModel) FindOneByLogoResourceId(ctx context.Context, logoResourceId string) (resp *FsUserMaterial, err error) {
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).Where("`resource_id` = ?", logoResourceId).Take(&resp).Error
2023-09-20 08:46:30 +00:00
return resp, err
}