智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.7 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type FactoryDevice struct {
  9. Db *xorm.Engine `json:"db"`
  10. TerminalId string `json:"terminal_id"`
  11. }
  12. func (factoryDeviceDb *FactoryDevice) Set(terminalId string) { // set方法
  13. factoryDeviceDb.Db = Db
  14. factoryDeviceDb.TerminalId = terminalId
  15. }
  16. func (factoryDeviceDb *FactoryDevice) GetFactoryDeviceById(id int) (m *model.FactoryDevice, err error) {
  17. m = new(model.FactoryDevice)
  18. has, err := factoryDeviceDb.Db.Where("id =?", id).Get(m)
  19. if err != nil {
  20. return nil, logx.Error(err)
  21. }
  22. if has == false {
  23. return nil, nil
  24. }
  25. return m, nil
  26. }
  27. func (factoryDeviceDb *FactoryDevice) GetFactoryDevice() (m *model.FactoryDevice, err error) {
  28. m = new(model.FactoryDevice)
  29. has, err := factoryDeviceDb.Db.Where("terminal_id =?", factoryDeviceDb.TerminalId).Get(m)
  30. if err != nil {
  31. return nil, logx.Error(err)
  32. }
  33. if has == false {
  34. return nil, nil
  35. }
  36. return m, nil
  37. }
  38. func (factoryDeviceDb *FactoryDevice) FactoryDeviceInsert(m *model.FactoryDevice) (int, error) {
  39. _, err := factoryDeviceDb.Db.InsertOne(m)
  40. if err != nil {
  41. return 0, err
  42. }
  43. return m.Id, nil
  44. }
  45. func (factoryDeviceDb *FactoryDevice) FactoryDeviceInsertBySession(session *xorm.Session, m *model.FactoryDevice) (int, error) {
  46. _, err := session.InsertOne(m)
  47. if err != nil {
  48. return 0, err
  49. }
  50. return m.Id, nil
  51. }
  52. func (factoryDeviceDb *FactoryDevice) BatchAddFactoryDevices(mm []*model.FactoryDevice) (int64, error) {
  53. affected, err := factoryDeviceDb.Db.Insert(mm)
  54. if err != nil {
  55. return 0, err
  56. }
  57. return affected, nil
  58. }
  59. func (factoryDeviceDb *FactoryDevice) FactoryDeviceDeleteById(id interface{}) (int64, error) {
  60. if reflect.TypeOf(id).Kind() == reflect.Slice {
  61. return Db.In("id", id).Delete(model.FactoryDevice{})
  62. } else {
  63. return Db.Where("id = ?", id).Delete(model.FactoryDevice{})
  64. }
  65. }
  66. func (factoryDeviceDb *FactoryDevice) FactoryDeviceDelete() (int64, error) {
  67. return Db.Where("terminal_id = ?", factoryDeviceDb.TerminalId).Delete(model.FactoryDevice{})
  68. }
  69. func (factoryDeviceDb *FactoryDevice) FactoryDeviceUpdate(m *model.FactoryDevice, forceColums ...string) (int64, error) {
  70. var (
  71. affected int64
  72. err error
  73. )
  74. if forceColums != nil {
  75. affected, err = factoryDeviceDb.Db.Where("terminal_id=?", factoryDeviceDb.TerminalId).Cols(forceColums...).Update(m)
  76. } else {
  77. affected, err = factoryDeviceDb.Db.Where("terminal_id=?", factoryDeviceDb.TerminalId).Update(m)
  78. }
  79. if err != nil {
  80. return 0, err
  81. }
  82. return affected, nil
  83. }
  84. type FactoryDeviceWithEnterprise struct {
  85. model.FactoryDevice `xorm:"extends"`
  86. model.Enterprise `xorm:"extends"`
  87. }
  88. func (FactoryDeviceWithEnterprise) TableName() string {
  89. return "factory_device"
  90. }