面包店
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_scheme.go 1.5 KiB

9 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type SchemeDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (schemeDb *SchemeDb) Set() { // set方法
  12. schemeDb.Db = Db
  13. }
  14. func (schemeDb *SchemeDb) FindScheme() (*[]model.Scheme, error) {
  15. var m []model.Scheme
  16. if err := schemeDb.Db.Desc("id").Find(&m); err != nil {
  17. return nil, logx.Error(err)
  18. }
  19. return &m, nil
  20. }
  21. func (schemeDb *SchemeDb) GetScheme(id int) (m *model.Scheme, err error) {
  22. m = new(model.Scheme)
  23. has, err := schemeDb.Db.Where("id =?", id).Get(m)
  24. if err != nil {
  25. return nil, logx.Error(err)
  26. }
  27. if has == false {
  28. return nil, nil
  29. }
  30. return m, nil
  31. }
  32. func (schemeDb *SchemeDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  33. if reflect.TypeOf(id).Kind() == reflect.Slice {
  34. return session.In("id", id).Delete(model.Scheme{})
  35. } else {
  36. return session.Where("id = ?", id).Delete(model.Scheme{})
  37. }
  38. }
  39. func (schemeDb *SchemeDb) BatchAddSchemeBySession(session *xorm.Session, mm []*model.Scheme) (int64, error) {
  40. affected, err := session.Insert(mm)
  41. if err != nil {
  42. return 0, err
  43. }
  44. return affected, nil
  45. }
  46. func (schemeDb *SchemeDb) SchemeInsert(m *model.Scheme) (int, error) {
  47. _, err := schemeDb.Db.InsertOne(m)
  48. if err != nil {
  49. return 0, err
  50. }
  51. return m.Id, nil
  52. }
  53. func (schemeDb *SchemeDb) UpdateScheme(m *model.Scheme, columns ...string) (int64, error) {
  54. affected, err := schemeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  55. if err != nil {
  56. return 0, err
  57. }
  58. return affected, nil
  59. }