在go語言中g(shù)in + gorm多表關(guān)聯(lián)實(shí)現(xiàn)一個(gè)店鋪商品搜索功能示例
Go  /  管理員 發(fā)布于 10個(gè)月前   489
跟上一篇一樣為了學(xué)習(xí)快速了解及跑通業(yè)務(wù)流程,快速開發(fā)實(shí)現(xiàn)功能。
有些拆分就不搞了,學(xué)習(xí)gorm多表關(guān)聯(lián)實(shí)現(xiàn)列表接口
環(huán)境啥的就不介紹了,跟之前一樣
go 1.18 + gin
接口測(cè)試apifox
進(jìn)入步驟:
加路由
g.POST("getXfSearchSpu", v1.GetXfSearchSpu)
添加分頁傳參
// 分頁列表
type GetListParam struct {
SortInfo
PageInfo
Type uint `json:"type"`
StoreId string `json:"storeId"`
UserId string `json:"userId"`
IsHidden *uint8 `json:"isHidden"`
Status *uint16 `json:"status"`
Search string `json:"search"`
CategoryId string `json:"categoryId"`
ColorId uint `json:"colorId"`
}
接口控制器
func GetXfSearchSpu(c *gin.Context) {
param := req.GetListParam{}
err := c.ShouldBindJSON(¶m)
if err != nil {
resp.ERRWithMsg(c, resp.SPU_PARAM_ERR, err.Error())
return
}
fmt.Println("param:", param)
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)
}
server
// 202405
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
//新增詳情頁搜索 搜索關(guān)鍵詞及店鋪id
if p.StoreId != "" && p.Search != "" {
cond = fmt.Sprintf("spus.store_id = '%s' and spus.spu_name like '%s'", p.StoreId, "%"+p.Search+"%")
} else {
cond = fmt.Sprintf("spus.store_id = '%s'", p.StoreId)
}
//顏色
if p.ColorId != 0 {
cond = fmt.Sprintf("sss.color_id = %d", p.ColorId)
}
if p.IsHidden != nil {
if cond != "" {
cond = fmt.Sprintf("%s and hidden = %d", cond, *p.IsHidden)
} else {
cond = fmt.Sprintf("hidden = %d", *p.IsHidden)
}
}
//字段
var SpuAlls = []string{"spus.id","spus.created_at", "spus.updated_at","spus.hidden"}
err = s.DB.Model(model.Spu{}).Joins("INNER JOIN skus ON spus.id = skus.spu_id").Joins("INNER JOIN spu_spec_skus AS sss ON spus.id = sss.spu_id ").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).Joins("INNER JOIN skus ON spus.id = skus.spu_id").Joins("INNER JOIN spu_spec_skus AS sss ON spus.id = sss.spu_id ").Where(cond).Find(&list).Error
if err != nil {
return
}
err = getSpuListProp(list)
return
}
// 獲取列表下的數(shù)據(jù)處理
func getSpuListProp(list []*model.Spu) (err error) {
categoryService := NewCategoryService()
specService := NewSpecService()
for i := range list {
list[i].ToObj()
// 獲取分類
var categoryList []model.Category
var specParam *model.SpecParam
list[i].ToCategoryIdList()
categoryList, err = categoryService.GetJustListByIdList(list[i].CategoryIdList)
if err != nil {
return
}
list[i].CategoryList = categoryList
// 獲取規(guī)格
specParam, err = specService.GetUseOfListBySpuId(list[i].ID)
if err != nil {
return
}
list[i].SpecParam = specParam
// 獲取翻譯
GetTranslationListBySpu(list[i])
}
return
}
效果
123 在
Clash for Windows作者刪庫跑路了,github已404中評(píng)論 按理說只要你在國內(nèi),所有的流量進(jìn)出都在監(jiān)控范圍內(nèi),不管你怎么隱藏也沒用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最簡單的方法中評(píng)論 好久好久都沒看友情鏈接申請(qǐng)了,今天剛看,已經(jīng)添加。..博主 在
佛跳墻vpn軟件不會(huì)用?上不了網(wǎng)?佛跳墻vpn常見問題以及解決辦法中評(píng)論 @1111老鐵這個(gè)不行了,可以看看近期評(píng)論的其他文章..1111 在
佛跳墻vpn軟件不會(huì)用?上不了網(wǎng)?佛跳墻vpn常見問題以及解決辦法中評(píng)論 網(wǎng)站不能打開,博主百忙中能否發(fā)個(gè)APP下載鏈接,佛跳墻或極光..路人 在
php中使用hyperf框架調(diào)用訊飛星火大模型實(shí)現(xiàn)國內(nèi)版chatgpt功能示例中評(píng)論 教程很詳細(xì),如果加個(gè)前端chatgpt對(duì)話頁面就完美了..
Copyright·? 2019 侯體宗版權(quán)所有·
粵ICP備20027696號(hào)