package services import ( "errors" "eta/eta_hub/models" "eta/eta_hub/models/report" "eta/eta_hub/utils" "fmt" "html" "sort" "strconv" "strings" ) // UpdateReportEs 更新报告/章节Es func UpdateReportEs(reportId int, publishState int) (err error) { if reportId <= 0 { return } reportInfo, err := models.GetReportByReportId(reportId) if err != nil { return } categories := "" if reportInfo.HasChapter == 1 { // 晨周报 chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id) if tmpErr != nil { return } if len(chapterList) > 0 { // 更新章节的es数据 for _, chapterInfo := range chapterList { err = updateReportChapterEsByChapter(chapterInfo) if err != nil { return } } } } else { //if utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox { permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword("rddp", reportInfo.ClassifyIdSecond) if tmpErr != nil { return } categoryArr := make([]string, 0) for i := 0; i < len(permissionList); i++ { categoryArr = append(categoryArr, permissionList[i].PermissionName) } aliasArr, _ := addCategoryAliasToArr(categoryArr) categories = strings.Join(aliasArr, ",") //} } // 最小单位的分类id minClassifyId, minClassifyName, err := getMinClassify(reportInfo) if err != nil { return } // 新增报告ES esReport := &models.ElasticReportDetail{ ReportId: reportInfo.Id, ReportChapterId: 0, Title: reportInfo.Title, Abstract: reportInfo.Abstract, BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)), PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime), PublishState: publishState, Author: reportInfo.Author, ClassifyIdFirst: reportInfo.ClassifyIdFirst, ClassifyNameFirst: reportInfo.ClassifyNameFirst, ClassifyIdSecond: reportInfo.ClassifyIdSecond, ClassifyNameSecond: reportInfo.ClassifyNameSecond, ClassifyId: minClassifyId, ClassifyName: minClassifyName, Categories: categories, StageStr: strconv.Itoa(reportInfo.Stage), } docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0) if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil { return } return } // updateReportChapterEsByChapter // @Description: 通过章节详情更新报告章节ES // @author: Roc // @datetime 2024-06-20 13:16:11 // @param chapterInfo *models.ReportChapter // @return err error func updateReportChapterEsByChapter(chapterInfo *models.ReportChapter) (err error) { // 章节对应的品种 obj := report.ReportChapterPermissionMapping{} permissionList, tmpErr := obj.GetPermissionItemListById(chapterInfo.ReportChapterId) if tmpErr != nil { return } categoryArr := make([]string, 0) if len(permissionList) > 0 { for ii := 0; ii < len(permissionList); ii++ { categoryArr = append(categoryArr, permissionList[ii].ChartPermissionName) } } aliasArr, _ := addCategoryAliasToArr(categoryArr) categories := strings.Join(aliasArr, ",") // 新增/编辑ES esChapter := &models.ElasticReportDetail{ ReportId: chapterInfo.ReportId, ReportChapterId: chapterInfo.ReportChapterId, Title: chapterInfo.Title, Abstract: chapterInfo.Abstract, BodyContent: utils.TrimHtml(html.UnescapeString(chapterInfo.Content)), PublishTime: chapterInfo.PublishTime.Format(utils.FormatDateTime), PublishState: chapterInfo.PublishState, Author: chapterInfo.Author, ClassifyIdFirst: chapterInfo.ClassifyIdFirst, ClassifyNameFirst: chapterInfo.ClassifyNameFirst, ClassifyIdSecond: 0, ClassifyNameSecond: "", ClassifyId: chapterInfo.ClassifyIdFirst, ClassifyName: chapterInfo.ClassifyNameFirst, Categories: categories, StageStr: strconv.Itoa(chapterInfo.Stage), } chapterDocId := fmt.Sprintf("%d-%d", chapterInfo.ReportId, chapterInfo.ReportChapterId) if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil { return } return } // addCategoryAliasToArr 品种别名 func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) { aliasArr = categoryArr if len(categoryArr) > 0 { for i := 0; i < len(categoryArr); i++ { if strings.Contains(categoryArr[i], "沥青") { aliasArr = append(aliasArr, "BU") } if strings.Contains(categoryArr[i], "MEG") { aliasArr = append(aliasArr, "EG", "乙二醇") } if strings.Contains(categoryArr[i], "聚酯") { aliasArr = append(aliasArr, "长丝", "短纤", "瓶片") } if strings.Contains(categoryArr[i], "纯苯+苯乙烯") { aliasArr = append(aliasArr, "EB") } if strings.Contains(categoryArr[i], "聚乙烯") { aliasArr = append(aliasArr, "PP", "PE") } if strings.Contains(categoryArr[i], "玻璃纯碱") { aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA") } if strings.Contains(categoryArr[i], "甲醇") { aliasArr = append(aliasArr, "甲醇", "MA") } if strings.Contains(categoryArr[i], "橡胶") { aliasArr = append(aliasArr, "橡胶", "RU") } } } return } // getMinClassify // @Description: 获取最小分类ID // @author: Roc // @datetime 2024-06-20 09:23:19 // @param reportInfo *models.Report // @return minClassifyId int // @return minClassifyName string // @return err error func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) { defer func() { if err != nil { utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error()) } }() minClassifyId = reportInfo.ClassifyIdThird minClassifyName = reportInfo.ClassifyNameThird if minClassifyId <= 0 { minClassifyId = reportInfo.ClassifyIdSecond minClassifyName = reportInfo.ClassifyNameSecond } if minClassifyId <= 0 { minClassifyId = reportInfo.ClassifyIdFirst minClassifyName = reportInfo.ClassifyNameFirst } if minClassifyId <= 0 { err = errors.New("分类异常") } return } // UpdateReportEs 更新报告/章节Es //func UpdateReportEs(reportId int, publishState int) (err error) { // if reportId <= 0 { // return // } // reportInfo, err := models.GetReportByReportId(reportId) // if err != nil { // return // } // categories := "" // // // 新增报告ES // esReport := &models.ElasticReportDetail{ // ReportId: reportInfo.Id, // ReportChapterId: 0, // Title: reportInfo.Title, // Abstract: reportInfo.Abstract, // BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)), // PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime), // PublishState: publishState, // Author: reportInfo.Author, // ClassifyIdFirst: reportInfo.ClassifyIdFirst, // ClassifyNameFirst: reportInfo.ClassifyNameFirst, // ClassifyIdSecond: reportInfo.ClassifyIdSecond, // ClassifyNameSecond: reportInfo.ClassifyNameSecond, // Categories: categories, // StageStr: strconv.Itoa(reportInfo.Stage), // } // docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0) // if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil { // return // } // // return //} // GetParentClassifyListByParentIdList // @Description: 递归获取父级分类信息,正常来讲只有三次 // @author: Roc // @datetime 2024-06-19 13:23:33 // @param parentClassifyIdList []int // @return list []*models.ClassifyList // @return err error func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.ClassifyListV2, err error) { num := len(parentClassifyIdList) if num <= 0 { return } list, err = models.GetClassifyListByParentIdList(parentClassifyIdList) if err != nil { return } // 是否还有上级 { currParentClassifyIdList := make([]int, 0) for _, v := range list { if v.ParentId > 0 { currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId) } } if len(currParentClassifyIdList) > 0 { tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList) if tmpErr != nil { err = tmpErr return } list = append(tmpList, list...) } } return } // GetClassifyListTreeRecursive // @Description: 递归获取分类树形结构 // @author: Roc // @datetime 2024-06-19 13:23:28 // @param list []*models.ClassifyList // @param parentId int // @return []*models.ClassifyListV2 func GetClassifyListTreeRecursive(list []*models.ClassifyListV2, parentId int) []*models.ClassifyListV2 { res := make([]*models.ClassifyListV2, 0) for _, v := range list { if v.ParentId == parentId { v.Child = GetClassifyListTreeRecursive(list, v.Id) res = append(res, v) } } // 前端的JP需要我这么返回 //if len(res) <= 0 { // res = nil //} return res } // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。 type BySortAndCreateTime []*models.ClassifyListV2 func (a BySortAndCreateTime) Len() int { return len(a) } func (a BySortAndCreateTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a BySortAndCreateTime) Less(i, j int) bool { if a[i].Sort == a[j].Sort { return a[i].CreateTime.Before(a[j].CreateTime) } return a[i].Sort < a[j].Sort } // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order. func SortClassifyListBySortAndCreateTime(classifyList []*models.ClassifyListV2) { sort.Sort(BySortAndCreateTime(classifyList)) }