b_demo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package internal
  2. import (
  3. "context"
  4. "github.com/gogf/gf/util/guid"
  5. "gxt-file-server/app/errors"
  6. "gxt-file-server/app/model"
  7. "gxt-file-server/app/schema"
  8. )
  9. // NewDemo 创建demo
  10. func NewDemo(mDemo model.IDemo) *Demo {
  11. return &Demo{
  12. DemoModel: mDemo,
  13. }
  14. }
  15. // Demo 示例程序
  16. type Demo struct {
  17. DemoModel model.IDemo
  18. }
  19. // Query 查询数据
  20. func (a *Demo) Query(ctx context.Context, params schema.DemoQueryParam, opts ...schema.DemoQueryOptions) (*schema.DemoQueryResult, error) {
  21. return a.DemoModel.Query(ctx, params, opts...)
  22. }
  23. // Get 查询指定数据
  24. func (a *Demo) Get(ctx context.Context, recordID string, opts ...schema.DemoQueryOptions) (*schema.Demo, error) {
  25. item, err := a.DemoModel.Get(ctx, recordID, opts...)
  26. if err != nil {
  27. return nil, err
  28. } else if item == nil {
  29. return nil, errors.ErrNotFound
  30. }
  31. return item, nil
  32. }
  33. func (a *Demo) checkCode(ctx context.Context, code string) error {
  34. result, err := a.DemoModel.Query(ctx, schema.DemoQueryParam{
  35. Code: code,
  36. }, schema.DemoQueryOptions{
  37. PageParam: &schema.PaginationParam{PageSize: -1},
  38. })
  39. if err != nil {
  40. return err
  41. } else if result.PageResult.Total > 0 {
  42. return errors.New400Response("编号已经存在")
  43. }
  44. return nil
  45. }
  46. func (a *Demo) getUpdate(ctx context.Context, recordID string) (*schema.Demo, error) {
  47. return a.Get(ctx, recordID)
  48. }
  49. // Create 创建数据
  50. func (a *Demo) Create(ctx context.Context, item schema.Demo) (*schema.Demo, error) {
  51. err := a.checkCode(ctx, item.Code)
  52. if err != nil {
  53. return nil, err
  54. }
  55. item.RecordID = guid.S()
  56. err = a.DemoModel.Create(ctx, item)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return a.getUpdate(ctx, item.RecordID)
  61. }
  62. // Update 更新数据
  63. func (a *Demo) Update(ctx context.Context, recordID string, item schema.Demo) (*schema.Demo, error) {
  64. oldItem, err := a.DemoModel.Get(ctx, recordID)
  65. if err != nil {
  66. return nil, err
  67. } else if oldItem == nil {
  68. return nil, errors.ErrNotFound
  69. } else if oldItem.Code != item.Code {
  70. err := a.checkCode(ctx, item.Code)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. err = a.DemoModel.Update(ctx, recordID, item)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return a.getUpdate(ctx, recordID)
  80. }
  81. // Delete 删除数据
  82. func (a *Demo) Delete(ctx context.Context, recordID string) error {
  83. oldItem, err := a.DemoModel.Get(ctx, recordID)
  84. if err != nil {
  85. return err
  86. } else if oldItem == nil {
  87. return errors.ErrNotFound
  88. }
  89. return a.DemoModel.Delete(ctx, recordID)
  90. }
  91. // UpdateStatus 更新状态
  92. func (a *Demo) UpdateStatus(ctx context.Context, recordID string, status int) error {
  93. oldItem, err := a.DemoModel.Get(ctx, recordID)
  94. if err != nil {
  95. return err
  96. } else if oldItem == nil {
  97. return errors.ErrNotFound
  98. }
  99. return a.DemoModel.UpdateStatus(ctx, recordID, status)
  100. }