面包店
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.

db_staff.go 2.1 KiB

9 months ago
9 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type StaffDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (staffDb *StaffDb) Set() { // set方法
  12. staffDb.Db = Db
  13. }
  14. func (staffDb *StaffDb) GetStaff(id int) (m *model.Staff, err error) {
  15. m = new(model.Staff)
  16. has, err := staffDb.Db.Where("id =?", id).Get(m)
  17. if err != nil {
  18. return nil, logx.Error(err)
  19. }
  20. if has == false {
  21. return nil, nil
  22. }
  23. return m, nil
  24. }
  25. func (staffDb *StaffDb) FindStaff(limit, start int) (*[]model.Staff, error) {
  26. var m []model.Staff
  27. if limit == 0 || start == 0 {
  28. if err := staffDb.Db.Desc("id").Find(&m); err != nil {
  29. return nil, logx.Error(err)
  30. }
  31. } else {
  32. if err := staffDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
  33. return nil, logx.Error(err)
  34. }
  35. }
  36. return &m, nil
  37. }
  38. func (staffDb *StaffDb) GetStaffByName(name string) (m *model.Staff, err error) {
  39. m = new(model.Staff)
  40. has, err := staffDb.Db.Where("name =?", name).Get(m)
  41. if err != nil {
  42. return nil, logx.Error(err)
  43. }
  44. if has == false {
  45. return nil, nil
  46. }
  47. return m, nil
  48. }
  49. func (staffDb *StaffDb) StaffInsert(m *model.Staff) (int, error) {
  50. _, err := staffDb.Db.InsertOne(m)
  51. if err != nil {
  52. return 0, err
  53. }
  54. return m.Id, nil
  55. }
  56. func (staffDb *StaffDb) StaffUpdate(m *model.Staff, columns ...string) (int64, error) {
  57. affected, err := staffDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return affected, nil
  62. }
  63. func (staffDb *StaffDb) StaffDelete(id interface{}) (int64, error) {
  64. if reflect.TypeOf(id).Kind() == reflect.Slice {
  65. return Db.In("id", id).Delete(model.Staff{})
  66. } else {
  67. return Db.Where("id = ?", id).Delete(model.Staff{})
  68. }
  69. }
  70. func GetStaff(eg *xorm.Engine) *[]model.Staff {
  71. var data []model.Staff
  72. err := eg.Where("1=1").Find(&data)
  73. if err != nil {
  74. return nil
  75. }
  76. return &data
  77. }
  78. func GetStaffMore(eg *xorm.Engine, ids []int) map[int]model.Staff {
  79. data := make(map[int]model.Staff)
  80. if len(ids) > 0 {
  81. list := make([]model.Staff, 0)
  82. eg.In("id", ids).Find(&list)
  83. for _, v := range list {
  84. data[v.Id] = v
  85. }
  86. }
  87. return data
  88. }