package services

import (
	"errors"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/utils"
	"html"
	"strings"
)

func GetReportContentSub(content string) (contentSub string, err error) {
	content = html.UnescapeString(content)
	doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
	if err != nil {
		fmt.Println("create doc err:", err.Error())
		return
	}
	n := 0
	doc.Find("p").Each(func(i int, s *goquery.Selection) {
		if n >= 5 {
			return
		}
		n++
		phtml, err := s.Html()
		if err != nil {
			fmt.Println("get html err", err.Error())
			return
		}
		if s.Text() != "" || strings.Contains(phtml, "src") {
			contentSub = contentSub + "<p>" + phtml + "</p>"
		}
	})
	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
}
