alarm.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "github.com/jinzhu/gorm"
  10. "go.uber.org/zap"
  11. )
  12. type TAlarm struct {
  13. Id int64 `gorm:"primary_key"`
  14. ProjectId int64 `gorm:"column:project_id"`
  15. Sn string `gorm:"column:sn"`
  16. AlarmCode string `gorm:"column:alarm_code"`
  17. AlarmReason string `gorm:"column:alarm_reason"`
  18. IsHandle bool `gorm:"column:is_handle"`
  19. Time string `gorm:"column:time"`
  20. DeviceCode int32
  21. CreatedAt string `gorm:"column:created_at"`
  22. UpdatedAt string `gorm:"column:updated_at"`
  23. First bool
  24. }
  25. func (TAlarm) TableName() string {
  26. return "t_alarm"
  27. }
  28. func (p *TAlarm) Insert(db *gorm.DB) error {
  29. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  30. p.CreatedAt = timeNow
  31. p.UpdatedAt = timeNow
  32. err := db.Create(p).Error
  33. if err != nil {
  34. fields, _ := json.MarshalToString(*p)
  35. logger.Error("mysql",
  36. zap.String("sql", "insert into t_alarm"),
  37. zap.String("fields", fields),
  38. zap.String("error", err.Error()))
  39. }
  40. return err
  41. }
  42. func (p *TAlarm) Delete(db *gorm.DB, filter map[string]interface{}) error {
  43. err := db.Where(filter).Delete(p).Error
  44. if err != nil {
  45. fields, _ := json.MarshalToString(filter)
  46. logger.Error("mysql",
  47. zap.String("sql", "delete from t_alarm"),
  48. zap.String("fields", fields),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  54. func (p *TAlarm) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  55. if filed == nil {
  56. return errors.ParamsError
  57. }
  58. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  59. filed["updated_at"] = timeNow
  60. err := db.Model(p).Updates(filed).Error
  61. if err != nil {
  62. fields, _ := json.MarshalToString(filed)
  63. logger.Error("mysql",
  64. zap.String("sql", "update t_alarm"),
  65. zap.String("fields", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. func (p *TAlarm) Query(db *gorm.DB, filter map[string]interface{}) error {
  71. err := db.Where(filter).Find(p).Error
  72. if err != nil {
  73. fields, _ := json.MarshalToString(filter)
  74. logger.Error("mysql",
  75. zap.String("sql", "select from t_alarm"),
  76. zap.String("fields", fields),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *TAlarm) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TAlarmContact, err error) {
  82. err = db.Where(filter).Find(&list).Error
  83. if err != nil {
  84. fields, _ := json.MarshalToString(filter)
  85. logger.Error("mysql",
  86. zap.String("sql", "select from t_alarm"),
  87. zap.String("fields", fields),
  88. zap.String("error", err.Error()))
  89. }
  90. return list, err
  91. }
  92. /*
  93. func Create(conn iclient.Client, db string) error {
  94. qt := iclient.Query{
  95. Command: "create database " + db,
  96. }
  97. _, err := conn.Query(qt)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. func Query(sql string, db string, result interface{}) ([]map[string]interface{}, error) {
  104. qt := iclient.Query{
  105. Database: db,
  106. Command: sql,
  107. }
  108. r, err := influxdb.InfluxCli.Query(qt)
  109. if err != nil {
  110. return nil, err
  111. }
  112. if r == nil {
  113. return nil, nil
  114. }
  115. if len(r.Results) == 0 {
  116. return nil, nil
  117. }
  118. if len(r.Results[0].Series) == 0 {
  119. return nil, nil
  120. }
  121. colNames := r.Results[0].Series[0].Columns
  122. var marray = make([]map[string]interface{}, len(r.Results[0].Series[0].Values))
  123. for i, row := range r.Results[0].Series[0].Values {
  124. item := map[string]interface{}{}
  125. for j, v := range row {
  126. if colNames[j] == "time" {
  127. t, _ := time.Parse(time.RFC3339, v.(string))
  128. v = t.Format("2006-01-02 15:04:05")
  129. }
  130. item[colNames[j]] = v
  131. }
  132. marray[i] = item
  133. }
  134. if result != nil {
  135. bytes, _ := json.Marshal(marray)
  136. err = json.Unmarshal(bytes, result)
  137. if err != nil {
  138. return nil, err
  139. }
  140. }
  141. return marray, nil
  142. }
  143. func WriteAlarmData(tags map[string]string, fields map[string]interface{}, t time.Time, project_id int64) error {
  144. dbName := "alarm"
  145. bp, err := iclient.NewBatchPoints(iclient.BatchPointsConfig{
  146. Database: dbName,
  147. Precision: "ns",
  148. })
  149. if err != nil {
  150. return errors.DataBaseError
  151. }
  152. measurement := fmt.Sprintf("project_%d", project_id)
  153. pt, err := iclient.NewPoint(
  154. measurement,
  155. tags,
  156. fields,
  157. t.Add(8*time.Hour),
  158. )
  159. if err != nil {
  160. return errors.DataBaseError
  161. }
  162. bp.AddPoint(pt)
  163. err = influxdb.InfluxCli.Write(bp)
  164. if err != nil {
  165. if strings.Contains(err.Error(), "database not found") {
  166. Create(influxdb.InfluxCli, dbName)
  167. err = influxdb.InfluxCli.Write(bp)
  168. if err != nil {
  169. return errors.DataBaseError
  170. }
  171. return nil
  172. }
  173. return errors.DataBaseError
  174. }
  175. return nil
  176. }
  177. */