elastic.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/smart_report"
  6. "eta/eta_mobile/services/alarm_msg"
  7. "eta/eta_mobile/utils"
  8. "fmt"
  9. "strings"
  10. )
  11. // EsAddOrEditReport 新增编辑es报告
  12. func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Println("EsAddOrEditReport Err:", err.Error())
  16. }
  17. }()
  18. client := utils.EsClient
  19. // docId为报告ID+章节ID
  20. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  21. if err != nil && !strings.Contains(err.Error(), "404") {
  22. fmt.Println("Get Err" + err.Error())
  23. return
  24. }
  25. if searchById != nil && searchById.Found {
  26. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  27. "ReportId": item.ReportId,
  28. "ReportChapterId": item.ReportChapterId,
  29. "Title": item.Title,
  30. "Abstract": item.Abstract,
  31. "BodyContent": item.BodyContent,
  32. "PublishTime": item.PublishTime,
  33. "PublishState": item.PublishState,
  34. "Author": item.Author,
  35. "ClassifyIdFirst": item.ClassifyIdFirst,
  36. "ClassifyNameFirst": item.ClassifyNameFirst,
  37. "ClassifyIdSecond": item.ClassifyIdSecond,
  38. "ClassifyNameSecond": item.ClassifyNameSecond,
  39. "Categories": item.Categories,
  40. "StageStr": item.StageStr,
  41. }).Do(context.Background())
  42. if err != nil {
  43. return err
  44. }
  45. //fmt.Println(resp.Status, resp.Result)
  46. if resp.Status == 0 {
  47. fmt.Println("修改成功" + docId)
  48. err = nil
  49. } else {
  50. fmt.Println("EditData", resp.Status, resp.Result)
  51. }
  52. } else {
  53. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  54. if err != nil {
  55. fmt.Println("新增失败:", err.Error())
  56. return err
  57. }
  58. if resp.Status == 0 && resp.Result == "created" {
  59. fmt.Println("新增成功" + docId)
  60. return nil
  61. } else {
  62. fmt.Println("AddData", resp.Status, resp.Result)
  63. }
  64. }
  65. return
  66. }
  67. // EsAddOrEditEnglishReport 新增编辑es英文报告
  68. func EsAddOrEditEnglishReport(indexName, docId string, item *models.ElasticEnglishReportDetail) (err error) {
  69. defer func() {
  70. if err != nil {
  71. fmt.Println("EsAddOrEditEnglishReport Err:", err.Error())
  72. go alarm_msg.SendAlarmMsg("新增编辑es英文报告 EsAddOrEditEnglishReport,Err:"+err.Error(), 3)
  73. }
  74. }()
  75. client := utils.EsClient
  76. // docId为报告ID
  77. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  78. if err != nil {
  79. if strings.Contains(err.Error(), "404") {
  80. err = nil
  81. } else {
  82. fmt.Println("Get Err" + err.Error())
  83. return
  84. }
  85. }
  86. if searchById != nil && searchById.Found {
  87. resp, e := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  88. "Id": item.Id,
  89. "ReportId": item.ReportId,
  90. "VideoId": item.VideoId,
  91. "Title": item.Title,
  92. "Abstract": item.Abstract,
  93. "BodyContent": item.BodyContent,
  94. "PublishTime": item.PublishTime,
  95. "PublishState": item.PublishState,
  96. "Author": item.Author,
  97. "ClassifyIdFirst": item.ClassifyIdFirst,
  98. "ClassifyNameFirst": item.ClassifyNameFirst,
  99. "ClassifyIdSecond": item.ClassifyIdSecond,
  100. "ClassifyNameSecond": item.ClassifyNameSecond,
  101. "CreateTime": item.CreateTime,
  102. "Overview": item.Overview,
  103. "ReportCode": item.ReportCode,
  104. "Frequency": item.Frequency,
  105. "StageStr": item.StageStr,
  106. "ContentSub": item.ContentSub,
  107. }).Do(context.Background())
  108. if e != nil {
  109. err = e
  110. return
  111. }
  112. //fmt.Println(resp.Status, resp.Result)
  113. if resp.Status == 0 {
  114. fmt.Println("修改成功" + docId)
  115. err = nil
  116. } else {
  117. fmt.Println("EditData", resp.Status, resp.Result)
  118. }
  119. } else {
  120. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  121. if e != nil {
  122. err = e
  123. fmt.Println("新增失败:", err.Error())
  124. return
  125. }
  126. if resp.Status == 0 && resp.Result == "created" {
  127. fmt.Println("新增成功" + docId)
  128. return
  129. } else {
  130. fmt.Println("AddData", resp.Status, resp.Result)
  131. }
  132. }
  133. return
  134. }
  135. // EsAddOrEditSmartReport 新增编辑es智能研报
  136. func EsAddOrEditSmartReport(indexName, docId string, item *smart_report.ElasticSmartReport) (err error) {
  137. defer func() {
  138. if err != nil {
  139. fmt.Println("EsAddOrEditSmartReport Err:", err.Error())
  140. }
  141. }()
  142. client := utils.EsClient
  143. // docId为报告ID
  144. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  145. if err != nil && !strings.Contains(err.Error(), "404") {
  146. fmt.Println("Get Err" + err.Error())
  147. return
  148. }
  149. if searchById != nil && searchById.Found {
  150. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  151. "SmartReportId": item.SmartReportId,
  152. "Title": item.Title,
  153. "Abstract": item.Abstract,
  154. "BodyContent": item.BodyContent,
  155. "PublishTime": item.PublishTime,
  156. "PublishState": item.PublishState,
  157. "Author": item.Author,
  158. "ClassifyIdFirst": item.ClassifyIdFirst,
  159. "ClassifyNameFirst": item.ClassifyNameFirst,
  160. "ClassifyIdSecond": item.ClassifyIdSecond,
  161. "ClassifyNameSecond": item.ClassifyNameSecond,
  162. "StageStr": item.StageStr,
  163. "Frequency": item.Frequency,
  164. }).Do(context.Background())
  165. if err != nil {
  166. return err
  167. }
  168. //fmt.Println(resp.Status, resp.Result)
  169. if resp.Status == 0 {
  170. fmt.Println("修改成功" + docId)
  171. err = nil
  172. } else {
  173. fmt.Println("EditData", resp.Status, resp.Result)
  174. }
  175. } else {
  176. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  177. if err != nil {
  178. fmt.Println("新增失败:", err.Error())
  179. return err
  180. }
  181. if resp.Status == 0 && resp.Result == "created" {
  182. fmt.Println("新增成功" + docId)
  183. return nil
  184. } else {
  185. fmt.Println("AddData", resp.Status, resp.Result)
  186. }
  187. }
  188. return
  189. }