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 GetJobDB(ctx context.Context, defDB *gorm.DB) *gorm.DB { return util.GetDB(ctx, defDB).Model(new(schema.Job)) } type Job struct { DB *gorm.DB } func (a *Job) Query(ctx context.Context, params schema.JobQueryParam, opts ...schema.JobQueryOptions) (*schema.JobQueryResult, error) { var opt schema.JobQueryOptions if len(opts) > 0 { opt = opts[0] } db := GetJobDB(ctx, a.DB) if v := params.LikeTitle; len(v) > 0 { db = db.Where("title LIKE ?", "%"+v+"%") } if v := params.Status; len(v) > 0 { db = db.Where("status = ?", v) } if v := params.JobAreaID; len(v) > 0 { db = db.Where("jobAreaId = ?", v) } var list schema.Jobs pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list) if err != nil { return nil, errors.WithStack(err) } queryResult := &schema.JobQueryResult{ PageResult: pageResult, Data: list, } return queryResult, nil } func (a *Job) Get(ctx context.Context, id string, opts ...schema.JobQueryOptions) (*schema.Job, error) { var opt schema.JobQueryOptions if len(opts) > 0 { opt = opts[0] } item := new(schema.Job) ok, err := util.FindOne(ctx, GetJobDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item) if err != nil { return nil, errors.WithStack(err) } else if !ok { return nil, nil } return item, nil } func (a *Job) Exists(ctx context.Context, id string) (bool, error) { ok, err := util.Exists(ctx, GetJobDB(ctx, a.DB).Where("id=?", id)) return ok, errors.WithStack(err) } func (a *Job) Create(ctx context.Context, item *schema.Job) error { result := GetJobDB(ctx, a.DB).Create(item) return errors.WithStack(result.Error) } func (a *Job) Update(ctx context.Context, item *schema.Job) error { result := GetJobDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item) return errors.WithStack(result.Error) } func (a *Job) Delete(ctx context.Context, id string) error { result := GetJobDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Job)) return errors.WithStack(result.Error) }