contact.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.Table(p.TableName()).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. cond, val, err := whereBuild(filter)
  57. if err != nil {
  58. return err
  59. }
  60. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  61. if err != nil {
  62. fields, _ := util.MarshalToString(filter)
  63. logger.Error("mysql",
  64. zap.String("sql", "delete from "+p.TableName()),
  65. zap.String("where", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. func (p *TContact) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  71. cond, val, err := whereBuild(where)
  72. if err != nil {
  73. if err != nil {
  74. fields, _ := util.MarshalToString(values)
  75. logger.Error("mysql",
  76. zap.String("sql", "update "+p.TableName()),
  77. zap.String("fields", fields),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  83. }
  84. func (p *TContact) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  85. cond, val, err := whereBuildAndOr(where, or)
  86. if err != nil {
  87. return 0, err
  88. }
  89. ret := int64(0)
  90. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  91. if err != nil {
  92. fields, _ := util.MarshalToString(where)
  93. logger.Error("mysql",
  94. zap.String("sql", "select count "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return ret, err
  99. }
  100. func (p *TContact) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TContact, err error) {
  101. cond, val, err := whereBuildAndOr(where, or)
  102. if err != nil {
  103. return list, err
  104. }
  105. if pageSize < 0 {
  106. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  107. if result.Error != nil {
  108. wherefields, _ := util.MarshalToString(where)
  109. logger.Error("mysql",
  110. zap.String("sql", "select * from "+p.TableName()),
  111. zap.String("where", wherefields),
  112. zap.String("error", result.Error.Error()))
  113. }
  114. return list, result.Error
  115. }
  116. offset := (page - 1) * pageSize
  117. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  118. if result.Error != nil {
  119. wherefields, _ := util.MarshalToString(where)
  120. logger.Error("mysql",
  121. zap.String("sql", "select * from "+p.TableName()),
  122. zap.String("where", wherefields),
  123. zap.String("error", result.Error.Error()))
  124. }
  125. return list, result.Error
  126. }