report.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/models/report"
  6. "eta/eta_hub/utils"
  7. "fmt"
  8. "html"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. )
  13. // UpdateReportEs 更新报告/章节Es
  14. func UpdateReportEs(reportId int, publishState int) (err error) {
  15. if reportId <= 0 {
  16. return
  17. }
  18. reportInfo, err := models.GetReportByReportId(reportId)
  19. if err != nil {
  20. return
  21. }
  22. categories := ""
  23. if reportInfo.HasChapter == 1 {
  24. // 晨周报
  25. chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id)
  26. if tmpErr != nil {
  27. return
  28. }
  29. if len(chapterList) > 0 {
  30. // 更新章节的es数据
  31. for _, chapterInfo := range chapterList {
  32. err = updateReportChapterEsByChapter(chapterInfo)
  33. if err != nil {
  34. return
  35. }
  36. }
  37. }
  38. } else {
  39. //if utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox {
  40. permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword("rddp", reportInfo.ClassifyIdSecond)
  41. if tmpErr != nil {
  42. return
  43. }
  44. categoryArr := make([]string, 0)
  45. for i := 0; i < len(permissionList); i++ {
  46. categoryArr = append(categoryArr, permissionList[i].PermissionName)
  47. }
  48. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  49. categories = strings.Join(aliasArr, ",")
  50. //}
  51. }
  52. // 最小单位的分类id
  53. minClassifyId, minClassifyName, err := getMinClassify(reportInfo)
  54. if err != nil {
  55. return
  56. }
  57. // 新增报告ES
  58. esReport := &models.ElasticReportDetail{
  59. ReportId: reportInfo.Id,
  60. ReportChapterId: 0,
  61. Title: reportInfo.Title,
  62. Abstract: reportInfo.Abstract,
  63. BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
  64. PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime),
  65. PublishState: publishState,
  66. Author: reportInfo.Author,
  67. ClassifyIdFirst: reportInfo.ClassifyIdFirst,
  68. ClassifyNameFirst: reportInfo.ClassifyNameFirst,
  69. ClassifyIdSecond: reportInfo.ClassifyIdSecond,
  70. ClassifyNameSecond: reportInfo.ClassifyNameSecond,
  71. ClassifyId: minClassifyId,
  72. ClassifyName: minClassifyName,
  73. Categories: categories,
  74. StageStr: strconv.Itoa(reportInfo.Stage),
  75. }
  76. docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0)
  77. if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil {
  78. return
  79. }
  80. return
  81. }
  82. // updateReportChapterEsByChapter
  83. // @Description: 通过章节详情更新报告章节ES
  84. // @author: Roc
  85. // @datetime 2024-06-20 13:16:11
  86. // @param chapterInfo *models.ReportChapter
  87. // @return err error
  88. func updateReportChapterEsByChapter(chapterInfo *models.ReportChapter) (err error) {
  89. // 章节对应的品种
  90. obj := report.ReportChapterPermissionMapping{}
  91. permissionList, tmpErr := obj.GetPermissionItemListById(chapterInfo.ReportChapterId)
  92. if tmpErr != nil {
  93. return
  94. }
  95. categoryArr := make([]string, 0)
  96. if len(permissionList) > 0 {
  97. for ii := 0; ii < len(permissionList); ii++ {
  98. categoryArr = append(categoryArr, permissionList[ii].ChartPermissionName)
  99. }
  100. }
  101. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  102. categories := strings.Join(aliasArr, ",")
  103. // 新增/编辑ES
  104. esChapter := &models.ElasticReportDetail{
  105. ReportId: chapterInfo.ReportId,
  106. ReportChapterId: chapterInfo.ReportChapterId,
  107. Title: chapterInfo.Title,
  108. Abstract: chapterInfo.Abstract,
  109. BodyContent: utils.TrimHtml(html.UnescapeString(chapterInfo.Content)),
  110. PublishTime: chapterInfo.PublishTime.Format(utils.FormatDateTime),
  111. PublishState: chapterInfo.PublishState,
  112. Author: chapterInfo.Author,
  113. ClassifyIdFirst: chapterInfo.ClassifyIdFirst,
  114. ClassifyNameFirst: chapterInfo.ClassifyNameFirst,
  115. ClassifyIdSecond: 0,
  116. ClassifyNameSecond: "",
  117. ClassifyId: chapterInfo.ClassifyIdFirst,
  118. ClassifyName: chapterInfo.ClassifyNameFirst,
  119. Categories: categories,
  120. StageStr: strconv.Itoa(chapterInfo.Stage),
  121. }
  122. chapterDocId := fmt.Sprintf("%d-%d", chapterInfo.ReportId, chapterInfo.ReportChapterId)
  123. if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil {
  124. return
  125. }
  126. return
  127. }
  128. // addCategoryAliasToArr 品种别名
  129. func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) {
  130. aliasArr = categoryArr
  131. if len(categoryArr) > 0 {
  132. for i := 0; i < len(categoryArr); i++ {
  133. if strings.Contains(categoryArr[i], "沥青") {
  134. aliasArr = append(aliasArr, "BU")
  135. }
  136. if strings.Contains(categoryArr[i], "MEG") {
  137. aliasArr = append(aliasArr, "EG", "乙二醇")
  138. }
  139. if strings.Contains(categoryArr[i], "聚酯") {
  140. aliasArr = append(aliasArr, "长丝", "短纤", "瓶片")
  141. }
  142. if strings.Contains(categoryArr[i], "纯苯+苯乙烯") {
  143. aliasArr = append(aliasArr, "EB")
  144. }
  145. if strings.Contains(categoryArr[i], "聚乙烯") {
  146. aliasArr = append(aliasArr, "PP", "PE")
  147. }
  148. if strings.Contains(categoryArr[i], "玻璃纯碱") {
  149. aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA")
  150. }
  151. if strings.Contains(categoryArr[i], "甲醇") {
  152. aliasArr = append(aliasArr, "甲醇", "MA")
  153. }
  154. if strings.Contains(categoryArr[i], "橡胶") {
  155. aliasArr = append(aliasArr, "橡胶", "RU")
  156. }
  157. }
  158. }
  159. return
  160. }
  161. // getMinClassify
  162. // @Description: 获取最小分类ID
  163. // @author: Roc
  164. // @datetime 2024-06-20 09:23:19
  165. // @param reportInfo *models.Report
  166. // @return minClassifyId int
  167. // @return minClassifyName string
  168. // @return err error
  169. func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) {
  170. defer func() {
  171. if err != nil {
  172. utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error())
  173. }
  174. }()
  175. minClassifyId = reportInfo.ClassifyIdThird
  176. minClassifyName = reportInfo.ClassifyNameThird
  177. if minClassifyId <= 0 {
  178. minClassifyId = reportInfo.ClassifyIdSecond
  179. minClassifyName = reportInfo.ClassifyNameSecond
  180. }
  181. if minClassifyId <= 0 {
  182. minClassifyId = reportInfo.ClassifyIdFirst
  183. minClassifyName = reportInfo.ClassifyNameFirst
  184. }
  185. if minClassifyId <= 0 {
  186. err = errors.New("分类异常")
  187. }
  188. return
  189. }
  190. // UpdateReportEs 更新报告/章节Es
  191. //func UpdateReportEs(reportId int, publishState int) (err error) {
  192. // if reportId <= 0 {
  193. // return
  194. // }
  195. // reportInfo, err := models.GetReportByReportId(reportId)
  196. // if err != nil {
  197. // return
  198. // }
  199. // categories := ""
  200. //
  201. // // 新增报告ES
  202. // esReport := &models.ElasticReportDetail{
  203. // ReportId: reportInfo.Id,
  204. // ReportChapterId: 0,
  205. // Title: reportInfo.Title,
  206. // Abstract: reportInfo.Abstract,
  207. // BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
  208. // PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime),
  209. // PublishState: publishState,
  210. // Author: reportInfo.Author,
  211. // ClassifyIdFirst: reportInfo.ClassifyIdFirst,
  212. // ClassifyNameFirst: reportInfo.ClassifyNameFirst,
  213. // ClassifyIdSecond: reportInfo.ClassifyIdSecond,
  214. // ClassifyNameSecond: reportInfo.ClassifyNameSecond,
  215. // Categories: categories,
  216. // StageStr: strconv.Itoa(reportInfo.Stage),
  217. // }
  218. // docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0)
  219. // if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil {
  220. // return
  221. // }
  222. //
  223. // return
  224. //}
  225. // GetParentClassifyListByParentIdList
  226. // @Description: 递归获取父级分类信息,正常来讲只有三次
  227. // @author: Roc
  228. // @datetime 2024-06-19 13:23:33
  229. // @param parentClassifyIdList []int
  230. // @return list []*models.ClassifyList
  231. // @return err error
  232. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.ClassifyListV2, err error) {
  233. num := len(parentClassifyIdList)
  234. if num <= 0 {
  235. return
  236. }
  237. list, err = models.GetClassifyListByParentIdList(parentClassifyIdList)
  238. if err != nil {
  239. return
  240. }
  241. // 是否还有上级
  242. {
  243. currParentClassifyIdList := make([]int, 0)
  244. for _, v := range list {
  245. if v.ParentId > 0 {
  246. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  247. }
  248. }
  249. if len(currParentClassifyIdList) > 0 {
  250. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  251. if tmpErr != nil {
  252. err = tmpErr
  253. return
  254. }
  255. list = append(tmpList, list...)
  256. }
  257. }
  258. return
  259. }
  260. // GetClassifyListTreeRecursive
  261. // @Description: 递归获取分类树形结构
  262. // @author: Roc
  263. // @datetime 2024-06-19 13:23:28
  264. // @param list []*models.ClassifyList
  265. // @param parentId int
  266. // @return []*models.ClassifyListV2
  267. func GetClassifyListTreeRecursive(list []*models.ClassifyListV2, parentId int) []*models.ClassifyListV2 {
  268. res := make([]*models.ClassifyListV2, 0)
  269. for _, v := range list {
  270. if v.ParentId == parentId {
  271. v.Child = GetClassifyListTreeRecursive(list, v.Id)
  272. res = append(res, v)
  273. }
  274. }
  275. // 前端的JP需要我这么返回
  276. //if len(res) <= 0 {
  277. // res = nil
  278. //}
  279. return res
  280. }
  281. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  282. type BySortAndCreateTime []*models.ClassifyListV2
  283. func (a BySortAndCreateTime) Len() int {
  284. return len(a)
  285. }
  286. func (a BySortAndCreateTime) Swap(i, j int) {
  287. a[i], a[j] = a[j], a[i]
  288. }
  289. func (a BySortAndCreateTime) Less(i, j int) bool {
  290. if a[i].Sort == a[j].Sort {
  291. return a[i].CreateTime.Before(a[j].CreateTime)
  292. }
  293. return a[i].Sort < a[j].Sort
  294. }
  295. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  296. func SortClassifyListBySortAndCreateTime(classifyList []*models.ClassifyListV2) {
  297. sort.Sort(BySortAndCreateTime(classifyList))
  298. }