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