com.snow.auto_monitor/app/models/whitelist/whitelist.go

136 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package whitelist
import (
"sync"
"time"
"github.com/qit-team/snow-core/db"
)
var (
once sync.Once
m *whitelistModel
)
/**
* Whitelist
*/
type Whitelist struct {
Id int64 `xorm:"pk autoincr"` //注使用getOne 或者ID() 需要设置主键
MerchantId int64
Ip string
CreatedAt time.Time `xorm:"created"`
}
/**
* 表名规则
* @wiki http://gobook.io/read/github.com/go-xorm/manual-zh-CN/chapter-02/3.tags.html
*/
func (m *Whitelist) TableName() string {
return "whitelist"
}
/**
* 私有化防止被外部new
*/
type whitelistModel struct {
db.Model //组合基础Model集成基础Model的属性和方法
}
// 单例模式
func GetInstance() *whitelistModel {
once.Do(func() {
m = new(whitelistModel)
//m.DiName = "" //设置数据库实例连接默认db.SingletonMain
})
return m
}
/**
* 查询主键ID的记录
* @param id 主键ID
* @return has 是否有记录
* @return err 错误信息
* @return whitelist 查询结果
*/
func (m *whitelistModel) GetById(id int64) (whitelist *Whitelist, has bool, err error) {
whitelist = &Whitelist{}
has, err = m.GetDb().ID(id).Get(whitelist)
if err != nil || !has {
whitelist = nil
}
return
}
func (m *whitelistModel) GetByIp(ip string) (whitelist *Whitelist, has bool, err error) {
whitelist = &Whitelist{}
has, err = m.GetDb().Where("ip = ?", ip).Get(whitelist)
if err != nil || !has {
whitelist = nil
}
return
}
func (m *whitelistModel) Search(id int64, merchant_id int64, ip string, startTime string, endTime string, limit int, page int) (whitelist []*Whitelist, err error) {
whitelist = make([]*Whitelist, 0)
sql := "1=1"
var args []interface{}
if id != 0 {
sql += " and id = ?"
args = append(args, id)
}
if merchant_id != 0 {
sql += " and merchant_id = ?"
args = append(args, merchant_id)
}
if ip != "" {
sql += " and ip = ?"
args = append(args, ip)
}
if startTime != "" && endTime != "" {
sql += " and created_at >= ? and created_at <= ?"
args = append(args, startTime, endTime)
}
err = m.GetDb().Where(sql, args...).OrderBy("created_at desc").Limit(limit, page).Find(&whitelist)
return
}
func (m *whitelistModel) CountAll(id int64, merchant_id int64, ip string, startTime string, endTime string) (res int64, err error) {
sql := "1=1"
var args []interface{}
if id != 0 {
sql += " and id = ?"
args = append(args, id)
}
if merchant_id != 0 {
sql += " and merchant_id = ?"
args = append(args, merchant_id)
}
if ip != "" {
sql += " and ip = ?"
args = append(args, ip)
}
if startTime != "" && endTime != "" {
sql += " and created_at >= ? and created_at <= ?"
args = append(args, startTime, endTime)
}
res, err = m.GetDb().Table("whitelist").Where(sql, args...).Count()
return
}
func (m *whitelistModel) Create(whitelist *Whitelist) (affected int64, err error) {
whitelist.CreatedAt = time.Now()
affected, err = m.GetDb().Insert(whitelist)
return
}
func (m *whitelistModel) Update(whitelist *Whitelist) (affected int64, err error) {
affected, err = m.GetDb().ID(whitelist.Id).Update(whitelist)
return
}
func (m *whitelistModel) Delete(id int64) (affected int64, err error) {
affected, err = m.GetDb().ID(id).Delete(&Whitelist{})
return
}