docinInfo.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
  4. "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/model"
  5. sm "app.yhyue.com/moapp/jy_docs/services/model"
  6. "app.yhyue.com/moapp/jy_docs/services/partner"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/date"
  9. "crypto/md5"
  10. "encoding/hex"
  11. "encoding/json"
  12. "fmt"
  13. "html"
  14. "strings"
  15. "time"
  16. )
  17. func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTotal int) {
  18. var docinInfos model.DocinInfoRes
  19. //str := string(b)
  20. //fmt.Println(str, "--------------")
  21. err = json.Unmarshal(b, &docinInfos)
  22. if err == nil {
  23. if expectTotal = len(docinInfos.Data); expectTotal > 0 {
  24. //剑鱼doc 一份 || 豆丁备份表一份
  25. var (
  26. docs []sm.Docin
  27. docsStatistics []sm.DocStatistics
  28. docsES []map[string]interface{}
  29. )
  30. for _, v := range docinInfos.Data {
  31. var (
  32. productName = html.UnescapeString(v.ProductName)
  33. desc = html.UnescapeString(v.Desc)
  34. id = fmt.Sprintf("%s-%d", entity.PartnerName, v.ProductId)
  35. md5Id = GetMD5(id, productName, v.Desc, v.FilePostfix) //判断数据是否已存在 md5
  36. price = int(v.Price * entity.Multiple) //价格转换
  37. docTags, docClass = partner.SwitchDocClass(v.PcatName, v.CatName, 1) //标签和分类
  38. fileType = GetDocFileType(v.FilePostfix)
  39. uploadDate = date.NowFormat(date.Date_Full_Layout)
  40. )
  41. if partner.CheckDocs(id, md5Id) {
  42. continue
  43. }
  44. if v.CreatedDate != "" {
  45. uploadDate = fmt.Sprintf("%s 00:00:00", strings.Split(v.CreatedDate, " ")[0])
  46. }
  47. lastId = v.ProductId
  48. //tidb 分类
  49. docsStatistics = append(docsStatistics, sm.DocStatistics{
  50. AppId: entity.AppId,
  51. DocId: id,
  52. UpdateDate: time.Now(),
  53. DownTimes: v.DownloadCount,
  54. ViewTimes: v.VisitCount,
  55. })
  56. //tidb 文档
  57. docs = append(docs, sm.Docin{
  58. Id: id,
  59. UserId: entity.PartnerName,
  60. AppId: entity.AppId,
  61. DocName: productName,
  62. DocFileType: fileType,
  63. DocFileSuffix: v.FilePostfix,
  64. DocFileSize: int(v.FileSize),
  65. DocPageSize: v.PageCount,
  66. DocTags: strings.Join(docTags, ","),
  67. DocClass: docClass,
  68. UploadDate: uploadDate,
  69. IsDelete: 0,
  70. OssDocId: "",
  71. Md5: md5Id, //
  72. OssPdfId: "",
  73. OssTxtId: "",
  74. Price: price,
  75. DownOrUp: 0,
  76. DocSummary: desc,
  77. PreviewImgId: common.InterfaceToStr(v.ProductId),
  78. EncryptionLevel: 0,
  79. Source: 2, //豆丁
  80. ProductType: v.Ifcharge + entity.Charge, //Ifcharge:是否付费,0:免费,1:收费;ProductType:商品类型:默认:0:全部;1:会员免费;2:精品(付费)
  81. UpdateDate: date.NowFormat(date.Date_Full_Layout),
  82. })
  83. //elastic
  84. docsES = append(docsES, map[string]interface{}{
  85. "_id": id,
  86. "docClass": docClass,
  87. "docFileSize": int(v.FileSize),
  88. "docFileType": fileType,
  89. "docName": productName,
  90. "docPageSize": v.PageCount,
  91. "docSummary": desc,
  92. "docTags": strings.Join(docTags, ","),
  93. "downTimes": v.DownloadCount,
  94. "previewImgId": common.InterfaceToStr(v.ProductId),
  95. "price": price,
  96. "uploadDate": uploadDate,
  97. "viewTimes": v.VisitCount,
  98. "source": 2, //豆丁
  99. "productType": v.Ifcharge + entity.Charge,
  100. })
  101. }
  102. if len(docs) > 0 {
  103. actualTotal = len(docs)
  104. //doc 文库文档数据信息
  105. partner.DocsInsert(docs, entity.InBatchesCount)
  106. //docsStatistics 文库文档浏览量及下载量信息
  107. partner.DocsStatistics(docsStatistics, entity.InBatchesCount)
  108. //es sync data
  109. partner.SyncDocsToES(docsES)
  110. }
  111. } else {
  112. msg := entity.Res[docinInfos.Msg]
  113. if msg == "" {
  114. msg = "同步豆丁数据异常"
  115. }
  116. err = fmt.Errorf(msg)
  117. }
  118. }
  119. return
  120. }
  121. func GetMD5(id, productName, desc, FilePostfix string) string {
  122. hash := md5.Sum([]byte(fmt.Sprintf("%s#%s#%#s#%s", id, productName, desc, FilePostfix)))
  123. return hex.EncodeToString(hash[:])
  124. }
  125. func GetDocFileType(fileSuffix string) (i int) {
  126. //1 doc 2 pdf 3 xls 4 ppt 5 txt 6 其他
  127. switch strings.ToLower(fileSuffix) {
  128. case "doc", "docx":
  129. i = 1
  130. case "pdf":
  131. i = 2
  132. case "xls", "xlsx":
  133. i = 3
  134. case "ppt", "pptx":
  135. i = 4
  136. case "txt":
  137. i = 5
  138. default:
  139. i = 6
  140. }
  141. return
  142. }