analyzeAudit.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/gogf/gf/v2/errors/gerror"
  8. "github.com/gogf/gf/v2/frame/g"
  9. "github.com/gogf/gf/v2/os/gctx"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. "jyseo/internal/consts"
  13. "jyseo/utility"
  14. "log"
  15. "strings"
  16. "time"
  17. )
  18. var (
  19. IndustryCodeMap map[string]string
  20. htmlUrlDir = g.Cfg().MustGet(context.Background(), "analysisReport.images.htmlUrl", "htmlUrl").String()
  21. )
  22. type analysisReport struct {
  23. Id string `json:"id"`
  24. Industry string `json:"industry"`
  25. Area string `json:"area"`
  26. KeyWord string `json:"key_word"`
  27. Audit int32 `json:"audit"`
  28. Desc string `json:"desc"`
  29. Content string `json:"content"`
  30. Imags []string `json:"imags"`
  31. Classify int32 `json:"classify"`
  32. ShowTime int64 `json:"show_time"`
  33. SearchId string `json:"search_id"`
  34. CreateTime int64 `json:"createTime"`
  35. }
  36. type AnalysisRequestFormat struct {
  37. KeysItems []keyWordGroup
  38. }
  39. // keyWordGroup 订阅词结构体
  40. type keyWordGroup struct {
  41. A_Key []viewKeyWord `json:"a_key"`
  42. ItemName string `json:"s_item"`
  43. UpdateTime int64 `json:"updatetime"`
  44. }
  45. type viewKeyWord struct {
  46. Keyword []string `json:"key"` //关键词
  47. Appended []string `json:"appendkey"` //附加词
  48. Exclude []string `json:"notkey"` //排除词
  49. MatchWay int `json:"matchway"` //匹配模式
  50. }
  51. func init() {
  52. IndustryCodeMap = make(map[string]string)
  53. res, err := g.DB().Query(context.Background(), `SELECT name,code FROM seo_industry_class `)
  54. if err == nil && !res.IsEmpty() {
  55. for _, m := range res.List() {
  56. IndustryCodeMap[gconv.String(m["name"])] = gconv.String(m["code"])
  57. }
  58. }
  59. }
  60. func AnalysisHtmlSave(byMap map[string]interface{}) error {
  61. rid := gconv.String(byMap["rid"])
  62. if rid == "" {
  63. return errors.New("rid不能为空")
  64. }
  65. audit := gconv.Int64(byMap["audit"])
  66. var (
  67. content, desc string
  68. imagArr []string
  69. err error
  70. )
  71. id := utility.DecodeId(rid)
  72. if id == "" {
  73. return errors.New("id错误")
  74. }
  75. res := gconv.Map(byMap["filters"])
  76. if res == nil || len(res) == 0 {
  77. return errors.New("条件获取失败")
  78. }
  79. ctx := gctx.New()
  80. times := strings.Split(gconv.String(res["rangeTime"]), "-")
  81. if len(times) != 2 {
  82. return errors.New("时间格式有误")
  83. }
  84. timestamp1 := time.Unix(gconv.Int64(times[0]), 0)
  85. timestamp2 := time.Unix(gconv.Int64(times[1]), 0)
  86. duration := timestamp2.Sub(timestamp1)
  87. var (
  88. classify int //分类 1周 2月
  89. cycle, area, industry string //周期 城市 行业
  90. showTime int64 // 排序时间
  91. keyItem AnalysisRequestFormat
  92. )
  93. switch duration > 7*24*time.Hour {
  94. case true:
  95. classify = 2
  96. cycle = timestamp1.Format("01")
  97. showTime = gconv.Int64(times[1])
  98. case false:
  99. classify = 1
  100. _, week := timestamp1.ISOWeek()
  101. cycle = fmt.Sprintf("%02d", week)
  102. showTime = gconv.Int64(times[0])
  103. }
  104. areaMap := gconv.Map(res["area"])
  105. if areaMap != nil {
  106. for key := range areaMap {
  107. area = key
  108. break
  109. }
  110. }
  111. industryMap := gconv.Map(res["industry"])
  112. if industryMap != nil {
  113. for key := range industryMap {
  114. industry = key
  115. break
  116. }
  117. }
  118. _ = json.Unmarshal([]byte(gconv.String(res["keysItems"])), &keyItem.KeysItems)
  119. var keyWords []string
  120. for _, item := range keyItem.KeysItems {
  121. for _, word := range item.A_Key {
  122. if len(word.Keyword) > 0 {
  123. keyWords = append(keyWords, word.Keyword...)
  124. }
  125. if len(word.Appended) > 0 {
  126. keyWords = append(keyWords, word.Appended...)
  127. }
  128. }
  129. }
  130. if len(keyWords) > 1 {
  131. return errors.New("关键词数量异常")
  132. }
  133. var areaCode string = "00"
  134. if JySeoAreaRoot.GetNodeByName(area) != nil {
  135. areaCode = JySeoAreaRoot.GetNodeByName(area).RCode[:2]
  136. }
  137. industryCode := "00" //行业code
  138. if industry != "" {
  139. if iCode, ok1 := IndustryCodeMap[industry]; ok1 && iCode != "" {
  140. industryCode = iCode
  141. }
  142. }
  143. var idStr string
  144. year := timestamp1.Year() //关键词&年&月&行业&城市r
  145. timeStr := fmt.Sprintf("%d%s%s%s", year, cycle, industryCode, areaCode)
  146. for _, word := range keyWords {
  147. if word != "" {
  148. idStr = fmt.Sprintf("%s%s", utility.KeyPinyin(word), timeStr)
  149. break
  150. }
  151. }
  152. if idStr == "" { //非关键词分析
  153. if industry == "" || area == "" {
  154. return errors.New("无关键词信息,类型不能为空")
  155. }
  156. //年&分类&周期&行业&城市
  157. idStr = fmt.Sprintf("%d%d%s%s%s", year, classify, cycle, industryCode, areaCode)
  158. }
  159. log.Println("参数id", idStr)
  160. res1, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT id FROM analysis_report WHERE search_id = '%s'`, id))
  161. if audit == 1 { //通过审批提取html
  162. content, desc, imagArr, err = utility.AnalysisHtml(fmt.Sprintf("%s%s.html", htmlUrlDir, strings.ReplaceAll(rid, `/`, "_123_")))
  163. if err != nil || content == "" || len(imagArr) == 0 {
  164. return gerror.Wrap(err, "html内容提取失败")
  165. }
  166. go func() { //通过时 降之前通过的修改为不通过 并同步mongo
  167. var (
  168. mgoIds []primitive.ObjectID
  169. )
  170. 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))
  171. if !res2.IsEmpty() {
  172. for _, m := range res2.List() {
  173. if gconv.String(m["search_id"]) != "" {
  174. mId, _ := primitive.ObjectIDFromHex(gconv.String(m["search_id"]))
  175. mgoIds = append(mgoIds, mId)
  176. }
  177. }
  178. 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))
  179. utility.MG.DB().Update(consts.ReportHistoryTable, map[string]interface{}{
  180. "_id": map[string]interface{}{
  181. "$in": mgoIds,
  182. },
  183. }, map[string]interface{}{
  184. "$set": map[string]interface{}{"audit": -1},
  185. }, false, true)
  186. }
  187. }()
  188. if res1.IsEmpty() {
  189. aReport := analysisReport{
  190. Id: idStr,
  191. Industry: industry,
  192. Area: area,
  193. KeyWord: strings.Join(keyWords, ","),
  194. Desc: desc,
  195. Audit: int32(audit),
  196. Content: content,
  197. Imags: imagArr,
  198. Classify: int32(classify),
  199. ShowTime: showTime,
  200. SearchId: id,
  201. CreateTime: time.Now().Unix(),
  202. }
  203. _, err = g.DB().Insert(ctx, consts.AnalysisReport, aReport)
  204. if err != nil {
  205. return err
  206. }
  207. } else {
  208. g.DB().Exec(ctx, fmt.Sprintf(`alter table analysis_report_do on CLUSTER default_cluster UPDATE audit = %d where search_id = '%s'`, audit, id))
  209. }
  210. } else {
  211. if !res1.IsEmpty() {
  212. g.DB().Exec(ctx, fmt.Sprintf(`alter table analysis_report_do on CLUSTER default_cluster UPDATE audit = %d where search_id = '%s'`, audit, id))
  213. }
  214. }
  215. go utility.MG.DB().UpdateById(consts.ReportHistoryTable, id, map[string]interface{}{
  216. "$set": map[string]interface{}{"audit": audit},
  217. })
  218. return nil
  219. }