123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package service
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/gf/v2/util/gconv"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "jyseo/internal/consts"
- "jyseo/utility"
- "log"
- "strings"
- "time"
- )
- var (
- IndustryCodeMap map[string]string
- htmlUrlDir = g.Cfg().MustGet(context.Background(), "analysisReport.images.htmlUrl", "htmlUrl").String()
- )
- type analysisReport struct {
- Id string `json:"id"`
- Industry string `json:"industry"`
- Area string `json:"area"`
- KeyWord string `json:"key_word"`
- Audit int32 `json:"audit"`
- Desc string `json:"desc"`
- Content string `json:"content"`
- Imags []string `json:"imags"`
- Classify int32 `json:"classify"`
- ShowTime int64 `json:"show_time"`
- SearchId string `json:"search_id"`
- CreateTime int64 `json:"createTime"`
- }
- type AnalysisRequestFormat struct {
- KeysItems []keyWordGroup
- }
- // keyWordGroup 订阅词结构体
- type keyWordGroup struct {
- A_Key []viewKeyWord `json:"a_key"`
- ItemName string `json:"s_item"`
- UpdateTime int64 `json:"updatetime"`
- }
- type viewKeyWord struct {
- Keyword []string `json:"key"` //关键词
- Appended []string `json:"appendkey"` //附加词
- Exclude []string `json:"notkey"` //排除词
- MatchWay int `json:"matchway"` //匹配模式
- }
- func init() {
- IndustryCodeMap = make(map[string]string)
- res, err := g.DB().Query(context.Background(), `SELECT name,code FROM seo_industry_class `)
- if err == nil && !res.IsEmpty() {
- for _, m := range res.List() {
- IndustryCodeMap[gconv.String(m["name"])] = gconv.String(m["code"])
- }
- }
- }
- func AnalysisHtmlSave(byMap map[string]interface{}) error {
- rid := gconv.String(byMap["rid"])
- if rid == "" {
- return errors.New("rid不能为空")
- }
- audit := gconv.Int64(byMap["audit"])
- var (
- content, desc string
- imagArr []string
- err error
- )
- id := utility.DecodeId(rid)
- if id == "" {
- return errors.New("id错误")
- }
- res := gconv.Map(byMap["filters"])
- if res == nil || len(res) == 0 {
- return errors.New("条件获取失败")
- }
- ctx := gctx.New()
- times := strings.Split(gconv.String(res["rangeTime"]), "-")
- if len(times) != 2 {
- return errors.New("时间格式有误")
- }
- timestamp1 := time.Unix(gconv.Int64(times[0]), 0)
- timestamp2 := time.Unix(gconv.Int64(times[1]), 0)
- duration := timestamp2.Sub(timestamp1)
- var (
- classify int //分类 1周 2月
- cycle, area, industry string //周期 城市 行业
- showTime int64 // 排序时间
- keyItem AnalysisRequestFormat
- )
- switch duration > 7*24*time.Hour {
- case true:
- classify = 2
- cycle = timestamp1.Format("01")
- showTime = gconv.Int64(times[1])
- case false:
- classify = 1
- _, week := timestamp1.ISOWeek()
- cycle = fmt.Sprintf("%02d", week)
- showTime = gconv.Int64(times[0])
- }
- areaMap := gconv.Map(res["area"])
- if areaMap != nil {
- for key := range areaMap {
- area = key
- break
- }
- }
- industryMap := gconv.Map(res["industry"])
- if industryMap != nil {
- for key := range industryMap {
- industry = key
- break
- }
- }
- _ = json.Unmarshal([]byte(gconv.String(res["keysItems"])), &keyItem.KeysItems)
- var keyWords []string
- for _, item := range keyItem.KeysItems {
- for _, word := range item.A_Key {
- if len(word.Keyword) > 0 {
- keyWords = append(keyWords, word.Keyword...)
- }
- if len(word.Appended) > 0 {
- keyWords = append(keyWords, word.Appended...)
- }
- }
- }
- if len(keyWords) > 1 {
- return errors.New("关键词数量异常")
- }
- var areaCode string = "00"
- if JySeoAreaRoot.GetNodeByName(area) != nil {
- areaCode = JySeoAreaRoot.GetNodeByName(area).RCode[:2]
- }
- industryCode := "00" //行业code
- if industry != "" {
- if iCode, ok1 := IndustryCodeMap[industry]; ok1 && iCode != "" {
- industryCode = iCode
- }
- }
- var idStr string
- year := timestamp1.Year() //关键词&年&月&行业&城市r
- timeStr := fmt.Sprintf("%d%s%s%s", year, cycle, industryCode, areaCode)
- for _, word := range keyWords {
- if word != "" {
- idStr = fmt.Sprintf("%s%s", utility.KeyPinyin(word), timeStr)
- break
- }
- }
- if idStr == "" { //非关键词分析
- if industry == "" || area == "" {
- return errors.New("无关键词信息,类型不能为空")
- }
- //年&分类&周期&行业&城市
- idStr = fmt.Sprintf("%d%d%s%s%s", year, classify, cycle, industryCode, areaCode)
- }
- log.Println("参数id", idStr)
- res1, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT id FROM analysis_report WHERE search_id = '%s'`, id))
- if audit == 1 { //通过审批提取html
- content, desc, imagArr, err = utility.AnalysisHtml(fmt.Sprintf("%s%s.html", htmlUrlDir, strings.ReplaceAll(rid, `/`, "_123_")))
- if err != nil || content == "" || len(imagArr) == 0 {
- return gerror.Wrap(err, "html内容提取失败")
- }
- go func() { //通过时 降之前通过的修改为不通过 并同步mongo
- var (
- mgoIds []primitive.ObjectID
- )
- res2, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT search_id FROM analysis_report WHERE id = '%s' and audit = 1 and search_id != '%s'`, idStr, id))
- if !res2.IsEmpty() {
- for _, m := range res2.List() {
- if gconv.String(m["search_id"]) != "" {
- mId, _ := primitive.ObjectIDFromHex(gconv.String(m["search_id"]))
- mgoIds = append(mgoIds, mId)
- }
- }
- g.DB().Exec(ctx, fmt.Sprintf(`alter table analysis_report_do on CLUSTER default_cluster UPDATE audit = -1 where id = '%s' and audit = 1 and search_id != '%s'`, idStr, id))
- utility.MG.DB().Update(consts.ReportHistoryTable, map[string]interface{}{
- "_id": map[string]interface{}{
- "$in": mgoIds,
- },
- }, map[string]interface{}{
- "$set": map[string]interface{}{"audit": -1},
- }, false, true)
- }
- }()
- if res1.IsEmpty() {
- aReport := analysisReport{
- Id: idStr,
- Industry: industry,
- Area: area,
- KeyWord: strings.Join(keyWords, ","),
- Desc: desc,
- Audit: int32(audit),
- Content: content,
- Imags: imagArr,
- Classify: int32(classify),
- ShowTime: showTime,
- SearchId: id,
- CreateTime: time.Now().Unix(),
- }
- _, err = g.DB().Insert(ctx, consts.AnalysisReport, aReport)
- if err != nil {
- return err
- }
- } else {
- g.DB().Exec(ctx, fmt.Sprintf(`alter table analysis_report_do on CLUSTER default_cluster UPDATE audit = %d where search_id = '%s'`, audit, id))
- }
- } else {
- if !res1.IsEmpty() {
- g.DB().Exec(ctx, fmt.Sprintf(`alter table analysis_report_do on CLUSTER default_cluster UPDATE audit = %d where search_id = '%s'`, audit, id))
- }
- }
- go utility.MG.DB().UpdateById(consts.ReportHistoryTable, id, map[string]interface{}{
- "$set": map[string]interface{}{"audit": audit},
- })
- return nil
- }
|