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

137 lines
3.2 KiB
Go
Raw Normal View History

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