knowledgeaddlogic.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package logic
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. . "knowledgeBase/rpc/knowledge/init"
  7. "knowledgeBase/rpc/knowledge/internal/svc"
  8. "knowledgeBase/rpc/knowledge/knowledgeclient"
  9. "knowledgeBase/rpc/knowledge/util"
  10. "log"
  11. "time"
  12. )
  13. type KnowledgeAddLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewKnowledgeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeAddLogic {
  19. return &KnowledgeAddLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. type Question struct {
  26. Question string `json:"question"`
  27. KeyWords string `json:"keyWords"`
  28. }
  29. // 知识新增
  30. func (l *KnowledgeAddLogic) KnowledgeAdd(in *knowledgeclient.AddRequest) (*knowledgeclient.AddResponse, error) {
  31. // todo: add your logic here and delete this line
  32. result := &knowledgeclient.AddResponse{}
  33. //先查找知识库Id
  34. query := map[string]interface{}{"state": 1, "appid": in.AppId, "tenant_id": in.TenantId}
  35. datalist := Mysql.Find(util.KNOWLEDGE, query, "id", "", -1, -1)
  36. if datalist != nil && *datalist != nil && len(*datalist) > 0 {
  37. fool := Mysql.ExecTx("创建其他订单生成订单信息和合同信息", func(tx *sql.Tx) bool {
  38. //插入答案
  39. answerData := map[string]interface{}{
  40. "knowledge_id": (*datalist)[0]["id"],
  41. "status": 1,
  42. "create_time": time.Now().Local().Format(util.Date_Full_Layout),
  43. "update_time": time.Now().Local().Format(util.Date_Full_Layout),
  44. "create_person": in.Person,
  45. "content": in.Answer,
  46. }
  47. answer_id := Mysql.Insert(util.ANSWER, answerData)
  48. if answer_id <= 0 {
  49. return false
  50. }
  51. //插入问题
  52. //先分词
  53. keywords := util.HttpDo(in.Question)
  54. questionData := map[string]interface{}{
  55. "answer_id": answer_id,
  56. "content": 1,
  57. "keywords": keywords,
  58. }
  59. question_id := Mysql.Insert(util.QUESTION, questionData)
  60. if question_id <= 0 {
  61. return false
  62. }
  63. return true
  64. })
  65. if fool {
  66. result.ErrorCode = 0
  67. } else {
  68. result.ErrorCode = -1
  69. result.ErrorMsg = "新建失败"
  70. }
  71. } else {
  72. log.Println("租户不存在")
  73. result.ErrorCode = -1
  74. result.ErrorMsg = "租户不存在"
  75. }
  76. return result, nil
  77. }