uniugm/admin/apps/game/domain/repo/comm_resource.go

130 lines
4.8 KiB
Go
Raw Normal View History

2025-04-24 20:39:31 +08:00
package repo
import (
"admin/apps/game/domain/entity"
"admin/apps/game/model"
"admin/apps/game/model/dto"
"admin/internal/errcode"
"errors"
"gorm.io/gorm"
"reflect"
)
type ICommonResourceRepo interface {
List(projectId string, pageNo, pageLen int, extraQuery string, args ...any) ([]*dto.CommonDtoFieldDesc, []*entity.CommonResource, error)
GetById(projectId string, id int) ([]*dto.CommonDtoFieldDesc, *entity.CommonResource, bool, error)
Create(projectId string, et dto.CommonDtoValues) (*entity.CommonResource, error)
Edit(projectId string, et dto.CommonDtoValues) error
2025-04-26 13:50:26 +08:00
Delete(projectId string, id int) (*entity.CommonResource, bool, error)
2025-04-24 20:39:31 +08:00
}
func NewCommonResourceRepo(db *gorm.DB, poTemplate model.IModel) ICommonResourceRepo {
return newCommonResourceRepoImpl(db, poTemplate)
}
type commonResourceRepoImpl struct {
db *gorm.DB
poTemplate model.IModel
fieldsDescInfoFun func(projectId string) []*dto.CommonDtoFieldDesc
}
func newCommonResourceRepoImpl(db *gorm.DB, poTemplate model.IModel) *commonResourceRepoImpl {
fieldsInfo := (&entity.CommonResource{}).FromPo(poTemplate).GetDtoFieldsDescInfo
return &commonResourceRepoImpl{db: db, poTemplate: poTemplate, fieldsDescInfoFun: fieldsInfo}
}
func (repo *commonResourceRepoImpl) List(projectId string, pageNo, pageLen int, extraQuery string, args ...any) ([]*dto.CommonDtoFieldDesc, []*entity.CommonResource, error) {
listType := reflect.New(reflect.SliceOf(reflect.TypeOf(repo.poTemplate)))
var err error
if extraQuery == "" {
err = repo.db.Find(listType.Interface()).Error
} else {
err = repo.db.Where(extraQuery, args...).Find(listType.Interface()).Error
}
if err != nil {
return nil, nil, errcode.New(errcode.DBError, "list resource %v error:%v", repo.poTemplate.TableName(), err)
}
listType1 := listType.Elem()
listLen := listType1.Len()
entityList := make([]*entity.CommonResource, 0, listLen)
for i := 0; i < listType1.Len(); i++ {
po := listType1.Index(i).Interface().(model.IModel)
et := &entity.CommonResource{}
et.FromPo(po)
entityList = append(entityList, et)
}
return repo.fieldsDescInfoFun(projectId), entityList, nil
}
func (repo *commonResourceRepoImpl) GetById(projectId string, id int) ([]*dto.CommonDtoFieldDesc, *entity.CommonResource, bool, error) {
po := repo.newEmptyPo()
err := repo.db.Where("id = ?", id).Find(po).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return repo.fieldsDescInfoFun(projectId), (&entity.CommonResource{}).FromPo(repo.newEmptyPo()), false, nil
}
return nil, nil, false, errcode.New(errcode.DBError, "get resource:%v by id:%v error:%v", repo.poTemplate.TableName(), id, err)
}
return repo.fieldsDescInfoFun(projectId), (&entity.CommonResource{}).FromPo(po), true, nil
}
func (repo *commonResourceRepoImpl) Create(projectId string, dtoObj dto.CommonDtoValues) (*entity.CommonResource, error) {
et := (&entity.CommonResource{}).FromPo(repo.newEmptyPo()).FromDto(dtoObj)
err := repo.db.Create(et.Po).Error
if err != nil {
return et, errcode.New(errcode.DBError, "create resource:%v obj:%+v error:%v", repo.poTemplate.TableName(), et, err)
}
return et, nil
}
func (repo *commonResourceRepoImpl) Edit(projectId string, dtoObj dto.CommonDtoValues) error {
et := (&entity.CommonResource{}).FromPo(repo.newEmptyPo()).FromDto(dtoObj)
err := repo.db.Where("id=?", et.Po.GetId()).Updates(et.Po).Error
if err != nil {
return errcode.New(errcode.DBError, "edit resource:%v obj:%+v error:%v", repo.poTemplate.TableName(), et, err)
}
return nil
}
2025-04-26 13:50:26 +08:00
func (repo *commonResourceRepoImpl) Delete(projectId string, id int) (*entity.CommonResource, bool, error) {
_, et, find, err := repo.GetById(projectId, id)
2025-04-24 20:39:31 +08:00
if err != nil {
2025-04-26 13:50:26 +08:00
return nil, false, err
2025-04-24 20:39:31 +08:00
}
2025-04-26 13:50:26 +08:00
if !find {
return et, false, nil
}
err = repo.db.Where("id=?", id).Unscoped().Delete(repo.poTemplate).Error
if err != nil {
return nil, false, errcode.New(errcode.DBError, "delete resource:%v obj:%+v error:%v", repo.poTemplate.TableName(), id, err)
}
return et, true, nil
2025-04-24 20:39:31 +08:00
}
func (repo *commonResourceRepoImpl) newEmptyPo() model.IModel {
return reflect.New(reflect.TypeOf(repo.poTemplate).Elem()).Interface().(model.IModel)
}
2025-04-26 13:50:26 +08:00
type nopRepoImpl struct {
}
func (impl *nopRepoImpl) List(projectId string, pageNo, pageLen int, extraQuery string, args ...any) ([]*dto.CommonDtoFieldDesc, []*entity.CommonResource, error) {
return nil, nil, nil
}
func (impl *nopRepoImpl) GetById(projectId string, id int) ([]*dto.CommonDtoFieldDesc, *entity.CommonResource, bool, error) {
return nil, nil, false, nil
}
func (impl *nopRepoImpl) Create(projectId string, et dto.CommonDtoValues) (*entity.CommonResource, error) {
return nil, nil
}
func (impl *nopRepoImpl) Edit(projectId string, et dto.CommonDtoValues) error {
return nil
}
func (impl *nopRepoImpl) Delete(projectId string, id int) error {
return nil
}