diff --git a/app/controller/goods/car.go b/app/controller/goods/car.go new file mode 100644 index 0000000..f5e2d33 --- /dev/null +++ b/app/controller/goods/car.go @@ -0,0 +1,93 @@ +package goods + +import ( + "github.com/gin-gonic/gin" + "live/app/common" + "live/app/lib" + "live/app/lib/back" + "live/app/lib/tools" + "live/app/logic/goods" +) + +type Car struct { +} + +// 橱柜列表请求参数 +type argsList struct { + lib.Page +} + +// @Title 获取直播车列表 +// @Param keyword string false "检索关键字" +// @Param page int false "页数 默认1" +// @Param limit int false "分页大小" +func (car *Car) List(c *gin.Context) { + uid := common.GetUserId(c) + if uid == 0 { + back.Fail(c, "未登录") + return + } + args := argsList{} + if err := tools.ParseParams(&args, c); err != nil { + back.Fail(c, err.Error()) + return + } + list, count := (&goods.Car{}).GetList(uid, args.Page) + + back.Suc(c, "操作成功", gin.H{ + "list": list, + "total": count, + }) +} + +type argsDel struct { + Ids []int `json:"ids" form:"ids"` +} + +// @Title 删除直播车商品 +// @Param Ids []int true "删除橱柜商品id数组" +func (car *Car) Delete(c *gin.Context) { + uid := common.GetUserId(c) + argsDel := argsDel{} + if err := tools.ParseParams(&argsDel, c); err != nil { + back.Fail(c, err.Error()) + return + } + if len(argsDel.Ids) == 0 { + back.Fail(c, "参数不全") + return + } + if (&goods.Car{}).Delete(uid, argsDel.Ids) { + back.Suc(c, "操作成功", "") + return + } else { + back.Fail(c, "操作失败") + return + } +} + +type argsAdd struct { + GoodsIds []uint `json:"goodsIds" form:"goodsIds"` +} + +// @Title 添加直播车商品 +// @Param Ids []int true "添加直播车商品id数组" +func (car *Car) Add(c *gin.Context) { + uid := common.GetUserId(c) + args := argsAdd{} + if err := tools.ParseParams(&args, c); err != nil { + back.Fail(c, err.Error()) + return + } + if len(args.GoodsIds) == 0 { + back.Fail(c, "参数不全") + return + } + if (&goods.Car{}).Add(uid, args.GoodsIds) > 0 { + back.Suc(c, "操作成功", "") + return + } else { + back.Fail(c, "操作失败") + return + } +} diff --git a/app/controller/goods/cupboard.go b/app/controller/goods/cupboard.go index ece214c..ba3764e 100644 --- a/app/controller/goods/cupboard.go +++ b/app/controller/goods/cupboard.go @@ -7,7 +7,6 @@ import ( "live/app/lib/back" "live/app/lib/tools" "live/app/logic/goods" - "log" ) type Cupboard struct { @@ -49,10 +48,6 @@ type ArgsDel struct { // @Param Ids []int true "删除橱柜商品id数组" func (cup *Cupboard) Delete(c *gin.Context) { uid := common.GetUserId(c) - if uid == 0 { - back.Fail(c, "未登录") - return - } argsDel := ArgsDel{} if err := tools.ParseParams(&argsDel, c); err != nil { back.Fail(c, err.Error()) @@ -62,7 +57,6 @@ func (cup *Cupboard) Delete(c *gin.Context) { back.Fail(c, "参数不全") return } - log.Println(argsDel.Ids) if (&goods.Cupboard{}).Delete(uid, argsDel.Ids) { back.Suc(c, "操作成功", "") return diff --git a/app/controller/topic/topic.go b/app/controller/topic/topic.go index 8c39eff..ff11e45 100644 --- a/app/controller/topic/topic.go +++ b/app/controller/topic/topic.go @@ -48,11 +48,12 @@ func (t *Topic) Add(c *gin.Context) { back.Fail(c, err.Error()) return } + userId := common.GetUserId(c) if args.Title == "" { back.Fail(c, "参数错误") return } - topicId, err := (&topic.Topic{}).Add(args.Title) + topicId, err := (&topic.Topic{}).Add(userId, args.Title) if err != nil { back.Fail(c, err.Error()) return diff --git a/app/logic/goods/car.go b/app/logic/goods/car.go new file mode 100644 index 0000000..337afaf --- /dev/null +++ b/app/logic/goods/car.go @@ -0,0 +1,62 @@ +package goods + +import ( + "live/app/lib" + "live/app/lib/recook" + "live/app/model/goods" +) + +type Car struct { +} + +// @Title 获取会员直播车列表 +// @Param uid uint true "会员id" +// @Param listArgs Listargs true "筛选分页参数" +func (c *Car) GetList(userId uint, page lib.Page) (result *[]recook.GoodsInfo, count int64) { + goodsCarModel := &goods.GoodsCar{} + result = &[]recook.GoodsInfo{} + + start := (page.GetPage() - 1) * page.GetLimit() + count = goodsCarModel.GetCountByUserId(userId) + if count <= int64(start) { + // 没有数据,不需要查库 + return + } + goodsWindows := goodsCarModel.GetListByUserId(userId, start, page.GetLimit()) + if len(*goodsWindows) == 0 { + // 未查询到数据 + return + } + goodsIds := []uint{} + for _, goodsWindow := range *goodsWindows { + goodsIds = append(goodsIds, goodsWindow.GoodsId) + } + goodsInfo, err := recook.Goods.GetListByIds(goodsIds) + if err != nil { + return + } + return goodsInfo, count +} + +// @Title 根据ids批量删除会员直播车数据 +func (c *Car) Delete(uid uint, ids []int) (err bool) { + upRow := (&goods.GoodsCar{}).DelByUserIdAndIds(uid, ids) + if upRow > 0 { + return true + } else { + return false + } +} + +// @Title 添加会员直播车数据 +// @Param uid int true "会员id" +func (c *Car) Add(userId uint, goodsIds []uint) int64 { + data := []goods.GoodsCar{} + for _, goodsId := range goodsIds { + data = append(data, goods.GoodsCar{ + UserId: userId, + GoodsId: goodsId, + }) + } + return (&goods.GoodsCar{}).CreateAll(&data) +} diff --git a/app/logic/short/short.go b/app/logic/short/short.go index ecb53a9..ea3d2ec 100644 --- a/app/logic/short/short.go +++ b/app/logic/short/short.go @@ -71,6 +71,7 @@ type TopicInfoItem struct { MediaUrl string `json:"mediaUrl"` TopicId uint `json:"topicId"` TopicName string `json:"topicName"` + IsFollow int `json:"isFollow"` } func (s *Short) List(userId uint, page lib.Page) (result *[]TopicInfoItem, count int64, err error) { @@ -142,10 +143,12 @@ func (s *Short) formtList(userId uint, lists []short.Short, result *[]TopicInfoI // 获取会员动态信息 trendLists := recook.Trend.Infos(shortIds, user.TREND_TYPE_short) trendIds := []uint{} + trendUserIds := []uint{} trendMap := map[uint]recook.TrendInfo{} for _, trendInfo := range *trendLists { trendIds = append(trendIds, trendInfo.Id) trendMap[trendInfo.OriginId] = trendInfo + trendUserIds = append(trendUserIds, trendInfo.UserId) } // 获取商品信息 @@ -169,12 +172,26 @@ func (s *Short) formtList(userId uint, lists []short.Short, result *[]TopicInfoI } } + // 当前会员是否关注过当前列表的数据 + userFollowMap := map[uint]user2.UserFollow{} + if userId > 0 { + userFollow := &user2.UserFollow{} + userFollows := userFollow.GetByUserIdAndFollowIds(userId, trendUserIds) + for _, follow := range userFollows { + userFollowMap[follow.FollowUserId] = follow + } + } + for _, list := range lists { trendInfo := trendMap[list.Id] hasPraise := 0 if _, ok := userPraiseMap[trendInfo.Id]; ok { hasPraise = 1 } + hasFollow := 0 + if _, ok := userFollowMap[trendInfo.UserId]; ok { + hasFollow = 1 + } *result = append(*result, TopicInfoItem{ ReplyUserItem: userMap[list.UserId], Content: list.Content, @@ -184,6 +201,7 @@ func (s *Short) formtList(userId uint, lists []short.Short, result *[]TopicInfoI TrendType: trendInfo.TrendType, Praise: userTrendDataMap[trendInfo.Id].Praise, IsPraise: hasPraise, + IsFollow: hasFollow, Goods: goodsInfos[list.GoodsId], MediaUrl: list.MediaUrl, TopicId: list.TopicId, diff --git a/app/logic/topic/topic.go b/app/logic/topic/topic.go index aca9f20..ab826b1 100644 --- a/app/logic/topic/topic.go +++ b/app/logic/topic/topic.go @@ -27,7 +27,7 @@ func (t *Topic) List(keyword string, page lib.Page) (result *[]topic.Topic, coun } // @Title 添加话题 -func (t *Topic) Add(title string) (uint, error) { +func (t *Topic) Add(userId uint, title string) (uint, error) { topicModel := &topic.Topic{} topicInfo := topicModel.GetByTitle(title) if topicInfo.Id > 0 { @@ -47,6 +47,7 @@ func (t *Topic) Add(title string) (uint, error) { if data.Id <= 0 { return 0, errors.New("网络异常") } + (&Follow{}).Add(userId, data.Id) return data.Id, nil } diff --git a/app/model/goods/goodsCar.go b/app/model/goods/goodsCar.go new file mode 100644 index 0000000..0575f72 --- /dev/null +++ b/app/model/goods/goodsCar.go @@ -0,0 +1,62 @@ +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 +} diff --git a/app/model/goods/goodsWindow.go b/app/model/goods/goodsWindow.go index 546cf6a..08224bc 100644 --- a/app/model/goods/goodsWindow.go +++ b/app/model/goods/goodsWindow.go @@ -40,7 +40,7 @@ func (g *GoodsWindow) GetListByUserId(userId uint, start, limit int) *[]GoodsWin return &[]GoodsWindow{} } goodsWindows := []GoodsWindow{} - g.GetDb().Model(g).Where(GoodsWindow{UserId: userId}).Where("is_del = ?", 0).Limit(limit).Offset(start).Order("id desc").Find(&goodsWindows) + g.GetDb().Model(g).Where("user_id = ?", userId).Where("is_del = ?", 0).Limit(limit).Offset(start).Order("id desc").Find(&goodsWindows) return &goodsWindows } @@ -49,7 +49,7 @@ func (g *GoodsWindow) GetCountByUserId(userId uint) (result int64) { if userId <= 0 { return } - g.GetDb().Model(g).Where(GoodsWindow{UserId: userId}).Where("is_del = ?", 0).Count(&result) + g.GetDb().Model(g).Where("user_id = ?", userId).Where("is_del = ?", 0).Count(&result) return } @@ -58,5 +58,5 @@ func (g *GoodsWindow) DelByUserIdAndIds(userId uint, ids []int) int64 { if userId <= 0 || len(ids) == 0 { return 0 } - return g.GetDb().Model(g).Where(GoodsWindow{UserId: userId}).Where("id in (?) and is_del = ?", ids, 0).Update("is_del", 1).RowsAffected + return g.GetDb().Model(g).Where("user_id = ?", userId).Where("goods_id in (?) and is_del = ?", ids, 0).Update("is_del", 1).RowsAffected } diff --git a/app/router/router.go b/app/router/router.go index 69719fd..f18935a 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -34,6 +34,15 @@ func Router(router *gin.Engine) { cupboardR.POST("list", cupboard.List) // 会员橱柜列表 cupboardR.POST("delete", cupboard.Delete) // 会员橱柜商品删除 } + // 直播车 + carR := liveRouter.Group("/car") + carR.Use(auth) + { + car := &goods.Car{} + carR.POST("list", car.List) // 会员直播车列表 + carR.POST("add", car.Add) // 会员直播车商品添加 + carR.POST("delete", car.Delete) // 会员直播车商品删除 + } // 商品 goodsR := liveRouter.Group("/goods") {