alarm.go 4.9 KB

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