59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package dal
|
|
|
|
import (
|
|
"context"
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/schema"
|
|
"github.com/guxuan/hailin_service/pkg/errors"
|
|
"github.com/guxuan/hailin_service/pkg/util"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func GetJobAreaDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
|
return util.GetDB(ctx, defDB).Model(new(schema.JobArea))
|
|
}
|
|
|
|
type JobArea struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func (a *JobArea) Query(ctx context.Context) (*[]schema.JobArea, error) {
|
|
|
|
var list []schema.JobArea
|
|
tx := GetJobAreaDB(ctx, a.DB).Find(&list)
|
|
if tx.Error != nil {
|
|
return nil, errors.WithStack(tx.Error)
|
|
|
|
}
|
|
return &list, nil
|
|
}
|
|
func (a *JobArea) Get(ctx context.Context, id string) (*schema.JobArea, error) {
|
|
|
|
item := new(schema.JobArea)
|
|
ok, err := util.FindOne(ctx, GetJobAreaDB(ctx, a.DB).Where("id=?", id), util.QueryOptions{}, item)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
} else if !ok {
|
|
return nil, nil
|
|
}
|
|
return item, nil
|
|
}
|
|
func (a *JobArea) Exists(ctx context.Context, name string) (bool, error) {
|
|
ok, err := util.Exists(ctx, GetJobAreaDB(ctx, a.DB).Where("name =?", name))
|
|
return ok, errors.WithStack(err)
|
|
}
|
|
|
|
func (a *JobArea) Create(ctx context.Context, item *schema.JobArea) error {
|
|
result := GetJobAreaDB(ctx, a.DB).Create(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
func (a *JobArea) Update(ctx context.Context, item *schema.JobArea) error {
|
|
result := GetJobAreaDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
|
return errors.WithStack(result.Error)
|
|
}
|
|
|
|
func (a *JobArea) Delete(ctx context.Context, id string) error {
|
|
result := GetJobAreaDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.JobArea))
|
|
return errors.WithStack(result.Error)
|
|
}
|