在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例
Go  /  管理员 发布于 3个月前   232
该需求是实现一个商品搜索分页列表功能go + gin+ gorm实现实例
实例代码:
控制器
api/v1/spu.go
func GetStoreSearchSpu(c *gin.Context) {
param := req.GetListParam{}
err := c.ShouldBindJSON(¶m)
if err != nil {
resp.ERRWithMsg(c, resp.SPU_PARAM_ERR, err.Error())
return
}
param.IsHidden = api.Uint8(0)
s := service.NewSpuService()
list, count, err := s.GetListV3(¶m)
if err != nil {
resp.ERRWithMsg(c, resp.SPU_FIND_ERR, err.Error())
return
}
resp.OkWithData(resp.PageResult{Rows: list, Count: count}, c)
}
service
service/spu.go
func (s *SpuService) GetListV3(p *req.GetListParam) (list []*model.Spu, count int64, err error) {
p.Search = quote(p.Search)
list = make([]*model.Spu, 0)
limit, offset := getPageInfo(&p.PageInfo)
var cond string
//店铺id
cond = fmt.Sprintf("spus.store_id = '%s'", p.StoreId)
//颜色
if p.Color != "" {
cond = fmt.Sprintf("%s and EXISTS ( SELECT 1 FROM specs WHERE specs.spu_id = spus.id AND specs.title = 'Color' AND specs.spec_name = '%s')", cond, p.Color)
}
//size
if p.Size != "" {
cond = fmt.Sprintf("%s and EXISTS ( SELECT 1 FROM specs WHERE specs.spu_id = spus.id AND specs.title = 'Size' AND specs.spec_name = '%s')", cond, p.Size)
}
//price区间
if p.MinPrice >= 0 && p.MaxPrice > 0 {
cond = fmt.Sprintf("%s and spus.min_price BETWEEN %d AND %d", cond, p.MinPrice, p.MaxPrice)
}
if p.IsHidden != nil {
if cond != "" {
cond = fmt.Sprintf("%s and hidden = %d", cond, *p.IsHidden)
} else {
cond = fmt.Sprintf("hidden = %d", *p.IsHidden)
}
}
//fmt.Println("cond:", cond)
var SpuAlls = []string{"spus.id", "spus.currency", "spus.pv", "spus.created_at", "spus.updated_at", "spus.store_id", "spus.spu_name", "spus.img", "spus.video", "spus.gallery", "spus.detail_dawing", "spus.category_id1", "spus.category_id2", "spus.category_id3", "spus.brand_id", "spus.oringin", "spus.minimun", "spus.inc_minimun", "spus.volume", "spus.box_cbm", "spus.cbm", "spus.weight", "spus.packaging", "spus.graphic", "spus.logo", "spus.description", "spus.attributes", "spus.min_price", "spus.max_price", "spus.sales", "spus.comment_num", "spus.order_view", "spus.hidden"}
err = s.DB.Model(model.Spu{}).Where(cond).Count(&count).Error
if err != nil || count == 0 {
return
}
err = s.DB.Order("spus.created_at desc").Limit(limit).Offset(offset).Select(SpuAlls).Where(cond).Find(&list).Error
if err != nil {
return
}
err = getSpuListProp(list)
return
}
ps:
getSpuListProp: 这是一分翻译数据处理函数 , 这里不作分享
路由
g.POST("getStoreSearchSpu", v1.GetStoreSearchSpu)
效果图:
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号