b_login_history.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package internal
  2. import (
  3. "context"
  4. "github.com/gogf/gf/util/guid"
  5. "yx-dataset-server/app/errors"
  6. "yx-dataset-server/app/model"
  7. "yx-dataset-server/app/schema"
  8. )
  9. // NewLoginHistory 创建LoginHistory
  10. func NewLoginHistory(mLoginHistory model.ILoginHistory) *LoginHistory {
  11. return &LoginHistory{
  12. LoginHistoryModel: mLoginHistory,
  13. }
  14. }
  15. // LoginHistory 创建LoginHistory对象
  16. type LoginHistory struct {
  17. LoginHistoryModel model.ILoginHistory
  18. }
  19. // Query 查询数据
  20. func (a *LoginHistory) Query(ctx context.Context, params schema.LoginHistoryQueryParam, opts ...schema.LoginHistoryQueryOptions) (*schema.LoginHistoryQueryResult, error) {
  21. return a.LoginHistoryModel.Query(ctx, params, opts...)
  22. }
  23. // Get 查询指定数据
  24. func (a *LoginHistory) Get(ctx context.Context, recordID string, opts ...schema.LoginHistoryQueryOptions) (*schema.LoginHistory, error) {
  25. item, err := a.LoginHistoryModel.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 *LoginHistory) checkCode(ctx context.Context, code string) error {
  34. result, err := a.LoginHistoryModel.Query(ctx, schema.LoginHistoryQueryParam{
  35. Code: code,
  36. }, schema.LoginHistoryQueryOptions{
  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 *LoginHistory) getUpdate(ctx context.Context, recordID string) (*schema.LoginHistory, error) {
  47. return a.Get(ctx, recordID)
  48. }
  49. // Create 创建数据
  50. func (a *LoginHistory) Create(ctx context.Context, item schema.LoginHistory) (*schema.LoginHistory, error) {
  51. item.RecordID = guid.S()
  52. err := a.LoginHistoryModel.Create(ctx, item)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return a.getUpdate(ctx, item.RecordID)
  57. }
  58. // Update 更新数据
  59. func (a *LoginHistory) Update(ctx context.Context, recordID string, item schema.LoginHistory) error {
  60. oldItem, err := a.LoginHistoryModel.Get(ctx, recordID)
  61. if err != nil {
  62. return err
  63. } else if oldItem == nil {
  64. return errors.ErrNotFound
  65. }
  66. return a.LoginHistoryModel.Update(ctx, recordID, item)
  67. }
  68. // Delete 删除数据
  69. func (a *LoginHistory) Delete(ctx context.Context, recordID string) error {
  70. oldItem, err := a.LoginHistoryModel.Get(ctx, recordID)
  71. if err != nil {
  72. return err
  73. } else if oldItem == nil {
  74. return errors.ErrNotFound
  75. }
  76. return a.LoginHistoryModel.Delete(ctx, recordID)
  77. }
  78. // UpdateStatus 更新状态
  79. func (a *LoginHistory) UpdateStatus(ctx context.Context, recordID string, status int) error {
  80. oldItem, err := a.LoginHistoryModel.Get(ctx, recordID)
  81. if err != nil {
  82. return err
  83. } else if oldItem == nil {
  84. return errors.ErrNotFound
  85. }
  86. return a.LoginHistoryModel.UpdateStatus(ctx, recordID, status)
  87. }