kefu/controller/learn.go

153 lines
3.8 KiB
Go
Raw Normal View History

2024-12-10 02:50:12 +00:00
package controller
import (
"github.com/gin-gonic/gin"
"kefu/lib"
"kefu/models"
"kefu/tools"
"kefu/types"
"strconv"
)
func GetLearnReplyContent(entId, content string) string {
replyContent := ""
replys := models.FindArticleRows("id,title,search_type", "ent_id = ? ", entId)
//按分值计算
var exactReply []lib.MatchData
var matchReply []lib.MatchData
for _, row := range replys {
//精准
if row.SearchType == 2 {
exactReply = append(exactReply, lib.MatchData{
ID: row.Id,
Text: row.Title,
})
}
//包含
if row.SearchType == 1 {
matchReply = append(matchReply, lib.MatchData{
ID: row.Id,
Text: row.Title,
})
}
//通配符
if row.Title == "*" {
exactReply = append(exactReply, lib.MatchData{
ID: row.Id,
Text: row.Title,
})
}
}
matchRes, ok := lib.NewMatcher(exactReply).Match(content, true)
if ok {
r := models.FindArticleRow("id = ?", matchRes.ID)
replyContent = r.Content
article := models.Article{
Score: r.Score + 1,
}
go article.SaveArticle("ent_id = ? and id = ?", entId, r.Id)
}
if replyContent == "" {
matchRes, ok = lib.NewMatcher(matchReply).Match(content, false)
if ok {
r := models.FindArticleRow("id = ?", matchRes.ID)
replyContent = r.Content
article := models.Article{
Score: r.Score + 1,
}
go article.SaveArticle("ent_id = ? and id = ?", entId, r.Id)
}
}
config := models.GetEntConfigsMap(entId, "KeywordFinalReply")
if replyContent == "" {
replyContent = config["KeywordFinalReply"]
}
return replyContent
//articles := models.FindArticleList(1, 10, "score desc", "ent_id= ? and title like ?", entId, "%"+content+"%")
//articleLen := len(articles)
//result := ""
////未匹配,进入学习库
//if articleLen == 0 {
// if len([]rune(content)) > 1 {
// go AddUpdateLearn(entId, content)
// }
//} else if articleLen == 1 {
// result = tools.TrimHtml(articles[0].Content)
// article := models.Article{
// Score: articles[0].Score + 1,
// }
// go article.SaveArticle("ent_id = ? and id = ?", entId, articles[0].Id)
//} else if articleLen > 1 {
// result = "您是不是想问:"
// for _, article := range articles {
// title := strings.Split(strings.ReplaceAll(article.Title, "", ","), ",")[0]
// result += fmt.Sprintf("<a href='#'>%s</a>\r\n", title)
// }
//}
//return result
}
// 插入或更新学习库
func AddUpdateLearn(entId, content string) {
learn := models.FindLearn("ent_id = ? and content = ?", entId, content)
if learn.ID != 0 {
learn.Score += 1
learn.SaveLearn("ent_id = ? and content = ?", entId, content)
} else {
m := &models.Learn{
EntId: entId,
Content: content,
CreatedAt: types.Time{},
Score: 1,
Finshed: 1,
}
m.AddLearn()
}
}
// 学习库列表
func GetLearnsList(c *gin.Context) {
entId, _ := c.Get("ent_id")
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("pagesize", "10"))
if pageSize > 50 {
pageSize = 10
}
var args = []interface{}{}
search := "ent_id = ? "
args = append(args, entId)
count := models.CountLearn(search, args...)
list := models.FindLearnList(page, pageSize, search, args...)
c.JSON(200, gin.H{
"code": types.ApiCode.SUCCESS,
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
"result": gin.H{
"list": list,
"count": count,
"pagesize": pageSize,
"page": page,
},
})
}
// 学习库状态
func PostLearnStatus(c *gin.Context) {
status := c.PostForm("status")
id := c.PostForm("id")
entId, _ := c.Get("ent_id")
kefuName, _ := c.Get("kefu_name")
learn := models.Learn{
Finshed: uint(tools.Str2Int(status)),
KefuName: kefuName.(string),
}
learn.SaveLearn("ent_id = ? and id = ?", entId, id)
c.JSON(200, gin.H{
"code": types.ApiCode.SUCCESS,
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
})
}