fusenapi/model/gmodel/fs_preprocess_logo_logic.go
2023-11-03 17:15:17 +08:00

103 lines
3.1 KiB
Go

package gmodel
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/zeromicro/go-zero/core/logx"
)
type PreLogoSearchResult struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // 自增的唯一id
RestaurantName *string `gorm:"index;default:'';" json:"restaurant_name"` //
ResourceUrl *string `gorm:"default:'';" json:"resource_url"` //
RestaurantType *string `gorm:"default:'';" json:"restaurant_type"` //
Address *string `gorm:"default:'';" json:"address"` //
ZipCode *string `gorm:"default:'';" json:"zip_code"` //
Phone *string `gorm:"default:'';" json:"phone"` //
Website *string `gorm:"default:'';" json:"website"` //
IsBranch *int64 `gorm:"default:0;" json:"is_branch"` // 是否分店
}
// TODO: 使用model的属性做你想做的
// 搜索
func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode string, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywordsList := SplitSearchKeywords(keywordsStr)
var keywords []string
for _, v := range keywordsList {
if len(v) > 1 {
keywords = append(keywords, "+"+v+"*")
}
}
if len(keywords) != 0 {
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) AND `is_del` = 0 AND `zip_code` = ? limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "), zipcode)
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
}
}
}
if resp == nil {
resp = make([]PreLogoSearchResult, 0)
}
return resp, nil
}
func SplitSearchKeywords(keywordsStr string) []string {
return regexp.MustCompile(`\s+|\++`).Split(keywordsStr, -1)
}
// 搜索建议
func (p *FsPreprocessLogoModel) PreLogoSearchSuggestions(ctx context.Context, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
// keywordsList := regexp.MustCompile(`\s+|\++`).Split(keywordsStr, -1)
keywordsList := SplitSearchKeywords(keywordsStr)
var keywords []string
for _, v := range keywordsList {
if len(v) > 1 {
keywords = append(keywords, "+"+v+"*")
}
}
if len(keywords) != 0 {
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) AND `is_del` = 0 limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "))
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
}
}
}
if resp == nil {
resp = make([]PreLogoSearchResult, 0)
}
return resp, nil
}