package dal import ( "context" "gitlab.guxuan.icu/jinshan_community/internal/mods/common/schema" "gitlab.guxuan.icu/jinshan_community/pkg/errors" "gitlab.guxuan.icu/jinshan_community/pkg/util" "gorm.io/gorm" ) // Get work order storage instance func GetWorkOrderDB(ctx context.Context, defDB *gorm.DB) *gorm.DB { return util.GetDB(ctx, defDB).Model(new(schema.WorkOrder)) } // Defining the `WorkOrder` data access object. type WorkOrder struct { DB *gorm.DB } // Query work orders from the database based on the provided parameters and options. func (a *WorkOrder) Query(ctx context.Context, params schema.WorkOrderQueryParam, opts ...schema.WorkOrderQueryOptions) (*schema.WorkOrderQueryResult, error) { var opt schema.WorkOrderQueryOptions if len(opts) > 0 { opt = opts[0] } db := GetWorkOrderDB(ctx, a.DB) var list schema.WorkOrders pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list) if err != nil { return nil, errors.WithStack(err) } queryResult := &schema.WorkOrderQueryResult{ PageResult: pageResult, Data: list, } return queryResult, nil } // Get the specified work order from the database. func (a *WorkOrder) Get(ctx context.Context, id string, opts ...schema.WorkOrderQueryOptions) (*schema.WorkOrder, error) { var opt schema.WorkOrderQueryOptions if len(opts) > 0 { opt = opts[0] } item := new(schema.WorkOrder) ok, err := util.FindOne(ctx, GetWorkOrderDB(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 } // Exists checks if the specified work order exists in the database. func (a *WorkOrder) Exists(ctx context.Context, id string) (bool, error) { ok, err := util.Exists(ctx, GetWorkOrderDB(ctx, a.DB).Where("id=?", id)) return ok, errors.WithStack(err) } // Create a new work order. func (a *WorkOrder) Create(ctx context.Context, item *schema.WorkOrder) error { result := GetWorkOrderDB(ctx, a.DB).Create(item) return errors.WithStack(result.Error) } // Update the specified work order in the database. func (a *WorkOrder) Update(ctx context.Context, item *schema.WorkOrder) error { result := GetWorkOrderDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item) return errors.WithStack(result.Error) } // Delete the specified work order from the database. func (a *WorkOrder) Delete(ctx context.Context, id string) error { result := GetWorkOrderDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.WorkOrder)) return errors.WithStack(result.Error) }