面包店
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_with_sku.go 2.1 KiB

9 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type SchemeWithSkuDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (schemeWithSkuDb *SchemeWithSkuDb) Set() { // set方法
  12. schemeWithSkuDb.Db = Db
  13. }
  14. func (schemeWithSkuDb *SchemeWithSkuDb) FindSchemeWithScheme(schemeId int) (*[]model.SchemeWithSku, error) {
  15. var m []model.SchemeWithSku
  16. if err := schemeWithSkuDb.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 (schemeWithSkuDb *SchemeWithSkuDb) FindSchemeWithGoods(schemeId int, goodsId int64) (*[]model.SchemeWithSku, error) {
  22. var m []model.SchemeWithSku
  23. if err := schemeWithSkuDb.Db.Where("goods_id =?", goodsId).And("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
  24. return nil, logx.Error(err)
  25. }
  26. return &m, nil
  27. }
  28. func (schemeWithSkuDb *SchemeWithSkuDb) GetSchemeWithSku(schemeId int, skuId int64) (m *model.SchemeWithSku, err error) {
  29. m = new(model.SchemeWithSku)
  30. has, err := schemeWithSkuDb.Db.Where("scheme_id =?", schemeId).And("sku_id =?", skuId).Get(m)
  31. if err != nil {
  32. return nil, logx.Error(err)
  33. }
  34. if has == false {
  35. return nil, nil
  36. }
  37. return m, nil
  38. }
  39. func (schemeWithSkuDb *SchemeWithSkuDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  40. if reflect.TypeOf(id).Kind() == reflect.Slice {
  41. return session.In("id", id).Delete(model.SchemeWithSku{})
  42. } else {
  43. return session.Where("id = ?", id).Delete(model.SchemeWithSku{})
  44. }
  45. }
  46. func (schemeWithSkuDb *SchemeWithSkuDb) SchemeDeleteByGoods(schemeId int, goodsId int64) (int64, error) {
  47. return schemeWithSkuDb.Db.Where("scheme_id = ?", schemeId).And("goods_id =?", goodsId).Delete(model.SchemeWithSku{})
  48. }
  49. func (schemeWithSkuDb *SchemeWithSkuDb) SchemeWithSkuInsert(m *model.SchemeWithSku) (int, error) {
  50. _, err := schemeWithSkuDb.Db.InsertOne(m)
  51. if err != nil {
  52. return 0, err
  53. }
  54. return m.Id, nil
  55. }
  56. func (schemeWithSkuDb *SchemeWithSkuDb) BatchAddSchemeWithSku(mm []*model.SchemeWithSku) (int64, error) {
  57. affected, err := schemeWithSkuDb.Db.Insert(mm)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return affected, nil
  62. }