/
db.go
53 lines (48 loc) · 1.27 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package search
import (
"encoding/json"
"github.com/go-resty/resty/v2"
"github.com/gookit/goutil/strutil"
"github.com/itihey/tikuAdapter/internal/dao"
"github.com/itihey/tikuAdapter/pkg/model"
"github.com/itihey/tikuAdapter/pkg/util"
)
// DB mysql 或者sqlite3
type dBSearch struct{}
var defaultDBSearch = &dBSearch{}
// GetDBSearch 获取DB搜索实例
func GetDBSearch() Search {
return defaultDBSearch
}
func (in *dBSearch) getHTTPClient() *resty.Client {
panic("implement me")
}
// SearchAnswer 搜索答案
func (in *dBSearch) SearchAnswer(req model.SearchRequest) (answer [][]string, err error) {
answer = make([][]string, 0)
questionText := util.GetQuestionText(req.Question)
questionHash := strutil.ShortMd5(questionText)
tiku := dao.Tiku
find, err := tiku.Where(tiku.QuestionHash.Eq(questionHash)).Find()
if err != nil {
return nil, err
}
if len(find) == 0 {
find2, err := tiku.Where(tiku.QuestionText.Like("%" + questionText + "%")).Find()
if err != nil {
return nil, err
}
find = find2
}
for i := range find {
var answers []string // 最后所有的答案的二维数组
err := json.Unmarshal([]byte(find[i].Answer), &answers)
if err != nil {
continue
}
if len(answers) > 0 {
answer = append(answer, answers)
}
}
return answer, nil
}