|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type FactoryDevice struct {
- Db *xorm.Engine `json:"db"`
- TerminalId string `json:"terminal_id"`
- }
-
- func (factoryDeviceDb *FactoryDevice) Set(terminalId string) { // set方法
- factoryDeviceDb.Db = Db
- factoryDeviceDb.TerminalId = terminalId
- }
-
- func (factoryDeviceDb *FactoryDevice) GetFactoryDeviceById(id int) (m *model.FactoryDevice, err error) {
- m = new(model.FactoryDevice)
- has, err := factoryDeviceDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (factoryDeviceDb *FactoryDevice) GetFactoryDevice() (m *model.FactoryDevice, err error) {
- m = new(model.FactoryDevice)
- has, err := factoryDeviceDb.Db.Where("terminal_id =?", factoryDeviceDb.TerminalId).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (factoryDeviceDb *FactoryDevice) FactoryDeviceInsert(m *model.FactoryDevice) (int, error) {
- _, err := factoryDeviceDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (factoryDeviceDb *FactoryDevice) FactoryDeviceInsertBySession(session *xorm.Session, m *model.FactoryDevice) (int, error) {
- _, err := session.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (factoryDeviceDb *FactoryDevice) BatchAddFactoryDevices(mm []*model.FactoryDevice) (int64, error) {
- affected, err := factoryDeviceDb.Db.Insert(mm)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (factoryDeviceDb *FactoryDevice) FactoryDeviceDeleteById(id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.FactoryDevice{})
- } else {
- return Db.Where("id = ?", id).Delete(model.FactoryDevice{})
- }
- }
-
- func (factoryDeviceDb *FactoryDevice) FactoryDeviceDelete() (int64, error) {
- return Db.Where("terminal_id = ?", factoryDeviceDb.TerminalId).Delete(model.FactoryDevice{})
- }
-
- func (factoryDeviceDb *FactoryDevice) FactoryDeviceUpdate(m *model.FactoryDevice, forceColums ...string) (int64, error) {
- var (
- affected int64
- err error
- )
- if forceColums != nil {
- affected, err = factoryDeviceDb.Db.Where("terminal_id=?", factoryDeviceDb.TerminalId).Cols(forceColums...).Update(m)
- } else {
- affected, err = factoryDeviceDb.Db.Where("terminal_id=?", factoryDeviceDb.TerminalId).Update(m)
- }
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- type FactoryDeviceWithEnterprise struct {
- model.FactoryDevice `xorm:"extends"`
- model.Enterprise `xorm:"extends"`
- }
-
- func (FactoryDeviceWithEnterprise) TableName() string {
- return "factory_device"
- }
|