package gmodel import ( "context" "fusenapi/utils/handlers" "reflect" "gorm.io/gorm" ) // TODO: 使用model的属性做你想做的 func (m *FsMerchantCategoryModel) FindOne(ctx context.Context, id int64) (resp *FsMerchantCategory, err error) { err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("id = ? and status = ?", id, 1).Take(&resp).Error return resp, err } // 获取默认那个 func (m *FsMerchantCategoryModel) FindDefualtOne(ctx context.Context) (resp *FsMerchantCategory, err error) { err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("status = ? and is_default = ?", 1, 1).Take(&resp).Error return resp, err } func (m *FsMerchantCategoryModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB { if selectData != nil { return m.db.WithContext(ctx).Select(selectData) } else { return m.db.WithContext(ctx).Select("*") } } func (m *FsMerchantCategoryModel) FindAll(gormDB *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsMerchantCategory, error) { var resp []*FsMerchantCategory // 过滤 if filterMap != nil { gormDB = gormDB.Scopes(handlers.FilterData(filterMap)) } // 排序 if orderBy != "" { var fieldsMap = make(map[string]struct{}) s := reflect.TypeOf(&FsOrder{}).Elem() //通过反射获取type定义 for i := 0; i < s.NumField(); i++ { fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} } gormDB = gormDB.Scopes(handlers.OrderCheck(orderBy, fieldsMap)) } result := gormDB.Find(&resp) if result.Error != nil { return nil, result.Error } else { return resp, nil } }