alarm_contact.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "smart-alarm/consts"
  6. "smart-alarm/errors"
  7. "time"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "go.uber.org/zap"
  10. "github.com/jinzhu/gorm"
  11. )
  12. type TAlarmContact struct {
  13. Id uint64 `gorm:"primary_key"`
  14. ProjectId int64 `gorm:"column:project_id"`
  15. Phone string `gorm:"column:phone"`
  16. Email string `gorm:"column:email"`
  17. CreatedAt string `gorm:"column:created_at"`
  18. UpdatedAt string `json:"updated_at"`
  19. }
  20. func (TAlarmContact) TableName() string {
  21. return "t_alarm_contact"
  22. }
  23. func (p *TAlarmContact) Insert(db *gorm.DB) error {
  24. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  25. p.CreatedAt = timeNow
  26. p.UpdatedAt = timeNow
  27. err := db.Create(p).Error
  28. if err != nil {
  29. fields, _ := json.MarshalToString(*p)
  30. logger.Error("mysql",
  31. zap.String("sql", "insert into t_alarm_contact"),
  32. zap.String("fields", fields),
  33. zap.String("error", err.Error()))
  34. }
  35. return err
  36. }
  37. func (p *TAlarmContact) Delete(db *gorm.DB, filter map[string]interface{}) error {
  38. err := db.Where(filter).Delete(p).Error
  39. if err != nil {
  40. fields, _ := json.MarshalToString(filter)
  41. logger.Error("mysql",
  42. zap.String("sql", "delete from t_alarm_contact"),
  43. zap.String("fields", fields),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  49. func (p *TAlarmContact) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  50. if filed == nil {
  51. return errors.ParamsError
  52. }
  53. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  54. filed["updated_at"] = timeNow
  55. err := db.Model(p).Updates(filed).Error
  56. if err != nil {
  57. fields, _ := json.MarshalToString(filed)
  58. logger.Error("mysql",
  59. zap.String("sql", "update t_alarm_contact"),
  60. zap.String("fields", fields),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. func (p *TAlarmContact) Query(db *gorm.DB, filter map[string]interface{}) error {
  66. err := db.Where(filter).Find(p).Error
  67. if err != nil {
  68. fields, _ := json.MarshalToString(filter)
  69. logger.Error("mysql",
  70. zap.String("sql", "select from t_alarm_contact"),
  71. zap.String("fields", fields),
  72. zap.String("error", err.Error()))
  73. }
  74. return err
  75. }
  76. func (p *TAlarmContact) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TAlarmContact, err error) {
  77. err = db.Where(filter).Find(&list).Error
  78. if err != nil {
  79. fields, _ := json.MarshalToString(filter)
  80. logger.Error("mysql",
  81. zap.String("sql", "select from t_alarm_contact"),
  82. zap.String("fields", fields),
  83. zap.String("error", err.Error()))
  84. }
  85. return list, err
  86. }