|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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"
- }
|