123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "time"
- )
- type TGate struct {
- ID int64 `gorm:"column:id;Primary_key" json:"id"`
- DeviceName string `gorm:"column:device_name" json:"device_name" form:"device_name"`
- Sn string `gorm:"column:sn" json:"sn" form:"sn"`
- Manufactor string `gorm:"column:manufactor" json:"manufactor" form:"manufactor"`
- AuthKey string `gorm:"column:auth_key" json:"auth_key" form:"auth_key"`
- GardenId int64 `gorm:"column:garden_id" json:"garden_id" form:"garden_id"`
- OutUser string `gorm:"column:out_user" json:"out_user" form:"out_user"`
- OutTime int64 `gorm:"column:out_time" json:"out_time" form:"out_time"`
- Status int32 `gorm:"column:status" json:"status" form:"status"`
- QcodeSupport int32 `gorm:"column:qcode_support" json:"qcode_support" form:"qcode_support"`
- Enable int32 `gorm:"column:enable" json:"enable" form:"enable"`
- Location string `gorm:"column:location" json:"location" form:"location"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
- Protocol int32 `gorm:"column:protocol" json:"protocol" form:"protocol"`
- Direction int32 `gorm:"column:direction" json:"direction" form:"direction"`
- PicSupport int32 `gorm:"column:pic_support" json:"pic_support" form:"pic_support"`
- CardSupport int32 `gorm:"column:card_support" json:"card_support" form:"card_support"`
- Ip string `gorm:"column:ip" json:"ip"`
- Mac string `gorm:"column:mac" json:"mac"`
- UserName string `gorm:"column:user_name" json:"user_name"`
- Password string `gorm:"column:password" json:"password"`
- Port int64 `gorm:"column:port" json:"port"`
- }
- func (p *TGate) TableName() string {
- return "t_gate"
- }
- func (p *TGate) Find(db *gorm.DB, where [][2]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "find "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TGate) Last(db *gorm.DB) error {
- err := db.Table(p.TableName()).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "select last from "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入一条记录
- func (p *TGate) Insert(db *gorm.DB) error {
- err := db.Create(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入多条记录
- func (p *TGate) InsertMulti(db *gorm.DB, values interface{}) error {
- err := db.Table(p.TableName()).Create(values).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TGate) Delete(db *gorm.DB, filter [][2]interface{}) error {
- cond, val, err := whereBuild(filter)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(filter)
- logger.Error("mysql",
- zap.String("sql", "delete "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- err = db.Where(cond, val...).Delete(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(filter)
- logger.Error("mysql",
- zap.String("sql", "delete from "+p.TableName()),
- zap.String("where", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TGate) Update(db *gorm.DB, where [][2]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *TGate) Count(db *gorm.DB, where [][2]interface{}, or [][2]interface{}) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return ret, err
- }
- func (p *TGate) List(db *gorm.DB, where [][2]interface{}, or [][2]interface{}, page int, pageSize int) (list []TGate, err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- func (p *TGate) SelectList(db *gorm.DB, where [][2]interface{}, selectStr string, ret interface{}) (err error) {
- cond, val, err := whereBuildAndOr(where, nil)
- if err != nil {
- return err
- }
- result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(ret)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select "+selectStr+" from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return result.Error
- }
|