123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- // 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"
- )
- type TAlarmContact struct {
- Id uint64 `gorm:"primary_key"`
- ProjectId int64 `gorm:"column:project_id"`
- Phone string `gorm:"column:phone"`
- Email string `gorm:"column:email"`
- CreatedAt string `gorm:"column:created_at"`
- UpdatedAt string `json:"updated_at"`
- }
- func (TAlarmContact) TableName() string {
- return "t_alarm_contact"
- }
- func (p *TAlarmContact) 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_contact"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TAlarmContact) 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_contact"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
- func (p *TAlarmContact) 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_contact"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TAlarmContact) 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_contact"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TAlarmContact) QueryAll(db *gorm.DB, filter map[string]interface{}) (list []TAlarmContact, 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_contact"),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return list, err
- }
|