|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type SchemeWithSkuDb struct {
- Db *xorm.Engine `json:"db"`
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) Set() { // set方法
- schemeWithSkuDb.Db = Db
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) FindSchemeWithScheme(schemeId int) (*[]model.SchemeWithSku, error) {
- var m []model.SchemeWithSku
- if err := schemeWithSkuDb.Db.Where("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) FindSchemeWithGoods(schemeId int, goodsId int64) (*[]model.SchemeWithSku, error) {
- var m []model.SchemeWithSku
- if err := schemeWithSkuDb.Db.Where("goods_id =?", goodsId).And("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) GetSchemeWithSku(schemeId int, skuId int64) (m *model.SchemeWithSku, err error) {
- m = new(model.SchemeWithSku)
- has, err := schemeWithSkuDb.Db.Where("scheme_id =?", schemeId).And("sku_id =?", skuId).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return session.In("id", id).Delete(model.SchemeWithSku{})
- } else {
- return session.Where("id = ?", id).Delete(model.SchemeWithSku{})
- }
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) SchemeDeleteByGoods(schemeId int, goodsId int64) (int64, error) {
- return schemeWithSkuDb.Db.Where("scheme_id = ?", schemeId).And("goods_id =?", goodsId).Delete(model.SchemeWithSku{})
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) SchemeWithSkuInsert(m *model.SchemeWithSku) (int, error) {
- _, err := schemeWithSkuDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (schemeWithSkuDb *SchemeWithSkuDb) BatchAddSchemeWithSku(mm []*model.SchemeWithSku) (int64, error) {
- affected, err := schemeWithSkuDb.Db.Insert(mm)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
|