2023-10-20 05:25:35 +00:00
package gmodel
import (
"context"
2023-10-20 06:23:13 +00:00
2023-10-20 05:25:35 +00:00
"fmt"
"regexp"
"strings"
2023-10-20 06:08:58 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-10-20 05:25:35 +00:00
)
2023-10-20 06:34:16 +00:00
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" ` // 是否分店
}
2023-10-20 05:25:35 +00:00
// TODO: 使用model的属性做你想做的
2023-10-20 07:59:03 +00:00
// 搜索
2023-10-20 06:34:16 +00:00
func ( p * FsPreprocessLogoModel ) PreLogoSearch ( ctx context . Context , zipcode string , keywordsStr string , count int ) ( resp [ ] PreLogoSearchResult , err error ) {
2023-10-25 02:23:21 +00:00
keywordsList := SplitSearchKeywords ( keywordsStr )
2023-10-25 02:15:15 +00:00
var keywords [ ] string
for _ , v := range keywordsList {
if len ( v ) > 1 {
keywords = append ( keywords , "+" + v + "*" )
}
2023-10-20 07:59:03 +00:00
}
2023-10-25 02:15:15 +00:00
if len ( keywords ) != 0 {
2023-10-20 07:59:03 +00:00
2023-10-25 02:15:15 +00:00
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 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 ( "" )
}
2023-10-20 07:59:03 +00:00
}
}
2023-10-20 08:05:12 +00:00
if resp == nil {
resp = make ( [ ] PreLogoSearchResult , 0 )
}
2023-10-20 07:59:03 +00:00
return resp , nil
}
2023-10-25 02:23:21 +00:00
func SplitSearchKeywords ( keywordsStr string ) [ ] string {
return regexp . MustCompile ( ` \s+|\++ ` ) . Split ( keywordsStr , - 1 )
}
2023-10-20 07:59:03 +00:00
// 搜索建议
func ( p * FsPreprocessLogoModel ) PreLogoSearchSuggestions ( ctx context . Context , keywordsStr string , count int ) ( resp [ ] PreLogoSearchResult , err error ) {
2023-10-25 02:23:21 +00:00
// keywordsList := regexp.MustCompile(`\s+|\++`).Split(keywordsStr, -1)
keywordsList := SplitSearchKeywords ( keywordsStr )
2023-10-25 02:15:15 +00:00
var keywords [ ] string
for _ , v := range keywordsList {
if len ( v ) > 1 {
keywords = append ( keywords , "+" + v + "*" )
}
2023-10-20 05:25:35 +00:00
}
2023-10-25 02:15:15 +00:00
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) 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 ( "" )
}
2023-10-20 06:37:00 +00:00
}
2023-10-25 02:15:15 +00:00
2023-10-20 06:23:13 +00:00
}
2023-10-20 08:05:12 +00:00
if resp == nil {
resp = make ( [ ] PreLogoSearchResult , 0 )
}
2023-10-20 05:25:35 +00:00
return resp , nil
}