docinInfo.go 3.9 KB

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