busniess-user-center/internal/repo/application.go

109 lines
3.7 KiB
Go

// Code generated by sql2gorm. DO NOT EDIT.
package repo
import (
"time"
"context"
"github.com/samber/do"
"gorm.io/gorm"
"fmt"
)
func init(){
do.Provide[ApplicationRepo](nil, NewApplicationRepoS)
}
// 应用表
type Application struct {
ID uint `gorm:"column:id;primary_key;AUTO_INCREMENT"` // id
Code string `gorm:"column:code;NOT NULL"` // code
Secret string `gorm:"column:secret;NOT NULL"` // secret
Name string `gorm:"column:name;NOT NULL"` // name
Description string `gorm:"column:description"` // 描述
CreatedBy string `gorm:"column:created_by"` // 创建人
CreatedOn time.Time `gorm:"column:created_on;default:CURRENT_TIMESTAMP;NOT NULL"` // 记录创建时间
ModifiedBy string `gorm:"column:modified_by"` // 修改人
ModifiedOn time.Time `gorm:"column:modified_on;default:CURRENT_TIMESTAMP"` // 记录修改时间
}
func (m *Application) TableName() string {
return "application"
}
type ApplicationRepo interface {
GetAppByCode(ctx context.Context, code string) (app Application, err error)
GetAppById(ctx context.Context, id uint) (app Application, err error)
SaveApplication(ctx context.Context, app Application) error
CreateApplication(ctx context.Context, app Application) error
Search(ctx context.Context, query Query) ([]Application, error)
ResetSecret(ctx context.Context, app Application) error
DelApp(ctx context.Context,id uint)error
SearchCount(ctx context.Context, query Query) (int64, error)
}
type applicationRepoS struct {
db *gorm.DB
}
func NewApplicationRepoS(i *do.Injector) (ApplicationRepo, error) {
return &applicationRepoS{
db: do.MustInvoke[*gorm.DB](i),
}, nil
}
func (u *applicationRepoS) GetAppByCode(ctx context.Context, code string) (application Application, err error) {
err = u.db.Where("code = ?", code).Take(&application).Error
return
}
func (u *applicationRepoS)GetAppById(ctx context.Context, id uint) (app Application, err error) {
err = u.db.Where("id = ?", id).Take(&app).Error
return
}
func (u *applicationRepoS) SaveApplication(ctx context.Context, application Application) error {
if application.ID <= 0 {
return fmt.Errorf("错误应用id:%d", application.ID)
}
return u.db.Save(application).Error
}
func (u *applicationRepoS) CreateApplication(ctx context.Context, application Application) error {
return u.db.Create(&application).Error
}
func (u *applicationRepoS) Search(ctx context.Context, query Query) ([]Application, error) {
applications := make([]Application, 0)
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
db := u.db.Model(&Application{})
if query.Keyword != "" {
db = db.Where("code like ? or name like ?", keyword, keyword)
}
err := db.Order(query.Sort).Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&applications).Error
return applications, err
}
func (u *applicationRepoS) ResetSecret(ctx context.Context, app Application) error {
err := u.db.Model(&Application{}).Where("code = ?", app.Code).Update("secret",app.Secret).Error
return err
}
func (u *applicationRepoS) DelApp(ctx context.Context,id uint)error{
return u.db.Where("id = ?",id).Delete(&Application{}).Error
}
func (u *applicationRepoS) SearchCount(ctx context.Context, query Query) (int64, error){
var total int64
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
db := u.db.Model(&Application{})
if query.Keyword != "" {
db = db.Where("code like ? or name like ?", keyword, keyword)
}
err := db.Count(&total).Error
return total, err
}