alarm_rule.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. json2 "encoding/json"
  12. )
  13. // 替换encoding/json包
  14. type JsonStruct struct {
  15. }
  16. func (p *JsonStruct)MarshalToString(v interface{}) (string, error) {
  17. bytes, err := json2.Marshal(v)
  18. return string(bytes), err
  19. }
  20. func (p *JsonStruct)Marshal(v interface{}) ([]byte, error) {
  21. bytes, err := json2.Marshal(v)
  22. return bytes, err
  23. }
  24. func (p *JsonStruct)Unmarshal(bytes []byte, v interface{}) (error) {
  25. err := json2.Unmarshal(bytes, v)
  26. return err
  27. }
  28. var json = &JsonStruct{}
  29. type TAlarmRule struct {
  30. Id int64 `gorm:"primary_key"`
  31. ProjectId int64 `gorm:"column:project_id"`
  32. Sn string `gorm:"sn"`
  33. ContinuePeriod int32 `gorm:"column:continue_period"`
  34. AlarmCount int32 `gorm:"column:alarm_count"`
  35. SilencePeriod int32 `gorm:"column:silence_period"`
  36. IsOn bool `gorm:"column:is_on"`
  37. CreatedAt string `gorm:"column:created_at"`
  38. UpdatedAt string `gorm:"column:updated_at"`
  39. }
  40. func (TAlarmRule) TableName() string {
  41. return "t_alarm_rule"
  42. }
  43. func (p *TAlarmRule) Insert(db *gorm.DB) error {
  44. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  45. p.CreatedAt = timeNow
  46. p.UpdatedAt = timeNow
  47. err := db.Create(p).Error
  48. if err != nil {
  49. fields, _ := json.MarshalToString(*p)
  50. logger.Error("mysql",
  51. zap.String("sql", "insert into t_alarm_rule"),
  52. zap.String("fields", fields),
  53. zap.String("error", err.Error()))
  54. }
  55. return err
  56. }
  57. func (p *TAlarmRule) Delete(db *gorm.DB, filter map[string]interface{}) error {
  58. err := db.Where(filter).Delete(p).Error
  59. if err != nil {
  60. fields, _ := json.MarshalToString(filter)
  61. logger.Error("mysql",
  62. zap.String("sql", "delete from t_alarm_rule"),
  63. zap.String("fields", fields),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  69. func (p *TAlarmRule) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  70. if filed == nil {
  71. return errors.ParamsError
  72. }
  73. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  74. filed["updated_at"] = timeNow
  75. err := db.Model(p).Updates(filed).Error
  76. if err != nil {
  77. fields, _ := json.MarshalToString(filed)
  78. logger.Error("mysql",
  79. zap.String("sql", "update t_alarm_rule"),
  80. zap.String("fields", fields),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. func (p *TAlarmRule) Query(db *gorm.DB, filter map[string]interface{}) error {
  86. err := db.Where(filter).Find(p).Error
  87. if err != nil {
  88. fields, _ := json.MarshalToString(filter)
  89. logger.Error("mysql",
  90. zap.String("sql", "select from t_alarm_rule"),
  91. zap.String("fields", fields),
  92. zap.String("error", err.Error()))
  93. }
  94. return err
  95. }
  96. func (p *TAlarmRule) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TAlarmRule, err error) {
  97. err = db.Where(filter).Find(&list).Error
  98. if err != nil {
  99. fields, _ := json.MarshalToString(filter)
  100. logger.Error("mysql",
  101. zap.String("sql", "select from t_alarm_rule"),
  102. zap.String("fields", fields),
  103. zap.String("error", err.Error()))
  104. }
  105. return list, err
  106. }