// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "smart-alarm/consts" "smart-alarm/errors" "time" "github.com/jaryhe/gopkgs/logger" "github.com/jinzhu/gorm" "go.uber.org/zap" ) type TAlarm struct { Id int64 `gorm:"column:ID;primary_key"` ProjectId int64 `gorm:"column:ProjectId"` Sn string `gorm:"column:SN"` AlarmCode string `gorm:"column:AlarmCode"` AlarmReason string `gorm:"column:AlarmReason"` IsHandle bool `gorm:"column:IsHandle"` Time string `gorm:"column:Time"` CreatedAt string `gorm:"column:CreatedAt"` UpdatedAt string `gorm:"column:UpdatedAt"` DeviceCode uint32 `gorm:"column:DeviceCode"` First bool `gorm:"column:First"` TaskID int64 `gorm:"column:TaskID"` } func (TAlarm) TableName() string { return "Alarm" } func (p *TAlarm) Insert(db *gorm.DB) error { timeNow := time.Now().Format(consts.TimeSecondLayOut) p.CreatedAt = timeNow p.UpdatedAt = timeNow err := db.Table(p.TableName()).Create(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "insert into t_alarm"), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *TAlarm) Delete(db *gorm.DB, filter map[string]interface{}) error { err := db.Table(p.TableName()).Where(filter).Delete(p).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "delete from t_alarm"), zap.String("fields", fields), zap.String("error", err.Error())) } return err } // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。 func (p *TAlarm) UpdateSome(db *gorm.DB, filed map[string]interface{}) error { if filed == nil { return errors.ParamsError } timeNow := time.Now().Format(consts.TimeSecondLayOut) filed["UpdatedAt"] = timeNow err := db.Table(p.TableName()).Model(p).Updates(filed).Error if err != nil { fields, _ := json.MarshalToString(filed) logger.Error("mysql", zap.String("sql", "update t_alarm"), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *TAlarm) Query(db *gorm.DB, filter map[string]interface{}) error { err := db.Table(p.TableName()).Where(filter).Find(p).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "select from t_alarm"), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *TAlarm) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TAlarmContact, err error) { err = db.Table(p.TableName()).Where(filter).Find(&list).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "select from t_alarm"), zap.String("fields", fields), zap.String("error", err.Error())) } return list, err } /* func Create(conn iclient.Client, db string) error { qt := iclient.Query{ Command: "create database " + db, } _, err := conn.Query(qt) if err != nil { return err } return nil } func Query(sql string, db string, result interface{}) ([]map[string]interface{}, error) { qt := iclient.Query{ Database: db, Command: sql, } r, err := influxdb.InfluxCli.Query(qt) if err != nil { return nil, err } if r == nil { return nil, nil } if len(r.Results) == 0 { return nil, nil } if len(r.Results[0].Series) == 0 { return nil, nil } colNames := r.Results[0].Series[0].Columns var marray = make([]map[string]interface{}, len(r.Results[0].Series[0].Values)) for i, row := range r.Results[0].Series[0].Values { item := map[string]interface{}{} for j, v := range row { if colNames[j] == "time" { t, _ := time.Parse(time.RFC3339, v.(string)) v = t.Format("2006-01-02 15:04:05") } item[colNames[j]] = v } marray[i] = item } if result != nil { bytes, _ := json.Marshal(marray) err = json.Unmarshal(bytes, result) if err != nil { return nil, err } } return marray, nil } func WriteAlarmData(tags map[string]string, fields map[string]interface{}, t time.Time, project_id int64) error { dbName := "alarm" bp, err := iclient.NewBatchPoints(iclient.BatchPointsConfig{ Database: dbName, Precision: "ns", }) if err != nil { return errors.DataBaseError } measurement := fmt.Sprintf("project_%d", project_id) pt, err := iclient.NewPoint( measurement, tags, fields, t.Add(8*time.Hour), ) if err != nil { return errors.DataBaseError } bp.AddPoint(pt) err = influxdb.InfluxCli.Write(bp) if err != nil { if strings.Contains(err.Error(), "database not found") { Create(influxdb.InfluxCli, dbName) err = influxdb.InfluxCli.Write(bp) if err != nil { return errors.DataBaseError } return nil } return errors.DataBaseError } return nil } */