|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
-
- "xorm.io/xorm"
- )
-
- // GetUserFav is 获取商品收藏
- func GetUserFav(Db *xorm.Engine, uid, itemid interface{}) (*model.UserFavorite, error) {
- var m model.UserFavorite
- if has, err := Db.Where("uid = ? AND item_id = ?", uid, itemid).Get(&m); err != nil || !has {
- return nil, logx.Warn(err)
- }
- return &m, nil
- }
-
- //InsertUserFavOne is 插入一条收藏记录
- func InsertUserFavOne(Db *xorm.Engine, m *model.UserFavorite) (int64, error) {
- affected, err := Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- //UserFavFindByProvider 根据提供商来筛选收藏的
- func UserFavFindByProvider(Db *xorm.Engine, uid interface{}, provider string, limit, start int) ([]*model.UserFavorite, error) {
- var m []*model.UserFavorite
- var err error
- if provider == "all" {
- if limit == 0 && start == 0 {
- err = Db.Where("uid = ?", uid).Desc("create_at").Find(&m)
- } else {
- err = Db.Where("uid = ?", uid).Desc("create_at").Limit(limit, start).Find(&m)
- }
- } else {
- if limit == 0 && start == 0 {
- err = Db.Where("uid = ? AND provider = ?", uid, provider).Desc("create_at").Find(&m)
- } else {
- err = Db.Where("uid = ? AND provider = ?", uid, provider).Desc("create_at").Limit(limit, start).Find(&m)
- }
-
- }
- if err != nil {
- return nil, logx.Warn(err)
- }
- return m, nil
- }
-
- //UserFavFindSearchByTitle is 我的收藏搜索
- func UserFavFindSearchByTitle(Db *xorm.Engine, uid interface{}, title string, limit, start int) ([]*model.UserFavorite, error) {
- var m []*model.UserFavorite
- var err error
- if limit == 0 && start == 0 {
- err = Db.Where("item_title like ?", "%"+title+"%").Desc("create_at").Find(&m)
- } else {
- err = Db.Where("item_title like ?", "%"+title+"%").Desc("create_at").Limit(limit, start).Find(&m)
- }
- if err != nil {
- return nil, logx.Warn(err)
- }
- return m, nil
- }
-
- // UserFavDelete is 删除收藏夹
- func UserFavDelete(Db *xorm.Engine, uid interface{}, itemIds []interface{}) (int64, error) {
- var m model.UserFavorite
- affect, err := Db.Where("uid = ?", uid).In("item_id", itemIds).Delete(&m)
- if err != nil {
- return 0, logx.Warn(err)
- }
- return affect, nil
- }
-
- // UserFavDeleteByUserDelete is 删除用户时删除对应的收藏夹
- func UserFavDeleteByUserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
- var m model.UserFavorite
- affect, err := Db.Where("uid = ?", uid).Delete(&m)
- if err != nil {
- return 0, logx.Warn(err)
- }
- return affect, nil
- }
|