appsmodelgen.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Code generated by goctl. DO NOT EDIT.
  2. // goctl 1.8.1
  3. package model
  4. import (
  5. "context"
  6. "time"
  7. "github.com/zeromicro/go-zero/core/stores/mon"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. )
  12. type appsModel interface {
  13. Insert(ctx context.Context, data *Apps) error
  14. FindOne(ctx context.Context, id string) (*Apps, error)
  15. Update(ctx context.Context, data *Apps) (*mongo.UpdateResult, error)
  16. Delete(ctx context.Context, id string) (int64, error)
  17. FindAll(ctx context.Context, teamId primitive. ObjectID, apptype string, keyword *string) ([]*Apps, error)
  18. }
  19. type defaultAppsModel struct {
  20. conn *mon.Model
  21. }
  22. func newDefaultAppsModel(conn *mon.Model) *defaultAppsModel {
  23. return &defaultAppsModel{conn: conn}
  24. }
  25. func (m *defaultAppsModel) Insert(ctx context.Context, data *Apps) error {
  26. if data.ID.IsZero() {
  27. data.ID = primitive.NewObjectID()
  28. data.UpdateTime = time.Now()
  29. }
  30. _, err := m.conn.InsertOne(ctx, data)
  31. return err
  32. }
  33. func (m *defaultAppsModel) FindOne(ctx context.Context, id string) (*Apps, error) {
  34. oid, err := primitive.ObjectIDFromHex(id)
  35. if err != nil {
  36. return nil, ErrInvalidObjectId
  37. }
  38. var data Apps
  39. err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
  40. switch err {
  41. case nil:
  42. return &data, nil
  43. case mon.ErrNotFound:
  44. return nil, ErrNotFound
  45. default:
  46. return nil, err
  47. }
  48. }
  49. func (m *defaultAppsModel) Update(ctx context.Context, data *Apps) (*mongo.UpdateResult, error) {
  50. data.UpdateTime = time.Now()
  51. res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
  52. return res, err
  53. }
  54. func (m *defaultAppsModel) Delete(ctx context.Context, id string) (int64, error) {
  55. oid, err := primitive.ObjectIDFromHex(id)
  56. if err != nil {
  57. return 0, ErrInvalidObjectId
  58. }
  59. res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
  60. return res, err
  61. }
  62. func (m *defaultAppsModel) FindAll(ctx context.Context, teamId primitive. ObjectID, apptype string, keyword *string) ([]*Apps, error) {
  63. var data []*Apps
  64. err := m.conn.Find(ctx, &data, bson.M{
  65. "teamId": teamId,
  66. "type": apptype,
  67. "name": bson.M{
  68. "$regex": keyword, // 用你想要匹配的模式替换 searchPattern
  69. "$options": "i", // 可选:i 表示不区分大小写
  70. },
  71. })
  72. switch err {
  73. case nil:
  74. return data, nil
  75. case mon.ErrNotFound:
  76. return nil, ErrNotFound
  77. default:
  78. return nil, err
  79. }
  80. }