API模块
// HotSearchListReq 热搜列表请求参数
type HotSearchListReq struct {
g.Meta `path:"/HotSearchList" tags:"热搜模块" method:"post" summary:"热搜列表"`
*model.HotSearchListReq
commonApi.Author
}
// HotSearchListRes 热搜列表返回结果
type HotSearchListRes struct {
List []*model.HotSearchListRes `json:"list" dc:"集合"`
CurrentPage int `json:"currentPage" dc:"第几页"`
TotalCount int `json:"totalCount" dc:"总共条数"`
commonApi.EmptyRes
}
controller模块
// HotSearchList 热搜列表
func (c *sysPublishProductsController) HotSearchList(ctx context.Context, req *system.HotSearchListReq) (res *system.HotSearchListRes, err error) {
res, err = service.SysPublishProducts().HotSearchList(ctx, req)
return
}
Logic模块
// HotSearchList 热搜列表
func (s *sSysPublishProducts) HotSearchList(ctx context.Context, req *system.HotSearchListReq) (res *system.HotSearchListRes, err error) {
res = new(system.HotSearchListRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.HotGoods.Ctx(ctx).WithAll()
res.TotalCount, err = m.Count()
fmt.Println(res.TotalCount)
liberr.ErrIsNil(ctx, err, "获取总行数失败")
if req.Page == 0 {
req.Page = 1
}
res.CurrentPage = req.Page
if req.PageSize == 0 {
req.PageSize = consts.PageSize
}
order := "id desc"
var resData []*model.HotSearchListRes
err = m.Page(req.Page, req.PageSize).Order(order).Scan(&resData)
liberr.ErrIsNil(ctx, err, "获取数据失败")
res.List = make([]*model.HotSearchListRes, len(resData))
for k, v := range resData {
res.List[k] = &model.HotSearchListRes{
Id: v.Id,
CommonId: v.CommonId,
Sort: v.Sort,
Icon: v.Icon,
CommonIdData: v.CommonIdData,
}
}
})
return
}
Model模块
// HotSearchListReq 热搜列表请求参数
type HotSearchListReq struct {
Page int `p:"Page" dc:"第几页"`
PageSize int `p:"PageSize" dc:"一页多少条数据"`
}
// HotSearchListRes 热搜列表返回参数
type HotSearchListRes struct {
gmeta.Meta `orm:"table:hot_goods"`
Id int `orm:"id" json:"id" dc:"热搜ID"`
CommonId int `orm:"common_id" json:"commonId" dc:"商品id goods_common表"`
CommonIdData *ProductsListRes `orm:"with:common_id=commonId" json:"common_id_data" dc:"商品信息"`
Sort int `orm:"sort" json:"sort" dc:"排序"`
Icon int `orm:"icon" json:"icon" dc:"图标"`
}
// ProductsListRes 后台查询统一商品列表
type ProductsListRes struct {
gmeta.Meta `orm:"table:goods_common"`
CommonId int64 `orm:"common_id" json:"commonId" dc:"商品主表ID"`
GoodsName string `json:"goodsName" dc:"商品名称"`
CreatedAt *gtime.Time `json:"createdAt" dc:"创建时间"`
UpdatedAt *gtime.Time `json:"updatedAt" dc:"修改时间"`
}
结果
{
"code": 0,
"message": "",
"data": {
"list": [
{
"id": 54,
"commonId": 15,
"common_id_data": {
"commonId": 15,
"goodsName": "实物商品-多规格-零售-仅快递-小白鞋",
"createdAt": "2024-03-17 22:21:20",
"updatedAt": "2024-03-17 22:21:45"
},
"sort": 1,
"icon": 0
}
],
"currentPage": 1,
"totalCount": 1
}
}
service使用gf工具生成