parent
57d4311eff
commit
3b48372ee5
@ -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
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in new issue