package goods import ( "github.com/golangkit/formatime" "gorm.io/gorm/clause" "live/app/lib/db" ) type GoodsCar struct { db.BaseModel Id uint `gorm:"column:id" json:"id"` UserId uint `gorm:"column:user_id" json:"userId"` GoodsId uint `gorm:"column:goods_id" json:"goodsId"` CreatedAt formatime.Second `gorm:"column:created_at" json:"createdAt"` UpdatedAt formatime.Second `gorm:"column:updated_at" json:"updatedAt"` IsDel int `gorm:"column:is_del" json:"isDel"` } // 插入 func (g *GoodsCar) Create(goodsCar *GoodsCar) uint { g.GetDb().Create(goodsCar) if goodsCar.Id > 0 { return goodsCar.Id } return 0 } // 批量插入 忽略重复 func (g *GoodsCar) CreateAll(goodsCars *[]GoodsCar) int64 { if len(*goodsCars) == 0 { return 0 } return g.GetDb().Clauses(clause.OnConflict{DoNothing: true}).Create(goodsCars).RowsAffected } // @Title 通过会员id获取数据 // @Param userId uint true "会员id" func (g *GoodsCar) GetListByUserId(userId uint, start, limit int) *[]GoodsCar { if userId <= 0 { return &[]GoodsCar{} } goodsCars := []GoodsCar{} g.GetDb().Model(g).Where("user_id = ?", userId).Where("is_del = ?", 0).Limit(limit).Offset(start).Order("id desc").Find(&goodsCars) return &goodsCars } // 获取会员橱柜商品数量 func (g *GoodsCar) GetCountByUserId(userId uint) (result int64) { if userId <= 0 { return } g.GetDb().Model(g).Where("user_id = ?", userId).Where("is_del = ?", 0).Count(&result) return } // 会员删除橱柜商品 func (g *GoodsCar) DelByUserIdAndIds(userId uint, ids []int) int64 { if userId <= 0 || len(ids) == 0 { return 0 } return g.GetDb().Model(g).Where("user_id = ?", userId).Where("goods_id in (?) and is_del = ?", ids, 0).Update("is_del", 1).RowsAffected }