|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type Device struct {
- Db *xorm.Engine `json:"db"`
- DeviceSn string `json:"device_sn"`
- }
-
- func (deviceOrdDb *Device) Set(deviceSn string) { // set方法
- deviceOrdDb.Db = Db
- deviceOrdDb.DeviceSn = deviceSn
- }
-
- func (deviceOrdDb *Device) GetDeviceById(id int) (m *model.Device, err error) {
- m = new(model.Device)
- has, err := deviceOrdDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (deviceOrdDb *Device) GetDevice() (m *model.Device, err error) {
- m = new(model.Device)
- has, err := deviceOrdDb.Db.Where("device_sn =?", deviceOrdDb.DeviceSn).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (deviceOrdDb *Device) DeviceInsert(m *model.Device) (int, error) {
- _, err := deviceOrdDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (deviceOrdDb *Device) DeviceInsertBySession(session *xorm.Session, m *model.Device) (int, error) {
- _, err := session.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (deviceOrdDb *Device) BatchAddDevices(mm []*model.Device) (int64, error) {
- affected, err := deviceOrdDb.Db.Insert(mm)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (deviceOrdDb *Device) DeviceDeleteById(id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.Device{})
- } else {
- return Db.Where("id = ?", id).Delete(model.Device{})
- }
- }
-
- func (deviceOrdDb *Device) DeviceDelete() (int64, error) {
- return Db.Where("device_sn = ?", deviceOrdDb.DeviceSn).Delete(model.Device{})
- }
-
- func (deviceOrdDb *Device) DeviceUpdate(m *model.Device, forceColums ...string) (int64, error) {
- var (
- affected int64
- err error
- )
- if forceColums != nil {
- affected, err = deviceOrdDb.Db.Where("device_sn=?", deviceOrdDb.DeviceSn).Cols(forceColums...).Update(m)
- } else {
- affected, err = deviceOrdDb.Db.Where("device_sn=?", deviceOrdDb.DeviceSn).Update(m)
- }
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- type DeviceWithEnterprise struct {
- model.Device `xorm:"extends"`
- model.Enterprise `xorm:"extends"`
- }
-
- func (DeviceWithEnterprise) TableName() string {
- return "device"
- }
|