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

99 line
2.1 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 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. }