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

79 lines
1.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 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. }