面包店

db_scheme_with_goods.go 1.4 KiB

před 8 měsíci
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type SchemeWithGoodsDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (schemeWithGoodsDb *SchemeWithGoodsDb) Set() { // set方法
  12. schemeWithGoodsDb.Db = Db
  13. }
  14. func (schemeWithGoodsDb *SchemeWithGoodsDb) FindSchemeWithGoods(schemeId int) (*[]model.SchemeWithGoods, error) {
  15. var m []model.SchemeWithGoods
  16. if err := schemeWithGoodsDb.Db.Where("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
  17. return nil, logx.Error(err)
  18. }
  19. return &m, nil
  20. }
  21. func (schemeWithGoodsDb *SchemeWithGoodsDb) GetSchemeWithGoods(schemeId int, goodsId int64) (m *model.SchemeWithGoods, err error) {
  22. m = new(model.SchemeWithGoods)
  23. has, err := schemeWithGoodsDb.Db.Where("scheme_id =?", schemeId).And("goods_id =?", goodsId).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 (schemeWithGoodsDb *SchemeWithGoodsDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  33. if reflect.TypeOf(id).Kind() == reflect.Slice {
  34. return session.In("id", id).Delete(model.SchemeWithGoods{})
  35. } else {
  36. return session.Where("id = ?", id).Delete(model.SchemeWithGoods{})
  37. }
  38. }
  39. func (schemeWithGoodsDb *SchemeWithGoodsDb) SchemeWithGoodsInsert(m *model.SchemeWithGoods) (int, error) {
  40. _, err := schemeWithGoodsDb.Db.InsertOne(m)
  41. if err != nil {
  42. return 0, err
  43. }
  44. return m.Id, nil
  45. }