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 Delete(projectId string, id int) error } 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 } func (repo *commonResourceRepoImpl) Delete(projectId string, id int) error { err := repo.db.Where("id=?", id).Unscoped().Delete(repo.poTemplate).Error if err != nil { return errcode.New(errcode.DBError, "delete resource:%v obj:%+v error:%v", repo.poTemplate.TableName(), id, err) } return nil } func (repo *commonResourceRepoImpl) newEmptyPo() model.IModel { return reflect.New(reflect.TypeOf(repo.poTemplate).Elem()).Interface().(model.IModel) }