123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package logic
- import (
- elastic "app.yhyue.com/moapp/jybase/esv1"
- "context"
- "database/sql"
- "github.com/zeromicro/go-zero/core/logx"
- . "knowledgeBase/rpc/knowledge/init"
- "knowledgeBase/rpc/knowledge/internal/svc"
- "knowledgeBase/rpc/knowledge/knowledgeclient"
- "knowledgeBase/rpc/knowledge/util"
- "log"
- "strconv"
- "time"
- )
- type KnowledgeEditLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewKnowledgeEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeEditLogic {
- return &KnowledgeEditLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // KnowledgeEdit 知识编辑
- func (l *KnowledgeEditLogic) KnowledgeEdit(in *knowledgeclient.KnowledgeEditReq) (*knowledgeclient.AddResponse, error) {
- // todo: add your logic here and delete this line
- result := &knowledgeclient.AddResponse{}
- //修改答案
- answerUpdate := map[string]interface{}{
- "update_time": time.Now().Local().Format(util.Date_Full_Layout),
- "content": in.Answer,
- }
- //获取问题分词
- keywords := util.HttpDo(in.Question)
- fool := Mysql.ExecTx("编辑问题、答案", func(tx *sql.Tx) bool {
- ok1 := Mysql.UpdateByTx(tx, util.ANSWER, map[string]interface{}{"id": in.AnswerId}, answerUpdate)
- //修改问题
- questionUpdate := map[string]interface{}{
- "content": in.Question,
- "keywords": keywords,
- }
- ok2 := Mysql.UpdateByTx(tx, util.QUESTION, map[string]interface{}{"answer_id": in.AnswerId}, questionUpdate)
- return ok1 && ok2
- })
- if fool {
- //先查询es获取es _id
- query := `{"query":{"bool":{"must":[{"term":{"smart_v.answerId":"` + strconv.Itoa(int(in.AnswerId)) + `"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"facets":{}}`
- log.Println(query)
- kw := elastic.Get(C.Es.Index, C.Es.Type, query)
- //log.Println("查询es的——id", kw)
- log.Println("查询es的——id", ((*kw)[0])["_id"])
- //修改es数据
- newKnowledge := map[string]interface{}{
- "knowledgeId": in.KnowledgeId,
- "status": 1,
- "createTime": time.Now().Local().Format(util.Date_Full_Layout),
- "createPerson": in.Person,
- "answer": in.Answer,
- "question": in.Question,
- "keywords": keywords,
- "answerId": in.AnswerId,
- "tenantId": in.TenantId,
- }
- ok := elastic.Del(C.Es.Index, C.Es.Type, query)
- b := elastic.Save(C.Es.Index, C.Es.Type, newKnowledge)
- log.Println("存es", b)
- if ok {
- result.ErrorCode = 0
- result.ErrorMsg = "修改问题成功"
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "修改es问题失败"
- }
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "修改mysql问题失败"
- }
- return result, nil
- }
|