docinInfo.go 4.1 KB

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