123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package logic
- import (
- elastic "app.yhyue.com/moapp/jybase/esv1"
- "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
- "context"
- "database/sql"
- "github.com/zeromicro/go-zero/core/logx"
- "knowledgeBase/entity"
- . "knowledgeBase/rpc/knowledge/init"
- "knowledgeBase/rpc/knowledge/internal/svc"
- "knowledgeBase/rpc/knowledge/knowledgeclient"
- "knowledgeBase/rpc/knowledge/util"
- "time"
- )
- type KnowledgeAddLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewKnowledgeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeAddLogic {
- return &KnowledgeAddLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- type Question struct {
- Question string `json:"question"`
- KeyWords string `json:"keyWords"`
- }
- // KnowledgeAdd 知识新增
- func (l *KnowledgeAddLogic) KnowledgeAdd(in *knowledgeclient.AddRequest) (*knowledgeclient.AddResponse, error) {
- result := &knowledgeclient.AddResponse{}
- //先查找知识库Id
- query := map[string]interface{}{"status": 1, "appid": in.AppId, "ent_id": in.EntId}
- datalist := Mysql.Find(util.KNOWLEDGE, query, "id", "", -1, -1)
- if datalist != nil && *datalist != nil && len(*datalist) > 0 {
- //问题进行分词
- keywords := util.HttpDo(in.Question)
- //通过entUserId获取创建人名称,调用用户中心
- req := &usercenter.EntUserReq{
- EntId: in.EntId,
- EntUserId: in.EntUserId,
- AppId: in.AppId,
- }
- resp, err := entity.UserCenterLib.GetEntUserInfo(context.Background(), req)
- if err != nil {
- logx.Infof("查询用户中台创建人信息失败", in.EntId, in.EntUserId, "err:", err)
- return nil, err
- }
- createPerson := resp.Data.Name
- var answerId int64
- var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海
- nowTime := time.Now().In(cstSh).Format(util.Date_Full_Layout)
- fool := Mysql.ExecTx("添加知识", func(tx *sql.Tx) bool {
- //插入答案
- answerData := map[string]interface{}{
- "knowledge_id": (*datalist)[0]["id"],
- "status": 1,
- "create_time": nowTime,
- "update_time": nowTime,
- "create_person": createPerson,
- "content": in.Answer,
- }
- answerId = Mysql.Insert(util.ANSWER, answerData)
- //插入问题
- questionData := map[string]interface{}{
- "answer_id": answerId,
- "content": in.Question,
- "keywords": keywords,
- }
- questionId := Mysql.Insert(util.QUESTION, questionData)
- return answerId > 0 && questionId > 0
- })
- if fool {
- //插入es
- knowledge := map[string]interface{}{
- "knowledgeId": (*datalist)[0]["id"],
- "status": 1,
- "createTime": time.Now().Unix(),
- "createPerson": createPerson,
- "answer": in.Answer,
- "question": in.Question,
- "keywords": keywords,
- "answerId": answerId,
- "entId": in.EntId,
- }
- b := elastic.Save(C.Es.Index, C.Es.Type, knowledge)
- if b {
- result.ErrorCode = 0
- result.ErrorMsg = "插入数据成功"
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "插入es失败"
- }
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "新建失败"
- }
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "租户不存在"
- }
- return result, nil
- }
|