contact.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TContact struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. Addr string `gorm:"column:addr" json:"addr"`
  14. Phone string `gorm:"column:phone" json:"phone"`
  15. Email string `gorm:"column:email" json:"email"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. }
  19. func (p *TContact) TableName() string {
  20. return "t_contact"
  21. }
  22. func (p *TContact) Find(db *gorm.DB, where map[string]interface{}) error {
  23. err := db.Table(p.TableName()).Where(where).First(p).Error
  24. if err != nil {
  25. fields, _ := util.MarshalToString(where)
  26. logger.Error("mysql",
  27. zap.String("sql", "select from "+p.TableName()),
  28. zap.String("fields", fields),
  29. zap.String("error", err.Error()))
  30. }
  31. return err
  32. }
  33. func (p *TContact) Last(db *gorm.DB) error {
  34. err := db.Table(p.TableName()).Last(p).Error
  35. if err != nil {
  36. logger.Error("mysql",
  37. zap.String("sql", "select last from "+p.TableName()),
  38. zap.String("fields", ""),
  39. zap.String("error", err.Error()))
  40. }
  41. return err
  42. }
  43. // Insert 插入一条记录
  44. func (p *TContact) Insert(db *gorm.DB) error {
  45. err := db.Create(p).Error
  46. if err != nil {
  47. fields, _ := util.MarshalToString(*p)
  48. logger.Error("mysql",
  49. zap.String("sql", "insert into "+p.TableName()),
  50. zap.String("fields", fields),
  51. zap.String("error", err.Error()))
  52. }
  53. return err
  54. }
  55. func (p *TContact) Delete(db *gorm.DB, filter map[string]interface{}) error {
  56. err := db.Where(filter).Delete(p).Error
  57. if err != nil {
  58. fields, _ := util.MarshalToString(filter)
  59. logger.Error("mysql",
  60. zap.String("sql", "delete from "+p.TableName()),
  61. zap.String("where", fields),
  62. zap.String("error", err.Error()))
  63. }
  64. return err
  65. }
  66. func (p *TContact) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  67. cond, val, err := whereBuild(where)
  68. if err != nil {
  69. if err != nil {
  70. fields, _ := util.MarshalToString(values)
  71. logger.Error("mysql",
  72. zap.String("sql", "update "+p.TableName()),
  73. zap.String("fields", fields),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  79. }
  80. func (p *TContact) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  81. cond, val, err := whereBuildAndOr(where, or)
  82. if err != nil {
  83. return 0, err
  84. }
  85. ret := int64(0)
  86. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  87. if err != nil {
  88. fields, _ := util.MarshalToString(where)
  89. logger.Error("mysql",
  90. zap.String("sql", "select count "+p.TableName()),
  91. zap.String("fields", fields),
  92. zap.String("error", err.Error()))
  93. }
  94. return ret, err
  95. }
  96. func (p *TContact) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TContact, err error) {
  97. cond, val, err := whereBuildAndOr(where, or)
  98. if err != nil {
  99. return list, err
  100. }
  101. if pageSize < 0 {
  102. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  103. if result.Error != nil {
  104. wherefields, _ := util.MarshalToString(where)
  105. logger.Error("mysql",
  106. zap.String("sql", "select * from "+p.TableName()),
  107. zap.String("where", wherefields),
  108. zap.String("error", result.Error.Error()))
  109. }
  110. return list, result.Error
  111. }
  112. offset := (page - 1) * pageSize
  113. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  114. if result.Error != nil {
  115. wherefields, _ := util.MarshalToString(where)
  116. logger.Error("mysql",
  117. zap.String("sql", "select * from "+p.TableName()),
  118. zap.String("where", wherefields),
  119. zap.String("error", result.Error.Error()))
  120. }
  121. return list, result.Error
  122. }