智慧食堂
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.3 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 Device struct {
  9. Db *xorm.Engine `json:"db"`
  10. DeviceSn string `json:"device_sn"`
  11. }
  12. func (deviceOrdDb *Device) Set(deviceSn string) { // set方法
  13. deviceOrdDb.Db = Db
  14. deviceOrdDb.DeviceSn = deviceSn
  15. }
  16. func (deviceOrdDb *Device) GetDeviceById(id int) (m *model.Device, err error) {
  17. m = new(model.Device)
  18. has, err := deviceOrdDb.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 (deviceOrdDb *Device) GetDevice() (m *model.Device, err error) {
  28. m = new(model.Device)
  29. has, err := deviceOrdDb.Db.Where("device_sn =?", deviceOrdDb.DeviceSn).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 (deviceOrdDb *Device) DeviceInsert(m *model.Device) (int, error) {
  39. _, err := deviceOrdDb.Db.InsertOne(m)
  40. if err != nil {
  41. return 0, err
  42. }
  43. return m.Id, nil
  44. }
  45. func (deviceOrdDb *Device) DeviceInsertBySession(session *xorm.Session, m *model.Device) (int, error) {
  46. _, err := session.InsertOne(m)
  47. if err != nil {
  48. return 0, err
  49. }
  50. return m.Id, nil
  51. }
  52. func (deviceOrdDb *Device) BatchAddDevices(mm []*model.Device) (int64, error) {
  53. affected, err := deviceOrdDb.Db.Insert(mm)
  54. if err != nil {
  55. return 0, err
  56. }
  57. return affected, nil
  58. }
  59. func (deviceOrdDb *Device) DeviceDeleteById(id interface{}) (int64, error) {
  60. if reflect.TypeOf(id).Kind() == reflect.Slice {
  61. return Db.In("id", id).Delete(model.Device{})
  62. } else {
  63. return Db.Where("id = ?", id).Delete(model.Device{})
  64. }
  65. }
  66. func (deviceOrdDb *Device) DeviceDelete() (int64, error) {
  67. return Db.Where("device_sn = ?", deviceOrdDb.DeviceSn).Delete(model.Device{})
  68. }
  69. func (deviceOrdDb *Device) DeviceUpdate(m *model.Device, forceColums ...string) (int64, error) {
  70. var (
  71. affected int64
  72. err error
  73. )
  74. if forceColums != nil {
  75. affected, err = deviceOrdDb.Db.Where("device_sn=?", deviceOrdDb.DeviceSn).Cols(forceColums...).Update(m)
  76. } else {
  77. affected, err = deviceOrdDb.Db.Where("device_sn=?", deviceOrdDb.DeviceSn).Update(m)
  78. }
  79. if err != nil {
  80. return 0, err
  81. }
  82. return affected, nil
  83. }
  84. type DeviceWithEnterprise struct {
  85. model.Device `xorm:"extends"`
  86. model.Enterprise `xorm:"extends"`
  87. }
  88. func (DeviceWithEnterprise) TableName() string {
  89. return "device"
  90. }