123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- // 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"
- "go.uber.org/zap"
- "github.com/jinzhu/gorm"
- json2 "encoding/json"
- )
- // 替换encoding/json包
- type JsonStruct struct {
- }
- func (p *JsonStruct)MarshalToString(v interface{}) (string, error) {
- bytes, err := json2.Marshal(v)
- return string(bytes), err
- }
- func (p *JsonStruct)Marshal(v interface{}) ([]byte, error) {
- bytes, err := json2.Marshal(v)
- return bytes, err
- }
- func (p *JsonStruct)Unmarshal(bytes []byte, v interface{}) (error) {
- err := json2.Unmarshal(bytes, v)
- return err
- }
- var json = &JsonStruct{}
- type TAlarmRule struct {
- Id int64 `gorm:"primary_key"`
- ProjectId int64 `gorm:"column:project_id"`
- Sn string `gorm:"sn"`
- ContinuePeriod int32 `gorm:"column:continue_period"`
- AlarmCount int32 `gorm:"column:alarm_count"`
- SilencePeriod int32 `gorm:"column:silence_period"`
- IsOn bool `gorm:"column:is_on"`
- CreatedAt string `gorm:"column:created_at"`
- UpdatedAt string `gorm:"column:updated_at"`
- }
- func (TAlarmRule) TableName() string {
- return "t_alarm_rule"
- }
- func (p *TAlarmRule) Insert(db *gorm.DB) error {
- timeNow := time.Now().Format(consts.TimeSecondLayOut)
- p.CreatedAt = timeNow
- p.UpdatedAt = timeNow
- err := db.Create(p).Error
- if err != nil {
- fields, _ := json.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into t_alarm_rule"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TAlarmRule) Delete(db *gorm.DB, filter map[string]interface{}) error {
- err := db.Where(filter).Delete(p).Error
- if err != nil {
- fields, _ := json.MarshalToString(filter)
- logger.Error("mysql",
- zap.String("sql", "delete from t_alarm_rule"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
- func (p *TAlarmRule) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
- if filed == nil {
- return errors.ParamsError
- }
- timeNow := time.Now().Format(consts.TimeSecondLayOut)
- filed["updated_at"] = timeNow
- err := db.Model(p).Updates(filed).Error
- if err != nil {
- fields, _ := json.MarshalToString(filed)
- logger.Error("mysql",
- zap.String("sql", "update t_alarm_rule"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TAlarmRule) Query(db *gorm.DB, filter map[string]interface{}) error {
- err := db.Where(filter).Find(p).Error
- if err != nil {
- fields, _ := json.MarshalToString(filter)
- logger.Error("mysql",
- zap.String("sql", "select from t_alarm_rule"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TAlarmRule) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TAlarmRule, err error) {
- err = db.Where(filter).Find(&list).Error
- if err != nil {
- fields, _ := json.MarshalToString(filter)
- logger.Error("mysql",
- zap.String("sql", "select from t_alarm_rule"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return list, err
- }
|